001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.undo;
021:
022: import java.util.Enumeration;
023: import java.util.Hashtable;
024:
025: public class StateEdit extends AbstractUndoableEdit {
026:
027: protected static final String RCSID = "$Id: StateEdit.java,v 1.6 1997/10"
028: + "/01 20:05:51 sandipc Exp $";
029:
030: protected StateEditable object;
031:
032: protected Hashtable<Object, Object> preState;
033:
034: protected Hashtable<Object, Object> postState;
035:
036: protected String undoRedoName;
037:
038: public StateEdit(final StateEditable anObject, final String name) {
039: super ();
040: init(anObject, name);
041: }
042:
043: public StateEdit(final StateEditable anObject) {
044: this (anObject, null);
045: }
046:
047: public void end() {
048: postState = initHashtable(postState);
049: object.storeState(postState);
050: removeRedundantState();
051: }
052:
053: public String getPresentationName() {
054: return undoRedoName;
055: }
056:
057: private Hashtable<Object, Object> initHashtable(
058: final Hashtable<Object, Object> ht) {
059: if (ht == null) {
060: return new Hashtable<Object, Object>();
061: }
062: ht.clear();
063: return ht;
064: }
065:
066: protected void init(final StateEditable anObject, final String name) {
067: object = anObject;
068: undoRedoName = name;
069: preState = initHashtable(preState);
070: object.storeState(preState);
071: }
072:
073: public void redo() {
074: super .redo();
075: object.restoreState(postState);
076: }
077:
078: protected void removeRedundantState() {
079: if (preState == null || postState == null) {
080: return;
081: }
082: for (Enumeration keys = preState.keys(); keys.hasMoreElements();) {
083: Object key = keys.nextElement();
084: Object preValue = preState.get(key);
085: if (!postState.containsKey(key)) {
086: continue;
087: }
088: Object postValue = postState.get(key);
089: if ((preValue == null && postValue == null)
090: || (preValue.equals(postValue))) {
091: preState.remove(key);
092: postState.remove(key);
093: }
094: }
095: }
096:
097: public void undo() {
098: super.undo();
099: object.restoreState(preState);
100: }
101: }
|