Index: main/trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java
===================================================================
--- main/trunk/gli/src/org/greenstone/gatherer/cdm/IndexManager.java	(revision 36147)
+++ 	(revision )
@@ -1,1121 +1,0 @@
-/**
- *#########################################################################
- *
- * A component of the Gatherer application, part of the Greenstone digital
- * library suite from the New Zealand Digital Library Project at the
- * University of Waikato, New Zealand.
- *
- * Author: John Thompson, Greenstone Digital Library, University of Waikato
- *
- * Copyright (C) 1999 New Zealand Digital Library Project
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *########################################################################
- */
-package org.greenstone.gatherer.cdm;
-
-import java.awt.*;
-import java.awt.event.*;
-import java.util.*;
-import javax.swing.*;
-import javax.swing.event.*;
-import org.greenstone.gatherer.Configuration;
-import org.greenstone.gatherer.DebugStream;
-import org.greenstone.gatherer.Dictionary;
-import org.greenstone.gatherer.Gatherer;
-import org.greenstone.gatherer.gui.DesignPaneHeader;
-import org.greenstone.gatherer.gui.GComboBox;
-import org.greenstone.gatherer.gui.GLIButton;
-import org.greenstone.gatherer.gui.ModalDialog;
-import org.greenstone.gatherer.gui.SimpleMenuBar;
-import org.greenstone.gatherer.metadata.MetadataElement;
-import org.greenstone.gatherer.metadata.MetadataSetManager;
-import org.greenstone.gatherer.util.CheckList;
-import org.greenstone.gatherer.util.JarTools;
-import org.greenstone.gatherer.util.XMLTools;
-import org.greenstone.gatherer.util.StaticStrings;
-import org.w3c.dom.*;
-/** This class is resposible for storing the indexes which have been assigned to this collection and the default index, and providing methods for interacting with both these data pools. It also knows how to turn itself into a String as it would be displayed in the collection configuration file.
- * @author John Thompson, Greenstone Digital Library, University of Waikato
- * @version 2.3
- */
-public class IndexManager
-    extends DOMProxyListModel 
-    implements BuildTypeManager.BuildTypeListener {
-
-    /** A reference to ourselves so our inner methods have access. */
-    private DOMProxyListModel index_model = null;
-    /** The default index. */
-    private Index default_index = null;
-
-    private Control controls = null;
-    private String build_type = null;
-    
-    static final private Dimension FIELD_SIZE = new Dimension(200,30);
-    static final private String ALLFIELDS = "allfields";
-
-    static final private Dimension PROMPT_SIZE = new Dimension(400,400);
-    public IndexManager(Element indexes, String current_build_type) {
-
-	super(indexes, StaticStrings.INDEX_ELEMENT, (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)?new MGIndex():new Index()));
-	DebugStream.println("IndexManager: " + getSize() + " indexes parsed.");
-	index_model = this;
-      
-	// Parse and retrieve the default index
-	NodeList default_index_elements = CollectionConfiguration.getElementsByTagName(StaticStrings.INDEX_DEFAULT_ELEMENT);
-	if(default_index_elements.getLength() > 0) {
-	    if (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
-		default_index = new MGIndex((Element)default_index_elements.item(0));
-	    } else {
-		default_index = new Index((Element)default_index_elements.item(0));
-	    }
-	}
-	build_type = current_build_type;
-    }
-
-    /** Method to add a new index.
-     * @param index The <strong>Index</strong> to add.
-     * @see org.greenstone.gatherer.Gatherer
-     * @see org.greenstone.gatherer.collection.CollectionManager
-     */
-    private void addIndex(Index index, CollectionMeta metadatum) {
-	///ystem.err.println("Adding an index: " + index.toString());
-	if(!contains(index)) {
-	    CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
-	    // Retrieve the currently last index
-	    if(getSize() > 0) {
-		Index last_index = (Index)getElementAt(getSize() - 1);
-		addAfter(index, last_index);
-		
-	    }
-	    else {
-		add(index);
-		// Also set this index as the default one, 
-		setDefault(index);
-	    }
-	}
-	else {
-	    JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
-	}
-    }
-
-    public void buildTypeChanged(String new_build_type) {
-	if (build_type.equals(new_build_type)) {
-	    return;
-	}
-	// we don;t care about this if old or new is not MG as MGPP and 
-	// Lucene have the same index specification
-	if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && !new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
-	    return;
-	}
-	boolean mg_to_mgpp = true;
-	if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
-	    mg_to_mgpp = false;
-	}
-	if (mg_to_mgpp) {
-	    changeToMGPPIndexes();
-	} else {
-	    changeToMGIndexes();
-	}
-	build_type  = new_build_type;
-	// its really hard to transfer defaults between mgpp/lucene and mg indexes, so we'll just set the first one to be the default
-	Index first_index = (Index) getElementAt(0);
-	setDefault(first_index);
-	first_index = null;
-    }
-    
-    private void changeToMGIndexes() {
-	this.setClassType(new MGIndex());
-	Element mgpp_element = root;
-	// Retrieve and assign MG element and default index element
-	Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
-	mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
-	NodeList indexes = mg_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
-	// Replace mgpp element with mg element
-	setRoot(mg_element);
-	
-	if(indexes.getLength() == 0) {
-	  // we need to create some based on the mgpp indexes
-	  
-	  // If the current mgpp index includes a text one, then generate text indexes for each of the registered levels.
-	  Index index = getIndex(StaticStrings.TEXT_STR);
-	  if(index != null) {
-	    ArrayList levels = CollectionDesignManager.index_manager.getLevels();
-	    int level_size = levels.size();
-	    for(int i = 0; i < level_size; i++) {
-	      IndexOption level = (IndexOption) levels.get(i);
-	      Index new_index = new MGIndex(level.getName(), index.getSources());
-	      // Try to retrieve existing metadatum
-	      String source_str = new_index.getID();
-	      CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
-	      // If no metadata was found, add new pseudo metadata using the id
-	      if(metadatum == null) {
-		metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
-		metadatum.setAssigned(true);
-		metadatum.setValue(source_str);
-	      }
-	      // If it was found, ensure it is assigned
-	      else {
-		metadatum.setAssigned(true);
-	      }
-	      source_str = null;
-	      addIndex(new_index, metadatum);
-	      new_index = null;
-	      level = null;
-	    }
-	  }
-	}
-	
-	// Unassign mgpp element 
-	mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
-	mgpp_element = null;
-	
-    }
-
-    private void changeToMGPPIndexes() {
-	this.setClassType(new Index());
-	Element mg_element = root;
-	// Retrieve and assign the MGPP indexes element.
-	Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
-	mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
-	NodeList indexes = mgpp_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
-	if(indexes.getLength() != 0) {
-	    // we just reinstate the indexes we had before the change
-	    setRoot(mgpp_element);
-	} else {
-	    // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
-	    
-	    ArrayList sources_list = new ArrayList();
-
-	    // We first use details from the default index if any
-	    if(default_index != null) {
-		ArrayList sources = default_index.getSources();
-		sources_list.addAll(sources);
-	    }
-	    int size = getSize();
-	    for(int i = 0; i < size; i++) {
-		Index index = (Index) getElementAt(i);
-		ArrayList sources = index.getSources();
-		sources.removeAll(sources_list);
-		sources_list.addAll(sources);
-		index = null;
-	    }
-	    // Replace mg element with mgpp element
-	    setRoot(mgpp_element);
-	    
-	    // We now have a list of sources, so create new indexes based on these
-	    int sources_list_size = sources_list.size();
-	    for(int j = 0; j < sources_list_size; j++) {
-		Object source_object = sources_list.get(j);
-		String source_str = null;
-		if(source_object instanceof MetadataElement) {
-		    source_str = ((MetadataElement) source_object).getFullName();
-		}
-		else {
-		    source_str = source_object.toString();
-		}
-		ArrayList new_sources = new ArrayList();
-		new_sources.add(source_object);
-		source_object = null;
-		Index new_index = new Index(new_sources);
-		// Try to retrieve existing metadatum
-		source_str = new_index.getID();
-		CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
-		// If no metadata was found, add new pseudo metadata using the id
-		if(metadatum == null) {
-		    metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
-		    metadatum.setAssigned(true);
-		    metadatum.setValue(source_str);
-		}
-		// If it was found, ensure it is assigned
-		else {
-		    metadatum.setAssigned(true);
-		}
-		source_str = null;
-		addIndex(new_index, metadatum);
-		metadatum = null;
-		new_index = null;
-		new_sources = null;
-		source_str = null;
-	    }	      
-	    
-	}
-
-	// Unassign MG element
-	mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
-	mg_element = null;
-	
-    }
-    
-    public Control getControls() {
-	if (controls == null) {
-	    controls = new IndexControl();
-	}
-	return controls;
-    }
-
-    /** Method to retrieve a certain index, as referenced by an index number.
-     * @param index An <i>int</i> which indicates the position of the desired index.
-     * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
-     */
-    public Index getIndex(int index) {
-	if(0 <= index && index < getSize()) {
-	    return (Index)getElementAt(index);
-	}
-	return null;
-    }
-
-    /** Method to retrieve a certain index, given its id.
-     * @param id the id of the index as a String
-     * @return the Index that matches id, or null if no such index exists
-     */
-    public Index getIndex(String id) {
-	int size = getSize();
-	for(int i = 0; i < size; i++) {
-	    Index index = (Index) getElementAt(i);
-	    if(index.getID().equals(id)) {
-		return index;
-	    }
-	}
-	return null;
-    }
-
-    public ArrayList getIndexes() {
-	return children();
-    }
-
-
-
-    /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
-     * @param mode the new mode as an int
-     */
-    public void modeChanged(int mode) {
-
-    }
-
-    private void moveIndex(Index index, boolean move_up)
-    {
-	// Determine the current position of the index
-	int position = indexOf(index);
-	// Determine if it can be moved, ie if its not already at the top trying to move up, or at the bottom trying to move down.
-	if(position == -1) {
-	    return;
-	}
-	if(position == 0 && move_up) {
-	    return;
-	}
-	if(position == (getSize()) - 1 && !move_up) {
-	    return;
-	}
-	
-	// Ok, move up
-	if (move_up) {
-	    position--;
-	    remove(index);
-	    add(position, index);
-	}
-
-	// Or move down
-	else {
-	    position++;
-	    remove(index);
-	    add(position, index);
-	}
-    }
-
-
-
-    /** Method to remove a certain index.
-     * @param index the Index to remove.
-     * @see org.greenstone.gatherer.Gatherer
-     * @see org.greenstone.gatherer.cdm.CollectionDesignManager
-     * @see org.greenstone.gatherer.cdm.CollectionMetaManager
-     * @see org.greenstone.gatherer.collection.CollectionManager
-     */
-    private void removeIndex(Index index) {
-	if(index != null) {
-	    // Remove any current metadata from this index
-	    CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + index.getID());
-	    // Remove the index
-	    remove(index);
-	    // Check if the index removed happens to be the default index
-	    if(default_index != null && default_index.equals(index)) {
-		// If so our first solution is to set the first index to be default
-		if(getSize() > 0) {
-		    Index another_index = (Index) getElementAt(0);
-		    setDefault(another_index);
-		    another_index = null;
-		}
-		else {
-		    default_index.setAssigned(false);
-		}
-	    }
-	}
-    }
-
-
-    /* replace an index in the list. new index may have the same sources but a
-       different name, or may be a new index altogether */
-    private void replaceIndex(Index old_index, Index new_index,
-			      CollectionMeta coll_meta) {
-	if (old_index == null || new_index == null || coll_meta == null) {
-	    return;
-	}
-	if (!old_index.getID().equals(new_index.getID()) && contains(new_index)) {
-	    // shoudl we output an error??
-	    return;
-	}
-	// Remove the old index coll meta
-	CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + old_index.getID());
-	// Add the new coll meta
-	CollectionDesignManager.collectionmeta_manager.addMetadatum(coll_meta);
-
-	// get the position of the old one
-	int position = indexOf(old_index);
-	// remove it
-	remove(old_index);
-	// add the new one at that position
-	add(position, new_index);
-    }
-
-
-    /** Method to set the default index.
-     * @param index the new default Index
-     * @see org.greenstone.gatherer.Gatherer
-     * @see org.greenstone.gatherer.collection.CollectionManager
-     */
-    public void setDefault(Index index) {
-	if(index != null) {
-	    if(default_index == null) {
-		// Create the default index element, and place immediately after indexes element.
-		Element default_index_element = root.getOwnerDocument().createElement(StaticStrings.INDEX_DEFAULT_ELEMENT);             
-		if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
-		    default_index = new MGIndex(default_index_element);
-		} else {
-		    default_index = new Index(default_index_element);
-		}
-		Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
-		if(target_node != null) {
-		    root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
-		}
-		else {
-		    root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
-		}
-	    } else {
-		if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG) &&! (default_index instanceof org.greenstone.gatherer.cdm.MGIndex)) {
-		    default_index = new MGIndex(default_index.getElement());
-		} else if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && default_index instanceof org.greenstone.gatherer.cdm.MGIndex) {
-		    default_index = new Index(default_index.getElement());
-		}
-	    }
-	    default_index.setAssigned(true);
-	    default_index.setSources(index.getSources());
-	    if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
-		((MGIndex)default_index).setLevel(((MGIndex)index).getLevel());
-	    }
-	    
-	}
-	else {
-	    if(default_index != null) {
-		default_index.setAssigned(false);
-	    }
-	}
-    }
-
-
-    private class IndexControl
-	extends JPanel
-	implements Control {
-
-	private JList index_list;
-	private JButton move_down_button;
-	private JButton move_up_button;
-	private JButton set_default_button;
-
-	private JButton new_button;
-	private JButton edit_button;
-	private JButton remove_button;
-
-	public IndexControl() {
-	    super();
-            this.setComponentOrientation(Dictionary.getOrientation());
-	    // Creation
-	    JPanel assigned_indexes_pane = new JPanel();
-	    assigned_indexes_pane.setComponentOrientation(Dictionary.getOrientation());
-            
-	    JLabel index_label = new JLabel(Dictionary.get("CDM.IndexManager.Indexes"));
-            index_label.setComponentOrientation(Dictionary.getOrientation());
-            
-	    index_list = new JList(index_model);
-	    index_list.setCellRenderer(new IndexListRenderer());
-	    index_list.setVisibleRowCount(6);
-	    index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
-            index_list.addMouseListener(new ClickListener());
-            index_list.setComponentOrientation(Dictionary.getOrientation());
-            
-	    JPanel movement_pane = new JPanel();
-            movement_pane.setComponentOrientation(Dictionary.getOrientation());
-            
-	    move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
-	    move_up_button.setEnabled(false);	    
-            
-	    move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
-	    move_down_button.setEnabled(false);
-	    	    
-	    set_default_button = new GLIButton(Dictionary.get("CDM.IndexManager.Set_Default"), Dictionary.get("CDM.IndexManager.Set_Default_Tooltip"));
-	    set_default_button.setEnabled(false);
-	    
-	    JPanel button_pane = new JPanel();
-	    button_pane.setComponentOrientation(Dictionary.getOrientation());
-            
-            new_button = new GLIButton(Dictionary.get("CDM.IndexManager.New_Index")+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get("CDM.IndexManager.New_Index_Tooltip"));
-	    new_button.setEnabled(true);
-	    
-	    edit_button = new GLIButton(Dictionary.get("CDM.IndexManager.Edit_Index")+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get("CDM.IndexManager.Edit_Index_Tooltip"));
-	    edit_button.setEnabled(false);
-	    
-	    remove_button = new GLIButton(Dictionary.get("CDM.IndexManager.Remove_Index"), Dictionary.get("CDM.IndexManager.Remove_Index_Tooltip"));
-	    remove_button.setEnabled(false);
-	    	  
-	    // Listeners
-	    new_button.addActionListener(new NewIndexListener());
-	    edit_button.addActionListener(new EditIndexListener());
-	    remove_button.addActionListener(new RemoveIndexListener());
-	    remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
-
-	    index_list.addListSelectionListener(new IndexListListener());
-
-	    move_down_button.addActionListener(new MoveListener(false));
-	    move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
-	    move_up_button.addActionListener(new MoveListener(true));
-	    move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
-
-	    set_default_button.addActionListener(new SetDefaultListener());
-	    // Layout
-	    movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
-	    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));
-	    assigned_indexes_pane.setLayout(new BorderLayout());
-	    assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
-	    assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
-	    assigned_indexes_pane.add(movement_pane, BorderLayout.LINE_END);
-
-	    button_pane.setLayout(new GridLayout(1,3,5,0));
-	    button_pane.add(new_button);
-	    button_pane.add(edit_button);
-	    button_pane.add(remove_button);
-
-	    setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
-	    setLayout(new BorderLayout());
-	    add(assigned_indexes_pane, BorderLayout.CENTER);
-	    add(button_pane, BorderLayout.SOUTH);
-	    
-	}
-	public void loseFocus() {}
-	public void gainFocus() {}
-	public void destroy() {}
-  
-	/** Listens for double clicks apon the list and react as if the configure button was pushed. */
-	private class ClickListener
-	    extends MouseAdapter {
-	    /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt.
-	     * @param event A <strong>MouseEvent</strong> containing information about the mouse click.
-	    */
-    public void mouseClicked(MouseEvent event) {
-      if(event.getClickCount() == 2 ) {
-        if(!index_list.isSelectionEmpty()) {
-          Index index = (Index) index_list.getSelectedValue();
-          NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
-          nip.destroy();          
-        }
-      }
-    }
-  }
-
-  
-	private class IndexListListener
-	    implements ListSelectionListener {
-	    
-	    /** 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.
-	     */
-	    public void valueChanged(ListSelectionEvent event)
-	    {
-		if (event.getValueIsAdjusting()) {
-		    return;
-		}
-		
-                set_default_button.setEnabled(true);
-		Object value = index_list.getSelectedValue();
-		if (value == null) {
-		    move_down_button.setEnabled(false);
-		    move_up_button.setEnabled(false);
-		    remove_button.setEnabled(false);
-		    edit_button.setEnabled(false);
-		    set_default_button.setEnabled(false);
-		    return;
-		}
-
-		// Enable the buttons appropriately
-		remove_button.setEnabled(true);
-		edit_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();
-		move_up_button.setEnabled((i>0));
-		move_down_button.setEnabled((i<size-1));
-	    }
-	}
-
-	private class IndexListRenderer
-	    extends DefaultListCellRenderer {
-
-	    /** Return a component that has been configured to display the specified value. */
-	    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
-		JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
-		if(default_index != null && default_index.equals(value)) {
-		    component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
-		}
-		return component;
-	    }
-
-	}
-
-	private class NewIndexListener
-	    implements ActionListener {
-
-	    public void actionPerformed(ActionEvent event) {
-		NewIndexPrompt nip = new NewIndexPrompt(build_type, null);
-		nip.destroy();
-	    }
-	}
-	
-	private class EditIndexListener
-	    implements ActionListener {
-
-	    public void actionPerformed(ActionEvent event) {
-		Index index = (Index) index_list.getSelectedValue();
-		NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
-		nip.destroy();
-	    }
-	}
-
-	private class RemoveIndexListener
-	    implements ActionListener {
-	    
-	    public void actionPerformed(ActionEvent event) {
-		
-		int i = index_list.getSelectedIndex();
-		if (i != -1) {
-		    removeIndex((Index) index_list.getSelectedValue());
-		}
-		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
-	    }
-	}
-
-	private class MoveListener
-	    implements ActionListener {
-
-	    private boolean move_up;
-
-	    public MoveListener(boolean move_up) {
-		this.move_up = move_up;
-	    }
-
-	    public void actionPerformed(ActionEvent event) {
-		// Retrieve the selected index
-		Index index = (Index) index_list.getSelectedValue();
-		if(index != null) {
-		    moveIndex(index, move_up);
-		    // Ensure the index that moved is still selected
-		    index_list.setSelectedValue(index, true);
-		    index = null;
-		}
-	    }
-	}
-
-	private class SetDefaultListener
-	    implements ActionListener {
-
-	    public void actionPerformed(ActionEvent event) {
-		Index index = (Index) index_list.getSelectedValue();
-		if(index != null) {
-		    setDefault(index);
-		    // This should cause a repaint of just the desired row
-		    index_list.setSelectedValue(index, true);
-		}
-		set_default_button.setEnabled(false);
-	    }
-	}
-
-	
-
-	private class NewIndexPrompt 
-	    extends ModalDialog {
-
-	    NewIndexPrompt new_index_prompt = null;
-	
-	    private JCheckBox text_checkbox;
-	    private CheckList source_list;
-	    // mg uses a level box
-	    private JComboBox level_combobox;
-	    // mgpp has a allfields selector
-	    private JCheckBox allfields_box;
-
-	    private JButton add_or_replace_button;
-	    private JButton select_all_button;
-	    private JButton select_none_button;
-	    private JButton add_all_button;
-	    //private JButton select_all_button;
-	    //private JButton replace_button;
-	    private JButton cancel_button;
-
-	    private boolean mgpp_enabled = false;
-	    private boolean editing = false;
-	
-	    public NewIndexPrompt(String build_type, Index existing_index) {
-		super(Gatherer.g_man, true);
-                this.setComponentOrientation(Dictionary.getOrientation());
-                
-		new_index_prompt = this;
-
-		setModal(true);
-		setSize(PROMPT_SIZE);
-		if (existing_index != null) {
-		    setTitle (Dictionary.get("CDM.IndexManager.Edit_Index"));
-		    editing = true;
-		} else {
-		    setTitle(Dictionary.get("CDM.IndexManager.New_Index"));
-		}
-		
-		setJMenuBar(new SimpleMenuBar("searchindexes"));
-		if (build_type.equals(BuildTypeManager.BUILD_TYPE_MGPP) || build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
-		    mgpp_enabled = true;
-		}
-		JPanel content_pane = (JPanel)this.getContentPane();
-                content_pane.setComponentOrientation(Dictionary.getOrientation());
-		ArrayList new_data = new ArrayList();
-		new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
-
-		JPanel details_pane = new JPanel();
-                details_pane.setComponentOrientation(Dictionary.getOrientation());
-		
-		text_checkbox = new JCheckBox(Dictionary.get("CDM.IndexManager.Text_Source"));
-		text_checkbox.setToolTipText(Dictionary.get("CDM.IndexManager.Text_Source_Tooltip"));
-                text_checkbox.setComponentOrientation(Dictionary.getOrientation());                
-		text_checkbox.addActionListener(new ActionListener() {
-			public void actionPerformed(ActionEvent event) {
-			    validateAddOrReplaceButton();
-			}
-		    });
-		JLabel source_label = new JLabel(Dictionary.get("CDM.IndexManager.Source"));
-                source_label.setComponentOrientation(Dictionary.getOrientation());
-		source_list = new CheckList(false);                
-		source_list.setListData(new_data);
-		source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
-		source_list.addListSelectionListener(new SourceListListener());
-
-		JPanel button_pane = new JPanel();
-		button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
-		button_pane.setComponentOrientation(Dictionary.getOrientation());
-                
-		if (existing_index == null && mgpp_enabled) {
-		    button_pane.setLayout(new GridLayout(2,3,5,0));
-                    JPanel tmp = new JPanel();
-                    tmp.setComponentOrientation(Dictionary.getOrientation());
-		    button_pane.add(tmp);
-		} else {
-		    button_pane.setLayout(new GridLayout(2,2,5,0));
-		}
-		
-		select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
-		select_all_button.addActionListener(new ActionListener() {
-			
-			public void actionPerformed(ActionEvent event) {
-			    text_checkbox.setSelected(true);
-			    source_list.setAllTicked();
-			    validateAddOrReplaceButton();
-			}
-		    });
-		
-		select_none_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_None"), Dictionary.get("CDM.IndexManager.Select_None_Tooltip"));
-		select_none_button.addActionListener(new ActionListener() {
-			
-			public void actionPerformed(ActionEvent event) {
-			    text_checkbox.setSelected(false);
-			    source_list.clearTicked();
-			    validateAddOrReplaceButton();
-			}
-		    });
-		
-		
-		button_pane.add(select_all_button);
-		button_pane.add(select_none_button);
-		
-		if (existing_index != null) {
-		    add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Replace_Index"), Dictionary.get("CDM.IndexManager.Replace_Index_Tooltip"));
-		    add_or_replace_button.addActionListener(new ReplaceIndexListener());
-		} else {
-		    add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_Index"), Dictionary.get("CDM.IndexManager.Add_Index_Tooltip"));
-		    add_or_replace_button.addActionListener(new AddIndexListener());
-		}
-
-		add_or_replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
-		add_or_replace_button.setEnabled(false);
-		button_pane.add(add_or_replace_button);
-		
-		if (existing_index == null && mgpp_enabled) {
-		    add_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
-		    add_all_button.setEnabled(true);
-		    add_all_button.addActionListener(new AddAllIndexActionListener());
-		    button_pane.add(add_all_button);
-		} 
-		
-		// always have a cancel button
-		cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
-		cancel_button.setEnabled(true);
-		cancel_button.addActionListener(new ActionListener() {
-			public void actionPerformed(ActionEvent event) {
-			    new_index_prompt.dispose();
-			}
-		    });
-		button_pane.add(cancel_button);
-
-		//Layout
-		details_pane.setLayout(new BorderLayout(10,10));
-		details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
-		details_pane.add(text_checkbox, BorderLayout.NORTH);
-		details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
-
-
-		// do type specific stuff
-		if (mgpp_enabled) {
-		    // allfields
-		    allfields_box = new JCheckBox(Dictionary.get("CDM.IndexManager.Allfields_Index"));
-		    allfields_box.addItemListener(new AllFieldsBoxListener());
-		    allfields_box.setComponentOrientation(Dictionary.getOrientation());
-                    //JLabel allfields_label = new JLabel(Dictionary.get("CDM.IndexManager.Allfields_Index"));
-		    details_pane.add(allfields_box, BorderLayout.SOUTH);
-		    
-		    
-		} else {
-		    // index level
-		    JLabel level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
-		    level_label.setComponentOrientation(Dictionary.getOrientation());
-                    
-                    level_combobox = new JComboBox();
-		    level_combobox.setOpaque(false);
-		    level_combobox.setPreferredSize(FIELD_SIZE);
-		    // Note the order of these must be the same as the
-		    // level order in Index
-		    level_combobox.addItem(StaticStrings.DOCUMENT_STR);//Dictionary.get("CDM.LevelManager.Document"));
-		    level_combobox.addItem(StaticStrings.SECTION_STR);//Dictionary.get("CDM.LevelManager.Section"));
-		    level_combobox.addItem(StaticStrings.PARAGRAPH_STR);//Dictionary.get("CDM.LevelManager.Paragraph"));
-		    level_combobox.setEditable(false);
-		    level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
-		    level_combobox.setComponentOrientation(Dictionary.getOrientation());
-                    level_combobox.addActionListener(new ActionListener() {
-			    public void actionPerformed(ActionEvent event) {
-				validateAddOrReplaceButton();
-			    }
-			});
-		    JPanel level_pane = new JPanel();
-                    level_pane.setComponentOrientation(Dictionary.getOrientation());
-		    level_pane.setLayout(new BorderLayout());
-		    level_pane.add(level_label, BorderLayout.LINE_START);
-		    level_pane.add(level_combobox, BorderLayout.CENTER);
-		    details_pane.add(level_pane, BorderLayout.SOUTH);
-
-		}
-		// if we are editing, fill in the controls
-		if (existing_index !=null) {
-		    ArrayList sources = existing_index.getSources();
-		    if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
-			allfields_box.setSelected(true);
-			source_list.setEnabled(false);
-		    } else {
-			source_list.setTickedObjects(sources.toArray());
-			source_list.setEnabled(true);
-			if (sources.contains(StaticStrings.TEXT_STR)) {
-			    text_checkbox.setSelected(true);
-			}
-		    } 
-		    if (!mgpp_enabled) {
-			level_combobox.setSelectedIndex(((MGIndex)existing_index).getLevel());
-		    }
-		    
-		}
-		content_pane.setLayout(new BorderLayout());
-		content_pane.add(source_label, BorderLayout.NORTH);
-		content_pane.add(details_pane, BorderLayout.CENTER);
-		content_pane.add(button_pane, BorderLayout.SOUTH);
-	    
-		// Display on screen.
-		Dimension screen_size = Configuration.screen_size;
-		setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
-		screen_size = null;
-		setVisible(true);
-	    
-	    }
-
-	    /** Method which actually forces the dialog to be shown on screen.
-	     * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
-	     */
-	    public boolean display() {
-		setVisible(true);
-		return true;
-		//    return success;
-	    }
-	
-	    public void destroy() {
-	    }
-
-	    // Checks that specified index not already in the collection
-	    private void validateAddOrReplaceButton() {
-		Index index;
-		ArrayList sources;
-		if (mgpp_enabled && allfields_box.isSelected()) {
-		    sources = new ArrayList();
-		    sources.add(ALLFIELDS);
-		    index = new Index(sources);
-		    
-		} else if (text_checkbox.isSelected() || 
-			   !source_list.isNothingTicked()) {
-		    sources = source_list.getTicked();
-		    if (text_checkbox.isSelected()) {
-			sources.add(0, StaticStrings.TEXT_STR);
-		    }		    
-		    if (mgpp_enabled) {
-			index = new Index(sources);
-		    } else {
-			index = new MGIndex(level_combobox.getSelectedIndex(), sources);
-		    }
-		} else {
-		    // nothing selected
-		    add_or_replace_button.setEnabled(false);
-		    return;
-		}
-		
-		sources = null;
-		if (index_model.contains(index)) {
-		    add_or_replace_button.setEnabled(false);
-		}
-		else {
-		    add_or_replace_button.setEnabled(true);
-		}
-		
-	    }
-
-	    private Index generateNewIndex() {
-		Index index = null;
-		ArrayList sources;
-		if (mgpp_enabled && allfields_box.isSelected()) {
-		    sources = new ArrayList();
-		    sources.add(ALLFIELDS);
-		    index = new Index(sources);
-		}
-		else if (text_checkbox.isSelected() || !source_list.isNothingTicked()) {
-		    sources = source_list.getTicked();
-		    if (text_checkbox.isSelected()) {
-			sources.add(0, StaticStrings.TEXT_STR);
-		    }
-		    if(mgpp_enabled) {
-			index = new Index(sources);
-		    } else {
-			index = new MGIndex(level_combobox.getSelectedIndex(), sources);
-		    }
-		}
-		return index;
-		 
-
-	    }
-	    
-	    private CollectionMeta generateCollectionMeta(Index index) {
-		CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
-		if (use_macro_as_display_name(index.getID())) {
-		    metadatum.setValue(get_macro_name(index.getID()));
-		} else {
-		    metadatum.setValue(index.getID());
-		}
-		return metadatum;
-	    }
-	    
-	    private class AddIndexListener
-		implements ActionListener
-	    {
-		public void actionPerformed(ActionEvent event)
-		{
-		    Index index = generateNewIndex();
-		    if (index != null) {
-			
-			CollectionMeta metadatum = generateCollectionMeta(index);
-			// Add the index
-			addIndex(index, metadatum);
-			index_list.setSelectedValue(index, true);
-		    }
-		    new_index_prompt.dispose();		    
-		}
-	    }
-
-	    /** add all sources as separate indexes (fields).  */
-	    private class AddAllIndexActionListener
-		implements ActionListener {
-
-		public void actionPerformed(ActionEvent event) {
-		    ArrayList all_sources = source_list.getAll();
-		    all_sources.add(0, StaticStrings.TEXT_STR);
-		    ArrayList new_sources = new ArrayList();
-		    for(int i = 0; i < all_sources.size(); i++) {
-			Object source = all_sources.get(i);
-
-			// Create new index
-			new_sources.clear();
-			new_sources.add(source);
-			Index index = new Index(new_sources);
-			if(!index_model.contains(index)) {
-			    // Determine the metadatum value
-			    String name = source.toString();
-			    if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
-				name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
-			    }
-			    // Create new metadatum
-			    CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
-			    metadatum.setValue(name);
-			    name = null;
-			    // Assign new index
-			    addIndex(index, metadatum);
-			}
-			source = null;
-			index = null;
-		    }
-		    new_sources = null;
-		    new_index_prompt.dispose();
-		
-		}
-	    }
-    
-	    private class AllFieldsBoxListener
-		implements ItemListener {
-		
-		public void itemStateChanged(ItemEvent event) {
-		    if (event.getStateChange() == ItemEvent.SELECTED) {
-			source_list.setEnabled(false);
-		    } else if (event.getStateChange() == ItemEvent.DESELECTED) {
-			source_list.setEnabled(true);
-		    }
-		    validateAddOrReplaceButton();
-		}
-		
-	    }
-
-	    private class SourceListListener
-		implements ListSelectionListener {
-	    
-		public void valueChanged(ListSelectionEvent event) {
-		    if (event.getValueIsAdjusting()) {
-			return;
-		    }
-		    validateAddOrReplaceButton();
-		}
-	    }
-	
-
-	    private class ReplaceIndexListener
-		implements ActionListener {
-	    
-		public void actionPerformed(ActionEvent event)
-		{
-		    Index index = generateNewIndex();
-		    if (index != null) {
-			CollectionMeta metadatum = generateCollectionMeta(index);
-			// replace the index
-			replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
-			index_list.setSelectedValue(index, true);
-		    }
-		    new_index_prompt.dispose();
-		}
-	    }
-
-	    /**
-	     * If the index is built with DC metadata, use macro variable as the display text, 
-	     * so that translations of DC metadata can be displayed in the search field drop-down list 
-	     * when interface language changes.
-	     *   
-	     * @param index_id Current index id.
-	     * @return Whether macro variable should be used.
-	     */
-	    private boolean use_macro_as_display_name(String index_id) {
-		//String metaname = index_id;	
-		if (index_id.indexOf(":") != -1) {
-		    index_id = index_id.substring(index_id.indexOf(":") + 1);
-		}
-		
-		String field = null;
-		String[] fields = index_id.split(",");
-		for (int i = 0; i < fields.length; i++) {
-		    String s = fields[i];
-		    if (s.indexOf(".") != -1) {	  
-			s = s.substring(s.indexOf(".") + 1);
-		    }
-		    if (field == null) {
-			field = s;
-		    }
-		    else if (!field.equals(s)) {
-			return false;
-		    }
-		}
-				
-		field = field.toLowerCase();
-		if (field.equals("text") || field.equals("title") || field.equals("creator") || field.equals("subject") || 
-		    field.equals("description") || field.equals("publisher") || field.equals("contributor") || field.equals("date") || 
-		    field.equals("type") || field.equals("format") || field.equals("identifier") || field.equals("source") || 
-		    field.equals("language") || field.equals("relation") || field.equals("coverage") || field.equals("rights")) {			  
-		    return true;
-		}
-		
-		return false;
-	    }
-
-	    /**
-	     * Get the corresponding macro variable name, eg. _labelTitle_, _labelCreator_.
-	     *   
-	     * @param index_id Current index id.
-	     * @return Name of the macro variable.
-	     */
-	    private String get_macro_name(String index_id) {
-		if (index_id.indexOf(":") != -1) {
-		    index_id = index_id.substring(index_id.indexOf(":") + 1);
-		}
-		if (index_id.indexOf(",") != -1) {
-		    index_id = index_id.substring(0, index_id.indexOf(","));
-		}
-		if (index_id.indexOf(".") != -1) {	  
-		    index_id = index_id.substring(index_id.indexOf(".") + 1);
-		}	
-		
-		return "_label" + index_id + "_";		
-	    }
-	}
-    }
-}
Index: main/trunk/gli/src/org/greenstone/gatherer/cdm/SearchIndexManager.java
===================================================================
--- main/trunk/gli/src/org/greenstone/gatherer/cdm/SearchIndexManager.java	(revision 36148)
+++ main/trunk/gli/src/org/greenstone/gatherer/cdm/SearchIndexManager.java	(revision 36148)
@@ -0,0 +1,1121 @@
+/**
+ *#########################################################################
+ *
+ * A component of the Gatherer application, part of the Greenstone digital
+ * library suite from the New Zealand Digital Library Project at the
+ * University of Waikato, New Zealand.
+ *
+ * Author: John Thompson, Greenstone Digital Library, University of Waikato
+ *
+ * Copyright (C) 1999 New Zealand Digital Library Project
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *########################################################################
+ */
+package org.greenstone.gatherer.cdm;
+
+import java.awt.*;
+import java.awt.event.*;
+import java.util.*;
+import javax.swing.*;
+import javax.swing.event.*;
+import org.greenstone.gatherer.Configuration;
+import org.greenstone.gatherer.DebugStream;
+import org.greenstone.gatherer.Dictionary;
+import org.greenstone.gatherer.Gatherer;
+import org.greenstone.gatherer.gui.DesignPaneHeader;
+import org.greenstone.gatherer.gui.GComboBox;
+import org.greenstone.gatherer.gui.GLIButton;
+import org.greenstone.gatherer.gui.ModalDialog;
+import org.greenstone.gatherer.gui.SimpleMenuBar;
+import org.greenstone.gatherer.metadata.MetadataElement;
+import org.greenstone.gatherer.metadata.MetadataSetManager;
+import org.greenstone.gatherer.util.CheckList;
+import org.greenstone.gatherer.util.JarTools;
+import org.greenstone.gatherer.util.XMLTools;
+import org.greenstone.gatherer.util.StaticStrings;
+import org.w3c.dom.*;
+/** This class is resposible for storing the indexes which have been assigned to this collection and the default index, and providing methods for interacting with both these data pools. It also knows how to turn itself into a String as it would be displayed in the collection configuration file.
+ * @author John Thompson, Greenstone Digital Library, University of Waikato
+ * @version 2.3
+ */
+public class IndexManager
+    extends DOMProxyListModel 
+    implements BuildTypeManager.BuildTypeListener {
+
+    /** A reference to ourselves so our inner methods have access. */
+    private DOMProxyListModel index_model = null;
+    /** The default index. */
+    private Index default_index = null;
+
+    private Control controls = null;
+    private String build_type = null;
+    
+    static final private Dimension FIELD_SIZE = new Dimension(200,30);
+    static final private String ALLFIELDS = "allfields";
+
+    static final private Dimension PROMPT_SIZE = new Dimension(400,400);
+    public IndexManager(Element indexes, String current_build_type) {
+
+	super(indexes, StaticStrings.INDEX_ELEMENT, (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)?new MGIndex():new Index()));
+	DebugStream.println("IndexManager: " + getSize() + " indexes parsed.");
+	index_model = this;
+      
+	// Parse and retrieve the default index
+	NodeList default_index_elements = CollectionConfiguration.getElementsByTagName(StaticStrings.INDEX_DEFAULT_ELEMENT);
+	if(default_index_elements.getLength() > 0) {
+	    if (current_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
+		default_index = new MGIndex((Element)default_index_elements.item(0));
+	    } else {
+		default_index = new Index((Element)default_index_elements.item(0));
+	    }
+	}
+	build_type = current_build_type;
+    }
+
+    /** Method to add a new index.
+     * @param index The <strong>Index</strong> to add.
+     * @see org.greenstone.gatherer.Gatherer
+     * @see org.greenstone.gatherer.collection.CollectionManager
+     */
+    private void addIndex(Index index, CollectionMeta metadatum) {
+	///ystem.err.println("Adding an index: " + index.toString());
+	if(!contains(index)) {
+	    CollectionDesignManager.collectionmeta_manager.addMetadatum(metadatum);
+	    // Retrieve the currently last index
+	    if(getSize() > 0) {
+		Index last_index = (Index)getElementAt(getSize() - 1);
+		addAfter(index, last_index);
+		
+	    }
+	    else {
+		add(index);
+		// Also set this index as the default one, 
+		setDefault(index);
+	    }
+	}
+	else {
+	    JOptionPane.showMessageDialog(Gatherer.g_man, Dictionary.get("CDM.IndexManager.Index_Exists"), Dictionary.get("General.Warning"), JOptionPane.WARNING_MESSAGE);
+	}
+    }
+
+    public void buildTypeChanged(String new_build_type) {
+	if (build_type.equals(new_build_type)) {
+	    return;
+	}
+	// we don;t care about this if old or new is not MG as MGPP and 
+	// Lucene have the same index specification
+	if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && !new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
+	    return;
+	}
+	boolean mg_to_mgpp = true;
+	if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
+	    mg_to_mgpp = false;
+	}
+	if (mg_to_mgpp) {
+	    changeToMGPPIndexes();
+	} else {
+	    changeToMGIndexes();
+	}
+	build_type  = new_build_type;
+	// its really hard to transfer defaults between mgpp/lucene and mg indexes, so we'll just set the first one to be the default
+	Index first_index = (Index) getElementAt(0);
+	setDefault(first_index);
+	first_index = null;
+    }
+    
+    private void changeToMGIndexes() {
+	this.setClassType(new MGIndex());
+	Element mgpp_element = root;
+	// Retrieve and assign MG element and default index element
+	Element mg_element = CollectionDesignManager.collect_config.getMGIndexes();
+	mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+	NodeList indexes = mg_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
+	// Replace mgpp element with mg element
+	setRoot(mg_element);
+	
+	if(indexes.getLength() == 0) {
+	  // we need to create some based on the mgpp indexes
+	  
+	  // If the current mgpp index includes a text one, then generate text indexes for each of the registered levels.
+	  Index index = getIndex(StaticStrings.TEXT_STR);
+	  if(index != null) {
+	    ArrayList levels = CollectionDesignManager.index_manager.getLevels();
+	    int level_size = levels.size();
+	    for(int i = 0; i < level_size; i++) {
+	      IndexOption level = (IndexOption) levels.get(i);
+	      Index new_index = new MGIndex(level.getName(), index.getSources());
+	      // Try to retrieve existing metadatum
+	      String source_str = new_index.getID();
+	      CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
+	      // If no metadata was found, add new pseudo metadata using the id
+	      if(metadatum == null) {
+		metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
+		metadatum.setAssigned(true);
+		metadatum.setValue(source_str);
+	      }
+	      // If it was found, ensure it is assigned
+	      else {
+		metadatum.setAssigned(true);
+	      }
+	      source_str = null;
+	      addIndex(new_index, metadatum);
+	      new_index = null;
+	      level = null;
+	    }
+	  }
+	}
+	
+	// Unassign mgpp element 
+	mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
+	mgpp_element = null;
+	
+    }
+
+    private void changeToMGPPIndexes() {
+	this.setClassType(new Index());
+	Element mg_element = root;
+	// Retrieve and assign the MGPP indexes element.
+	Element mgpp_element = CollectionDesignManager.collect_config.getMGPPIndexes();
+	mgpp_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.TRUE_STR);
+	NodeList indexes = mgpp_element.getElementsByTagName(StaticStrings.INDEX_ELEMENT);
+	if(indexes.getLength() != 0) {
+	    // we just reinstate the indexes we had before the change
+	    setRoot(mgpp_element);
+	} else {
+	    // If the MGPP indexes element is empty (ie was created by CollectionConfiguration), generate new MGPP index from the existing index
+	    
+	    ArrayList sources_list = new ArrayList();
+
+	    // We first use details from the default index if any
+	    if(default_index != null) {
+		ArrayList sources = default_index.getSources();
+		sources_list.addAll(sources);
+	    }
+	    int size = getSize();
+	    for(int i = 0; i < size; i++) {
+		Index index = (Index) getElementAt(i);
+		ArrayList sources = index.getSources();
+		sources.removeAll(sources_list);
+		sources_list.addAll(sources);
+		index = null;
+	    }
+	    // Replace mg element with mgpp element
+	    setRoot(mgpp_element);
+	    
+	    // We now have a list of sources, so create new indexes based on these
+	    int sources_list_size = sources_list.size();
+	    for(int j = 0; j < sources_list_size; j++) {
+		Object source_object = sources_list.get(j);
+		String source_str = null;
+		if(source_object instanceof MetadataElement) {
+		    source_str = ((MetadataElement) source_object).getFullName();
+		}
+		else {
+		    source_str = source_object.toString();
+		}
+		ArrayList new_sources = new ArrayList();
+		new_sources.add(source_object);
+		source_object = null;
+		Index new_index = new Index(new_sources);
+		// Try to retrieve existing metadatum
+		source_str = new_index.getID();
+		CollectionMeta metadatum = CollectionDesignManager.collectionmeta_manager.getMetadatum(StaticStrings.STOP_CHARACTER + source_str, false);
+		// If no metadata was found, add new pseudo metadata using the id
+		if(metadatum == null) {
+		    metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + source_str);
+		    metadatum.setAssigned(true);
+		    metadatum.setValue(source_str);
+		}
+		// If it was found, ensure it is assigned
+		else {
+		    metadatum.setAssigned(true);
+		}
+		source_str = null;
+		addIndex(new_index, metadatum);
+		metadatum = null;
+		new_index = null;
+		new_sources = null;
+		source_str = null;
+	    }	      
+	    
+	}
+
+	// Unassign MG element
+	mg_element.setAttribute(StaticStrings.ASSIGNED_ATTRIBUTE, StaticStrings.FALSE_STR);
+	mg_element = null;
+	
+    }
+    
+    public Control getControls() {
+	if (controls == null) {
+	    controls = new IndexControl();
+	}
+	return controls;
+    }
+
+    /** Method to retrieve a certain index, as referenced by an index number.
+     * @param index An <i>int</i> which indicates the position of the desired index.
+     * @return The <strong>Index</strong> at the given index, or <i>null</i> if no such index exists.
+     */
+    public Index getIndex(int index) {
+	if(0 <= index && index < getSize()) {
+	    return (Index)getElementAt(index);
+	}
+	return null;
+    }
+
+    /** Method to retrieve a certain index, given its id.
+     * @param id the id of the index as a String
+     * @return the Index that matches id, or null if no such index exists
+     */
+    public Index getIndex(String id) {
+	int size = getSize();
+	for(int i = 0; i < size; i++) {
+	    Index index = (Index) getElementAt(i);
+	    if(index.getID().equals(id)) {
+		return index;
+	    }
+	}
+	return null;
+    }
+
+    public ArrayList getIndexes() {
+	return children();
+    }
+
+
+
+    /** Called when the detail mode has changed which in turn may cause several design elements to be available/hidden
+     * @param mode the new mode as an int
+     */
+    public void modeChanged(int mode) {
+
+    }
+
+    private void moveIndex(Index index, boolean move_up)
+    {
+	// Determine the current position of the index
+	int position = indexOf(index);
+	// Determine if it can be moved, ie if its not already at the top trying to move up, or at the bottom trying to move down.
+	if(position == -1) {
+	    return;
+	}
+	if(position == 0 && move_up) {
+	    return;
+	}
+	if(position == (getSize()) - 1 && !move_up) {
+	    return;
+	}
+	
+	// Ok, move up
+	if (move_up) {
+	    position--;
+	    remove(index);
+	    add(position, index);
+	}
+
+	// Or move down
+	else {
+	    position++;
+	    remove(index);
+	    add(position, index);
+	}
+    }
+
+
+
+    /** Method to remove a certain index.
+     * @param index the Index to remove.
+     * @see org.greenstone.gatherer.Gatherer
+     * @see org.greenstone.gatherer.cdm.CollectionDesignManager
+     * @see org.greenstone.gatherer.cdm.CollectionMetaManager
+     * @see org.greenstone.gatherer.collection.CollectionManager
+     */
+    private void removeIndex(Index index) {
+	if(index != null) {
+	    // Remove any current metadata from this index
+	    CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + index.getID());
+	    // Remove the index
+	    remove(index);
+	    // Check if the index removed happens to be the default index
+	    if(default_index != null && default_index.equals(index)) {
+		// If so our first solution is to set the first index to be default
+		if(getSize() > 0) {
+		    Index another_index = (Index) getElementAt(0);
+		    setDefault(another_index);
+		    another_index = null;
+		}
+		else {
+		    default_index.setAssigned(false);
+		}
+	    }
+	}
+    }
+
+
+    /* replace an index in the list. new index may have the same sources but a
+       different name, or may be a new index altogether */
+    private void replaceIndex(Index old_index, Index new_index,
+			      CollectionMeta coll_meta) {
+	if (old_index == null || new_index == null || coll_meta == null) {
+	    return;
+	}
+	if (!old_index.getID().equals(new_index.getID()) && contains(new_index)) {
+	    // shoudl we output an error??
+	    return;
+	}
+	// Remove the old index coll meta
+	CollectionDesignManager.collectionmeta_manager.removeMetadata(StaticStrings.STOP_CHARACTER + old_index.getID());
+	// Add the new coll meta
+	CollectionDesignManager.collectionmeta_manager.addMetadatum(coll_meta);
+
+	// get the position of the old one
+	int position = indexOf(old_index);
+	// remove it
+	remove(old_index);
+	// add the new one at that position
+	add(position, new_index);
+    }
+
+
+    /** Method to set the default index.
+     * @param index the new default Index
+     * @see org.greenstone.gatherer.Gatherer
+     * @see org.greenstone.gatherer.collection.CollectionManager
+     */
+    public void setDefault(Index index) {
+	if(index != null) {
+	    if(default_index == null) {
+		// Create the default index element, and place immediately after indexes element.
+		Element default_index_element = root.getOwnerDocument().createElement(StaticStrings.INDEX_DEFAULT_ELEMENT);             
+		if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
+		    default_index = new MGIndex(default_index_element);
+		} else {
+		    default_index = new Index(default_index_element);
+		}
+		Node target_node = CollectionConfiguration.findInsertionPoint(default_index_element);
+		if(target_node != null) {
+		    root.getOwnerDocument().getDocumentElement().insertBefore(default_index_element, target_node);
+		}
+		else {
+		    root.getOwnerDocument().getDocumentElement().appendChild(default_index_element);
+		}
+	    } else {
+		if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG) &&! (default_index instanceof org.greenstone.gatherer.cdm.MGIndex)) {
+		    default_index = new MGIndex(default_index.getElement());
+		} else if (!build_type.equals(BuildTypeManager.BUILD_TYPE_MG) && default_index instanceof org.greenstone.gatherer.cdm.MGIndex) {
+		    default_index = new Index(default_index.getElement());
+		}
+	    }
+	    default_index.setAssigned(true);
+	    default_index.setSources(index.getSources());
+	    if (build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
+		((MGIndex)default_index).setLevel(((MGIndex)index).getLevel());
+	    }
+	    
+	}
+	else {
+	    if(default_index != null) {
+		default_index.setAssigned(false);
+	    }
+	}
+    }
+
+
+    private class IndexControl
+	extends JPanel
+	implements Control {
+
+	private JList index_list;
+	private JButton move_down_button;
+	private JButton move_up_button;
+	private JButton set_default_button;
+
+	private JButton new_button;
+	private JButton edit_button;
+	private JButton remove_button;
+
+	public IndexControl() {
+	    super();
+            this.setComponentOrientation(Dictionary.getOrientation());
+	    // Creation
+	    JPanel assigned_indexes_pane = new JPanel();
+	    assigned_indexes_pane.setComponentOrientation(Dictionary.getOrientation());
+            
+	    JLabel index_label = new JLabel(Dictionary.get("CDM.IndexManager.Indexes"));
+            index_label.setComponentOrientation(Dictionary.getOrientation());
+            
+	    index_list = new JList(index_model);
+	    index_list.setCellRenderer(new IndexListRenderer());
+	    index_list.setVisibleRowCount(6);
+	    index_list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
+            index_list.addMouseListener(new ClickListener());
+            index_list.setComponentOrientation(Dictionary.getOrientation());
+            
+	    JPanel movement_pane = new JPanel();
+            movement_pane.setComponentOrientation(Dictionary.getOrientation());
+            
+	    move_up_button = new GLIButton(Dictionary.get("CDM.Move.Move_Up"), JarTools.getImage("arrow-up.gif"), Dictionary.get("CDM.Move.Move_Up_Tooltip"));
+	    move_up_button.setEnabled(false);	    
+            
+	    move_down_button = new GLIButton(Dictionary.get("CDM.Move.Move_Down"), JarTools.getImage("arrow-down.gif"), Dictionary.get("CDM.Move.Move_Down_Tooltip"));
+	    move_down_button.setEnabled(false);
+	    	    
+	    set_default_button = new GLIButton(Dictionary.get("CDM.IndexManager.Set_Default"), Dictionary.get("CDM.IndexManager.Set_Default_Tooltip"));
+	    set_default_button.setEnabled(false);
+	    
+	    JPanel button_pane = new JPanel();
+	    button_pane.setComponentOrientation(Dictionary.getOrientation());
+            
+            new_button = new GLIButton(Dictionary.get("CDM.IndexManager.New_Index")+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get("CDM.IndexManager.New_Index_Tooltip"));
+	    new_button.setEnabled(true);
+	    
+	    edit_button = new GLIButton(Dictionary.get("CDM.IndexManager.Edit_Index")+StaticStrings.FURTHER_DIALOG_INDICATOR, Dictionary.get("CDM.IndexManager.Edit_Index_Tooltip"));
+	    edit_button.setEnabled(false);
+	    
+	    remove_button = new GLIButton(Dictionary.get("CDM.IndexManager.Remove_Index"), Dictionary.get("CDM.IndexManager.Remove_Index_Tooltip"));
+	    remove_button.setEnabled(false);
+	    	  
+	    // Listeners
+	    new_button.addActionListener(new NewIndexListener());
+	    edit_button.addActionListener(new EditIndexListener());
+	    remove_button.addActionListener(new RemoveIndexListener());
+	    remove_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
+
+	    index_list.addListSelectionListener(new IndexListListener());
+
+	    move_down_button.addActionListener(new MoveListener(false));
+	    move_down_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
+	    move_up_button.addActionListener(new MoveListener(true));
+	    move_up_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
+
+	    set_default_button.addActionListener(new SetDefaultListener());
+	    // Layout
+	    movement_pane.setBorder(BorderFactory.createEmptyBorder(0,2,0,0));
+	    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));
+	    assigned_indexes_pane.setLayout(new BorderLayout());
+	    assigned_indexes_pane.add(index_label, BorderLayout.NORTH);
+	    assigned_indexes_pane.add(new JScrollPane(index_list), BorderLayout.CENTER);
+	    assigned_indexes_pane.add(movement_pane, BorderLayout.LINE_END);
+
+	    button_pane.setLayout(new GridLayout(1,3,5,0));
+	    button_pane.add(new_button);
+	    button_pane.add(edit_button);
+	    button_pane.add(remove_button);
+
+	    setBorder(BorderFactory.createEmptyBorder(0,5,0,0));
+	    setLayout(new BorderLayout());
+	    add(assigned_indexes_pane, BorderLayout.CENTER);
+	    add(button_pane, BorderLayout.SOUTH);
+	    
+	}
+	public void loseFocus() {}
+	public void gainFocus() {}
+	public void destroy() {}
+  
+	/** Listens for double clicks apon the list and react as if the configure button was pushed. */
+	private class ClickListener
+	    extends MouseAdapter {
+	    /** Called whenever the mouse is clicked over a registered component, we use this to chain through to the configure prompt.
+	     * @param event A <strong>MouseEvent</strong> containing information about the mouse click.
+	    */
+    public void mouseClicked(MouseEvent event) {
+      if(event.getClickCount() == 2 ) {
+        if(!index_list.isSelectionEmpty()) {
+          Index index = (Index) index_list.getSelectedValue();
+          NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
+          nip.destroy();          
+        }
+      }
+    }
+  }
+
+  
+	private class IndexListListener
+	    implements ListSelectionListener {
+	    
+	    /** 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.
+	     */
+	    public void valueChanged(ListSelectionEvent event)
+	    {
+		if (event.getValueIsAdjusting()) {
+		    return;
+		}
+		
+                set_default_button.setEnabled(true);
+		Object value = index_list.getSelectedValue();
+		if (value == null) {
+		    move_down_button.setEnabled(false);
+		    move_up_button.setEnabled(false);
+		    remove_button.setEnabled(false);
+		    edit_button.setEnabled(false);
+		    set_default_button.setEnabled(false);
+		    return;
+		}
+
+		// Enable the buttons appropriately
+		remove_button.setEnabled(true);
+		edit_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();
+		move_up_button.setEnabled((i>0));
+		move_down_button.setEnabled((i<size-1));
+	    }
+	}
+
+	private class IndexListRenderer
+	    extends DefaultListCellRenderer {
+
+	    /** Return a component that has been configured to display the specified value. */
+	    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
+		JLabel component = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
+		if(default_index != null && default_index.equals(value)) {
+		    component.setText(component.getText() + " " + Dictionary.get("CDM.IndexManager.Default_Index_Indicator"));
+		}
+		return component;
+	    }
+
+	}
+
+	private class NewIndexListener
+	    implements ActionListener {
+
+	    public void actionPerformed(ActionEvent event) {
+		NewIndexPrompt nip = new NewIndexPrompt(build_type, null);
+		nip.destroy();
+	    }
+	}
+	
+	private class EditIndexListener
+	    implements ActionListener {
+
+	    public void actionPerformed(ActionEvent event) {
+		Index index = (Index) index_list.getSelectedValue();
+		NewIndexPrompt nip = new NewIndexPrompt(build_type, index);
+		nip.destroy();
+	    }
+	}
+
+	private class RemoveIndexListener
+	    implements ActionListener {
+	    
+	    public void actionPerformed(ActionEvent event) {
+		
+		int i = index_list.getSelectedIndex();
+		if (i != -1) {
+		    removeIndex((Index) index_list.getSelectedValue());
+		}
+		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
+	    }
+	}
+
+	private class MoveListener
+	    implements ActionListener {
+
+	    private boolean move_up;
+
+	    public MoveListener(boolean move_up) {
+		this.move_up = move_up;
+	    }
+
+	    public void actionPerformed(ActionEvent event) {
+		// Retrieve the selected index
+		Index index = (Index) index_list.getSelectedValue();
+		if(index != null) {
+		    moveIndex(index, move_up);
+		    // Ensure the index that moved is still selected
+		    index_list.setSelectedValue(index, true);
+		    index = null;
+		}
+	    }
+	}
+
+	private class SetDefaultListener
+	    implements ActionListener {
+
+	    public void actionPerformed(ActionEvent event) {
+		Index index = (Index) index_list.getSelectedValue();
+		if(index != null) {
+		    setDefault(index);
+		    // This should cause a repaint of just the desired row
+		    index_list.setSelectedValue(index, true);
+		}
+		set_default_button.setEnabled(false);
+	    }
+	}
+
+	
+
+	private class NewIndexPrompt 
+	    extends ModalDialog {
+
+	    NewIndexPrompt new_index_prompt = null;
+	
+	    private JCheckBox text_checkbox;
+	    private CheckList source_list;
+	    // mg uses a level box
+	    private JComboBox level_combobox;
+	    // mgpp has a allfields selector
+	    private JCheckBox allfields_box;
+
+	    private JButton add_or_replace_button;
+	    private JButton select_all_button;
+	    private JButton select_none_button;
+	    private JButton add_all_button;
+	    //private JButton select_all_button;
+	    //private JButton replace_button;
+	    private JButton cancel_button;
+
+	    private boolean mgpp_enabled = false;
+	    private boolean editing = false;
+	
+	    public NewIndexPrompt(String build_type, Index existing_index) {
+		super(Gatherer.g_man, true);
+                this.setComponentOrientation(Dictionary.getOrientation());
+                
+		new_index_prompt = this;
+
+		setModal(true);
+		setSize(PROMPT_SIZE);
+		if (existing_index != null) {
+		    setTitle (Dictionary.get("CDM.IndexManager.Edit_Index"));
+		    editing = true;
+		} else {
+		    setTitle(Dictionary.get("CDM.IndexManager.New_Index"));
+		}
+		
+		setJMenuBar(new SimpleMenuBar("searchindexes"));
+		if (build_type.equals(BuildTypeManager.BUILD_TYPE_MGPP) || build_type.equals(BuildTypeManager.BUILD_TYPE_LUCENE)) {
+		    mgpp_enabled = true;
+		}
+		JPanel content_pane = (JPanel)this.getContentPane();
+                content_pane.setComponentOrientation(Dictionary.getOrientation());
+		ArrayList new_data = new ArrayList();
+		new_data.addAll(MetadataSetManager.getEveryMetadataSetElement());
+
+		JPanel details_pane = new JPanel();
+                details_pane.setComponentOrientation(Dictionary.getOrientation());
+		
+		text_checkbox = new JCheckBox(Dictionary.get("CDM.IndexManager.Text_Source"));
+		text_checkbox.setToolTipText(Dictionary.get("CDM.IndexManager.Text_Source_Tooltip"));
+                text_checkbox.setComponentOrientation(Dictionary.getOrientation());                
+		text_checkbox.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent event) {
+			    validateAddOrReplaceButton();
+			}
+		    });
+		JLabel source_label = new JLabel(Dictionary.get("CDM.IndexManager.Source"));
+                source_label.setComponentOrientation(Dictionary.getOrientation());
+		source_list = new CheckList(false);                
+		source_list.setListData(new_data);
+		source_list.setToolTipText(Dictionary.get("CDM.IndexManager.Source_Tooltip"));
+		source_list.addListSelectionListener(new SourceListListener());
+
+		JPanel button_pane = new JPanel();
+		button_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
+		button_pane.setComponentOrientation(Dictionary.getOrientation());
+                
+		if (existing_index == null && mgpp_enabled) {
+		    button_pane.setLayout(new GridLayout(2,3,5,0));
+                    JPanel tmp = new JPanel();
+                    tmp.setComponentOrientation(Dictionary.getOrientation());
+		    button_pane.add(tmp);
+		} else {
+		    button_pane.setLayout(new GridLayout(2,2,5,0));
+		}
+		
+		select_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_All"), Dictionary.get("CDM.IndexManager.Select_All_Tooltip"));
+		select_all_button.addActionListener(new ActionListener() {
+			
+			public void actionPerformed(ActionEvent event) {
+			    text_checkbox.setSelected(true);
+			    source_list.setAllTicked();
+			    validateAddOrReplaceButton();
+			}
+		    });
+		
+		select_none_button = new GLIButton(Dictionary.get("CDM.IndexManager.Select_None"), Dictionary.get("CDM.IndexManager.Select_None_Tooltip"));
+		select_none_button.addActionListener(new ActionListener() {
+			
+			public void actionPerformed(ActionEvent event) {
+			    text_checkbox.setSelected(false);
+			    source_list.clearTicked();
+			    validateAddOrReplaceButton();
+			}
+		    });
+		
+		
+		button_pane.add(select_all_button);
+		button_pane.add(select_none_button);
+		
+		if (existing_index != null) {
+		    add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Replace_Index"), Dictionary.get("CDM.IndexManager.Replace_Index_Tooltip"));
+		    add_or_replace_button.addActionListener(new ReplaceIndexListener());
+		} else {
+		    add_or_replace_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_Index"), Dictionary.get("CDM.IndexManager.Add_Index_Tooltip"));
+		    add_or_replace_button.addActionListener(new AddIndexListener());
+		}
+
+		add_or_replace_button.addActionListener(CollectionDesignManager.buildcol_change_listener);
+		add_or_replace_button.setEnabled(false);
+		button_pane.add(add_or_replace_button);
+		
+		if (existing_index == null && mgpp_enabled) {
+		    add_all_button = new GLIButton(Dictionary.get("CDM.IndexManager.Add_All"), Dictionary.get("CDM.IndexManager.Add_All_Tooltip"));
+		    add_all_button.setEnabled(true);
+		    add_all_button.addActionListener(new AddAllIndexActionListener());
+		    button_pane.add(add_all_button);
+		} 
+		
+		// always have a cancel button
+		cancel_button = new GLIButton(Dictionary.get("General.Cancel"), Dictionary.get("General.Cancel"));
+		cancel_button.setEnabled(true);
+		cancel_button.addActionListener(new ActionListener() {
+			public void actionPerformed(ActionEvent event) {
+			    new_index_prompt.dispose();
+			}
+		    });
+		button_pane.add(cancel_button);
+
+		//Layout
+		details_pane.setLayout(new BorderLayout(10,10));
+		details_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
+		details_pane.add(text_checkbox, BorderLayout.NORTH);
+		details_pane.add(new JScrollPane(source_list), BorderLayout.CENTER);
+
+
+		// do type specific stuff
+		if (mgpp_enabled) {
+		    // allfields
+		    allfields_box = new JCheckBox(Dictionary.get("CDM.IndexManager.Allfields_Index"));
+		    allfields_box.addItemListener(new AllFieldsBoxListener());
+		    allfields_box.setComponentOrientation(Dictionary.getOrientation());
+                    //JLabel allfields_label = new JLabel(Dictionary.get("CDM.IndexManager.Allfields_Index"));
+		    details_pane.add(allfields_box, BorderLayout.SOUTH);
+		    
+		    
+		} else {
+		    // index level
+		    JLabel level_label = new JLabel(Dictionary.get("CDM.IndexManager.Level"));
+		    level_label.setComponentOrientation(Dictionary.getOrientation());
+                    
+                    level_combobox = new JComboBox();
+		    level_combobox.setOpaque(false);
+		    level_combobox.setPreferredSize(FIELD_SIZE);
+		    // Note the order of these must be the same as the
+		    // level order in Index
+		    level_combobox.addItem(StaticStrings.DOCUMENT_STR);//Dictionary.get("CDM.LevelManager.Document"));
+		    level_combobox.addItem(StaticStrings.SECTION_STR);//Dictionary.get("CDM.LevelManager.Section"));
+		    level_combobox.addItem(StaticStrings.PARAGRAPH_STR);//Dictionary.get("CDM.LevelManager.Paragraph"));
+		    level_combobox.setEditable(false);
+		    level_combobox.setToolTipText(Dictionary.get("CDM.IndexManager.Level_Tooltip"));
+		    level_combobox.setComponentOrientation(Dictionary.getOrientation());
+                    level_combobox.addActionListener(new ActionListener() {
+			    public void actionPerformed(ActionEvent event) {
+				validateAddOrReplaceButton();
+			    }
+			});
+		    JPanel level_pane = new JPanel();
+                    level_pane.setComponentOrientation(Dictionary.getOrientation());
+		    level_pane.setLayout(new BorderLayout());
+		    level_pane.add(level_label, BorderLayout.LINE_START);
+		    level_pane.add(level_combobox, BorderLayout.CENTER);
+		    details_pane.add(level_pane, BorderLayout.SOUTH);
+
+		}
+		// if we are editing, fill in the controls
+		if (existing_index !=null) {
+		    ArrayList sources = existing_index.getSources();
+		    if (mgpp_enabled && sources.get(0).equals(ALLFIELDS)) {
+			allfields_box.setSelected(true);
+			source_list.setEnabled(false);
+		    } else {
+			source_list.setTickedObjects(sources.toArray());
+			source_list.setEnabled(true);
+			if (sources.contains(StaticStrings.TEXT_STR)) {
+			    text_checkbox.setSelected(true);
+			}
+		    } 
+		    if (!mgpp_enabled) {
+			level_combobox.setSelectedIndex(((MGIndex)existing_index).getLevel());
+		    }
+		    
+		}
+		content_pane.setLayout(new BorderLayout());
+		content_pane.add(source_label, BorderLayout.NORTH);
+		content_pane.add(details_pane, BorderLayout.CENTER);
+		content_pane.add(button_pane, BorderLayout.SOUTH);
+	    
+		// Display on screen.
+		Dimension screen_size = Configuration.screen_size;
+		setLocation((screen_size.width - PROMPT_SIZE.width) / 2, (screen_size.height - PROMPT_SIZE.height) / 2);
+		screen_size = null;
+		setVisible(true);
+	    
+	    }
+
+	    /** Method which actually forces the dialog to be shown on screen.
+	     * @return <i>true</i> if the user completed configuration and pressed ok, <i>false</i> otherwise.
+	     */
+	    public boolean display() {
+		setVisible(true);
+		return true;
+		//    return success;
+	    }
+	
+	    public void destroy() {
+	    }
+
+	    // Checks that specified index not already in the collection
+	    private void validateAddOrReplaceButton() {
+		Index index;
+		ArrayList sources;
+		if (mgpp_enabled && allfields_box.isSelected()) {
+		    sources = new ArrayList();
+		    sources.add(ALLFIELDS);
+		    index = new Index(sources);
+		    
+		} else if (text_checkbox.isSelected() || 
+			   !source_list.isNothingTicked()) {
+		    sources = source_list.getTicked();
+		    if (text_checkbox.isSelected()) {
+			sources.add(0, StaticStrings.TEXT_STR);
+		    }		    
+		    if (mgpp_enabled) {
+			index = new Index(sources);
+		    } else {
+			index = new MGIndex(level_combobox.getSelectedIndex(), sources);
+		    }
+		} else {
+		    // nothing selected
+		    add_or_replace_button.setEnabled(false);
+		    return;
+		}
+		
+		sources = null;
+		if (index_model.contains(index)) {
+		    add_or_replace_button.setEnabled(false);
+		}
+		else {
+		    add_or_replace_button.setEnabled(true);
+		}
+		
+	    }
+
+	    private Index generateNewIndex() {
+		Index index = null;
+		ArrayList sources;
+		if (mgpp_enabled && allfields_box.isSelected()) {
+		    sources = new ArrayList();
+		    sources.add(ALLFIELDS);
+		    index = new Index(sources);
+		}
+		else if (text_checkbox.isSelected() || !source_list.isNothingTicked()) {
+		    sources = source_list.getTicked();
+		    if (text_checkbox.isSelected()) {
+			sources.add(0, StaticStrings.TEXT_STR);
+		    }
+		    if(mgpp_enabled) {
+			index = new Index(sources);
+		    } else {
+			index = new MGIndex(level_combobox.getSelectedIndex(), sources);
+		    }
+		}
+		return index;
+		 
+
+	    }
+	    
+	    private CollectionMeta generateCollectionMeta(Index index) {
+		CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
+		if (use_macro_as_display_name(index.getID())) {
+		    metadatum.setValue(get_macro_name(index.getID()));
+		} else {
+		    metadatum.setValue(index.getID());
+		}
+		return metadatum;
+	    }
+	    
+	    private class AddIndexListener
+		implements ActionListener
+	    {
+		public void actionPerformed(ActionEvent event)
+		{
+		    Index index = generateNewIndex();
+		    if (index != null) {
+			
+			CollectionMeta metadatum = generateCollectionMeta(index);
+			// Add the index
+			addIndex(index, metadatum);
+			index_list.setSelectedValue(index, true);
+		    }
+		    new_index_prompt.dispose();		    
+		}
+	    }
+
+	    /** add all sources as separate indexes (fields).  */
+	    private class AddAllIndexActionListener
+		implements ActionListener {
+
+		public void actionPerformed(ActionEvent event) {
+		    ArrayList all_sources = source_list.getAll();
+		    all_sources.add(0, StaticStrings.TEXT_STR);
+		    ArrayList new_sources = new ArrayList();
+		    for(int i = 0; i < all_sources.size(); i++) {
+			Object source = all_sources.get(i);
+
+			// Create new index
+			new_sources.clear();
+			new_sources.add(source);
+			Index index = new Index(new_sources);
+			if(!index_model.contains(index)) {
+			    // Determine the metadatum value
+			    String name = source.toString();
+			    if(name.startsWith(StaticStrings.EXTRACTED_NAMESPACE) && name.indexOf(StaticStrings.NS_SEP, StaticStrings.EXTRACTED_NAMESPACE.length()) == -1) {
+				name = name.substring(StaticStrings.EXTRACTED_NAMESPACE.length());
+			    }
+			    // Create new metadatum
+			    CollectionMeta metadatum = new CollectionMeta(StaticStrings.STOP_CHARACTER + index.getID());
+			    metadatum.setValue(name);
+			    name = null;
+			    // Assign new index
+			    addIndex(index, metadatum);
+			}
+			source = null;
+			index = null;
+		    }
+		    new_sources = null;
+		    new_index_prompt.dispose();
+		
+		}
+	    }
+    
+	    private class AllFieldsBoxListener
+		implements ItemListener {
+		
+		public void itemStateChanged(ItemEvent event) {
+		    if (event.getStateChange() == ItemEvent.SELECTED) {
+			source_list.setEnabled(false);
+		    } else if (event.getStateChange() == ItemEvent.DESELECTED) {
+			source_list.setEnabled(true);
+		    }
+		    validateAddOrReplaceButton();
+		}
+		
+	    }
+
+	    private class SourceListListener
+		implements ListSelectionListener {
+	    
+		public void valueChanged(ListSelectionEvent event) {
+		    if (event.getValueIsAdjusting()) {
+			return;
+		    }
+		    validateAddOrReplaceButton();
+		}
+	    }
+	
+
+	    private class ReplaceIndexListener
+		implements ActionListener {
+	    
+		public void actionPerformed(ActionEvent event)
+		{
+		    Index index = generateNewIndex();
+		    if (index != null) {
+			CollectionMeta metadatum = generateCollectionMeta(index);
+			// replace the index
+			replaceIndex((Index) index_list.getSelectedValue(), index, metadatum);
+			index_list.setSelectedValue(index, true);
+		    }
+		    new_index_prompt.dispose();
+		}
+	    }
+
+	    /**
+	     * If the index is built with DC metadata, use macro variable as the display text, 
+	     * so that translations of DC metadata can be displayed in the search field drop-down list 
+	     * when interface language changes.
+	     *   
+	     * @param index_id Current index id.
+	     * @return Whether macro variable should be used.
+	     */
+	    private boolean use_macro_as_display_name(String index_id) {
+		//String metaname = index_id;	
+		if (index_id.indexOf(":") != -1) {
+		    index_id = index_id.substring(index_id.indexOf(":") + 1);
+		}
+		
+		String field = null;
+		String[] fields = index_id.split(",");
+		for (int i = 0; i < fields.length; i++) {
+		    String s = fields[i];
+		    if (s.indexOf(".") != -1) {	  
+			s = s.substring(s.indexOf(".") + 1);
+		    }
+		    if (field == null) {
+			field = s;
+		    }
+		    else if (!field.equals(s)) {
+			return false;
+		    }
+		}
+				
+		field = field.toLowerCase();
+		if (field.equals("text") || field.equals("title") || field.equals("creator") || field.equals("subject") || 
+		    field.equals("description") || field.equals("publisher") || field.equals("contributor") || field.equals("date") || 
+		    field.equals("type") || field.equals("format") || field.equals("identifier") || field.equals("source") || 
+		    field.equals("language") || field.equals("relation") || field.equals("coverage") || field.equals("rights")) {			  
+		    return true;
+		}
+		
+		return false;
+	    }
+
+	    /**
+	     * Get the corresponding macro variable name, eg. _labelTitle_, _labelCreator_.
+	     *   
+	     * @param index_id Current index id.
+	     * @return Name of the macro variable.
+	     */
+	    private String get_macro_name(String index_id) {
+		if (index_id.indexOf(":") != -1) {
+		    index_id = index_id.substring(index_id.indexOf(":") + 1);
+		}
+		if (index_id.indexOf(",") != -1) {
+		    index_id = index_id.substring(0, index_id.indexOf(","));
+		}
+		if (index_id.indexOf(".") != -1) {	  
+		    index_id = index_id.substring(index_id.indexOf(".") + 1);
+		}	
+		
+		return "_label" + index_id + "_";		
+	    }
+	}
+    }
+}
