? awt/patchsrc Index: awt/build.xml =================================================================== RCS file: /cvs/openide/awt/build.xml,v retrieving revision 1.7 diff -u -r1.7 build.xml --- awt/build.xml 1 Jul 2006 09:07:24 -0000 1.7 +++ awt/build.xml 9 May 2007 05:25:26 -0000 @@ -21,5 +21,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Index: awt/src/org/openide/awt/UndoRedo.java =================================================================== RCS file: /cvs/openide/awt/src/org/openide/awt/UndoRedo.java,v retrieving revision 1.5 diff -u -r1.5 UndoRedo.java --- awt/src/org/openide/awt/UndoRedo.java 27 Mar 2007 01:26:06 -0000 1.5 +++ awt/src/org/openide/awt/UndoRedo.java 9 May 2007 05:25:26 -0000 @@ -93,7 +93,7 @@ /** An undo manager which fires a change event each time it consumes a new undoable edit. */ - public static class Manager extends UndoManager implements UndoRedo { + public static class Manager extends UndoGroupManager implements UndoRedo { static final long serialVersionUID = 6721367974521509720L; private final ChangeSupport cs = new ChangeSupport(this); @@ -251,5 +251,243 @@ public String getRedoPresentationName() { return ""; // NOI18N } + } + + /** UndoGroupManager extends {@link UndoManager} + * and allows explicit control of what + * UndoableEdits are coalesced into compound edits, + * rather than using the rules defined by the edits themselves. + * Other than the default usage, special handling is initiated by invoking + * beginUndoGroup(). + *

+ * Three use cases are supported. + *

+ *
    + *
  1. Default behavior is defined by {@link UndoManager}.
  2. + *
  3. UnddoableEdits issued between {@link #beginUndoGroup} + * and {@link endUndoGroup} are placed into a single {@link CompoundEdit}. + * Thus undo() and redo() treat them atomically.
  4. + *
  5. Use {@link commitUndoGroup} to place any accumulated + * UndoableEdits into a CompoundEdit; + * an application could do this at strategic points, such as EndOfLine + * input or cursor movement. In this way, the application can accumulate + * large chunks, but this behavior would not be enabled unless + * beginUndoGroup is first invoked.
  6. + *
+ * Note that the semantics of {@link UndoManager} are preserved at all + * times, so certain methods, such as undo(), automatically issue + * commitUndoGroup(). + * @see UndoManager + */ + public static class UndoGroupManager extends UndoManager { + /** signals that edits should be accumulated */ + private boolean buildUndoGroup; + /** accumulate edits here in undoGroup */ + private CompoundEdit undoGroup; + + /** + * Direct this UndoGroupManager to begin coalescing any + * UndoableEdits that are added into a CompoundEdit. + *

If edits are already being coalesced and some have been + * accumulated, they are commited as an atomic group and a new + * group is started. + * @see #addEdit + * @see #endUndoGroup + * @see #commitUndoGroup + */ + public synchronized void beginUndoGroup() { + commitUndoGroup(); + buildUndoGroup = true; + } + + /** + * Direct this UndoGroupManager to stop coalescing edits. + * {@link #commitUndoGroup} is invoked to save accumulated edits as + * an atomic group. + * Until beginUndoGroupManager is invoked, + * received UndoableEdits are added singly. + *

+ * This has no effect if edits are not being coalesced, for example + * if beginUndoGroup has not been called. + * @see #commitUndoGroup + */ + public synchronized void endUndoGroup() { + buildUndoGroup = false; + commitUndoGroup(); + } + + /** + * Commit any accumulated UndoableEdits as an atomic + * undo/redo group. {@link CompoundEdit#end} + * is invoked on the CompoundEdit and it is added as a single + * UndoableEdit to this UndoManager. + *

+ * If edits are currently being coalesced, a new undo group is started. + * This has no effect if edits are not being coalesced, for example + * beginUndoGroup has not been called. + */ + public synchronized void commitUndoGroup() { + if(undoGroup == null) { + return; + } + // super.addEdit may end up in this.addEdit, + // so buildUndoGroup must be false + boolean restoreBuildUndoGroup = buildUndoGroup; + buildUndoGroup = false; + + undoGroup.end(); + super.addEdit(undoGroup); + undoGroup = null; + + buildUndoGroup = restoreBuildUndoGroup; + } + + + /** + * If this UndoManager is coalescing edits then add + * anEdit to the accumulating CompoundEdit. + * Otherwise, add it to this UndoManager. In either case the + * edit is saved for later undo or redo. + * @return {@inheritDoc} + * @see #beginUndoGroup + * @see #endUndoGroup + * @see #commitUndoGroup + */ + public synchronized boolean addEdit(UndoableEdit anEdit) { + if(!isInProgress()) + return false; + if(buildUndoGroup) { + if(undoGroup == null) + undoGroup = new CompoundEdit(); + return undoGroup.addEdit(anEdit); + } else { + return super.addEdit(anEdit); + } + } + + /** {@inheritDoc} */ + public synchronized void discardAllEdits() { + commitUndoGroup(); + super.discardAllEdits(); + } + + // + // TODO: limits + // + + /** {@inheritDoc} */ + public synchronized void undoOrRedo() { + commitUndoGroup(); + super.undoOrRedo(); + } + + /** {@inheritDoc} */ + public synchronized boolean canUndoOrRedo() { + if(undoGroup != null) + return true; + return super.canUndoOrRedo(); + } + + /** {@inheritDoc} */ + public synchronized void undo() { + commitUndoGroup(); + super.undo(); + } + + /** {@inheritDoc} */ + public synchronized boolean canUndo() { + if(undoGroup != null) + return true; + return super.canUndo(); + } + + /** {@inheritDoc} */ + public synchronized void redo() { + if(undoGroup != null) + throw new CannotRedoException(); + super.redo(); + } + + /** {@inheritDoc} */ + public synchronized boolean canRedo() { + if(undoGroup != null) + return false; + return super.canRedo(); + } + + /** {@inheritDoc} */ + public synchronized void end() { + commitUndoGroup(); + super.end(); + } + + /** {@inheritDoc} */ + public synchronized String getUndoOrRedoPresentationName() { + if(undoGroup != null) + return undoGroup.getUndoPresentationName(); + return super.getUndoOrRedoPresentationName(); + } + + /** {@inheritDoc} */ + public synchronized String getUndoPresentationName() { + if(undoGroup != null) + return undoGroup.getUndoPresentationName(); + return super.getUndoPresentationName(); + } + + /** {@inheritDoc} */ + public synchronized String getRedoPresentationName() { + if(undoGroup != null) + return undoGroup.getRedoPresentationName(); + return super.getRedoPresentationName(); + } + + /** {@inheritDoc} */ + public boolean isSignificant() { + if(undoGroup != null && undoGroup.isSignificant()) { + return true; + } + return super.isSignificant(); + } + + /** {@inheritDoc} */ + public synchronized void die() { + commitUndoGroup(); + super.die(); + } + + /** {@inheritDoc} */ + public String getPresentationName() { + if(undoGroup != null) + return undoGroup.getPresentationName(); + return super.getPresentationName(); + } + + // The protected methods are only accessed from + // synchronized methods that commitUndoGroup, + // they do not need to be in this class + /*protected UndoableEdit editToBeUndone() { + if(undoGroup != null) + return null; + return super.editToBeUndone(); + } + + protected UndoableEdit editToBeRedone() { + if(undoGroup != null) + return null; + return super.editToBeRedone(); + } + + protected void undoTo(UndoableEdit edit) { + if(undoGroup != null) + throw new CannotUndoException(); + super.undoTo(edit); + } + + protected void redoTo(UndoableEdit edit) { + if(undoGroup != null) + throw new CannotRedoException(); + super.redoTo(edit); + }*/ } }