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.util.ArrayList;
020    
021    import javax.swing.undo.CannotRedoException;
022    import javax.swing.undo.CannotUndoException;
023    import javax.swing.undo.UndoableEdit;
024    
025    import sears.file.Subtitle;
026    import sears.file.SubtitleFile;
027    
028    /**
029     * @author booba
030     * 
031     */
032    public class SearsUndoEdit implements UndoableEdit {
033    
034            /** (<b>SubtitleFile</b>) subtitleFile: The subtitleFile */
035            private SubtitleFile subtitleFile;
036            /** (<b>ArrayList<Subtitle></b>) beforeActionList: The beforeActionList */
037            private ArrayList<Subtitle> beforeActionList;
038            /** (<b>ArrayList<Subtitle></b>) afterActionList: The afterActionList */
039            protected ArrayList<Subtitle> afterActionList;
040            /** (<b>String</b>) editTitle: The editTitle */
041            private String editTitle;
042    
043            public SearsUndoEdit(SubtitleFile subtitleFile,
044                            ArrayList<Subtitle> beforeActionList,
045                            ArrayList<Subtitle> afterActionList, String editTitle) {
046                    this.subtitleFile = subtitleFile;
047                    this.beforeActionList = beforeActionList;
048                    this.afterActionList = afterActionList;
049                    this.editTitle=editTitle;
050            }
051    
052            /*
053             * (non-Javadoc)
054             * 
055             * @see javax.swing.undo.UndoableEdit#addEdit(javax.swing.undo.UndoableEdit)
056             */
057            public boolean addEdit(UndoableEdit anEdit) {
058                    //The result of the method.
059                    boolean result = false;
060                    //Only merge non atomic edit!
061                    if(anEdit instanceof SearsUndoEdit && !((SearsUndoEdit) anEdit).isAtomic()){
062                            //change final point here.
063                            afterActionList = ((SearsUndoEdit) anEdit).afterActionList;
064                            result = true;
065                    }
066                    return result;
067            }
068    
069            /*
070             * (non-Javadoc)
071             * 
072             * @see javax.swing.undo.UndoableEdit#canRedo()
073             */
074            public boolean canRedo() {
075                    return true;
076            }
077    
078            /*
079             * (non-Javadoc)
080             * 
081             * @see javax.swing.undo.UndoableEdit#canUndo()
082             */
083            public boolean canUndo() {
084                    return true;
085            }
086    
087            /*
088             * (non-Javadoc)
089             * 
090             * @see javax.swing.undo.UndoableEdit#die()
091             */
092            public void die() {
093                    //nothing to do here.
094            }
095    
096            /*
097             * (non-Javadoc)
098             * 
099             * @see javax.swing.undo.UndoableEdit#getPresentationName()
100             */
101            public String getPresentationName() {
102                    return editTitle;
103            }
104    
105            /*
106             * (non-Javadoc)
107             * 
108             * @see javax.swing.undo.UndoableEdit#getRedoPresentationName()
109             */
110            public String getRedoPresentationName() {
111                    return editTitle;
112            }
113    
114            /*
115             * (non-Javadoc)
116             * 
117             * @see javax.swing.undo.UndoableEdit#getUndoPresentationName()
118             */
119            public String getUndoPresentationName() {
120                    return editTitle;
121            }
122    
123            /*
124             * (non-Javadoc)
125             * 
126             * @see javax.swing.undo.UndoableEdit#isSignificant()
127             */
128            public boolean isSignificant() {
129                    return true;
130            }
131    
132            /*
133             * (non-Javadoc)
134             * 
135             * @see javax.swing.undo.UndoableEdit#redo()
136             */
137            public void redo() throws CannotRedoException {
138                    //To redo the action is to put as subtitle list the final one.
139                    subtitleFile.setSubtitleList(afterActionList);
140            }
141    
142            /*
143             * (non-Javadoc)
144             * 
145             * @see javax.swing.undo.UndoableEdit#replaceEdit(javax.swing.undo.UndoableEdit)
146             */
147            public boolean replaceEdit(UndoableEdit anEdit) {
148                    // TODO Auto-generated method stub
149                    return false;
150            }
151    
152            /*
153             * (non-Javadoc)
154             * 
155             * @see javax.swing.undo.UndoableEdit#undo()
156             */
157            public void undo() throws CannotUndoException {
158                    //To undo the action is to put as subtitle list the original one.
159                    subtitleFile.setSubtitleList(beforeActionList);
160            }
161            
162            /**
163             * Method isAtomic.
164             * <br><b>Summary:</b><br>
165             * returns true if action is atomic.
166             * Non atomic edit will be merged with previous in undo manager, and will be not single undoable. 
167             * @return  (<b>boolean</b>)   true if action is atomic.
168             */
169            protected boolean isAtomic(){
170                    return true;
171            }
172    }