--- a/openide.awt/src/org/openide/awt/UndoRedo.java +++ a/openide.awt/src/org/openide/awt/UndoRedo.java @@ -115,7 +115,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); @@ -275,4 +275,251 @@ 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 + * as a single undo/redo.
  4. + *
  5. Use {@link beginUndoGroup} to commit accumulated + * UndoableEdits into a single CompoundEdit + * (and to continue accumulating); + * an application could do this at strategic points, such as EndOfLine + * input or cursor movement. In this way, the application can accumulate + * large chunks.
  6. + *
+ * @see UndoManager + */ + public static class UndoGroupManager extends UndoManager { + /** signals that edits are being 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 + */ + public synchronized void beginUndoGroup() { + commitUndoGroup(); + buildUndoGroup = true; + } + + /** + * Direct this UndoGroupManager to stop coalescing edits. + * Until beginUndoGroupManager is invoked, + * any received UndoableEdits are added singly. + *

+ * This has no effect if edits are not being coalesced, for example + * if beginUndoGroup has not been called. + */ + 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. + */ + private synchronized void commitUndoGroup() { + if(undoGroup == null) { + return; + } + // super.addEdit may end up in this.addEdit, + // so buildUndoGroup must be false + boolean saveBuildUndoGroup = buildUndoGroup; + buildUndoGroup = false; + + undoGroup.end(); + super.addEdit(undoGroup); + undoGroup = null; + + buildUndoGroup = saveBuildUndoGroup; + } + + /** Add this edit separately, not part of a group. + * @return super.addEdit + */ + private boolean commitAddEdit(UndoableEdit anEdit) { + commitUndoGroup(); + + boolean saveBuildUndoGroup = buildUndoGroup; + buildUndoGroup = false; + boolean f = super.addEdit(anEdit); + //boolean f = addEdit(undoGroup); + buildUndoGroup = saveBuildUndoGroup; + return f; + } + + /** SeparateEdit tags an UndoableEdit so the + * UndoGroupManager does not coalesce it. + */ + public interface SeparateEdit { + } + + /** + * 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 + */ + @Override + public synchronized boolean addEdit(UndoableEdit anEdit) { + if(!isInProgress()) + return false; + + if(buildUndoGroup) { + if(anEdit instanceof SeparateEdit) + return commitAddEdit(anEdit); + if(undoGroup == null) + undoGroup = new CompoundEdit(); + return undoGroup.addEdit(anEdit); + } else { + return super.addEdit(anEdit); + } + } + + /** {@inheritDoc} */ + @Override + public synchronized void discardAllEdits() { + commitUndoGroup(); + super.discardAllEdits(); + } + + // + // TODO: limits + // + + /** {@inheritDoc} */ + @Override + public synchronized void undoOrRedo() { + commitUndoGroup(); + super.undoOrRedo(); + } + + /** {@inheritDoc} */ + @Override + public synchronized boolean canUndoOrRedo() { + if(undoGroup != null) + return true; + return super.canUndoOrRedo(); + } + + /** {@inheritDoc} */ + @Override + public synchronized void undo() { + commitUndoGroup(); + super.undo(); + } + + /** {@inheritDoc} */ + @Override + public synchronized boolean canUndo() { + if(undoGroup != null) + return true; + return super.canUndo(); + } + + /** {@inheritDoc} */ + @Override + public synchronized void redo() { + if(undoGroup != null) + throw new CannotRedoException(); + super.redo(); + } + + /** {@inheritDoc} */ + @Override + public synchronized boolean canRedo() { + if(undoGroup != null) + return false; + return super.canRedo(); + } + + /** {@inheritDoc} */ + @Override + public synchronized void end() { + commitUndoGroup(); + super.end(); + } + + /** {@inheritDoc} */ + @Override + public synchronized String getUndoOrRedoPresentationName() { + if(undoGroup != null) + return undoGroup.getUndoPresentationName(); + return super.getUndoOrRedoPresentationName(); + } + + /** {@inheritDoc} */ + @Override + public synchronized String getUndoPresentationName() { + if(undoGroup != null) + return undoGroup.getUndoPresentationName(); + return super.getUndoPresentationName(); + } + + /** {@inheritDoc} */ + @Override + public synchronized String getRedoPresentationName() { + if(undoGroup != null) + return undoGroup.getRedoPresentationName(); + return super.getRedoPresentationName(); + } + + /** {@inheritDoc} */ + @Override + public boolean isSignificant() { + if(undoGroup != null && undoGroup.isSignificant()) { + return true; + } + return super.isSignificant(); + } + + /** {@inheritDoc} */ + @Override + public synchronized void die() { + commitUndoGroup(); + super.die(); + } + + /** {@inheritDoc} */ + @Override + public String getPresentationName() { + if(undoGroup != null) + return undoGroup.getPresentationName(); + return super.getPresentationName(); + } + + // The protected methods are only accessed from + // synchronized methods that do commitUndoGroup + // so they do not need to be overridden in this class + } + } --- a/openide.text/src/org/openide/text/CloneableEditorSupport.java +++ a/openide.text/src/org/openide/text/CloneableEditorSupport.java @@ -2983,7 +2983,8 @@ } /** Generic undoable edit that delegates to the given undoable edit. */ - private class FilterUndoableEdit implements UndoableEdit { + private class FilterUndoableEdit + implements UndoableEdit, UndoRedo.Manager.SeparateEdit { protected UndoableEdit delegate; FilterUndoableEdit() {