001    /* StateEdit.java -- UndoableEdit for StateEditable implementations.
002       Copyright (C) 2002, 2003 Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010    
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package javax.swing.undo;
040    
041    import java.util.Hashtable;
042    import java.util.Iterator;
043    
044    /**
045     * A helper class, making it easy to support undo and redo.
046     *
047     * <p>The following example shows how to use this class.</p>
048     *
049     * <pre>
050     * Foo foo; // class Foo implements {@link StateEditable}
051     * StateEdit edit;
052     *
053     * edit = new StateEdit(foo, "Name Change");
054     * foo.setName("Jane Doe");
055     * edit.end();
056     * undoManager.addEdit(edit);
057     * </pre>
058     *
059     * <p>If <code>Foo</code>&#x2019;s implementation of {@link
060     * StateEditable} considers the name as part of the editable state,
061     * the user can now choose &#x201c;Undo Name Change&#x201d; or
062     * &#x201c;Redo Name Change&#x201d; from the respective menu. No
063     * further undo support is needed from the application.</p>
064     *
065     * <p>The following explains what happens in the example.</p>
066     *
067     * <ol>
068     * <li>When a <code>StateEdit</code> is created, the associated
069     *     {@link StateEditable} gets asked to store its state into a hash
070     *     table, {@link #preState}.</li>
071     * <li>The application will now perform some changes to the edited
072     *     object. This typically happens by invoking methods on the edited
073     *     object.</li>
074     * <li>The editing phase is terminated by invoking the {@link #end()}
075     *     method of the <code>StateEdit</code>. The <code>end()</code> method
076     *     does two things.
077     *
078     *     <ul>
079     *     <li>The edited object receives a second request for storing
080     *         its state.  This time, it will use a different hash table, {@link
081     *         #postState}.</li>
082     *     <li>To increase efficiency, the <code>StateEdit</code> now removes
083     *         any entries from {@link #preState} and {@link #postState} that have
084     *         the same key, and whose values are equal. Equality is determined
085     *         by invoking the <code>equals</code> method inherited from
086     *         {@link java.lang.Object}.</li>
087     *     </ul></li>
088     * <li>When the user later chooses to undo the <code>StateEdit</code>,
089     * the edited object is asked to {@linkplain StateEditable#restoreState
090     * restore its state} from the {@link #preState} table.  Similarly,
091     * when the user chooses to <i>redo</i> the <code>StateEdit</code>,
092     * the edited object gets asked to restore its state from the {@link
093     * #postState}.</li>
094     * </ol>
095     *
096     * @author Andrew Selkirk (aselkirk@sympatico.ca)
097     * @author Sascha Brawer (brawer@dandelis.ch)
098     */
099    public class StateEdit
100      extends AbstractUndoableEdit
101    {
102      /**
103       * The ID of the Java source file in Sun&#x2019;s Revision Control
104       * System (RCS).  This certainly should not be part of the API
105       * specification. But in order to be API-compatible with
106       * Sun&#x2019;s reference implementation, GNU Classpath also has to
107       * provide this field and match its value. The value used here has
108       * been in every JDK release at least from 1.2 to 1.5.
109       */
110      protected static final String RCSID = "$" +
111        "Id: StateEdit.java,v 1.6 1997/10/01 20:05:51 sandipc Exp $";
112    
113    
114      /**
115       * The object which is being edited by this <code>StateEdit</code>.
116       */
117      protected StateEditable object;
118    
119    
120      /**
121       * The state of <code>object</code> at the time of constructing
122       * this <code>StateEdit</code>.
123       */
124      protected Hashtable<Object, Object> preState;
125    
126    
127      /**
128       * The state of <code>object</code> at the time when {@link #end()}
129       * was called.
130       */
131      protected Hashtable<Object, Object> postState;
132    
133    
134      /**
135       * A human-readable name for this edit action.
136       */
137      protected String undoRedoName;
138    
139    
140      /**
141       * Constructs a <code>StateEdit</code>, specifying the object whose
142       * state is being edited.
143       *
144       * @param obj the object whose state is being edited by this
145       * <code>StateEdit</code>.
146       */
147      public StateEdit(StateEditable obj)
148      {
149        init(obj, null);
150      }
151    
152    
153      /**
154       * Constructs a <code>StateEdit</code>, specifying the object whose
155       * state is being edited.
156       *
157       * @param obj the object whose state is being edited by this
158       * <code>StateEdit</code>.
159       *
160       * @param name the human-readable name of the editing action.
161       */
162      public StateEdit(StateEditable obj, String name)
163      {
164        init(obj, name);
165      }
166    
167    
168      /**
169       * Initializes this <code>StateEdit</code>. The edited object will
170       * be asked to store its current state into {@link #preState}.
171       *
172       * @param obj the object being edited.
173       *
174       * @param name the human-readable name of the editing action.
175       */
176      protected void init(StateEditable obj, String name)
177      {
178        object = obj;
179        undoRedoName = name;
180        preState = new Hashtable<Object,Object>();
181        postState = new Hashtable<Object,Object>();
182        obj.storeState(preState);
183      }
184    
185    
186      /**
187       * Informs this <code>StateEdit</code> that all edits are finished.
188       * The edited object will be asked to store its state into {@link
189       * #postState}, and any redundant entries will get removed from
190       * {@link #preState} and {@link #postState}.
191       */
192      public void end()
193      {
194        object.storeState(postState);
195        removeRedundantState();
196      }
197    
198    
199      /**
200       * Undoes this edit operation. The edited object will be asked to
201       * {@linkplain StateEditable#restoreState restore its state} from
202       * {@link #preState}.
203       *
204       * @throws CannotUndoException if {@link #canUndo()} returns
205       * <code>false</code>, for example because this action has already
206       * been undone.
207       */
208      public void undo()
209      {
210        super.undo();
211        object.restoreState(preState);
212      }
213    
214    
215      /**
216       * Redoes this edit operation. The edited object will be asked to
217       * {@linkplain StateEditable#restoreState restore its state} from
218       * {@link #postState}.
219       *
220       * @throws CannotRedoException if {@link #canRedo()} returns
221       * <code>false</code>, for example because this action has not yet
222       * been undone.
223       */
224      public void redo()
225      {
226        super.redo();
227        object.restoreState(postState);
228      }
229    
230    
231      /**
232       * Returns a human-readable, localized name that describes this
233       * editing action and can be displayed to the user.
234       *
235       * @return the name, or <code>null</code> if no presentation
236       * name is available.
237       */
238      public String getPresentationName()
239      {
240        return undoRedoName;
241      }
242    
243    
244      /**
245       * Removes all redundant entries from the pre- and post-edit state
246       * hash tables. An entry is considered redundant if it is present
247       * both before and after the edit, and if the two values are equal.
248       */
249      protected void removeRedundantState()
250      {
251        Iterator i = preState.keySet().iterator();
252        while (i.hasNext())
253          {
254            Object key = i.next();
255            if (postState.containsKey(key))
256              {
257                if (preState.get(key).equals(postState.get(key)))
258                  {
259                    i.remove();
260                    postState.remove(key);
261                  }
262              }
263          }
264      }
265    }