Class UndoTransaction

  • All Implemented Interfaces:
    org.eclipse.core.commands.operations.IUndoableOperation

    public class UndoTransaction
    extends org.eclipse.core.commands.operations.AbstractOperation
    Class used to create transactions in the operations history. Construct a new UndoTransaction instance to start the transaction, and call commit at the end, as simple as that! If it fails, call rollback, but ALWAYS make sure either commit or rollback is called using try-catch-finally, otherwise there may be leaks due to history listeners.
    Author:
    Christopher Mindus
    • Constructor Summary

      Constructors 
      ConstructorDescription
      UndoTransaction​(java.lang.String description, org.eclipse.core.commands.operations.IUndoContext undoContext)
      Constructor used to begin the undo transaction.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and TypeMethodDescription
      booleancanExecute() 
      booleancanRedo() 
      booleancanUndo() 
      voidcommit()
      Commits a series of operations in a transaction initiated with a prior call to begin.
      voiddispose() 
      org.eclipse.core.runtime.IStatusexecute​(org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.runtime.IAdaptable info) 
      org.eclipse.core.runtime.IStatusredo​(org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.runtime.IAdaptable info) 
      booleanrollback​(org.eclipse.swt.widgets.Shell shell, boolean performUndo)
      Performs a rollback.
      org.eclipse.core.runtime.IStatusundo​(org.eclipse.core.runtime.IProgressMonitor monitor, org.eclipse.core.runtime.IAdaptable info) 
      • Methods inherited from class org.eclipse.core.commands.operations.AbstractOperation

        addContext, getContexts, getLabel, hasContext, removeContext, setLabel, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • UndoTransaction

        public UndoTransaction​(java.lang.String description,
                               org.eclipse.core.commands.operations.IUndoContext undoContext)
        Constructor used to begin the undo transaction.

        It is very important to call commit or rollback in all cases, use try-catch-finally to ensure this is done even when exceptions occur.

        Parameters:
        description - The description of the entire operation.
    • Method Detail

      • rollback

        public boolean rollback​(org.eclipse.swt.widgets.Shell shell,
                                boolean performUndo)
        Performs a rollback.
        Parameters:
        shell - The shell, can be set to null if performUndo is false.
        performUndo - Flag indicating that operations already performed should be undone as much as possible. The operations that have been undone are removed from history, the ones not successful are left without change and no "global" transaction undo operation is used.
        Returns:
        Success flag when performUndo is true. false indicates at least one operation failed to be undone, true indicates all operations are undone.
      • commit

        public void commit()
        Commits a series of operations in a transaction initiated with a prior call to begin.
      • dispose

        public void dispose()
        Specified by:
        dispose in interface org.eclipse.core.commands.operations.IUndoableOperation
        Overrides:
        dispose in class org.eclipse.core.commands.operations.AbstractOperation
      • canExecute

        public boolean canExecute()
        Specified by:
        canExecute in interface org.eclipse.core.commands.operations.IUndoableOperation
        Overrides:
        canExecute in class org.eclipse.core.commands.operations.AbstractOperation
      • canRedo

        public boolean canRedo()
        Specified by:
        canRedo in interface org.eclipse.core.commands.operations.IUndoableOperation
        Overrides:
        canRedo in class org.eclipse.core.commands.operations.AbstractOperation
      • canUndo

        public boolean canUndo()
        Specified by:
        canUndo in interface org.eclipse.core.commands.operations.IUndoableOperation
        Overrides:
        canUndo in class org.eclipse.core.commands.operations.AbstractOperation
      • execute

        public org.eclipse.core.runtime.IStatus execute​(org.eclipse.core.runtime.IProgressMonitor monitor,
                                                        org.eclipse.core.runtime.IAdaptable info)
                                                 throws org.eclipse.core.commands.ExecutionException
        Specified by:
        execute in interface org.eclipse.core.commands.operations.IUndoableOperation
        Specified by:
        execute in class org.eclipse.core.commands.operations.AbstractOperation
        Throws:
        org.eclipse.core.commands.ExecutionException
      • redo

        public org.eclipse.core.runtime.IStatus redo​(org.eclipse.core.runtime.IProgressMonitor monitor,
                                                     org.eclipse.core.runtime.IAdaptable info)
                                              throws org.eclipse.core.commands.ExecutionException
        Specified by:
        redo in interface org.eclipse.core.commands.operations.IUndoableOperation
        Specified by:
        redo in class org.eclipse.core.commands.operations.AbstractOperation
        Throws:
        org.eclipse.core.commands.ExecutionException
      • undo

        public org.eclipse.core.runtime.IStatus undo​(org.eclipse.core.runtime.IProgressMonitor monitor,
                                                     org.eclipse.core.runtime.IAdaptable info)
                                              throws org.eclipse.core.commands.ExecutionException
        Specified by:
        undo in interface org.eclipse.core.commands.operations.IUndoableOperation
        Specified by:
        undo in class org.eclipse.core.commands.operations.AbstractOperation
        Throws:
        org.eclipse.core.commands.ExecutionException