Search in sources :

Example 1 with OperationHistoryActionHandler

use of org.eclipse.ui.operations.OperationHistoryActionHandler in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method createUndoRedoActions.

/**
 * Creates this editor's undo/redo actions.
 * <p>
 * Subclasses may override or extend.</p>
 *
 * @since 3.1
 */
protected void createUndoRedoActions() {
    IUndoContext undoContext = getUndoContext();
    if (undoContext != null) {
        // Use actions provided by global undo/redo
        // Create the undo action
        OperationHistoryActionHandler undoAction = new UndoActionHandler(getEditorSite(), undoContext);
        PlatformUI.getWorkbench().getHelpSystem().setHelp(undoAction, IAbstractTextEditorHelpContextIds.UNDO_ACTION);
        undoAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_UNDO);
        registerUndoRedoAction(ITextEditorActionConstants.UNDO, undoAction);
        // Create the redo action.
        OperationHistoryActionHandler redoAction = new RedoActionHandler(getEditorSite(), undoContext);
        PlatformUI.getWorkbench().getHelpSystem().setHelp(redoAction, IAbstractTextEditorHelpContextIds.REDO_ACTION);
        redoAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_REDO);
        registerUndoRedoAction(ITextEditorActionConstants.REDO, redoAction);
        // Install operation approvers
        IOperationHistory history = OperationHistoryFactory.getOperationHistory();
        // The first approver will prompt when operations affecting outside elements are to be undone or redone.
        if (fNonLocalOperationApprover != null)
            history.removeOperationApprover(fNonLocalOperationApprover);
        fNonLocalOperationApprover = getUndoRedoOperationApprover(undoContext);
        history.addOperationApprover(fNonLocalOperationApprover);
        // and it is not the most recent operation in the editor.
        if (fLinearUndoViolationApprover != null)
            history.removeOperationApprover(fLinearUndoViolationApprover);
        fLinearUndoViolationApprover = new LinearUndoViolationUserApprover(undoContext, this);
        history.addOperationApprover(fLinearUndoViolationApprover);
    } else {
        // Use text operation actions (pre 3.1 style)
        ResourceAction action;
        if (getAction(ITextEditorActionConstants.UNDO) == null) {
            // $NON-NLS-1$
            action = new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Undo.", this, ITextOperationTarget.UNDO);
            action.setHelpContextId(IAbstractTextEditorHelpContextIds.UNDO_ACTION);
            action.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_UNDO);
            setAction(ITextEditorActionConstants.UNDO, action);
        }
        if (getAction(ITextEditorActionConstants.REDO) == null) {
            // $NON-NLS-1$
            action = new TextOperationAction(EditorMessages.getBundleForConstructedKeys(), "Editor.Redo.", this, ITextOperationTarget.REDO);
            action.setHelpContextId(IAbstractTextEditorHelpContextIds.REDO_ACTION);
            action.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_REDO);
            setAction(ITextEditorActionConstants.REDO, action);
        }
    }
}
Also used : IUndoContext(org.eclipse.core.commands.operations.IUndoContext) OperationHistoryActionHandler(org.eclipse.ui.operations.OperationHistoryActionHandler) LinearUndoViolationUserApprover(org.eclipse.ui.operations.LinearUndoViolationUserApprover) IOperationHistory(org.eclipse.core.commands.operations.IOperationHistory) RedoActionHandler(org.eclipse.ui.operations.RedoActionHandler) UndoActionHandler(org.eclipse.ui.operations.UndoActionHandler)

Example 2 with OperationHistoryActionHandler

use of org.eclipse.ui.operations.OperationHistoryActionHandler in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method doSetInput.

/**
 * Called directly from <code>setInput</code> and from within a workspace
 * runnable from <code>init</code>, this method does the actual setting
 * of the editor input. Closes the editor if <code>input</code> is
 * <code>null</code>. Disconnects from any previous editor input and its
 * document provider and connects to the new one.
 * <p>
 * Subclasses may extend.
 * </p>
 *
 * @param input the input to be set
 * @exception CoreException if input cannot be connected to the document
 *            provider
 */
protected void doSetInput(IEditorInput input) throws CoreException {
    ISaveablesLifecycleListener listener = getSite().getService(ISaveablesLifecycleListener.class);
    if (listener == null)
        fSavable = null;
    if (input == null) {
        close(isSaveOnCloseNeeded());
        if (fSavable != null) {
            listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_CLOSE, getSaveables(), false));
            fSavable.disconnectEditor();
            fSavable = null;
        }
    } else {
        boolean mustSendLifeCycleEvent = false;
        if (fSavable != null) {
            listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_CLOSE, getSaveables(), false));
            fSavable.disconnectEditor();
            fSavable = null;
            mustSendLifeCycleEvent = true;
        }
        IEditorInput oldInput = getEditorInput();
        if (oldInput != null)
            getDocumentProvider().disconnect(oldInput);
        super.setInput(input);
        updateDocumentProvider(input);
        IDocumentProvider provider = getDocumentProvider();
        if (provider == null) {
            IStatus s = new Status(IStatus.ERROR, PlatformUI.PLUGIN_ID, IStatus.OK, EditorMessages.Editor_error_no_provider, null);
            throw new CoreException(s);
        }
        provider.connect(input);
        initializeTitle(input);
        if (fSourceViewer != null) {
            initializeSourceViewer(input);
            // Reset the undo context for the undo and redo action handlers
            IAction undoAction = getAction(ITextEditorActionConstants.UNDO);
            IAction redoAction = getAction(ITextEditorActionConstants.REDO);
            boolean areOperationActionHandlersInstalled = undoAction instanceof OperationHistoryActionHandler && redoAction instanceof OperationHistoryActionHandler;
            IUndoContext undoContext = getUndoContext();
            if (undoContext != null && areOperationActionHandlersInstalled) {
                ((OperationHistoryActionHandler) undoAction).setContext(undoContext);
                ((OperationHistoryActionHandler) redoAction).setContext(undoContext);
            } else {
                createUndoRedoActions();
            }
        }
        if (fIsOverwriting)
            toggleOverwriteMode();
        setInsertMode(getLegalInsertModes().get(0));
        updateCaret();
        updateStatusField(ITextEditorActionConstants.STATUS_CATEGORY_ELEMENT_STATE);
        if (fSelectionListener != null)
            fSelectionListener.setDocument(getDocumentProvider().getDocument(input));
        IVerticalRuler ruler = getVerticalRuler();
        if (ruler instanceof CompositeRuler)
            updateContributedRulerColumns((CompositeRuler) ruler);
        // Send savable life-cycle if needed.
        if (mustSendLifeCycleEvent && listener != null)
            listener.handleLifecycleEvent(new SaveablesLifecycleEvent(this, SaveablesLifecycleEvent.POST_OPEN, getSaveables(), false));
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) OperationHistoryActionHandler(org.eclipse.ui.operations.OperationHistoryActionHandler) IStatus(org.eclipse.core.runtime.IStatus) IAction(org.eclipse.jface.action.IAction) IVerticalRuler(org.eclipse.jface.text.source.IVerticalRuler) CompositeRuler(org.eclipse.jface.text.source.CompositeRuler) ISaveablesLifecycleListener(org.eclipse.ui.ISaveablesLifecycleListener) IUndoContext(org.eclipse.core.commands.operations.IUndoContext) CoreException(org.eclipse.core.runtime.CoreException) IEditorInput(org.eclipse.ui.IEditorInput) SaveablesLifecycleEvent(org.eclipse.ui.SaveablesLifecycleEvent)

Example 3 with OperationHistoryActionHandler

use of org.eclipse.ui.operations.OperationHistoryActionHandler in project eclipse.platform.text by eclipse.

the class AbstractTextEditor method registerUndoRedoAction.

/**
 * Registers the given undo/redo action under the given ID and ensures that previously installed
 * actions get disposed. It also takes care of re-registering the new action with the global
 * action handler.
 *
 * @param actionId the action id under which to register the action
 * @param action the action to register or <code>null</code> to dispose them
 * @since 3.1
 */
private void registerUndoRedoAction(String actionId, OperationHistoryActionHandler action) {
    IAction oldAction = getAction(actionId);
    if (oldAction instanceof OperationHistoryActionHandler)
        ((OperationHistoryActionHandler) oldAction).dispose();
    if (action == null)
        return;
    setAction(actionId, action);
    IActionBars actionBars = getEditorSite().getActionBars();
    if (actionBars != null)
        actionBars.setGlobalActionHandler(actionId, action);
}
Also used : OperationHistoryActionHandler(org.eclipse.ui.operations.OperationHistoryActionHandler) IAction(org.eclipse.jface.action.IAction) IActionBars(org.eclipse.ui.IActionBars)

Aggregations

OperationHistoryActionHandler (org.eclipse.ui.operations.OperationHistoryActionHandler)3 IUndoContext (org.eclipse.core.commands.operations.IUndoContext)2 IAction (org.eclipse.jface.action.IAction)2 IOperationHistory (org.eclipse.core.commands.operations.IOperationHistory)1 CoreException (org.eclipse.core.runtime.CoreException)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 CompositeRuler (org.eclipse.jface.text.source.CompositeRuler)1 IVerticalRuler (org.eclipse.jface.text.source.IVerticalRuler)1 IActionBars (org.eclipse.ui.IActionBars)1 IEditorInput (org.eclipse.ui.IEditorInput)1 ISaveablesLifecycleListener (org.eclipse.ui.ISaveablesLifecycleListener)1 SaveablesLifecycleEvent (org.eclipse.ui.SaveablesLifecycleEvent)1 LinearUndoViolationUserApprover (org.eclipse.ui.operations.LinearUndoViolationUserApprover)1 RedoActionHandler (org.eclipse.ui.operations.RedoActionHandler)1 UndoActionHandler (org.eclipse.ui.operations.UndoActionHandler)1