1 19 20 package org.netbeans.modules.xml.xam.ui.undo; 21 22 import javax.swing.event.UndoableEditEvent ; 23 import javax.swing.undo.AbstractUndoableEdit ; 24 import javax.swing.undo.CannotRedoException ; 25 import javax.swing.undo.CannotUndoException ; 26 import javax.swing.undo.UndoableEdit ; 27 import org.openide.awt.UndoRedo; 28 29 38 public class CompoundUndoManager extends FilterUndoManager { 39 40 private static final long serialVersionUID = 1L; 41 42 private boolean isCompound; 43 45 private boolean undoRedoComplete; 46 48 private boolean undoRedoCompound; 49 51 private boolean consumeNextEdit; 52 54 private boolean appendEndEdit; 55 58 private boolean openCompound; 59 62 private boolean compoundInProgress; 63 64 69 public CompoundUndoManager(UndoRedo.Manager original) { 70 super(original); 71 } 72 73 78 public synchronized void beginCompound() { 79 if (!isCompound) { 80 isCompound = true; 81 consumeNextEdit = true; 82 appendEndEdit = false; 83 } 84 } 85 86 91 public synchronized void endCompound() { 92 if (isCompound) { 93 isCompound = false; 94 if (appendEndEdit) { 95 openCompound = true; 98 } 99 } 100 } 101 102 107 public synchronized boolean isCompound() { 108 return isCompound; 109 } 110 111 @Override 112 public boolean addEdit(UndoableEdit anEdit) { 113 anEdit = processEdit(anEdit); 116 return super.addEdit(anEdit); 117 } 118 119 @Override 120 public void discardAllEdits() { 121 super.discardAllEdits(); 122 if (isCompound) { 123 consumeNextEdit = true; 125 } 126 appendEndEdit = false; 128 openCompound = false; 130 } 131 132 138 private UndoableEdit processEdit(UndoableEdit anEdit) { 139 if (isCompound) { 140 if (openCompound) { 141 openCompound = false; 145 appendEndEdit = true; 146 } else if (!appendEndEdit) { 147 appendEndEdit = true; 148 if (consumeNextEdit) { 149 consumeNextEdit = false; 150 anEdit = new BeginCompoundEdit(anEdit); 152 } 153 } 154 } else if (appendEndEdit) { 155 appendEndEdit = false; 157 openCompound = false; 161 super.addEdit(new EndCompoundEdit()); 162 } 163 return anEdit; 164 } 165 166 @Override 167 public void undoableEditHappened(UndoableEditEvent event) { 168 UndoableEdit anEdit = processEdit(event.getEdit()); 170 event = new UndoableEditEvent (event.getSource(), anEdit); 173 super.undoableEditHappened(event); 176 } 177 178 @Override 179 public synchronized void redo() throws CannotRedoException { 180 if (isCompound) { 181 super.redo(); 183 } else { 184 compoundInProgress = true; 185 try { 186 undoRedoCompound = false; 189 super.redo(); 190 if (undoRedoCompound || openCompound) { 191 undoRedoComplete = false; 192 while (!undoRedoComplete && canRedo()) { 193 super.redo(); 194 } 195 } 196 } finally { 197 compoundInProgress = false; 198 } 199 } 200 } 201 202 @Override 203 public synchronized void undo() throws CannotUndoException { 204 if (isCompound) { 205 super.undo(); 207 } else { 208 compoundInProgress = true; 209 try { 210 undoRedoCompound = false; 213 super.undo(); 214 if (undoRedoCompound || openCompound) { 215 undoRedoComplete = false; 216 while (!undoRedoComplete && canUndo()) { 217 super.undo(); 218 } 219 } 220 } finally { 221 compoundInProgress = false; 222 } 223 } 224 } 225 226 229 private class BeginCompoundEdit extends FilterUndoableEdit { 230 231 private static final long serialVersionUID = 1L; 232 233 238 public BeginCompoundEdit(UndoableEdit delegate) { 239 super(); 240 this.delegate = delegate; 241 } 242 243 @Override 244 public boolean isSignificant() { 245 if (delegate != null) { 246 return compoundInProgress || delegate.isSignificant(); 247 } else { 248 return compoundInProgress; 249 } 250 } 251 252 @Override 253 public void redo() throws CannotRedoException { 254 super.redo(); 255 undoRedoCompound = true; 256 openCompound = true; 257 } 258 259 @Override 260 public void undo() throws CannotUndoException { 261 super.undo(); 262 undoRedoComplete = true; 263 openCompound = false; 264 } 265 } 266 267 274 private class EndCompoundEdit extends AbstractUndoableEdit { 275 276 private static final long serialVersionUID = 1L; 277 278 @Override 279 public boolean isSignificant() { 280 return compoundInProgress; 286 } 287 288 @Override 289 public void redo() throws CannotUndoException { 290 super.redo(); 291 undoRedoComplete = true; 292 openCompound = false; 293 } 294 295 @Override 296 public void undo() throws CannotUndoException { 297 super.undo(); 298 undoRedoCompound = true; 299 openCompound = true; 300 } 301 } 302 } 303 | Popular Tags |