1 15 16 package swingwtx.swing.undo; 17 18 import java.util.*; 19 20 26 public class CompoundEdit extends AbstractUndoableEdit 27 { 28 29 protected Vector edits = new Vector(); 30 31 public CompoundEdit() 32 { 33 super(); 34 } 35 36 public boolean canUndo() { return !isInProgress() && super.canUndo(); } 37 public void undo() throws CannotUndoException 38 { 39 super.undo(); 40 41 for (int i=(edits.size()-1); i >= 0; i--) 43 { 44 ((UndoableEdit)edits.elementAt(i)).undo(); 45 } 46 } 47 48 public boolean canRedo() { return !isInProgress() && super.canRedo(); } 49 public void redo() throws CannotRedoException 50 { 51 super.redo(); 52 53 Iterator iterator = edits.iterator(); 55 while (iterator.hasNext()) 56 { 57 UndoableEdit undoableEdit = (UndoableEdit) iterator.next(); 58 undoableEdit.redo(); 59 } 60 } 61 62 protected UndoableEdit lastEdit() 63 { 64 return edits.size() > 0 ? (UndoableEdit)edits.lastElement() : null; 65 } 66 67 private boolean inProgress = true; 68 public boolean isInProgress() { return inProgress; } 69 public void end() { inProgress = false; } 70 71 public void die() 72 { 73 Iterator iterator = edits.iterator(); 75 while (iterator.hasNext()) 76 { 77 UndoableEdit undoableEdit = (UndoableEdit) iterator.next(); 78 undoableEdit.die(); 79 } 80 81 super.die(); 82 } 83 84 public boolean addEdit(UndoableEdit undoableEdit) 85 { 86 boolean success = false; 87 88 do if (!isInProgress()) 90 { 91 UndoableEdit lastEdit = lastEdit(); 92 93 success = true; 94 if (edits.size() == 0) 96 { 97 edits.add(undoableEdit); 98 break; 99 } 100 else if (lastEdit.addEdit(undoableEdit)) 102 { 103 break; 104 } 105 else if (undoableEdit.replaceEdit(lastEdit)) 107 { 108 edits.remove(edits.size()-1); 109 break; 110 } 111 112 edits.add(undoableEdit); 114 } while (false); 115 116 return success; 117 } 118 119 public boolean isSignificant() 120 { 121 boolean isSignificant = false; 122 123 Iterator iterator = edits.iterator(); 125 while (iterator.hasNext()) 126 { 127 UndoableEdit undoableEdit = (UndoableEdit) iterator.next(); 128 if (undoableEdit.isSignificant()) 129 { 130 isSignificant = true; 131 break; 132 } 133 } 134 135 return isSignificant; 136 } 137 138 public String getPresentationName() 139 { 140 String presentationName; 141 142 UndoableEdit lastEdit = lastEdit(); 143 if (lastEdit == null) 145 presentationName = super.getPresentationName(); 146 else 148 presentationName = lastEdit.getPresentationName(); 149 150 return presentationName; 151 } 152 153 public String getUndoPresentationName() 154 { 155 UndoableEdit lastEdit = lastEdit(); 156 if (lastEdit == null) 158 return super.getUndoPresentationName(); 159 else 161 return lastEdit.getUndoPresentationName(); 162 } 163 164 public String getRedoPresentationName() 165 { 166 UndoableEdit lastEdit = lastEdit(); 167 if (lastEdit == null) 169 return super.getUndoPresentationName(); 170 else 172 return lastEdit.getUndoPresentationName(); 173 } 174 } 175 | Popular Tags |