Changeset 12367


Ignore:
Timestamp:
2006-08-01T13:45:40+12:00 (18 years ago)
Author:
kjdon
Message:

this now saves on losefocus, and has undo and redo buttons rather than save and revert

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gli/src/org/greenstone/gatherer/cdm/MacrosManager.java

    r12298 r12367  
    3232import javax.swing.*;
    3333import javax.swing.event.*;
     34import javax.swing.undo.*;
    3435import java.io.File;
    3536import java.io.FileInputStream;
     
    4344import org.greenstone.gatherer.Configuration;
    4445import org.greenstone.gatherer.Dictionary;
     46import org.greenstone.gatherer.Gatherer;
    4547import org.greenstone.gatherer.gui.DesignPaneHeader;
    4648import org.greenstone.gatherer.gui.GLIButton;
    4749
    48 public class MacrosManager
    49     extends JPanel {
     50public class MacrosManager {
    5051
    5152    /** The controls used to modify the general options. */
     
    9192    private class MacrosControl
    9293    extends JPanel
    93     implements Control {
     94    implements Control, WindowFocusListener {
    9495
    9596    JTextArea macros_textarea = null;
    96     JButton save_button = null;
    97     JButton revert_button = null;
    98    
     97    JButton undo_button = null;
     98    JButton redo_button = null;
     99    private final UndoManager undo = new UndoManager();
     100    private boolean macros_changed = false;
    99101    public MacrosControl() {
    100102        super();
     
    110112        macros_textarea.setWrapStyleWord(false);
    111113        macros_textarea.setToolTipText(Dictionary.get("CDM.MacrosManager.Editor_Tooltip"));
     114        readMacroFile(); // load in the original contents
     115        macros_textarea.getDocument().addDocumentListener(new EditorListener());
     116
     117        // Listen for undo and redo events
     118        macros_textarea.getDocument().addUndoableEditListener(new UndoableEditListener() {
     119            public void undoableEditHappened(UndoableEditEvent evt) {
     120            undo.addEdit(evt.getEdit());
     121            }
     122        });
    112123
    113124        JPanel macros_pane = new JPanel();
     
    115126        macros_pane.setLayout(new BorderLayout());
    116127        macros_pane.add(new JScrollPane(macros_textarea), BorderLayout.CENTER);
    117        
    118         save_button = new GLIButton(Dictionary.get("CDM.MacrosManager.Save"), Dictionary.get("CDM.MacrosManager.Save_Tooltip"));
    119         revert_button = new GLIButton(Dictionary.get("CDM.MacrosManager.Revert"), Dictionary.get("CDM.MacrosManager.Revert_Tooltip"));
    120 
    121         save_button.addActionListener(new SaveListener());
    122         revert_button.addActionListener(new RevertListener());
     128        undo_button = new GLIButton(Dictionary.get("General.Undo"), Dictionary.get("General.Undo_Tooltip"));
     129        undo_button.setEnabled(false);
     130        undo_button.addActionListener(new UndoListener());
     131
     132        redo_button = new GLIButton(Dictionary.get("General.Redo"), Dictionary.get("General.Redo_Tooltip"));
     133        redo_button.setEnabled(false);
     134        redo_button.addActionListener(new RedoListener());
     135       
    123136        JPanel button_pane = new JPanel();
    124137        button_pane.setLayout(new GridLayout(1,2));
    125         button_pane.add(save_button);
    126         button_pane.add(revert_button);
     138        button_pane.add(undo_button);
     139        button_pane.add(redo_button);
    127140
    128141        main_pane.add(macros_pane, BorderLayout.CENTER);
     
    133146        add(header_pane, BorderLayout.NORTH);
    134147        add(main_pane, BorderLayout.CENTER);
    135         readMacroFile();
     148        Gatherer.g_man.addWindowFocusListener(this);
    136149    }
    137150
     
    140153
    141154    public void loseFocus() {
     155        if (macros_changed) {
     156        writeMacroFile();
     157        macros_changed = false;
     158        }
    142159    }
    143160
    144161    public void gainFocus() {
    145         //readMacroFile();
     162    }
     163   
     164    public void windowGainedFocus(WindowEvent e) {
     165    }
     166   
     167    public void windowLostFocus(WindowEvent e) {
     168        if (macros_changed) {
     169        writeMacroFile();
     170        macros_changed = false;
     171        }       
    146172    }
    147173
     
    161187            br.close();
    162188        } catch (Exception e) {
    163             System.err.println("Exception happened, "+e.getMessage());
     189            System.err.println("MacrosManager Exception: "+e.getMessage());
    164190        }
    165191        }
     
    182208        out.close();
    183209        } catch (Exception e) {
    184        
    185         }
    186     }
    187     private class SaveListener
     210        System.err.println("MacrosManager Exception: "+e.getMessage());
     211        }
     212    }
     213
     214    private class EditorListener
     215        implements DocumentListener {
     216        public void changedUpdate(DocumentEvent e) {
     217        macros_changed = true;
     218        undo_button.setEnabled(true);
     219        }
     220         
     221        public void insertUpdate(DocumentEvent e) {
     222        macros_changed = true;
     223        undo_button.setEnabled(true);
     224        }
     225         
     226        public void removeUpdate(DocumentEvent e) {
     227        macros_changed = true;
     228        undo_button.setEnabled(true);
     229        }
     230
     231    }
     232
     233    private class UndoListener
    188234        implements ActionListener {
    189 
     235       
    190236        public void actionPerformed(ActionEvent event) {
    191         writeMacroFile();
    192         }
    193     }
    194    
    195     private class RevertListener
    196         implements ActionListener {
    197 
    198         public void actionPerformed(ActionEvent event) {
    199         readMacroFile();
    200         }
    201     }
    202     }
     237        try {
     238                    if (undo.canUndo()) {
     239                        int pos = macros_textarea.getCaretPosition();
     240                        undo.undo();
     241            macros_textarea.setCaretPosition(pos-1);
     242            macros_textarea.grabFocus();
     243            redo_button.setEnabled(true);
     244           
     245                    }
     246                    if (!undo.canUndo()){
     247            undo_button.setEnabled(false);
     248            }
     249            else{
     250                        undo_button.setEnabled(true);
     251            }   
     252           
     253                } catch (Exception e) {
     254            System.err.println("MacrosManager Exception: "+e.getMessage());
     255                }           
     256        }
     257    }
     258   
     259       private class RedoListener
     260       implements ActionListener {
     261       
     262       public void actionPerformed(ActionEvent evt) {
     263           try {
     264           if (undo.canRedo()) {
     265               int pos = macros_textarea.getCaretPosition();
     266               undo.redo();
     267               macros_textarea.setCaretPosition(pos+1);
     268               macros_textarea.grabFocus();
     269           }
     270           if (!undo.canRedo()){
     271               redo_button.setEnabled(false);
     272           }
     273           else{
     274               redo_button.setEnabled(true);
     275           }                     
     276           
     277           } catch (Exception e) {
     278           System.err.println("MacrosManager Exception: "+e.getMessage());
     279           }
     280       }
     281       }
     282   
     283    }
     284   
     285   
    203286   
    204287}
Note: See TracChangeset for help on using the changeset viewer.