Index: /trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java	(revision 4467)
+++ /trunk/gli/src/org/greenstone/gatherer/mem/MetadataEditorManager.java	(revision 4468)
@@ -635,89 +635,26 @@
 	    Object source = event.getSource();
 	    if(source == ok_button) {
-		boolean cont = true;
-		AttributeTableModel model = null;
+		boolean success = true;
+		AttributeTableModel model;
+
+		// Add or edit a collect/set/element attribute
 		switch(current_attribute_type) {
 		case MEMNode.COLLECTION:
 		    model = (AttributeTableModel) profile_attributes.getModel();
+		    success = addOrEditCollectionAttribute(model);
+		    break;
+		case MEMNode.SET:
+		    model = (AttributeTableModel) set_attributes.getModel();
+		    success = addOrEditSetAttribute(model);		    
 		    break;
 		case MEMNode.ELEMENT:
 		    model = (AttributeTableModel) element_attributes.getModel();
+		    success = addOrEditElementAttribute(model);
 		    break;
-		case MEMNode.SET:
-		    model = (AttributeTableModel) set_attributes.getModel();
-		    break;
-		}
-		// Now we add the new or improved values, removing old values if this is an edit.
-		// Depends on what we are adding the attribute to.
-		if(model != null) {
-		    if(current_collection_file != null) {
-			String name_str = name.getSelectedItem().toString();
-			String value_str = value.getText();
-			// Remove the existing one if this is an edit.
-			if(!add_type && current_attribute != -1) {
-			    String old_source = (String) model.getValueAt(current_attribute, 0);
-			    Gatherer.c_man.msm.profiler.removeAction(current_collection_file, old_source);
-			    old_source = null;
-			    model.removeRow(current_attribute);
-			}
-			// First thing we check from profiles is that there isn't already an entry for this name.
-			if(!model.contains(name_str, 0)) {
-			    // Add profile
-			    Gatherer.c_man.msm.profiler.addAction(current_collection_file, name_str, value_str);
-			    // Update attribute table.
-			    model.add(new Attribute(name_str, value_str));
-			}
-			else {
-			    // Show an error message and do not proceed.
-			    cont = false;
-			    JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
-			}
-			value_str = null;
-			name_str = null;
-		    }
-		    else if(current_element != null) {
-			// Add the attribute, even if one of the same name already exists. Maybe one day someone would like to enforce the occurance rules but not me and not today.
-			String name_str = name.getSelectedItem().toString();
-			String language_code = ((Language)language_box.getSelectedItem()).getCode();
-			String value_str = value.getText();
-			// Remove the existing one if this is an edit.
-			if(!add_type && current_attribute != -1) {
-			    model.removeRow(current_attribute);
-			}
-			// Add attribute
-			current_element.addAttribute(name_str, language_code, value_str);
-			// Update the attribute table
-			model.add(new Attribute(name_str, language_code, value_str));
-			value_str = null;
-			language_code = null;
-			name_str = null;
-		    }
-		    else if(current_set != null) {
-			String name_str = name.getSelectedItem().toString();
-			String value_str = value.getText();						
-			// Remove the existing one if this is an edit.
-			if(!add_type && current_attribute != -1) {
-			    model.removeRow(current_attribute);
-			}
-			// First thing we check from profiles is that there isn't already an entry for this name.
-			if(!model.contains(name_str, 0)) {
-			    // Add profile
-			    current_set.addAttribute(name_str, value_str);
-			    // Update attribute table.
-			    model.add(new Attribute(name_str, value_str));
-			}
-			// Otherwise provide an error message and do not proceed.
-			else {
-			    cont = false;
-			    JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
-			}
-			value_str = null;
-			name_str = null;
-		    }
-		    // Hide dialog if we are allowed to continue.
-		    if(cont) {
-			setVisible(false);
-		    }
-		    model = null;
+		}
+
+		if (success) {
+		    // Hide dialog
+		    setVisible(false);
 		}
 	    }
@@ -886,5 +823,98 @@
 	    source = null;
 	}
-		  
+
+
+	private boolean addOrEditCollectionAttribute(AttributeTableModel model)
+	{
+	    String name_str = name.getSelectedItem().toString();
+	    String value_str = value.getText();
+
+	    // Remove the existing attribute if this is an edit
+	    if(!add_type && current_attribute != -1) {
+		String old_source = (String) model.getValueAt(current_attribute, 0);
+		Gatherer.c_man.msm.profiler.removeAction(current_collection_file, old_source);
+		old_source = null;
+		model.removeRow(current_attribute);
+	    }
+
+	    boolean cont = true;
+	    // Check that there isn't already an entry for this attribute
+	    if (!model.contains(name_str, 0)) {
+		// Add profile
+		Gatherer.c_man.msm.profiler.addAction(current_collection_file, name_str, value_str);
+		// Update attribute table
+		model.add(new Attribute(name_str, value_str));
+	    }
+	    // Otherwise show an error message and do not proceed
+	    else {
+		cont = false;
+		JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
+	    }
+
+	    value_str = null;
+	    name_str = null;
+	    return cont;
+	}
+
+
+	private boolean addOrEditSetAttribute(AttributeTableModel model)
+	{
+	    String name_str = name.getSelectedItem().toString();
+	    String value_str = value.getText();						
+
+	    // Remove the existing attribute if this is an edit
+	    if(!add_type && current_attribute != -1) {
+		current_set.removeAttribute(name_str);
+		// Update attribute table
+		model.removeRow(current_attribute);
+	    }
+
+	    boolean cont = true;
+	    // Check that there isn't already an entry for this attribute
+  	    if (!model.contains(name_str, 0)) {
+ 		// Add the new attribute
+ 		current_set.addAttribute(name_str, value_str);
+ 		// Update the attribute table
+ 		model.add(new Attribute(name_str, value_str));
+ 	    }
+ 	    // Otherwise show an error message and do not proceed
+ 	    else {
+ 		cont = false;
+ 		JOptionPane.showMessageDialog(self, get("Attribute_Already_Exists"), get("General.Error"), JOptionPane.ERROR_MESSAGE);
+ 	    }
+
+	    value_str = null;
+	    name_str = null;
+	    return cont;
+	}
+
+
+	private boolean addOrEditElementAttribute(AttributeTableModel model)
+	{
+	    // Add the attribute, even if one of the same name already exists. Maybe one day someone would like to enforce the occurance rules but not me and not today.
+	    String name_str = name.getSelectedItem().toString();
+	    String language_code = ((Language)language_box.getSelectedItem()).getCode();
+	    String value_str = value.getText();
+
+	    // Remove the existing attribute if this is an edit
+	    if(!add_type && current_attribute != -1) {
+		String current_value_str = model.getAttribute(current_attribute).value;
+		current_element.removeAttribute(name_str, language_code, current_value_str);
+		// Update the attribute table
+		model.removeRow(current_attribute);
+	    }
+
+	    // Add the new attribute
+	    current_element.addAttribute(name_str, language_code, value_str);
+	    // Update the attribute table
+	    model.add(new Attribute(name_str, language_code, value_str));
+
+	    value_str = null;
+	    language_code = null;
+	    name_str = null;
+	    return true;
+	}
+
+
 	public void dispose() {
 	    cancel_button = null;
@@ -1537,6 +1567,8 @@
 			// Remove element attribute
 			String name = (String) element_attributes.getValueAt(current_attribute, 0);
-			String value = (String) element_attributes.getValueAt(current_attribute, 1);
-			current_element.removeAttribute(name, value); // Can be multiple authors for instance
+			String language = (String) element_attributes.getValueAt(current_attribute, 1);
+			String value = (String) element_attributes.getValueAt(current_attribute, 2);
+			current_element.removeAttribute(name, language, value);
+
 			// Refresh table
 			((AttributeTableModel)element_attributes.getModel()).removeRow(current_attribute);
