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.ListIterator;
023: import java.util.NoSuchElementException;
024: import java.util.Vector;
025:
026: import org.apache.harmony.x.swing.Utilities;
027:
028: public class CompoundEdit extends AbstractUndoableEdit {
029:
030: /**
031: * This field is added as performance optimization but not as
032: * a guarantee of correct deserialization.
033: */
034: private static final long serialVersionUID = -6512679249930119683L;
035:
036: protected Vector<UndoableEdit> edits = new Vector<UndoableEdit>();
037:
038: /**
039: * This is flag that indicates edits are still being added to it.
040: * The method end() sets inProgress to false.
041: */
042: boolean inProgress = true;
043:
044: @Override
045: public boolean addEdit(final UndoableEdit anEdit) {
046: if (!inProgress) {
047: return false;
048: }
049:
050: UndoableEdit last = lastEdit();
051:
052: if (last != null
053: && (last.addEdit(anEdit) || anEdit.replaceEdit(last))) {
054: return true;
055: }
056:
057: return edits.add(anEdit);
058: }
059:
060: protected UndoableEdit lastEdit() {
061: UndoableEdit last = null;
062: try {
063: last = edits.lastElement();
064: } catch (final NoSuchElementException e) {
065: }
066:
067: return last;
068: }
069:
070: /*
071: * The format of the string is based on 1.5 release behavior
072: * which can be revealed using the following code:
073: *
074: * CompoundEdit obj = new CompoundEdit();
075: * obj.addEdit(new CompoundEdit());
076: * System.out.println(obj.toString());
077: */
078: @Override
079: public String toString() {
080: String str = super .toString();
081: str += " inProgress: " + inProgress;
082: str += " edits: [";
083: for (ListIterator li = edits.listIterator(); li.hasNext();) {
084: str += li.next().toString() + ((li.hasNext()) ? "," : "");
085: }
086: str += "]";
087: return str;
088: }
089:
090: @Override
091: public String getUndoPresentationName() {
092: UndoableEdit last = lastEdit();
093:
094: if (last != null) {
095: String undoName = last.getUndoPresentationName();
096: if (undoName.length() != 0) {
097: return undoName;
098: }
099: }
100:
101: return super .getUndoPresentationName();
102: }
103:
104: @Override
105: public String getRedoPresentationName() {
106: UndoableEdit last = lastEdit();
107:
108: if (last != null) {
109: String redoName = last.getRedoPresentationName();
110: if (redoName.length() != 0) {
111: return redoName;
112: }
113: }
114:
115: return super .getRedoPresentationName();
116: }
117:
118: @Override
119: public String getPresentationName() {
120: UndoableEdit last = lastEdit();
121:
122: if (last != null) {
123: String name = last.getPresentationName();
124: if (!Utilities.isEmptyString(name)) {
125: return name;
126: }
127: }
128:
129: return super .getPresentationName();
130: }
131:
132: @Override
133: public boolean isSignificant() {
134: for (ListIterator li = edits.listIterator(); li.hasNext();) {
135: if (((UndoableEdit) li.next()).isSignificant()) {
136: return true;
137: }
138: }
139: return false;
140: }
141:
142: public boolean isInProgress() {
143: return inProgress;
144: }
145:
146: @Override
147: public boolean canUndo() {
148: return !isInProgress() && super .canUndo();
149: }
150:
151: @Override
152: public boolean canRedo() {
153: return !isInProgress() && super .canRedo();
154: }
155:
156: @Override
157: public void undo() {
158: super .undo();
159: for (ListIterator li = edits.listIterator(edits.size()); li
160: .hasPrevious();) {
161: ((UndoableEdit) li.previous()).undo();
162: }
163: }
164:
165: @Override
166: public void redo() {
167: super .redo();
168: for (ListIterator li = edits.listIterator(); li.hasNext();) {
169: ((UndoableEdit) li.next()).redo();
170: }
171: }
172:
173: public void end() {
174: inProgress = false;
175: }
176:
177: @Override
178: public void die() {
179: super .die();
180: for (ListIterator li = edits.listIterator(edits.size()); li
181: .hasPrevious();) {
182: ((UndoableEdit) li.previous()).die();
183: }
184: }
185:
186: }
|