Index: trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java
===================================================================
--- trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java	(revision 4402)
+++ trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java	(revision 4403)
@@ -101,4 +101,5 @@
     private JScrollPane set_attributes_scroll = null;
     private JPanel details_pane = null;
+    private KeepTreeRootExpandedListener keep_root_expanded_listener = null;
     private MEMModel model = null;
     private MEMNode current_node = null;
@@ -228,5 +229,7 @@
 	element_values.setSelectionColor(Gatherer.config.getColor("coloring.collection_selection_background", false));
 	element_values.setSelectedTextColor(Gatherer.config.getColor("coloring.collection_selection_foreground", false));
-		  
+	    
+	keep_root_expanded_listener = new KeepTreeRootExpandedListener();
+
 	JPanel profile_details_pane = new JPanel();
 	profile_details_pane.setOpaque(false);
@@ -1210,5 +1213,5 @@
 	    this.setModal(true);
 	    this.setSize(ADD_OR_EDIT_VALUE_SIZE);
-				// Create
+	    // Create
 	    JPanel content_pane = (JPanel) getContentPane();
 	    content_pane.setBackground(Gatherer.config.getColor("coloring.collection_heading_background", false));
@@ -1249,12 +1252,12 @@
 	    ok_button = new JButton(get("General.OK"));
 	    ok_button.setEnabled(false);
+	    TextFieldEnabler ok_button_enabler = new TextFieldEnabler(ok_button);
 	    cancel_button = new JButton(get("General.Cancel"));
-				// Connect
-	    TextFieldEnabler ok_button_enabler = new TextFieldEnabler(ok_button);
+	    // Connect
+	    ok_button_enabler.add(value);
 	    cancel_button.addActionListener(this);
 	    ok_button.addActionListener(this);
 	    subject_tree.addTreeSelectionListener(this);
-	    ok_button_enabler.add(value);
-				// Layout
+	    // Layout
 	    subject_tree_pane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
 	    subject_tree_pane.setLayout(new BorderLayout());
@@ -1299,8 +1302,7 @@
 		GValueModel model = Gatherer.c_man.msm.getValueTree(current_element);
 		if(add_type) {
+		    ///ystem.err.println("Add type - insert node: " + value.getText() + " into " + subject_node);
 		    // Add this value to the tree.
-		    GValueNode parent = null;
 		    model.addValue(value.getText(), subject_node, alias.getText());
-		    parent = null;
 		}
 		else {
@@ -1343,6 +1345,6 @@
 			}
 			// Inform the tree model what two nodes structures have changed (origin and destination).
-			subject_node.unmap();
-			old_subject_node.unmap();
+			//subject_node.unmap();
+			//old_subject_node.unmap();
 			model.nodeStructureChanged(old_subject_node);
 			model.nodeStructureChanged(subject_node);
@@ -1443,4 +1445,23 @@
 	    self.dispose();
 	}
+    }
+
+    /** This listener is responsible for keeping the root node of a value tree expanded where and when possible. */
+    private class KeepTreeRootExpandedListener
+	implements TreeModelListener {
+	/** Invoked after a node (or a set of siblings) has changed in some way. */
+	public void treeNodesChanged(TreeModelEvent e) {
+	}
+        /** Invoked after nodes have been inserted into the tree. */
+	public void treeNodesInserted(TreeModelEvent e) {
+	    element_values.expandPath(new TreePath(element_values.getModel().getRoot()));
+	}
+	/** Invoked after nodes have been removed from the tree. */
+	public void treeNodesRemoved(TreeModelEvent e) {
+	}
+	/** Invoked after the tree has drastically changed structure from a given node down. */
+	public void treeStructureChanged(TreeModelEvent e) {
+	    element_values.expandPath(new TreePath(element_values.getModel().getRoot()));
+	} 
     }
 
@@ -1593,5 +1614,6 @@
     /** Used to enable a certain target component if and only if all the registered text fields contain non-zero length strings. */
     private class TextFieldEnabler
-	extends KeyAdapter {
+	implements DocumentListener {
+	private boolean ignore = false;
 	private Component target = null;
 	private JTextComponent[] fields = null;
@@ -1601,5 +1623,5 @@
 	}
 	public void add(JTextComponent field) {
-	    field.addKeyListener(this);
+	    // Record this field as being one we depend upon
 	    if(fields == null) {
 		fields = new JTextComponent[1];
@@ -1611,12 +1633,31 @@
 		temp[fields.length] = field;
 		fields = temp;
-	    }
-	}
-	public void keyReleased(KeyEvent event) {
-	    boolean enable = true;
-	    for(int i = 0; i < fields.length; i++) {
-		enable = enable && fields[i].getText().length() > 0;
-	    }
-	    target.setEnabled(enable);
+		temp = null;
+	    }
+	    // Add the appropriate listener
+	    field.getDocument().addDocumentListener(this);
+	}
+	/** Gives notification that an attribute or set of attributes changed. */
+	public void changedUpdate(DocumentEvent e) {
+	    canEnable();
+	}
+	/** Gives notification that there was an insert into the document. */
+	public void insertUpdate(DocumentEvent e) {
+	    canEnable();
+	}
+        /** Gives notification that a portion of the document has been removed. */
+	public void removeUpdate(DocumentEvent e) {
+	    canEnable();
+	}
+	private void canEnable() {
+	    if(!ignore) {
+		ignore = true;
+		boolean can_enable = true;
+		for(int i = 0; can_enable && i < fields.length; i++) {
+		    can_enable = can_enable && (fields[i].getText().length() > 0);
+		}
+		target.setEnabled(can_enable);
+		ignore = false;
+	    }
 	}
     }
@@ -1709,5 +1750,9 @@
 		    atm.setTable(element_attributes);
 
-		    element_values.setModel(Gatherer.c_man.msm.getValueTree(current_element));
+		    // Be careful not to add the keep_root_expanded_listener over and over!
+		    GValueModel value_model = Gatherer.c_man.msm.getValueTree(current_element);
+		    value_model.removeTreeModelListener(keep_root_expanded_listener); // Remove it if its already there
+		    value_model.addTreeModelListener(keep_root_expanded_listener); // Then add again.
+		    element_values.setModel(value_model);
 		    card_layout.show(details_pane, ELEMENT);
 		    // Meanwhile disable/enable controls depending on this Element selection.
