jdk/src/share/classes/javax/swing/undo/StateEdit.java
author rupashka
Tue, 26 Aug 2008 15:12:54 +0400
changeset 1301 15e81207e1f2
parent 2 90ce3da70b43
child 1639 a97859015238
permissions -rw-r--r--
6727662: Code improvement and warnings removing from swing packages Summary: Removed unnecessary castings and other warnings Reviewed-by: malenkov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing.undo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <P>StateEdit is a general edit for objects that change state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Objects being edited must conform to the StateEditable interface.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <P>This edit class works by asking an object to store it's state in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Hashtables before and after editing occurs.  Upon undo or redo the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * object is told to restore it's state from these Hashtables.</P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * A state edit is used as follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      // Create the edit during the "before" state of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      StateEdit newEdit = new StateEdit(myObject);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      // Modify the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      myObject.someStateModifyingMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      // "end" the edit when you are done modifying the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      newEdit.end();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * </PRE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <P><EM>Note that when a StateEdit ends, it removes redundant state from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the Hashtables - A state Hashtable is not guaranteed to contain all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * keys/values placed into it when the state is stored!</EM></P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @see StateEditable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @author Ray Ryan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class StateEdit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        extends AbstractUndoableEdit {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected static final String RCSID = "$Id: StateEdit.java,v 1.6 1997/10/01 20:05:51 sandipc Exp $";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The object being edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    protected StateEditable object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * The state information prior to the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected Hashtable<Object,Object> preState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The state information after the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected Hashtable<Object,Object> postState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * The undo/redo presentation name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    protected String undoRedoName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    // Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Create and return a new StateEdit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param anObject The object to watch for changing state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @see StateEdit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public StateEdit(StateEditable anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        init (anObject,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Create and return a new StateEdit with a presentation name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @param anObject The object to watch for changing state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param name The presentation name to be used for this edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @see StateEdit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public StateEdit(StateEditable anObject, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        init (anObject,name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    protected void init (StateEditable anObject, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        this.object = anObject;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   119
        this.preState = new Hashtable<Object, Object>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        this.object.storeState(this.preState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        this.postState = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        this.undoRedoName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    // Operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Gets the post-edit state of the StateEditable object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * ends the edit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public void end() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   136
        this.postState = new Hashtable<Object, Object>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        this.object.storeState(this.postState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        this.removeRedundantState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Tells the edited object to apply the state prior to the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public void undo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        super.undo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.object.restoreState(preState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Tells the edited object to apply the state after the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void redo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        super.redo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.object.restoreState(postState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Gets the presentation name for this edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public String getPresentationName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return this.undoRedoName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    // Internal support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Remove redundant key/values in state hashtables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    protected void removeRedundantState() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   173
        Vector<Object> uselessKeys = new Vector<Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        Enumeration myKeys = preState.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // Locate redundant state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        while (myKeys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            Object myKey = myKeys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (postState.containsKey(myKey) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                postState.get(myKey).equals(preState.get(myKey))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                uselessKeys.addElement(myKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // Remove redundant state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        for (int i = uselessKeys.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            Object myKey = uselessKeys.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            preState.remove(myKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            postState.remove(myKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
} // End of class StateEdit