Index: trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionManager.java
===================================================================
--- trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionManager.java	(revision 4591)
+++ trunk/gli/src/org/greenstone/gatherer/cdm/SubcollectionManager.java	(revision 4592)
@@ -417,5 +417,5 @@
 	 */
 	public Control() {
-				// Create
+	    // Create
 	    JPanel border_pane = new JPanel();
 	    JPanel header_pane = new JPanel();
@@ -429,6 +429,6 @@
 	    JLabel title = new JLabel(get("Title"));
 	    title.setHorizontalAlignment(JLabel.CENTER);
-	    createSubcollection();
-	    createSubindex();
+	    createSubCollection();
+	    createSubIndex();
 	    // Add listeners
 	    // Layout
@@ -449,15 +449,16 @@
 	}
 	/** Create the subcollection controls.
-			* @see org.greenstone.gatherer.Gatherer
-			* @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.AddListener
-			* @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.ListListener
-			* @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.RemoveListener
-			* @see org.greenstone.gatherer.collection.CollectionManager
-			* @see org.greenstone.gatherer.msm.MetadataSetManager
-			*/
-	public void createSubcollection() {
+	 * @see org.greenstone.gatherer.Gatherer
+	 * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.AddListener
+	 * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.ListListener
+	 * @see org.greenstone.gatherer.cdm.SubcollectionManager.Control.RemoveListener
+	 * @see org.greenstone.gatherer.collection.CollectionManager
+	 * @see org.greenstone.gatherer.msm.MetadataSetManager
+	 */
+	public void createSubCollection() {
 	    // Create
 	    add = new JButton(get("Add"));
 	    add.setMnemonic(KeyEvent.VK_A);
+	    add.setEnabled(false);
 	    JPanel button_pane = new JPanel();
 	    JPanel button_pane_1 = new JPanel();
@@ -477,4 +478,5 @@
 	    remove = new JButton(get("Remove"));
 	    remove.setMnemonic(KeyEvent.VK_R);
+	    remove.setEnabled(false);
 	    Vector source_model = gatherer.c_man.msm.getAssignedElements();
 	    source_model.add(0, "Filename");
@@ -492,7 +494,14 @@
 
 	    // Add listeners
-	    add.addActionListener(new AddListener());
-	    remove.addActionListener(new RemoveListener());
-	    subcollection_list.addListSelectionListener(new ListListener());
+	    SubCollectionChangeListener cl = new SubCollectionChangeListener();
+	    add.addActionListener(new AddSubCollectionListener());
+	    remove.addActionListener(new RemoveSubCollectionListener());
+	    subcollection_list.addListSelectionListener(new SubCollectionListListener());
+	    //exclude.addActionListener(cl);
+	    //include.addActionListener(cl);
+	    //source.addActionListener(cl);
+	    //flags.addDocumentListener(cl);
+	    match.getDocument().addDocumentListener(cl);
+	    name.getDocument().addDocumentListener(cl);
 
 	    // Layout
@@ -535,5 +544,5 @@
 	 * @see org.greenstone.gatherer.util.ExclusiveListListener
 	 */
-	public void createSubindex() {
+	public void createSubIndex() {
 	    // Creation
 	    JPanel subindex_name_panel = new JPanel();
@@ -577,21 +586,14 @@
 
 	    // Add listeners
-	    ChangeListener cl = new ChangeListener();
 	    ExclusiveListSelectionListener ell = new ExclusiveListSelectionListener();
 	    ell.add(subcollection_list_2);
 	    ell.add(subindexes_list);
 	    add_index.addActionListener(new AddSubIndexListener());
-	    clear_default.addActionListener(new ClearDefaultListener());
-	    exclude.addActionListener(cl);
-	    include.addActionListener(cl);
+	    clear_default.addActionListener(new ClearDefaultSubIndexListener());
 	    remove_index.addActionListener(new RemoveSubIndexListener());
-	    set_default.addActionListener(new SetDefaultListener());
-	    source.addActionListener(cl);
-	    flags.addKeyListener(cl);
-	    match.addKeyListener(cl);
-	    name.addKeyListener(cl);
+	    set_default.addActionListener(new SetDefaultSubIndexListener());
 
 	    subindex_name.getDocument().addDocumentListener(new SubIndexNameDocumentListener());
-	    subcollection_list_2.addListSelectionListener(new SubcollectionListSelectionListener());
+	    subcollection_list_2.addListSelectionListener(new SubCollectionList2Listener());
 
 	    // Layout
@@ -634,52 +636,6 @@
 	    subindex_pane.add(button_pane_2, BorderLayout.SOUTH);
 	}
-
-	private class SubIndexListCellRenderer
-	    extends DefaultListCellRenderer {
-	    
-	    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
-		StringBuffer text = new StringBuffer(value.toString());
-		// Retrieve the indexes name if any.
-		CollectionMeta metadata = manager.collectionmetadatum.getMetadata(value, manager.languages.getDefaultLanguage(), true);
-		if(metadata != null) {
-		    text.append(" \"");
-		    text.append(metadata.getValue());
-		    text.append("\"");
-		}
-		return super.getListCellRendererComponent(list, text.toString(), index, isSelected, cellHasFocus);
-	    }
-
-	}
-
-	private class SubcollectionListSelectionListener
-	    implements ListSelectionListener {
-
-	    public void valueChanged(ListSelectionEvent event) {
-		if(!event.getValueIsAdjusting()) {
-		    add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
-		}
-	    }
-	}
 	
-	private class SubIndexNameDocumentListener
-	    implements DocumentListener {
-	    /** Gives notification that an attribute or set of attributes changed. */
-	    public void changedUpdate(DocumentEvent e) {
-		update();
-	    }
-	    /** Gives notification that there was an insert into the document. */
-	    public void insertUpdate(DocumentEvent e) {
-		update();
-	    }
-	    /** Gives notification that a portion of the document has been removed. */
-	    public void removeUpdate(DocumentEvent e) {
-		update();
-	    }
-	    /** The text area has changed in some way. Given that this can only happed when we are editing or adding a text fragment we better respond appropriately. */
-	    private void update() {
-		add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
-	    }
-	}
-
+	
 	/** Method to unregister any listeners to avoid memory leaks.
 	 */
@@ -687,9 +643,9 @@
 	}
 	/** We have overriden this method to provide completely different functionality, given that our JPanel will never actually have focus. We call this method in GUI on the view we are about to replace, and this method checks if a change has occured and if so updates the current object.
-			* @return A <i>boolean</i> which is always <i>false</i>.
-			* @see org.greenstone.gatherer.msm.ElementWrapper
-			*/
+	 * @return A <i>boolean</i> which is always <i>false</i>.
+	 * @see org.greenstone.gatherer.msm.ElementWrapper
+	 */
 	public boolean hasFocus() {
-				// If we have a current metadata open, and something has changed then save the change.
+	    // If we have a current metadata open, and something has changed then save the change.
 	    if(changed && current != null) {
 		String n = name.getText();
@@ -716,5 +672,5 @@
 	}
 	/** Overriden to ensure the instructions are scrolled to top.
-		  */
+	 */
 	public void updateUI() {
 	    if(instructions != null) {
@@ -723,6 +679,23 @@
 	    super.updateUI();
 	}
+
+	/** Method to validate the current subcollection editor values, and enable or disable controls (add button) based on said values. 
+	 * we want to disable add until a new name is present 
+	 */
+	protected void validateAdd() {
+	    if(changed && name.getText().length() > 0 && match.getText().length() > 0) {
+		System.out.println("in validate, name="+name.getText());
+		if (subcollections.containsKey(name.getText())) {
+		    add.setEnabled(false);
+		} else {
+		    add.setEnabled(true);
+		}
+	    }
+	    else {
+		add.setEnabled(false);
+	    }
+	}
 	/** Listens for actions apon the 'add' button in the SubcollectionManager controls, and if detected calls the addSubcollection method of the manager with a newly created subcollection. */
-	private class AddListener
+	private class AddSubCollectionListener
 	    implements ActionListener {
 	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we wish to retrieve information from the various edit controls, and if we have sufficient data to build a new subcollection do so.
@@ -750,4 +723,5 @@
 		}
 		changed = false;
+		validateAdd();
 	    }
 	}
@@ -775,7 +749,6 @@
 	}
 	/** This class listens for any key entry in a text field, selection change in a combobox or button click, and when detected sets the changed flag to true. Its also convenient to use this class to test if the add button should be active yet. */
-	public class ChangeListener
-	    extends KeyAdapter
-	    implements ActionListener {
+	private class SubCollectionChangeListener
+	    implements DocumentListener, ActionListener {
 	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to record that somethings changed, then validate the controls.
 	     * @param event An <strong>ActionEvent</strong> containing information about the event.
@@ -783,25 +756,35 @@
 	    public void actionPerformed(ActionEvent event) {
 		changed = true;
-		validate();
+		validateAdd();
+	    }
+
+	    public void changedUpdate(DocumentEvent event) {
+		System.out.println("changed");
+		changed = true;
+		validateAdd();
+	    }
+	    public void insertUpdate(DocumentEvent event) {
+		System.out.println("inserted");
+		changed = true;
+		validateAdd();
+
+	    }
+
+	    public void removeUpdate(DocumentEvent event) {
+		System.out.println("removed");
+		changed = true;
+		validateAdd();
 	    }
 	    /** Any inheritor of KeyAdapter can override this method so we can be informed when an key has been type in one of our target controls. In this case we want to record that somethings changed, then validate the controls.
 	     * @param event An <strong>KeyEvent</strong> containing information about the event.
 	     */
-	    public void keyPressed(KeyEvent event) {
-		changed = true;
-		validate();
-	    }
-	    /** Method to validate the current editor values, and enable or disable controls based on said values. */
-	    private void validate() {
-		if(changed && name.getText().length() > 0 && match.getText().length() > 0) {
-		    add.setEnabled(true);
-		}
-		else {
-		    add.setEnabled(false);
-		}
-	    }
+	    // public void keyPressed(KeyEvent event) {
+	    //changed = true;
+	    //validate();
+	    // }
+
 	}
 	/** Listens for actions apon the 'clear default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubIndex() method of the manager with <i>null</i>. */
-	private class ClearDefaultListener
+	private class ClearDefaultSubIndexListener
 	    implements ActionListener {
 	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to clear the default subindex.
@@ -814,6 +797,52 @@
 	    }
 	}
+	/** Listens for actions apon the 'remove' button in the SubcollectionManager controls, and if detected calls the remove method of the manager with the SubIndex selected for removal. */
+	private class RemoveSubCollectionListener
+	    implements ActionListener {
+	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subcolleciton selected, and if so remove both it and any subindexes based on it.
+	     * @param event An <strong>ActionEvent</strong> containing information about the event.
+	     * @see org.greenstone.gatherer.cdm.Subcollection
+	     */
+	    public void actionPerformed(ActionEvent event) {
+		if(!subcollection_list.isSelectionEmpty()) {
+		    Subcollection sub_col = (Subcollection)subcollection_list.getSelectedValue();
+		    removeSubIndexes(sub_col);
+		    removeSubcollection(sub_col);
+		}
+	    }
+	}
+	/** Listens for actions apon the 'remove subindex' button in the SubcollectionManager controls, and if detected calls the removeSubIndex method of the manager with the SubIndex selected for removal. */
+	private class RemoveSubIndexListener
+	    implements ActionListener {
+	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so remove it.
+	     * @param event An <strong>ActionEvent</strong> containing information about the event.
+	     * @see org.greenstone.gatherer.cdm.SubIndex
+	     */
+	    public void actionPerformed(ActionEvent event) {
+		if(!subindexes_list.isSelectionEmpty()) {
+		    removeSubIndex((SubIndex)subindexes_list.getSelectedValue());
+		}
+	    }
+	}
+	/** Listens for actions apon the 'set default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubIndex method of the manager with the SubIndex selected for default. */
+	private class SetDefaultSubIndexListener
+	    implements ActionListener {
+	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so set it as default.
+	     * @param event An <strong>ActionEvent</strong> containing information about the event.
+	     * @see org.greenstone.gatherer.cdm.DefaultSubIndex
+	     * @see org.greenstone.gatherer.cdm.SubIndex
+	     */
+	    public void actionPerformed(ActionEvent event) {
+		if(!subindexes_list.isSelectionEmpty()) {
+		    setDefaultSubIndex(new DefaultSubIndex((SubIndex)subindexes_list.getSelectedValue()));
+		    clear_default.setEnabled(true);
+		    default_value.setText(default_index.getSubIndex().toString());
+		}
+	    }
+	}
+
+
 	/** This class listens for selections in the list on the subcollections pane of the SubcollectionManager, and updates the controls as necessary to reflect selection. */
-	private class ListListener
+	private class SubCollectionListListener
 	    implements ListSelectionListener {
 	    /** Any implementation of ListSelectionListener must include this method so we can be informed when the selection changes. In this case we want to execute any changes the users made to the entry, then update the controls with details of the new selection.
@@ -891,50 +920,55 @@
 		// Have to do this after so we don't get called when nothings actually changed.
 		changed = false;
-	    }
-	}
-	/** Listens for actions apon the 'remove' button in the SubcollectionManager controls, and if detected calls the remove method of the manager with the SubIndex selected for removal. */
-	private class RemoveListener
-	    implements ActionListener {
-	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subcolleciton selected, and if so remove both it and any subindexes based on it.
-	     * @param event An <strong>ActionEvent</strong> containing information about the event.
-	     * @see org.greenstone.gatherer.cdm.Subcollection
-	     */
-	    public void actionPerformed(ActionEvent event) {
-		if(!subcollection_list.isSelectionEmpty()) {
-		    Subcollection sub_col = (Subcollection)subcollection_list.getSelectedValue();
-		    removeSubIndexes(sub_col);
-		    removeSubcollection(sub_col);
-		}
-	    }
-	}
-	/** Listens for actions apon the 'remove subindex' button in the SubcollectionManager controls, and if detected calls the removeSubIndex method of the manager with the SubIndex selected for removal. */
-	private class RemoveSubIndexListener
-	    implements ActionListener {
-	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so remove it.
-	     * @param event An <strong>ActionEvent</strong> containing information about the event.
-	     * @see org.greenstone.gatherer.cdm.SubIndex
-	     */
-	    public void actionPerformed(ActionEvent event) {
-		if(!subindexes_list.isSelectionEmpty()) {
-		    removeSubIndex((SubIndex)subindexes_list.getSelectedValue());
-		}
-	    }
-	}
-	/** Listens for actions apon the 'set default subindex' button in the SubcollectionManager controls, and if detected calls the setDefaultSubIndex method of the manager with the SubIndex selected for default. */
-	private class SetDefaultListener
-	    implements ActionListener {
-	    /** Any implementation of ActionListener must include this method so we can be informed when an action has been performed on one of our target controls. In this case we want to check if they have a subindex selected, and if so set it as default.
-	     * @param event An <strong>ActionEvent</strong> containing information about the event.
-	     * @see org.greenstone.gatherer.cdm.DefaultSubIndex
-	     * @see org.greenstone.gatherer.cdm.SubIndex
-	     */
-	    public void actionPerformed(ActionEvent event) {
-		if(!subindexes_list.isSelectionEmpty()) {
-		    setDefaultSubIndex(new DefaultSubIndex((SubIndex)subindexes_list.getSelectedValue()));
-		    clear_default.setEnabled(true);
-		    default_value.setText(default_index.getSubIndex().toString());
-		}
-	    }
-	}
+		
+	    }
+	}
+
+	private class SubCollectionList2Listener
+	    implements ListSelectionListener {
+
+	    public void valueChanged(ListSelectionEvent event) {
+		if(!event.getValueIsAdjusting()) {
+		    add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
+		}
+	    }
+	}
+	
+	private class SubIndexListCellRenderer
+	    extends DefaultListCellRenderer {
+	    
+	    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+		StringBuffer text = new StringBuffer(value.toString());
+		// Retrieve the indexes name if any.
+		CollectionMeta metadata = manager.collectionmetadatum.getMetadata(value, manager.languages.getDefaultLanguage(), true);
+		if(metadata != null) {
+		    text.append(" \"");
+		    text.append(metadata.getValue());
+		    text.append("\"");
+		}
+		return super.getListCellRendererComponent(list, text.toString(), index, isSelected, cellHasFocus);
+	    }
+
+	}
+	
+	private class SubIndexNameDocumentListener
+	    implements DocumentListener {
+	    /** Gives notification that an attribute or set of attributes changed. */
+	    public void changedUpdate(DocumentEvent e) {
+		update();
+	    }
+	    /** Gives notification that there was an insert into the document. */
+	    public void insertUpdate(DocumentEvent e) {
+		update();
+	    }
+	    /** Gives notification that a portion of the document has been removed. */
+	    public void removeUpdate(DocumentEvent e) {
+		update();
+	    }
+	    /** The text area has changed in some way. Given that this can only happed when we are editing or adding a text fragment we better respond appropriately. */
+	    private void update() {
+		add_index.setEnabled(!subcollection_list_2.isSelectionEmpty() && subindex_name.getText().length() > 0);
+	    }
+	}
+
     }
 }
