001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.examples.undo;
011:
012: import org.eclipse.core.commands.operations.IOperationApprover;
013: import org.eclipse.core.commands.operations.IOperationHistory;
014: import org.eclipse.core.commands.operations.IUndoContext;
015: import org.eclipse.core.commands.operations.IUndoableOperation;
016: import org.eclipse.core.runtime.IAdaptable;
017: import org.eclipse.core.runtime.IStatus;
018: import org.eclipse.core.runtime.Status;
019: import org.eclipse.jface.dialogs.MessageDialogWithToggle;
020: import org.eclipse.jface.window.Window;
021: import org.eclipse.osgi.util.NLS;
022: import org.eclipse.swt.widgets.Shell;
023: import org.eclipse.ui.examples.undo.preferences.PreferenceConstants;
024:
025: /**
026: * An operation approver that prompts the user to see whether an undo or redo
027: * should continue. An example preference is checked to determine if prompting
028: * should occur.
029: */
030: public final class PromptingUserApprover implements IOperationApprover {
031:
032: private IUndoContext context;
033:
034: /*
035: * Create the operation approver.
036: */
037: public PromptingUserApprover(IUndoContext context) {
038: super ();
039: this .context = context;
040: }
041:
042: /*
043: * (non-Javadoc)
044: *
045: * @see org.eclipse.core.commands.operations.IOperationApprover#proceedRedoing(org.eclipse.core.commands.operations.IUndoableOperation,
046: * org.eclipse.core.commands.operations.IOperationHistory,
047: * org.eclipse.core.runtime.IAdaptable)
048: */
049: public IStatus proceedRedoing(IUndoableOperation operation,
050: IOperationHistory history, IAdaptable uiInfo) {
051:
052: // return immediately if the operation is not relevant
053: if (!operation.hasContext(context))
054: return Status.OK_STATUS;
055:
056: // allow the operation if we are not prompting
057: boolean prompt = UndoPlugin.getDefault().getPreferenceStore()
058: .getBoolean(PreferenceConstants.PREF_CONFIRMUNDO);
059: if (!prompt)
060: return Status.OK_STATUS;
061: return prompt(false, operation, uiInfo);
062: }
063:
064: /*
065: * (non-Javadoc)
066: *
067: * @see org.eclipse.core.commands.operations.IOperationApprover#proceedUndoing(org.eclipse.core.commands.operations.IUndoableOperation,
068: * org.eclipse.core.commands.operations.IOperationHistory,
069: * org.eclipse.core.runtime.IAdaptable)
070: */
071: public IStatus proceedUndoing(IUndoableOperation operation,
072: IOperationHistory history, IAdaptable uiInfo) {
073:
074: // return immediately if the operation is not relevant
075: if (!operation.hasContext(context))
076: return Status.OK_STATUS;
077:
078: // allow the operation if we are not prompting
079: boolean prompt = UndoPlugin.getDefault().getPreferenceStore()
080: .getBoolean(PreferenceConstants.PREF_CONFIRMUNDO);
081: if (!prompt)
082: return Status.OK_STATUS;
083: return prompt(true, operation, uiInfo);
084: }
085:
086: /*
087: * Prompt the user as to whether to continue the undo or redo, and return an
088: * OK_STATUS if we should continue, or a CANCEL_STATUS if we should not.
089: */
090: private IStatus prompt(boolean undoing,
091: IUndoableOperation operation, IAdaptable uiInfo) {
092: boolean createdShell = false;
093: Shell shell = getShell(uiInfo);
094: if (shell == null) {
095: if (shell == null) {
096: createdShell = true;
097: shell = new Shell();
098: }
099: }
100: String command = undoing ? UndoExampleMessages.BoxView_Undo
101: : UndoExampleMessages.BoxView_Redo;
102: String message = NLS.bind(
103: UndoExampleMessages.BoxView_ConfirmUndo, command,
104: operation.getLabel());
105: MessageDialogWithToggle dialog = MessageDialogWithToggle
106: .openOkCancelConfirm(
107: shell,
108: UndoExampleMessages.BoxView_Title,
109: message,
110: UndoExampleMessages.UndoPreferences_DoNotConfirm,
111: false, null, null);
112: UndoPlugin.getDefault().getPreferenceStore().setValue(
113: PreferenceConstants.PREF_CONFIRMUNDO,
114: !dialog.getToggleState());
115:
116: if (createdShell)
117: shell.dispose();
118: if (dialog.getReturnCode() == Window.OK)
119: return Status.OK_STATUS;
120: return Status.CANCEL_STATUS;
121: }
122:
123: /*
124: * Return the shell described by the supplied uiInfo, or null if no shell is
125: * described.
126: */
127: Shell getShell(IAdaptable uiInfo) {
128: if (uiInfo != null) {
129: Shell shell = (Shell) uiInfo.getAdapter(Shell.class);
130: if (shell != null)
131: return shell;
132: }
133: return null;
134: }
135: }
|