001    //////////////////////////////////////////////////
002    // This file is part of Sears project.
003    // Subtitle Editor And Re-Synch
004    // A tool to easily modify and resynch movies subtitles.
005    /////////////////////////////////////////////////
006    //This program is free software; 
007    //you can redistribute it and/or modify it under the terms 
008    //of the GNU General Public License 
009    //as published by the Free Software Foundation; 
010    //either version 2 of the License, or (at your option) any later version.
011    /////////////////////////////////////////////////
012    //Sears project is available under sourceforge
013    // at adress: http://sourceforge.net/projects/sears/
014    //Copyright (C) 2005 Booba Skaya
015    //Mail: booba.skaya@gmail.com
016    ////////////////////////////////////////////////
017    package sears.gui.undo;
018    
019    import java.awt.event.ActionEvent;
020    
021    import javax.swing.event.UndoableEditEvent;
022    import javax.swing.event.UndoableEditListener;
023    import javax.swing.undo.UndoManager;
024    
025    import sears.gui.MainWindow;
026    import sears.tools.SearsAction;
027    
028    /**
029     * SearsUndoManager This manage the undo/redo for Sears.
030     */
031    public class SearsUndoManager extends UndoManager implements
032                    UndoableEditListener {
033            /** (<b>long</b>) serialVersionUID: The serialVersionUID */
034            private static final long serialVersionUID = 5177764530749161060L;
035    
036            /** UNDO action key. */
037            public static final String ACTION_KEY_UNDO = "Undo";
038            /** REDO action key. */
039            public static final String ACTION_KEY_REDO = "Redo";
040    
041            /** (<b>SearsAction</b>) undoAction: The undoAction */
042            private SearsAction undoAction;
043    
044            /** (<b>SearsAction</b>) redoAction: The redoAction */
045            private SearsAction redoAction;
046    
047            /*
048             * (non-Javadoc)
049             * 
050             * @see javax.swing.event.UndoableEditListener#undoableEditHappened(javax.swing.event.UndoableEditEvent)
051             */
052            public void undoableEditHappened(UndoableEditEvent e) {
053                    addEdit(e.getEdit());
054                    // Update action status
055                    undoAction.setEnabled(canUndo());
056                    redoAction.setEnabled(canRedo());
057            }
058    
059            /**
060             * Class UndoAction. <br>
061             * <b>Summary:</b><br>
062             * The undo Action
063             */
064            class UndoAction extends SearsAction {
065    
066                    /** (<b>long</b>) serialVersionUID: The serialVersionUID */
067                    private static final long serialVersionUID = -513067279434959525L;
068    
069                    public UndoAction() {
070                            super(ACTION_KEY_UNDO);
071                            setEnabled(false);
072                    }
073    
074                    /*
075                     * (non-Javadoc)
076                     * 
077                     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
078                     */
079                    public void actionPerformed(ActionEvent e) {
080                            undo();
081                            MainWindow.instance.updateTableAndActions();
082                            setEnabled(canUndo());
083                            redoAction.setEnabled(canRedo());
084                    }
085            }
086    
087            /**
088             * Class RedoAction. <br>
089             * <b>Summary:</b><br>
090             * The undo Action
091             */
092            class RedoAction extends SearsAction {
093    
094                    /** (<b>long</b>) serialVersionUID: The serialVersionUID */
095                    private static final long serialVersionUID = -513067279434959525L;
096    
097                    public RedoAction() {
098                            super(ACTION_KEY_REDO);
099                            setEnabled(false);
100                    }
101    
102                    /*
103                     * (non-Javadoc)
104                     * 
105                     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
106                     */
107                    public void actionPerformed(ActionEvent e) {
108                            redo();
109                            MainWindow.instance.updateTableAndActions();
110                            undoAction.setEnabled(canUndo());
111                            setEnabled(canRedo());
112                    }
113            }
114    
115            /**
116             * Method getUndoAction. <br>
117             * <b>Summary:</b><br>
118             * Return the undoAction.
119             * 
120             * @return the undoAction
121             */
122            public SearsAction getUndoAction() {
123                    if (undoAction == null) {
124                            undoAction = new UndoAction();
125    
126                    }
127    
128                    return undoAction;
129            }
130    
131            /**
132             * Method getRedoAction. <br>
133             * <b>Summary:</b><br>
134             * Return the redoAction.
135             * 
136             * @return the redoAction
137             */
138            public SearsAction getRedoAction() {
139                    if (redoAction == null) {
140                            redoAction = new RedoAction();
141    
142                    }
143    
144                    return redoAction;
145            }
146    
147    }