Index: /trunk/gli/src/org/greenstone/gatherer/cdm/MacrosManager.java
===================================================================
--- /trunk/gli/src/org/greenstone/gatherer/cdm/MacrosManager.java	(revision 12366)
+++ /trunk/gli/src/org/greenstone/gatherer/cdm/MacrosManager.java	(revision 12367)
@@ -32,4 +32,5 @@
 import javax.swing.*;
 import javax.swing.event.*;
+import javax.swing.undo.*;
 import java.io.File;
 import java.io.FileInputStream;
@@ -43,9 +44,9 @@
 import org.greenstone.gatherer.Configuration;
 import org.greenstone.gatherer.Dictionary;
+import org.greenstone.gatherer.Gatherer;
 import org.greenstone.gatherer.gui.DesignPaneHeader;
 import org.greenstone.gatherer.gui.GLIButton;
 
-public class MacrosManager
-    extends JPanel {
+public class MacrosManager {
 
     /** The controls used to modify the general options. */
@@ -91,10 +92,11 @@
     private class MacrosControl
 	extends JPanel
-	implements Control {
+	implements Control, WindowFocusListener {
 
 	JTextArea macros_textarea = null;
-	JButton save_button = null;
-	JButton revert_button = null;
-	
+	JButton undo_button = null;
+	JButton redo_button = null;
+	private final UndoManager undo = new UndoManager();
+	private boolean macros_changed = false;
 	public MacrosControl() {
 	    super();
@@ -110,4 +112,13 @@
 	    macros_textarea.setWrapStyleWord(false);
 	    macros_textarea.setToolTipText(Dictionary.get("CDM.MacrosManager.Editor_Tooltip"));
+	    readMacroFile(); // load in the original contents
+	    macros_textarea.getDocument().addDocumentListener(new EditorListener());
+
+	    // Listen for undo and redo events
+	    macros_textarea.getDocument().addUndoableEditListener(new UndoableEditListener() {
+		    public void undoableEditHappened(UndoableEditEvent evt) {
+			undo.addEdit(evt.getEdit());
+		    }
+		});
 
 	    JPanel macros_pane = new JPanel();
@@ -115,14 +126,16 @@
 	    macros_pane.setLayout(new BorderLayout());
 	    macros_pane.add(new JScrollPane(macros_textarea), BorderLayout.CENTER);
-	    
-	    save_button = new GLIButton(Dictionary.get("CDM.MacrosManager.Save"), Dictionary.get("CDM.MacrosManager.Save_Tooltip"));
-	    revert_button = new GLIButton(Dictionary.get("CDM.MacrosManager.Revert"), Dictionary.get("CDM.MacrosManager.Revert_Tooltip"));
-
-	    save_button.addActionListener(new SaveListener());
-	    revert_button.addActionListener(new RevertListener());
+	    undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
+	    undo_button.setEnabled(false);
+	    undo_button.addActionListener(new UndoListener());
+
+	    redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
+	    redo_button.setEnabled(false);
+	    redo_button.addActionListener(new RedoListener());
+	    
 	    JPanel button_pane = new JPanel();
 	    button_pane.setLayout(new GridLayout(1,2));
-	    button_pane.add(save_button);
-	    button_pane.add(revert_button);
+	    button_pane.add(undo_button);
+	    button_pane.add(redo_button);
 
 	    main_pane.add(macros_pane, BorderLayout.CENTER);
@@ -133,5 +146,5 @@
 	    add(header_pane, BorderLayout.NORTH);
 	    add(main_pane, BorderLayout.CENTER);
-	    readMacroFile();
+	    Gatherer.g_man.addWindowFocusListener(this);
 	}
 
@@ -140,8 +153,21 @@
 
 	public void loseFocus() {
+	    if (macros_changed) {
+		writeMacroFile();
+		macros_changed = false;
+	    }
 	}
 
 	public void gainFocus() {
-	    //readMacroFile();
+	}
+	
+	public void windowGainedFocus(WindowEvent e) {
+	}
+	
+	public void windowLostFocus(WindowEvent e) {
+	    if (macros_changed) {
+		writeMacroFile();
+		macros_changed = false;
+	    }	    
 	}
 
@@ -161,5 +187,5 @@
 		    br.close();
 		} catch (Exception e) {
-		    System.err.println("Exception happened, "+e.getMessage());
+		    System.err.println("MacrosManager Exception: "+e.getMessage());
 		}
 	    }
@@ -182,23 +208,80 @@
 		out.close();
 	    } catch (Exception e) {
-		
-	    }
-	}
-	private class SaveListener
+		System.err.println("MacrosManager Exception: "+e.getMessage());
+	    }
+	}
+
+	private class EditorListener
+	    implements DocumentListener {
+	    public void changedUpdate(DocumentEvent e) {
+		macros_changed = true;
+		undo_button.setEnabled(true);
+	    }
+          
+	    public void insertUpdate(DocumentEvent e) {
+		macros_changed = true;
+		undo_button.setEnabled(true);
+	    }
+          
+	    public void removeUpdate(DocumentEvent e) {
+		macros_changed = true;
+		undo_button.setEnabled(true);
+	    }
+
+	}
+
+	private class UndoListener
 	    implements ActionListener {
-
+	    
 	    public void actionPerformed(ActionEvent event) {
-		writeMacroFile();
-	    }
-	}
-	
-	private class RevertListener
-	    implements ActionListener {
-
-	    public void actionPerformed(ActionEvent event) {
-		readMacroFile();
-	    }
-	}
-    }
+		try {
+                    if (undo.canUndo()) {
+                        int pos = macros_textarea.getCaretPosition();
+                        undo.undo();
+			macros_textarea.setCaretPosition(pos-1);
+			macros_textarea.grabFocus();
+			redo_button.setEnabled(true); 
+			
+                    }
+                    if (!undo.canUndo()){
+			undo_button.setEnabled(false);
+		    }
+		    else{
+                      	undo_button.setEnabled(true); 
+		    }   
+		    
+                } catch (Exception e) {
+		    System.err.println("MacrosManager Exception: "+e.getMessage());
+                }           
+	    }
+	}
+    
+       private class RedoListener
+	   implements ActionListener {
+	   
+	   public void actionPerformed(ActionEvent evt) {
+	       try {
+		   if (undo.canRedo()) {
+		       int pos = macros_textarea.getCaretPosition();
+		       undo.redo();
+		       macros_textarea.setCaretPosition(pos+1); 
+		       macros_textarea.grabFocus();
+		   }
+		   if (!undo.canRedo()){
+		       redo_button.setEnabled(false);
+		   }
+		   else{
+		       redo_button.setEnabled(true); 
+		   }                     
+		   
+	       } catch (Exception e) {
+		   System.err.println("MacrosManager Exception: "+e.getMessage());
+	       }
+	   }
+       }
+	
+    }
+    
+    
 	
 }
