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    import java.util.ArrayList;
021    
022    import javax.swing.event.UndoableEditEvent;
023    
024    import sears.file.Subtitle;
025    import sears.file.SubtitleFile;
026    import sears.gui.MainWindow;
027    import sears.tools.SearsAction;
028    import sears.tools.SearsResourceBundle;
029    
030    /**
031     * SearsUndoAction This is a generic undo/redoable action. It hardly save whole
032     * subtitles just before the actions and after. 
033     * And creates a basic SearsUndoEdit that could restore old list.
034     */
035    public abstract class SearsUndoAction extends SearsAction {
036    
037            /** (<b>SearsUndoListener</b>) listener: The listener for this undo action. */
038            private SearsUndoManager undoManager;
039    
040            /**
041             * (<b>String</b>) UNDOTEXT_LABEL: The UNDOTEXT_LABEL a label to store
042             * undo text in action.
043             */
044            private final static String UNDOTEXT_LABEL = "UndoTextLabel";
045    
046            /**
047             * Constructor SearsUndoAction. <br>
048             * <b>Summary:</b><br>
049             * The constructor of the class SearsUndoAction
050             * 
051             * @param actionTag
052             *            The action tag off the action.
053             * @param undoManager
054             *            The undoManager to be notified when this action happens.
055             */
056            public SearsUndoAction(String actionTag, SearsUndoManager undoManager) {
057                    super(actionTag);
058                    this.undoManager = undoManager;
059                    putValue(UNDOTEXT_LABEL, SearsResourceBundle.getResource(actionTag
060                                    + "UndoText"));
061            }
062    
063            /*
064             * (non-Javadoc)
065             * 
066             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
067             */
068            @SuppressWarnings("unchecked")
069            public final void actionPerformed(ActionEvent e) {
070                    SubtitleFile subtitleFile = MainWindow.instance.getSubtitleFile();
071                    if (subtitleFile != null) {
072                            // Create a clone of the current subtitle list.
073                            ArrayList<Subtitle> beforeList = (ArrayList<Subtitle>) subtitleFile
074                                            .getSubtitleListClone();
075                            // Do the action.
076                            boolean success = doUndoableAction(e);
077                            // Create a clone of the current subtitle list.
078                            ArrayList<Subtitle> afterList = (ArrayList<Subtitle>) subtitleFile
079                                            .getSubtitleListClone();
080                            if (success) {
081                                    // Create the corresponding undoable edit.
082                                    SearsUndoEdit searsUndoEdit = new SearsUndoEdit(subtitleFile,
083                                                    beforeList, afterList,
084                                                    (String) getValue(UNDOTEXT_LABEL));
085                                    // Add it to manager
086                                    undoManager.undoableEditHappened(new UndoableEditEvent(e
087                                                    .getSource(), searsUndoEdit));
088                            }
089                    } else {
090                            // IF there is no subtitle files, this action will not be undoable,
091                            // just do it.
092                            doUndoableAction(e);
093                    }
094            }
095    
096            /**
097             * Method doAction. <br>
098             * <b>Summary:</b><br>
099             * Do the undoable action. Return true if action has been done, false
100             * otherwise, not to keep in undo/redo queue actions that failed.
101             * 
102             * @param e
103             *            The initial actionEvent.
104             * @return (<b>boolean</b>) Return true if action has been done, false
105             *         otherwise.
106             */
107            public abstract boolean doUndoableAction(ActionEvent e);
108    }