API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing.undo. StateEdit View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

/*
 * @(#)StateEdit.java	1.15 05/11/17
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package javax.swing.undo;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

/**
 * <P>StateEdit is a general edit for objects that change state.
 * Objects being edited must conform to the StateEditable interface.</P>
 * 
 * <P>This edit class works by asking an object to store it's state in
 * Hashtables before and after editing occurs.  Upon undo or redo the
 * object is told to restore it's state from these Hashtables.</P>
 * 
 * A state edit is used as follows:
 * <PRE>
 *	// Create the edit during the "before" state of the object
 *	StateEdit newEdit = new StateEdit(myObject);
 *	// Modify the object
 *	myObject.someStateModifyingMethod();
 *	// "end" the edit when you are done modifying the object
 *	newEdit.end();
 * </PRE>
 * 
 * <P><EM>Note that when a StateEdit ends, it removes redundant state from
 * the Hashtables - A state Hashtable is not guaranteed to contain all
 * keys/values placed into it when the state is stored!</EM></P>
 *
 * @see StateEditable
 *
 * @version 1.15 11/17/05
 * @author Ray Ryan
 */

public class StateEdit extends AbstractUndoableEdit {

    protected static final String RCSID = "$Id: StateEdit.java,v 1.6 1997/10/01 20:05:51 sandipc Exp $";

    //
    // Attributes
    //

    /**
     * The object being edited
     */
    protected StateEditable object;

    /**
     * The state information prior to the edit
     */
    protected Hashtable<Object,Object> preState;

    /**
     * The state information after the edit
     */
    protected Hashtable<Object,Object> postState;

    /**
     * The undo/redo presentation name
     */
    protected String undoRedoName;

    //
    // Constructors
    //
    
    /**
     * Create and return a new StateEdit.
     *
     * @param anObject The object to watch for changing state
     *
     * @see StateEdit
     */
    public StateEdit(StateEditable anObject) {
        super();
	init (anObject,null);
    }

    /**
     * Create and return a new StateEdit with a presentation name.
     *
     * @param anObject The object to watch for changing state
     * @param name The presentation name to be used for this edit
     *
     * @see StateEdit
     */
    public StateEdit(StateEditable anObject, String name) {
	super();
	init (anObject,name);
    }

    protected void init (StateEditable anObject, String name) {
	this.object = anObject;
	this.preState = new Hashtable(11);
	this.object.storeState(this.preState);
	this.postState = null;
	this.undoRedoName = name;
    }


    //
    // Operation
    //


    /**
     * Gets the post-edit state of the StateEditable object and
     * ends the edit.
     */
    public void end() {
	this.postState = new Hashtable(11);
	this.object.storeState(this.postState);
	this.removeRedundantState();
    }

    /**
     * Tells the edited object to apply the state prior to the edit
     */
    public void undo() {
	super.undo();
	this.object.restoreState(preState);
    }

    /**
     * Tells the edited object to apply the state after the edit
     */
    public void redo() {
	super.redo();
	this.object.restoreState(postState);
    }

    /**
     * Gets the presentation name for this edit
     */
    public String getPresentationName() {
	return this.undoRedoName;
    }


    //
    // Internal support
    //

    /**
     * Remove redundant key/values in state hashtables.
     */
    protected void removeRedundantState() {
	Vector uselessKeys = new Vector();
	Enumeration myKeys = preState.keys();

	// Locate redundant state
	while (myKeys.hasMoreElements()) {
	    Object myKey = myKeys.nextElement();
	    if (postState.containsKey(myKey) &&
		postState.get(myKey).equals(preState.get(myKey))) {
		uselessKeys.addElement(myKey);
	    }
	}

	// Remove redundant state
	for (int i = uselessKeys.size()-1; i >= 0; i--) {
	    Object myKey = uselessKeys.elementAt(i);
	    preState.remove(myKey);
	    postState.remove(myKey);
	}
    }

} // End of class StateEdit

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar