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            /** Default serial UID */
038            private static final long serialVersionUID = 1L;
039    
040            /** (<b>SearsUndoListener</b>) listener: The listener for this undo action. */
041            private SearsUndoManager undoManager;
042    
043            /**
044             * (<b>String</b>) UNDOTEXT_LABEL: The UNDOTEXT_LABEL a label to store
045             * undo text in action.
046             */
047            private final static String UNDOTEXT_LABEL = "UndoTextLabel";
048    
049            /**
050             * Constructor SearsUndoAction. <br>
051             * <b>Summary:</b><br>
052             * The constructor of the class SearsUndoAction
053             * 
054             * @param actionTag
055             *            The action tag off the action.
056             * @param undoManager
057             *            The undoManager to be notified when this action happens.
058             */
059            public SearsUndoAction(String actionTag, SearsUndoManager undoManager) {
060                    super(actionTag);
061                    this.undoManager = undoManager;
062                    putValue(UNDOTEXT_LABEL, SearsResourceBundle.getResource(actionTag
063                                    + "UndoText"));
064            }
065    
066            /*
067             * (non-Javadoc)
068             * 
069             * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
070             */
071            public final void actionPerformed(ActionEvent e) {
072                    SubtitleFile subtitleFile = MainWindow.instance.getSubtitleFile();
073                    if (subtitleFile != null) {
074                            // Create a clone of the current subtitle list.
075                            ArrayList<Subtitle> beforeList = (ArrayList<Subtitle>) subtitleFile
076                                            .getSubtitleListClone();
077                            // Do the action.
078                            boolean success = doUndoableAction(e);
079                            // Create a clone of the current subtitle list.
080                            ArrayList<Subtitle> afterList = (ArrayList<Subtitle>) subtitleFile
081                                            .getSubtitleListClone();
082                            if (success) {
083                                    // Create the corresponding undoable edit.
084                                    SearsUndoEdit searsUndoEdit = new SearsUndoEdit(subtitleFile,
085                                                    beforeList, afterList,
086                                                    (String) getValue(UNDOTEXT_LABEL));
087                                    // Add it to manager
088                                    undoManager.undoableEditHappened(new UndoableEditEvent(e
089                                                    .getSource(), searsUndoEdit));
090                            }
091                    } else {
092                            // IF there is no subtitle files, this action will not be undoable,
093                            // just do it.
094                            doUndoableAction(e);
095                    }
096            }
097    
098            /**
099             * Method doAction. <br>
100             * <b>Summary:</b><br>
101             * Do the undoable action. Return true if action has been done, false
102             * otherwise, not to keep in undo/redo queue actions that failed.
103             * 
104             * @param e
105             *            The initial actionEvent.
106             * @return (<b>boolean</b>) Return true if action has been done, false
107             *         otherwise.
108             */
109            public abstract boolean doUndoableAction(ActionEvent e);
110    }