Index: /trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java	(revision 8380)
+++ /trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java	(revision 8381)
@@ -53,4 +53,5 @@
 
     static final private Dimension FIELD_SIZE = new Dimension(200,30);
+    static final private Dimension BUTTON_SIZE = new Dimension(100,30);
     static final private String MGINDEXES = "mg indexes";
     static final private String MGPPINDEXES = "mgpp indexes";
@@ -94,4 +95,5 @@
 		Index last_index = (Index)getElementAt(getSize() - 1);
 		addAfter(index, last_index);
+		
 	    }
 	    else {
@@ -496,4 +498,5 @@
 	private JButton move_up_button;
 	private JButton remove_button;
+	private JButton replace_button;
 	private JButton set_default_button;
 	private JComboBox level_combobox;
@@ -546,5 +549,5 @@
 	    index_list = new JList(model);
 	    index_list.setCellRenderer(new IndexListRenderer());
-
+	    index_list.setVisibleRowCount(5);
 	    JPanel movement_pane = new JPanel();
 
@@ -599,4 +602,8 @@
 	    remove_button.setMnemonic(KeyEvent.VK_R);
 	    Dictionary.registerBoth(remove_button, "CDM.IndexManager.Remove_Index", "CDM.IndexManager.Remove_Index_Tooltip");
+	    replace_button = new GLIButton();
+	    replace_button.setEnabled(false);
+	    replace_button.setMnemonic(KeyEvent.VK_P);
+	    Dictionary.registerBoth(replace_button, "CDM.IndexManager.MGPP.Replace_Index", "CDM.IndexManager.MGPP.Replace_Index_Tooltip");
 
 	    // This class is getting a little crowded, so I'll generate the many mgpp controls in a inner class.
@@ -608,12 +615,10 @@
 	    move_up_button.addActionListener(new MoveListener(true));
 	    remove_button.addActionListener(new RemoveListener());
+	    replace_button.addActionListener(new ReplaceListener());
 	    set_default_button.addActionListener(new SetDefaultListener());
 	    name_textfield.getDocument().addDocumentListener(new NameListener());
+	    level_combobox.addItemListener(new LevelListener());
 	    index_list.addListSelectionListener(new IndexListListener());
 	    source_list.addListSelectionListener(new SourceListListener());
-	    ExclusiveListSelectionListener elsl = new ExclusiveListSelectionListener();
-	    elsl.add(index_list);
-	    elsl.add(source_list);
-	    elsl = null; // We don't want to hold a reference to this
 
 	    // Layout
@@ -624,7 +629,8 @@
 
 	    movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
-	    movement_pane.setLayout(new GridLayout(2,1));
+	    movement_pane.setLayout(new GridLayout(3,1));
 	    movement_pane.add(move_up_button);
 	    movement_pane.add(move_down_button);
+	    movement_pane.add(set_default_button);
 
 	    assigned_indexes_pane.setBorder(BorderFactory.createEmptyBorder(0,0,5,0));
@@ -651,5 +657,5 @@
 	    button_pane.setLayout(new GridLayout(1,3));
 	    button_pane.add(add_button);
-	    button_pane.add(set_default_button);
+	    button_pane.add(replace_button);
 	    button_pane.add(remove_button);
 
@@ -692,4 +698,8 @@
 		source_list.setListData(new_data.toArray());
 		new_data = null;
+		// refresh the indexList
+		index_list.updateUI();
+		// if there is one selected, fill in the controls
+		updateControlsWithSelectedIndex();
 	    }
 	    // Bring the appropriate card to the front
@@ -705,6 +715,26 @@
 	}
 
+	private void updateControlsWithSelectedIndex() {
+	    
+	    Index selected_index = (Index)index_list.getSelectedValue();
+	    if (selected_index == null) {
+		return;
+	    }
+	    String id = selected_index.getID();
+	    if (id == null || id.equals("")) {
+		return;
+	    }
+	    String name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+selected_index.getID()).getValue(true);
+	    name_textfield.setText(name);
+	    level_combobox.setSelectedIndex(selected_index.getLevel());
+	    source_list.clearSelection();
+	    ArrayList sources = selected_index.getSources();
+	    source_list.setSelectedObjects(sources.toArray());
+	    
+	}
+	
 	private void validateAddButton() {
 	    boolean add_enabled = false;
+	    boolean replace_enabled = false;
 	    // Indexes must have a name
 	    if(name_textfield.getText().length() == 0) {
@@ -727,11 +757,26 @@
 		if(model.contains(index)) {
 		    add_enabled = false;
+		    // here we need to check if we have changed the name - if so, we can enable the replace button
+		    if (index_list.getSelectedIndex() != -1) {
+			Index selected_index = (Index)index_list.getSelectedValue();
+			String name = name_textfield.getText();
+			String index_name = CollectionDesignManager.collectionmeta_manager.getMetadatum(CollectionConfiguration.STOP_CHARACTER+selected_index.getID()).getValue(true);
+			if (!index_name.equals(name)){
+			    replace_enabled = true;
+			}
+		    }
 		}
 		else {
 		    add_enabled = true;
+		    // we have a new index, do we have something selected in the index list? if so, enable the replace button
+		    if (index_list.getSelectedIndex() != -1) {
+			replace_enabled = true;
+		    }
+		    
 		}
 	    }
 	    // We should now know the add_button state
 	    add_button.setEnabled(add_enabled);
+	    replace_button.setEnabled(replace_enabled);
 	}
 
@@ -759,4 +804,5 @@
 		    // Finally add index.
 		    addIndex(index, metadatum);
+		    index_list.setSelectedValue(index, true);
 		    metadatum = null;
 		    index = null;
@@ -769,5 +815,5 @@
 	private class IndexListListener
 	    implements ListSelectionListener {
-	    /** This method is called whenever the source list selection changes. When it does we need to check if the add button should now be enabled.
+	    /** This method is called whenever the source list selection changes. When it does we need to fill in the various parts of the list description panel
 	     * @param event A <strong>ListSelectionEvent</strong> containing further information about the list selection.
 	     */
@@ -779,8 +825,30 @@
 
 		Object value = index_list.getSelectedValue();
-		move_down_button.setEnabled(value != null);
-		move_up_button.setEnabled(value != null);
-		remove_button.setEnabled(value != null);
-		set_default_button.setEnabled(value != null && (default_index == null || !default_index.equals(value)));
+		if (value == null) {
+		    move_down_button.setEnabled(false);
+		    move_up_button.setEnabled(false);
+		    remove_button.setEnabled(false);
+		    replace_button.setEnabled(false);
+		    set_default_button.setEnabled(false);
+		    return;
+		}
+		// enable the buttons appropriately
+		remove_button.setEnabled(true);
+		set_default_button.setEnabled(default_index == null || !default_index.equals(value));
+		int i = index_list.getSelectedIndex();
+		int size = index_list.getModel().getSize();
+		if (i > 0) {
+		    move_up_button.setEnabled(true);
+		} else {
+		    move_up_button.setEnabled(false);
+		}
+		if (i<size-1){
+		    move_down_button.setEnabled(true);
+		} else {
+		    move_down_button.setEnabled(false);
+		}
+
+		// need to fill in the rest of the bits
+		updateControlsWithSelectedIndex();		
 	    }
 	}
@@ -798,4 +866,15 @@
 	    }
 
+	}
+
+	private class LevelListener
+	    implements ItemListener {
+	    public void itemStateChanged(ItemEvent event) {
+		if(event.getStateChange() == ItemEvent.SELECTED) {
+		    // just want to validate the add button
+		    validateAddButton();
+
+		}
+	    }
 	}
 
@@ -829,10 +908,41 @@
 	     */
 	    public void actionPerformed(ActionEvent event) {
-		if(!index_list.isSelectionEmpty()) {
+		int i = index_list.getSelectedIndex();
+		if(i != -1) {
 		    removeIndex((Index)index_list.getSelectedValue());
 		}
-		move_down_button.setEnabled(false);
-		move_up_button.setEnabled(false);
-		remove_button.setEnabled(false);
+		int size = index_list.getModel().getSize();
+		if (i == size) {
+		    i--;
+		}
+		
+		index_list.setSelectedIndex(i);
+		// this will produce an event on the list, updating the other buttons
+		if (size == 0) {
+		    // we have removed the last index, should be able to add whats filled in currently, if valid
+		    validateAddButton();
+		}	    
+	    }
+	}
+
+	/** Listens for actions apon the 'remove' button in the IndexManager controls, and if detected calls the remove method of the manager with the index selected for removal. */
+	private class ReplaceListener
+	    implements ActionListener {
+	    /** If called when an action occurs on a registered component, we replace the currently selected index, with the new details
+	     * @param event An <strong>ActionEvent</strong> containing extra information about the action that occured.
+	     * @see org.greenstone.gatherer.cdm.Index
+	     */
+	    public void actionPerformed(ActionEvent event) {
+		if(index_list.isSelectionEmpty()) {
+		    // this should never happen, but just in case..
+		    replace_button.setEnabled(false);
+		    return;
+		}
+		// we'll just remove the old one and add the new one
+		removeIndex((Index)index_list.getSelectedValue());
+		replace_button.setEnabled(false);
+		add_button.setEnabled(true);
+		add_button.doClick();
+		add_button.setEnabled(false);
 	    }
 	}
@@ -1219,4 +1329,7 @@
 		level = null;
 	    }
+
+	    // refresh
+	    current_indexes_list.updateUI();
 	}
 
@@ -1355,5 +1468,5 @@
 	     */
 	    public void actionPerformed(ActionEvent event) {
-		validateAddButton();
+		validateAddButtonIndex();
 	    }
 
@@ -1362,5 +1475,5 @@
 	     */
 	    public void changedUpdate(DocumentEvent event) {
-		validateAddButton();
+		validateAddButtonIndex();
 	    }
 
@@ -1369,5 +1482,5 @@
 	     */
 	    public void insertUpdate(DocumentEvent event) {
-		validateAddButton();
+		validateAddButtonIndex();
 	    }
 
@@ -1376,9 +1489,9 @@
 	     */
 	    public void removeUpdate(DocumentEvent event) {
-		validateAddButton();
+		validateAddButtonIndex();
 	    }
 
 	    /** Change the enable state of the add button depending on the current value in the search type combobox. */
-	    public void validateAddButton() {
+	    public void validateAddButtonIndex() {
 		String name = index_name_field.getText();
 		Object selected_object = index_combobox.getSelectedItem();
@@ -1417,5 +1530,5 @@
 	     */
 	    public void actionPerformed(ActionEvent event) {
-		validateAddButton();
+		validateAddButtonLevel();
 	    }
 
@@ -1424,5 +1537,5 @@
 	     */
 	    public void changedUpdate(DocumentEvent event) {
-		validateAddButton();
+		validateAddButtonLevel();
 	    }
 
@@ -1431,5 +1544,5 @@
 	     */
 	    public void insertUpdate(DocumentEvent event) {
-		validateAddButton();
+		validateAddButtonLevel();
 	    }
 
@@ -1438,9 +1551,9 @@
 	     */
 	    public void removeUpdate(DocumentEvent event) {
-		validateAddButton();
-	    }
-
+		validateAddButtonLevel();
+	    }
+    
 	    /** Change the enable state of the add button depending on the current value in the search type combobox. */
-	    public void validateAddButton() {
+	    public void validateAddButtonLevel() {
 		String name = level_name_field.getText();
 		Object selected_object = level_combobox.getSelectedItem();
@@ -1546,5 +1659,5 @@
 		remove_index_button.setEnabled(false);
 		// Check if add should be reenabled
-		eal.validateAddButton();
+		eal.validateAddButtonIndex();
 	    }
 	}
@@ -1584,7 +1697,8 @@
 		}
 		// Check if add should be reenabled
-		eal.validateAddButton();
-	    }
-	}
-    }
+		eal.validateAddButtonLevel();
+	    }
+	}
+    } // MGPPControls
+
 }
