1 7 package javax.swing.undo; 8 9 import java.util.*; 10 11 18 public class CompoundEdit extends AbstractUndoableEdit { 19 22 boolean inProgress; 23 24 28 protected Vector<UndoableEdit > edits; 29 30 public CompoundEdit() { 31 super(); 32 inProgress = true; 33 edits = new Vector<UndoableEdit >(); 34 } 35 36 41 public void undo() throws CannotUndoException { 42 super.undo(); 43 int i = edits.size(); 44 while (i-- > 0) { 45 UndoableEdit e = (UndoableEdit )edits.elementAt(i); 46 e.undo(); 47 } 48 } 49 50 55 public void redo() throws CannotRedoException { 56 super.redo(); 57 Enumeration cursor = edits.elements(); 58 while (cursor.hasMoreElements()) { 59 ((UndoableEdit )cursor.nextElement()).redo(); 60 } 61 } 62 63 68 protected UndoableEdit lastEdit() { 69 int count = edits.size(); 70 if (count > 0) 71 return (UndoableEdit )edits.elementAt(count-1); 72 else 73 return null; 74 } 75 76 80 public void die() { 81 int size = edits.size(); 82 for (int i = size-1; i >= 0; i--) 83 { 84 UndoableEdit e = (UndoableEdit )edits.elementAt(i); 85 e.die(); 88 } 89 super.die(); 90 } 91 92 107 public boolean addEdit(UndoableEdit anEdit) { 108 if (!inProgress) { 109 return false; 110 } else { 111 UndoableEdit last = lastEdit(); 112 113 118 if (last == null) { 119 edits.addElement(anEdit); 120 } 121 else if (!last.addEdit(anEdit)) { 122 if (anEdit.replaceEdit(last)) { 123 edits.removeElementAt(edits.size()-1); 124 } 125 edits.addElement(anEdit); 126 } 127 128 return true; 129 } 130 } 131 132 138 public void end() { 139 inProgress = false; 140 } 141 142 148 public boolean canUndo() { 149 return !isInProgress() && super.canUndo(); 150 } 151 152 158 public boolean canRedo() { 159 return !isInProgress() && super.canRedo(); 160 } 161 162 169 public boolean isInProgress() { 170 return inProgress; 171 } 172 173 178 public boolean isSignificant() { 179 Enumeration cursor = edits.elements(); 180 while (cursor.hasMoreElements()) { 181 if (((UndoableEdit )cursor.nextElement()).isSignificant()) { 182 return true; 183 } 184 } 185 return false; 186 } 187 188 194 public String getPresentationName() { 195 UndoableEdit last = lastEdit(); 196 if (last != null) { 197 return last.getPresentationName(); 198 } else { 199 return super.getPresentationName(); 200 } 201 } 202 203 209 public String getUndoPresentationName() { 210 UndoableEdit last = lastEdit(); 211 if (last != null) { 212 return last.getUndoPresentationName(); 213 } else { 214 return super.getUndoPresentationName(); 215 } 216 } 217 218 224 public String getRedoPresentationName() { 225 UndoableEdit last = lastEdit(); 226 if (last != null) { 227 return last.getRedoPresentationName(); 228 } else { 229 return super.getRedoPresentationName(); 230 } 231 } 232 233 239 public String toString() 240 { 241 return super.toString() 242 + " inProgress: " + inProgress 243 + " edits: " + edits; 244 } 245 } 246 | Popular Tags |