Index: trunk/gli/src/org/greenstone/gatherer/cdm/FormatManager.java
===================================================================
--- trunk/gli/src/org/greenstone/gatherer/cdm/FormatManager.java	(revision 11992)
+++ trunk/gli/src/org/greenstone/gatherer/cdm/FormatManager.java	(revision 11993)
@@ -33,4 +33,5 @@
 import javax.swing.*;
 import javax.swing.event.*;
+import javax.swing.undo.*;
 import org.greenstone.gatherer.Configuration;
 import org.greenstone.gatherer.DebugStream;
@@ -72,44 +73,64 @@
     static final private String DOCUMENTBUTTONS = "DocumentButtons";
     static final private String DOCUMENTBUTTONS_DEFAULT_FORMAT = "Detach|Highlight";
-    
+    static private  HashMap format_map = null ;     
+
     /** The controls used to edit the format commands. */
     private Control controls = null;
     /** A reference to ourselves so inner classes can get at the model. */
     private DOMProxyListModel model = null;
-
+   
+   
     /** Constructor. */
     public FormatManager() {
 	super(CollectionDesignManager.collect_config.getDocumentElement(), CollectionConfiguration.FORMAT_ELEMENT, new Format());
 	this.model = this;
+
+        format_map = new HashMap();
+	format_map.put(DATELIST,"<td>[link][icon][/link]</td>\n<td>[highlight]{Or}{[dls.Title],[dc.Title],[ex.Title],Untitled}[/highlight]</td>\n<td>[ex.Date]</td>");
+	format_map.put(HLIST,"[link][highlight][ex.Title][/highlight][/link]");
+	format_map.put(VLIST,"<td valign=\"top\">[link][icon][/link]</td>\n<td valign=\"top\">[ex.srclink]{Or}{[ex.thumbicon],[ex.srcicon]}[ex./srclink]</td>\n<td valign=\"top\">[highlight]\n{Or}{[dls.Title],[dc.Title],[ex.Title],Untitled}\n[/highlight]{If}{[ex.Source],<br><i>([ex.Source])</i>}</td>");   
+	format_map.put(DOCUMENTHEADING,"{Or}{[parent(Top):Title],[Title],untitled}<br>");	
+	format_map.put(DOCUMENTTEXT,"[Text]");	    
+	format_map.put( DOCUMENTBUTTONS,"Detach|Highlight");
+	format_map.put( "","");
+       	
 	DebugStream.println("FormatManager: parsed " + getSize() + " format statements.");
 	// Establish all of the format objects, so that classifier indexes are initially correct (subsequent refreshes of the model will be sufficient to keep these up to date, as long as we start with a live reference to a classifier.
 	int size = getSize();
-	for(int i = 0; i < size; i++) {
+    	for(int i = 0; i < size; i++) {
 	    getElementAt(i);
 	}
-
-	// Ensure the default formats for DateList, HList and VList are assigned
+         
+       	// Ensure the default formats for DateList, HList and VList are assigned
 	if (getFormat(DATELIST) == null) {
-	    addFormat(new Format("",DATELIST,DATELIST_DEFAULT_FORMAT));
-	}
-
+            addFormat(new Format("",DATELIST,DATELIST_DEFAULT_FORMAT));        
+	}
+
+	
 	if (getFormat(HLIST) == null) {
-	    addFormat(new Format("",HLIST,HLIST_DEFAULT_FORMAT));
-	}
-
+	    addFormat(new Format("",HLIST,HLIST_DEFAULT_FORMAT));               
+	}
+
+     
 	if (getFormat(VLIST) == null) {
-	    addFormat(new Format("",VLIST,VLIST_DEFAULT_FORMAT));
-	}
+	    addFormat(new Format("",VLIST,VLIST_DEFAULT_FORMAT));            
+	}
+    
 
 	if (getFormat(DOCUMENTHEADING) == null) {
 	    addFormat(new Format(DOCUMENTHEADING, "", DOCUMENTHEADING_DEFAULT_FORMAT));
-	}
-	if (getFormat(DOCUMENTTEXT) == null) {
+     	}
+
+  
+	if (getFormat(DOCUMENTTEXT)== null) {
 	    addFormat(new Format(DOCUMENTTEXT,"",DOCUMENTTEXT_DEFAULT_FORMAT));
-	}
-	if (getFormat(DOCUMENTBUTTONS) == null) {
+   	}
+	
+	
+
+      if (getFormat(DOCUMENTBUTTONS) == null) {
 	    addFormat(new Format(DOCUMENTBUTTONS,"",DOCUMENTBUTTONS_DEFAULT_FORMAT));
-	}
-	
+  	}
+     
     }
 
@@ -118,5 +139,5 @@
      */
     private void addFormat(Format format) {
-	if(!contains(format)) {
+       	if(!contains(format)) {
 	    Element element = format.getElement();
 	    // Locate where we should insert this new classifier. 
@@ -126,4 +147,5 @@
 	}
     }
+
 
     public void destroy() {
@@ -181,4 +203,5 @@
 	remove(format);
 	Gatherer.c_man.configurationChanged();
+       
     }
 
@@ -212,4 +235,5 @@
     {
 	ArrayList variable_model = new ArrayList();
+       	variable_model.add(""); 
 	variable_model.add("[Text]");
 	ArrayList every_metadata_set_element = MetadataSetManager.getEveryMetadataSetElement();
@@ -235,5 +259,5 @@
     private class FormatControl
 	extends JPanel 
-	implements Control {
+	implements Control{
 	
 	private ArrayList feature_model;
@@ -246,6 +270,8 @@
 	private JButton insert_button;
 	private JButton remove_button;
-	private JButton replace_button;
-	private JCheckBox enabled_checkbox;
+	private JButton default_button;
+        private JButton undo_button;    
+	private JButton redo_button; 
+   	private JCheckBox enabled_checkbox;
 	private JComboBox feature_combobox;
 	private JComboBox part_combobox;
@@ -258,11 +284,16 @@
 	private JPanel selection_pane;
 	private String view_type;
-
-	private final Dimension FIELD_SIZE = new Dimension(200, 30);
-	public FormatControl() {
+  	private final Dimension FIELD_SIZE = new Dimension(200, 30);
+        private final UndoManager undo = new UndoManager();
+        private boolean newtext = true;
+        private Format previousFormat = null;
+        private Format currentFormat = null;        
+        private boolean fresh = true;
+       
+        public FormatControl() {
 	    feature_model = buildFeatureModel();
 	    part_model = buildPartModel();
 	    variable_model = buildVariableModel();
-
+	 
 	    // Create
 	    JPanel header_pane = new DesignPaneHeader("CDM.GUI.Formats", "formatstatements");
@@ -310,5 +341,5 @@
 	    editor_textarea.setRows(6);
 	    editor_textarea.setWrapStyleWord(false);
-	    Dictionary.registerTooltip(editor_textarea, "CDM.FormatManager.Editor_Tooltip");
+	    Dictionary.registerTooltip(editor_textarea, "CDM.FormatManager.Add_Tooltip");
 
 	    JPanel variable_pane = new JPanel();
@@ -321,5 +352,9 @@
 	    insert_button.setMnemonic(KeyEvent.VK_I);
 	    Dictionary.registerBoth(insert_button, "CDM.FormatManager.Insert", "CDM.FormatManager.Insert_Tooltip");
-
+      
+            default_button = new GLIButton();
+	    default_button.setMnemonic(KeyEvent.VK_D);
+	    Dictionary.registerBoth(default_button, "CDM.FormatManager.Default", "CDM.FormatManager.Default_Tooltip");
+      
 	    JPanel flag_pane = new JPanel();
 	    enabled_checkbox = new JCheckBox();
@@ -331,8 +366,6 @@
 	    add_button.setMnemonic(KeyEvent.VK_A);
 	    Dictionary.registerBoth(add_button, "CDM.FormatManager.Add", "CDM.FormatManager.Add_Tooltip");
-	    replace_button = new GLIButton();
-	    replace_button.setEnabled(false);
-	    replace_button.setMnemonic(KeyEvent.VK_C);
-	    Dictionary.registerBoth(replace_button, "CDM.FormatManager.Replace", "CDM.FormatManager.Replace_Tooltip");
+
+	   
 	    remove_button = new GLIButton();
 	    remove_button.setEnabled(false);
@@ -340,19 +373,41 @@
 	    Dictionary.registerBoth(remove_button, "CDM.FormatManager.Remove", "CDM.FormatManager.Remove_Tooltip");
 
+            undo_button = new GLIButton();
+	    undo_button.setEnabled(false);
+	    undo_button.setMnemonic(KeyEvent.VK_U);
+	    Dictionary.registerBoth(undo_button, "CDM.FormatManager.Undo", "CDM.FormatManager.Undo_Tooltip");
+
+            redo_button = new GLIButton();
+	    redo_button.setEnabled(false);
+	    redo_button.setMnemonic(KeyEvent.VK_O);
+	    Dictionary.registerBoth(redo_button, "CDM.FormatManager.Redo", "CDM.FormatManager.Redo_Tooltip");
+
+
 	    // Connect
 	    add_button.addActionListener(new AddListener());
 	    add_button.addActionListener(CollectionDesignManager.collect_cfg_change_listener);
-	    insert_button.addActionListener(new InsertListener());
-	    insert_button.addActionListener(CollectionDesignManager.collect_cfg_change_listener);
+	 
 	    remove_button.addActionListener(new RemoveListener());
 	    remove_button.addActionListener(CollectionDesignManager.collect_cfg_change_listener);
-	    replace_button.addActionListener(new ReplaceListener());
-	    replace_button.addActionListener(CollectionDesignManager.collect_cfg_change_listener);
+            default_button.addActionListener(new DefaultListener());
+	    default_button.addActionListener(CollectionDesignManager.collect_cfg_change_listener);
+            undo_button.addActionListener(new UndoListener());
+            redo_button.addActionListener(new RedoListener());
 	    enabled_checkbox.addActionListener(new EnabledListener());
 	    enabled_checkbox.addActionListener(CollectionDesignManager.collect_cfg_change_listener);
 	    feature_combobox.addActionListener(new FeatureListener());
 	    part_combobox.addActionListener(new PartListener());
-	    editor_textarea.getDocument().addDocumentListener(new EditorListener());
-	    format_list.addListSelectionListener(new FormatListListener());
+	    
+            editor_textarea.getDocument().addDocumentListener(new EditorListener());
+	    // Listen for undo and redo events
+	    editor_textarea.getDocument().addUndoableEditListener(new UndoableEditListener() {
+		    public void undoableEditHappened(UndoableEditEvent evt) {
+			undo.addEdit(evt.getEdit());
+		    }
+		});
+
+
+            format_list.addListSelectionListener(new FormatListListener());
+            variable_combobox.addActionListener(new VariableListener());
 
 	    // Layout
@@ -361,4 +416,5 @@
 	    format_list_pane.setLayout(new BorderLayout());
 	    format_list_pane.add(new JScrollPane(format_list), BorderLayout.CENTER);
+            
 
 	    feature_pane.setBorder(BorderFactory.createEmptyBorder(5,0,0,0));
@@ -372,7 +428,4 @@
 	    part_pane.add(part_combobox, BorderLayout.CENTER);
 
-	    //selection_pane.setLayout(new GridLayout(2,1,0,2));
-	    //selection_pane.add(feature_pane);
-	    //selection_pane.add(part_pane);
 
 	    flag_pane.add(enabled_checkbox);
@@ -381,15 +434,19 @@
 	    editor_header_pane.setLayout(new GridLayout(1,3));
 	    editor_header_pane.add(editor_label);
-
+	    
+	    JPanel rupanel = new JPanel();
+            rupanel.setLayout(new GridLayout(1,2));
+	    rupanel.add(undo_button);      
+	    rupanel.add(redo_button);
+     
 	    variable_pane.setBorder(BorderFactory.createEmptyBorder(2,0,2,0));
 	    variable_pane.setLayout(new GridLayout(1,3));
 	    variable_pane.add(variable_label);
 	    variable_pane.add(variable_combobox);
-	    variable_pane.add(insert_button);
+	    variable_pane.add(rupanel);
 
 	    editor_pane.setLayout(new BorderLayout());
 	    editor_pane.add(editor_header_pane, BorderLayout.NORTH);
-	    editor_pane.add(new JScrollPane(editor_textarea), BorderLayout.CENTER);
-	    //editor_pane.add(variable_pane, BorderLayout.SOUTH);
+            editor_pane.add(new JScrollPane(editor_textarea), BorderLayout.CENTER);
 
 	    selection_pane.setLayout(new BorderLayout());
@@ -402,9 +459,11 @@
 	    control_pane.add(selection_pane, VALUE);
 	    control_pane.add(blank_pane, BLANK);
-
+           
+           
 	    button_pane.setLayout(new GridLayout(1,3));
 	    button_pane.add(add_button);
-	    button_pane.add(replace_button);
 	    button_pane.add(remove_button);
+            button_pane.add(default_button);      
+            
 
 	    center_pane.setLayout(new BorderLayout());
@@ -424,4 +483,6 @@
 	}
        
+
+
 	/** Overriden to ensure that the instructions pane is scrolled to the top.
 	 */
@@ -442,5 +503,5 @@
 		variable_combobox.setModel(new DefaultComboBoxModel(variable_model.toArray()));
 		variable_combobox.setSelectedItem(selected_variable);
-	    }   
+ 	    }   
 	}
 
@@ -451,4 +512,6 @@
 	}
 
+
+    
 	/** Listens for clicks on the add button, and if the relevant details are provided adds a new format. Note that formats are responsible for codecing the values into something that can be a) stored in a DOM and b) written to file */
 	private class AddListener
@@ -457,4 +520,5 @@
 	    public void actionPerformed(ActionEvent event)
 	    {
+	
 		ignore_event = true; // Prevent format_list excetera propagating events
 
@@ -470,18 +534,18 @@
 		if (view_type.equals(FLAG)) {
 		    format = new Format(f, p, enabled_checkbox.isSelected());
-		}
+           	}
 		else {
 		    format = new Format(f, p, editor_textarea.getText());
+		   
 		}
+
+	
 		addFormat(format);
-
+		existingFormat();
 		// Save the collection configuration file immediately
-		Gatherer.g_man.design_pane.saveConfiguration();
-
-		add_button.setEnabled(false);
-		replace_button.setEnabled(false);
-		remove_button.setEnabled(true);
-
-		// Update list selection
+	
+		Gatherer.c_man.configurationChanged();
+
+       		// Update list selection
 		format_list.setSelectedValue(format, true);
 		format = null;
@@ -497,24 +561,50 @@
  
 	    public void changedUpdate(DocumentEvent e) {
+             
 		update();
 	    }
           
 	    public void insertUpdate(DocumentEvent e) {
-		update();
+		update();	
+		updateUndo("insert");                   
+	    
 	    }
           
 	    public void removeUpdate(DocumentEvent e) {
 		update();
+		updateUndo("remove");         
+	   
+	    }
+
+            private void updateUndo(String from){
+
+	  	if (!newtext){
+		    undo_button.setEnabled(true);
+		}                   
+
+		if (editor_textarea.getText().length()!=0 && newtext){
+		    newtext = false;                                                               		
+   		}               
 	    }
 
 	    public void update() {
+	                              
 		// Determine if replace should be enabled
 		if(!format_list.isSelectionEmpty()) {
 		    Format format = (Format)format_list.getSelectedValue();
-		    replace_button.setEnabled(!format.isParamType() && editor_textarea.getText() != format.getValue());
+		    boolean shouldSave = !format.isParamType();
+		    
+                    if (shouldSave){
+			format.setValue(editor_textarea.getText());
+                        Gatherer.c_man.configurationChanged();
+                        model.refresh((DOMProxyListEntry)format);
+		    }
+                                                                         
 		}
 		else {
-		    replace_button.setEnabled(false);
+		    add_button.setEnabled(false);
+		   
 		}
+        	
 	    }
 	}
@@ -527,6 +617,12 @@
 		if(!format_list.isSelectionEmpty()) {
 		    Format format = (Format)format_list.getSelectedValue();
-		    replace_button.setEnabled(format.isParamType() && enabled_checkbox.isSelected() != format.getState());
+		    boolean shouldSave = format.isParamType();
+		    if (shouldSave){
+			format.setState(enabled_checkbox.isSelected());
+                        Gatherer.c_man.configurationChanged();
+                        model.refresh((DOMProxyListEntry)format);
+		    } 
 		}
+
 		// Thats it. Add would have been enabled upon feature/part selection depending if no existing format, um, existed.
 	    }
@@ -536,4 +632,9 @@
 	    implements ActionListener {
 	    public void actionPerformed(ActionEvent event) {
+                undo_button.setEnabled(false);
+                redo_button.setEnabled(false);
+                default_button.setEnabled(true);
+		newtext = true; 
+	
 		if(!ignore_event) {
 		    ignore_event = true;
@@ -549,5 +650,5 @@
 		    }
 		    // Step two: the rest
-		    
+		   
 		    String name = entry.toString();
 		    // Add is only enabled if there isn't already a format for the choosen feature and part. Create a dummy format and test if itsa already in the model
@@ -558,5 +659,4 @@
 		    if(f.toString().length() == 0 && pname.length() == 0) {
                         add_button.setEnabled(false);
-			replace_button.setEnabled(false);
 			remove_button.setEnabled(false);
 		    }
@@ -565,4 +665,5 @@
 		       Format format = getFormat(Format.generateName(f, pname));
 		       // If there is an existing feature, select it, and use it to determine what controls are visible
+		      
 		       if(format != null) {
 			  ///ystem.err.println("There is an existing format!");
@@ -584,14 +685,16 @@
 				view_type = VALUE;
 				// Initial value
-				editor_textarea.setText(format.getValue());
+
+                                editor_textarea.setText(format.getValue());
+                                editor_textarea.setCaretPosition(0);
+                                			                              
 			    }
+                            existingFormat(); 
 			    control_pane.updateUI();
-			    remove_button.setEnabled(true);
-                            add_button.setEnabled(false);
 		       }
 		       // Otherwise there is no existing format, so we proceed by checking against the feature name
 			else {
 			     ///ystem.err.println("No existing format");
-			    format_list.clearSelection();
+                            format_list.clearSelection();
 			    if(Format.isParamType(name)) {
 			       ///ystem.err.println("Flag type");
@@ -602,4 +705,5 @@
 				// Initial value
 				enabled_checkbox.setSelected(false);
+				enabled_checkbox.setEnabled(false);
 			    }
 			    else {
@@ -609,7 +713,10 @@
 				view_type = VALUE;
 				// Initial value
+
 				editor_textarea.setText(part.getDefaultFormat());
+                                editor_textarea.setCaretPosition(0);
+	                   
 			    }
-			    add_button.setEnabled(true);
+			    newFormat();
 			}
 			format = null;
@@ -619,9 +726,9 @@
 		    pname = null;
 		    f = null;
-		    replace_button.setEnabled(false);
 		    name = null;
 		    entry = null;
 		    ignore_event = false;
 		}
+                undo.discardAllEdits();	
 	    }
 	}
@@ -630,7 +737,14 @@
 	    implements ListSelectionListener {
 	    public void valueChanged(ListSelectionEvent event) {
+                undo_button.setEnabled(false);
+                redo_button.setEnabled(false);
+                default_button.setEnabled(true);
+		newtext = true;
+		
 		if(!ignore_event && !event.getValueIsAdjusting()) {
+		
 		    if(!format_list.isSelectionEmpty()) {
-			ignore_event = true;
+                        existingFormat();
+                    	ignore_event = true;
 			Format format = (Format)format_list.getSelectedValue();
 			// Try to match the target, remembering the entries within are Entry's
@@ -679,32 +793,71 @@
 			      part_combobox.insertItemAt(a_part, part_combobox.getItemCount());
 			      part_combobox.setSelectedItem(a_part);
-			   }
 			   
+                           }
+			  
 			   editor_textarea.setText(format.getValue());
-			}
+			   editor_textarea.setCaretPosition(0);
+                          			  
+                        }
+                            
 			//control_pane.updateUI();
-			remove_button.setEnabled(true);
 			ignore_event = false;
 		    }
-		    else {
-			remove_button.setEnabled(false);
-		    }
-		    add_button.setEnabled(false);
-		    replace_button.setEnabled(false);
+		  
 		}
-	    }
-	}
-
-	private class InsertListener
+		undo.discardAllEdits();
+    	    }
+         
+	}
+
+        private void newFormat(){
+	    editor_textarea.setEditable(false);
+	    editor_textarea.setBackground(Color.lightGray); 
+	    Dictionary.registerTooltip(editor_textarea, "CDM.FormatManager.Add_Tooltip");
+
+	    enabled_checkbox.setEnabled(false);
+            undo_button.setEnabled(false);
+	    redo_button.setEnabled(false);
+            variable_combobox.setEnabled(false);  
+	    add_button.setEnabled(true);
+	    remove_button.setEnabled(false);
+            default_button.setEnabled(false);
+                  
+	}
+
+          private void existingFormat(){
+	    editor_textarea.setEditable(true);
+	    editor_textarea.setBackground(Color.white);
+	    Dictionary.registerTooltip(editor_textarea, "CDM.FormatManager.Edit_Tooltip");
+	    enabled_checkbox.setEnabled(true);
+            variable_combobox.setEnabled(true);  
+	    add_button.setEnabled(false);
+	    remove_button.setEnabled(true);
+            default_button.setEnabled(true);
+                  
+	}
+             
+	private class VariableListener
 	    implements ActionListener {
 	    public void actionPerformed(ActionEvent event) {
-		editor_textarea.insert((String)variable_combobox.getSelectedItem(), editor_textarea.getCaretPosition());
-	    }
-	}
-       
+                String selected_value = (String)variable_combobox.getSelectedItem();
+              
+		if (!selected_value.equals("")){
+		    editor_textarea.insert(selected_value, editor_textarea.getCaretPosition());   
+		    undo_button.setEnabled(true);
+
+		}
+	    }
+	    
+	}
+
 	private class PartListener
 	    implements ActionListener {
 	    public void actionPerformed(ActionEvent event) {
-		if(!ignore_event) {
+		undo_button.setEnabled(false);
+                redo_button.setEnabled(false);
+                default_button.setEnabled(true);
+	     	newtext = true;
+          	if(!ignore_event) {
 		    // Add is only enabled if there isn't already a format for the choosen feature and part. Create a dummy format and test if its already in the model
 		    Entry entry = (Entry) feature_combobox.getSelectedItem();
@@ -715,5 +868,4 @@
 		    if(f.toString().length() == 0 && pname.length() == 0) {
 			add_button.setEnabled(false);
-			replace_button.setEnabled(false);
 			remove_button.setEnabled(false);
 		    }
@@ -738,8 +890,11 @@
 				view_type = VALUE;
 				// Initial value
+                              
 				editor_textarea.setText(format.getValue());
+			        editor_textarea.setCaretPosition(0);
+                                
 			    }
 			    control_pane.updateUI();
-			    remove_button.setEnabled(true);
+			    existingFormat(); 
 			}
 			else {
@@ -752,4 +907,5 @@
 				// Initial value
 				enabled_checkbox.setSelected(false);
+				enabled_checkbox.setEnabled(false);
 			    }
 			    else {
@@ -758,7 +914,10 @@
 				view_type = VALUE;
 				// Initial value
-				editor_textarea.setText(part.getDefaultFormat());
+                               
+                                editor_textarea.setText(part.getDefaultFormat());
+				editor_textarea.setCaretPosition(0);
+			       
 			    }
-			    add_button.setEnabled(true);
+                            newFormat();
 			}
 			format = null;
@@ -770,6 +929,6 @@
 		    f = null;
 		    entry = null;
-		    replace_button.setEnabled(false);
 		}
+		undo.discardAllEdits();
 	    }
 	}
@@ -785,60 +944,118 @@
 
 		    // Save the collection configuration file immediately
-		    Gatherer.g_man.design_pane.saveConfiguration();
-
+		    Gatherer.c_man.configurationChanged();
 		    // Change buttons
 		    add_button.setEnabled(true);
-		    replace_button.setEnabled(false);
+		    newFormat();
+                    
 		}
-
-		remove_button.setEnabled(false);
-	    }
-	}
-
-	private class ReplaceListener
+	    }
+	}
+
+
+
+ private class DefaultListener
 	    implements ActionListener {
 
 	    public void actionPerformed(ActionEvent event)
 	    {
-		ignore_event = true; // Prevent format_list excetera propagating events
-
-		if (!format_list.isSelectionEmpty()) {
-		    // Remove the current format
-		    removeFormat((Format) format_list.getSelectedValue());
-
+		newtext = false;
+		if(!ignore_event) {	 
 		    Entry entry = (Entry) feature_combobox.getSelectedItem();
 		    Object f = entry.getFeature();
-		    String p = "";
-		    if (entry.canHavePart()) {
-			p = part_combobox.getSelectedItem().toString();
-		    }
-
-		    // Add a new format string of the appropriate type
-		    Format format = null;
-		    if (view_type.equals(FLAG)) {
-			format = new Format(f, p, enabled_checkbox.isSelected());
+		    String name ="";
+		    String pname="";
+                    Part part = null;
+
+		    if (entry.canHavePart()){
+			part = (Part) part_combobox.getSelectedItem();
+			pname = part.getName();
+			name = Format.generateName(f, pname);		
+		    }
+		    else{
+			name = entry.toString();
+		    }
+
+		    Format format = getFormat(name);
+		    // If there is an existing feature, select it.
+		    if(format != null) {
+			remove_button.setEnabled(true);
 		    }
 		    else {
-			format = new Format(f, p, editor_textarea.getText());
-		    }
-		    addFormat(format);
-
-		    // Save the collection configuration file immediately
-		    Gatherer.g_man.design_pane.saveConfiguration();
-
-		    add_button.setEnabled(false);
-		    remove_button.setEnabled(true);
-		    // Update list selection
-		    format_list.setSelectedValue(format, true);
-		    format = null;
-		    p = null;
-		    f = null;
-		    entry = null;
+						
+			add_button.setEnabled(true);
+		    }//endif (format !=null)
+	
+		    if(Format.isParamType(name)) {
+			// Flags first.
+			card_layout.show(control_pane, FLAG);
+			view_type = FLAG;
+			    // Initial value
+			enabled_checkbox.setSelected(false);
+		    }
+		    else {
+			card_layout.show(control_pane, VALUE);
+			view_type = VALUE;
+			// Initial value
+			if (pname !=null && pname.length()!=0 ){
+			    editor_textarea.setText((String)format_map.get(pname));
+			    editor_textarea.setCaretPosition(0);
+			}
+			else{
+			    editor_textarea.setText((String)format_map.get(name));
+			    editor_textarea.setCaretPosition(0);
+			}
+		    }
+		    
 		}
-
-		replace_button.setEnabled(false);
-		ignore_event = false;
-	    }
-	}
+	    }
+
+ }
+
+        private class UndoListener
+	    implements ActionListener {
+
+	    public void actionPerformed(ActionEvent event)
+	    {
+                try {
+                    if (undo.canUndo()) {
+                        int pos = editor_textarea.getCaretPosition();
+                        undo.undo();
+			editor_textarea.setCaretPosition(pos);
+                        redo_button.setEnabled(true); 
+
+                    }
+                    if (!undo.canUndo()){
+			undo_button.setEnabled(false);
+		    }
+		    else{
+                      	undo_button.setEnabled(true); 
+		    }   
+
+                } catch (Exception e) {
+                }           
+	    }
+	}
+    
+       private class RedoListener
+	    implements ActionListener {
+         public void actionPerformed(ActionEvent evt) {
+                try {
+                    if (undo.canRedo()) {
+			int pos = editor_textarea.getCaretPosition();
+                        undo.redo();
+                        editor_textarea.setCaretPosition(pos); 
+                    }
+                    if (!undo.canRedo()){
+			redo_button.setEnabled(false);
+		    }
+		    else{
+                      	redo_button.setEnabled(true); 
+		    }                     
+
+                } catch (Exception e) {}
+	 }
+       }
+
     }
 
