Index: /trunk/gli/src/org/greenstone/gatherer/cdm/LevelManager.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/cdm/LevelManager.java	(revision 12074)
+++ /trunk/gli/src/org/greenstone/gatherer/cdm/LevelManager.java	(revision 12074)
@@ -0,0 +1,299 @@
+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.w3c.dom.*;
+public class LevelManager 
+    extends DOMProxyListModel 
+    implements BuildTypeManager.BuildTypeListener {
+
+    /** A reference to ourselves so our inner methods have access. */
+    private DOMProxyListModel levels_model = null;
+    
+    private Level default_level = null;
+
+    private Control controls = null;
+    
+    public LevelManager(Element levels) {
+	super(levels, CollectionConfiguration.LEVEL_ELEMENT, new Level());
+	DebugStream.println("LevelManager: " + getSize() + " levels parsed.");
+
+	levels_model = this;
+
+	// Parse and retrieve the default level
+	NodeList default_level_elements = CollectionDesignManager.collect_config.getDocumentElement().getElementsByTagName(CollectionConfiguration.LEVEL_DEFAULT_ELEMENT);
+	if(default_level_elements.getLength() > 0) {
+	    default_level = new Level((Element)default_level_elements.item(0));
+	}
+    }
+	
+    private void addLevel(String level) {
+	if (getLevel(level) == null) {
+	    int position = 0;
+	    
+	    if (level.equals(CollectionConfiguration.SECTION_STR) && getSize()==1) {
+		position = 1;
+	    }
+	    add(position, new Level(level));
+	    // add in a default collection meta
+	    CollectionMeta meta = new CollectionMeta(CollectionConfiguration.STOP_CHARACTER + level);
+	    meta.setAssigned(true);
+	    meta.setValue(level);
+	    CollectionDesignManager.collectionmeta_manager.addMetadatum(meta);
+
+	    Gatherer.c_man.configurationChanged();
+	}
+	
+    }
+    
+    public void buildTypeChanged(String new_build_type) {
+	boolean enabled = true;
+	if (new_build_type.equals(BuildTypeManager.BUILD_TYPE_MG)) {
+	    enabled = false;
+	}
+	setAssigned(enabled);
+	if (default_level != null) {
+	    default_level.setAssigned(enabled);
+	}
+	
+    }
+
+    /** Method to acquire the controls for editing the indexes.
+     * @return the Control
+     */
+    public Control getControls() {
+	if(controls == null) {
+	    // Build controls
+	    controls = new LevelControl();
+	}
+	return controls;
+    }
+    
+    public Level getLevel(String name) {
+	int levels_model_size = levels_model.getSize();
+	for(int i = 0; i < levels_model_size; i++) {
+	    Level level = (Level) levels_model.getElementAt(i);
+	    if(level.getLevel().equals(name)) {
+		return level;
+	    }
+	}
+	return null;
+    }
+
+    public ArrayList getLevels() {
+	return children();
+    }
+    
+    private void removeLevel(String level_name) {
+	// Remove any current metadata from this level
+	CollectionDesignManager.collectionmeta_manager.removeMetadata(CollectionConfiguration.STOP_CHARACTER + level_name);
+	// Remove the level
+	Level level_elem = null;
+	if ((level_elem = getLevel(level_name)) != null) {
+	    levels_model.remove(level_elem);
+	    Gatherer.c_man.configurationChanged();
+	}
+    }
+
+   /** 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(String level) {
+	if(level != null) {
+	    if(default_level == null) {
+		// Create the default level element, and place immediately after leveles element.
+		Element default_level_element = root.getOwnerDocument().createElement(CollectionConfiguration.LEVEL_DEFAULT_ELEMENT);
+		default_level = new Level(default_level_element);
+		Node target_node = CollectionConfiguration.findInsertionPoint(default_level_element);
+		if(target_node != null) {
+		    root.getOwnerDocument().getDocumentElement().insertBefore(default_level_element, target_node);
+		}
+		else {
+		    root.getOwnerDocument().getDocumentElement().appendChild(default_level_element);
+		}
+	    }
+	    default_level.setAssigned(true);
+	    default_level.setLevel(level);
+	    
+	    
+	}
+	else {
+	    if(default_level != null) {
+		default_level.setAssigned(false);
+	    }
+	}
+	Gatherer.c_man.configurationChanged();
+    }
+
+    /** This class creates a set of controls for editing the levels. */
+    private class LevelControl
+	extends JPanel
+	implements Control {
+	
+	JCheckBox document_checkbox = null;
+	JCheckBox section_checkbox = null;
+	JRadioButton document_default_radio = null;
+	JRadioButton section_default_radio = null;
+	
+	public LevelControl() {
+	    
+	    JPanel checkbox_panel = new JPanel();
+	    JPanel default_panel = new JPanel();
+	    document_checkbox = new JCheckBox();
+	    document_checkbox.setText(Dictionary.get("CDM.LevelManager.Document"));
+	    document_default_radio = new JRadioButton();
+	    document_default_radio.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
+	    document_default_radio.setOpaque(false);
+	    document_default_radio.setActionCommand(CollectionConfiguration.DOCUMENT_STR);
+
+	    section_checkbox = new JCheckBox();
+	    section_checkbox.setText(Dictionary.get("CDM.LevelManager.Section"));
+	    section_default_radio = new JRadioButton();
+	    //section_default_radio.setOpaque(true);
+	    section_default_radio.setBackground(Configuration.getColor("coloring.collection_tree_background", false));
+	    section_default_radio.setOpaque(false);
+
+	    section_default_radio.setActionCommand(CollectionConfiguration.SECTION_STR);
+	    ButtonGroup default_group = new ButtonGroup();
+	    default_group.add(document_default_radio);
+	    default_group.add(section_default_radio);
+	    
+	    if (levels_model.getSize()==0) {
+		// no levels yet, set document
+		document_checkbox.setSelected(true);
+		addLevel(CollectionConfiguration.DOCUMENT_STR);
+	    } else {
+		if (getLevel(CollectionConfiguration.DOCUMENT_STR)!= null) {
+		    document_checkbox.setSelected(true);
+		}
+		if (getLevel(CollectionConfiguration.SECTION_STR) != null) {
+		    section_checkbox.setSelected(true);
+		}
+	    }
+
+	    if (default_level != null && default_level.isAssigned()) {
+		// set the default based on specified default
+		if (default_level.getLevel().equals(CollectionConfiguration.DOCUMENT_STR) && document_checkbox.isSelected()) {
+		    document_default_radio.setSelected(true);
+		} else if (default_level.getLevel().equals(CollectionConfiguration.SECTION_STR) && section_checkbox.isSelected()) {
+		    section_default_radio.setSelected(true);
+		} else {
+		    default_level.setAssigned(false);
+		    
+		}
+	    }
+	    // if have no valid specified default
+	    if (default_level == null || !default_level.isAssigned()) {
+		String default_l = CollectionConfiguration.DOCUMENT_STR;
+		if (document_checkbox.isSelected()) {
+		    document_default_radio.setSelected(true);
+		    default_l = CollectionConfiguration.DOCUMENT_STR;
+		} else if (section_checkbox.isSelected()) {
+		    section_default_radio.setSelected(true);
+		    default_l = CollectionConfiguration.SECTION_STR;
+		}
+		setDefault(default_l);
+	    }
+	    validateControls();
+	    JLabel checkbox_label = new JLabel(Dictionary.get("CDM.LevelManager.Level_Title"));
+	    checkbox_panel.setLayout(new GridLayout(3,1));
+	    
+	    checkbox_panel.add(checkbox_label);
+	    checkbox_panel.add(document_checkbox);
+	    checkbox_panel.add(section_checkbox);
+
+	    JLabel default_label = new JLabel(Dictionary.get("CDM.LevelManager.Default"));
+	    default_panel.setLayout(new GridLayout(3,1));
+	    default_panel.add(default_label);
+	    default_panel.add(document_default_radio);
+	    default_panel.add(section_default_radio);
+
+	    CheckBoxListener cbl = new CheckBoxListener();
+	    document_checkbox.addActionListener(cbl);
+	    section_checkbox.addActionListener(cbl);
+
+	    RadioListener rl = new RadioListener();
+	    document_default_radio.addActionListener(rl);
+	    section_default_radio.addActionListener(rl);
+
+	    setLayout(new BorderLayout(10,10));
+	    setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
+	    add(checkbox_panel, BorderLayout.WEST);
+	    add(default_panel, BorderLayout.CENTER);
+	} // LevelControl
+			
+	public void gainFocus() {
+
+	}
+
+	public void loseFocus() {
+
+	}
+
+	public void destroy() {
+	}
+
+	private class RadioListener
+	    implements ActionListener {
+	    public void actionPerformed(ActionEvent event) {
+		String new_level = event.getActionCommand();
+		setDefault(new_level);
+	    }
+	}
+	private class CheckBoxListener
+	    implements ActionListener {
+	    
+	    public void actionPerformed(ActionEvent event) {
+		if (!((JCheckBox)event.getSource()).isSelected()) {
+		    // deselecting
+		    if (noLevelsSelected()) {
+			// can't do this
+			((JCheckBox)event.getSource()).setSelected(true);
+		    } else {
+			// remove the level
+			removeLevel(event.getActionCommand());
+		    }
+		} else {
+		    // selecting a new one
+		    addLevel(event.getActionCommand());
+		}
+		validateControls();
+	    }
+	}
+	    
+	private boolean noLevelsSelected() {
+	    if (!document_checkbox.isSelected() && !section_checkbox.isSelected()) {
+		return true;
+
+	    }
+	    return false;
+	}
+
+	private void validateControls() {
+	    boolean document_enabled = document_checkbox.isSelected();
+	    document_default_radio.setEnabled(document_enabled);
+	    
+	    boolean section_enabled = section_checkbox.isSelected();
+	    section_default_radio.setEnabled(section_enabled);
+	    
+	    // have we disabled our default??
+	    if (!document_enabled && document_default_radio.isSelected()) {
+		section_default_radio.setSelected(true);
+		setDefault(CollectionConfiguration.SECTION_STR);
+	    } else if (!section_enabled && section_default_radio.isSelected()) {
+		document_default_radio.setSelected(true);
+		setDefault(CollectionConfiguration.DOCUMENT_STR);
+	    }
+	}
+    }
+
+}
