jdk/src/java.desktop/share/classes/javax/swing/undo/StateEdit.java
author darcy
Fri, 03 Apr 2015 10:41:34 -0700
changeset 29894 3e16b51732f5
parent 25859 3317bb8137f4
permissions -rw-r--r--
8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}} Reviewed-by: alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
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
 */
23697
e556a715949f 8034169: Fix serial lint warnings in javax.swing
darcy
parents: 5506
diff changeset
    58
@SuppressWarnings("serial") // Same-version serialization only
2
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
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    62
    /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    63
     * Obsolete RCS version identity.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
    64
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    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
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // Attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * The object being edited
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    protected StateEditable object;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * The state information prior to the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected Hashtable<Object,Object> preState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The state information after the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected Hashtable<Object,Object> postState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The undo/redo presentation name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    protected String undoRedoName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // Constructors
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Create and return a new StateEdit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param anObject The object to watch for changing state
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @see StateEdit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public StateEdit(StateEditable anObject) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        init (anObject,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Create and return a new StateEdit with a presentation name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @param anObject The object to watch for changing state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param name The presentation name to be used for this edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @see StateEdit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public StateEdit(StateEditable anObject, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        init (anObject,name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
29894
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
   120
    /**
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
   121
     * Initialize the state edit.
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
   122
     * @param anObject The object to watch for changing state
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
   123
     * @param name The presentation name to be used for this edit
3e16b51732f5 8076520: Fix missing doclint warnings in javax.swing.{table, tree, undo, plaf.{metal, nimbus, synth}}
darcy
parents: 25859
diff changeset
   124
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    protected void init (StateEditable anObject, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        this.object = anObject;
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   127
        this.preState = new Hashtable<Object, Object>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        this.object.storeState(this.preState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        this.postState = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        this.undoRedoName = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    // Operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Gets the post-edit state of the StateEditable object and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * ends the edit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public void end() {
1301
15e81207e1f2 6727662: Code improvement and warnings removing from swing packages
rupashka
parents: 2
diff changeset
   144
        this.postState = new Hashtable<Object, Object>(11);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        this.object.storeState(this.postState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        this.removeRedundantState();
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 prior to the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void undo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        super.undo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.object.restoreState(preState);
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
     * Tells the edited object to apply the state after the edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public void redo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        super.redo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        this.object.restoreState(postState);
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
     * Gets the presentation name for this edit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public String getPresentationName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return this.undoRedoName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    // Internal support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Remove redundant key/values in state hashtables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    protected void removeRedundantState() {
25568
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   181
        Vector<Object> uselessKeys = new Vector<>();
b906a74c6882 8043550: Fix raw and unchecked lint warnings in javax.swing.*
darcy
parents: 23697
diff changeset
   182
        Enumeration<Object> myKeys = preState.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // Locate redundant state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        while (myKeys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            Object myKey = myKeys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (postState.containsKey(myKey) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                postState.get(myKey).equals(preState.get(myKey))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                uselessKeys.addElement(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
        // Remove redundant state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        for (int i = uselessKeys.size()-1; i >= 0; i--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            Object myKey = uselessKeys.elementAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            preState.remove(myKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            postState.remove(myKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
} // End of class StateEdit