KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > operations > LinearUndoViolationUserApprover


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.operations;
12
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.IOperationHistory;
15 import org.eclipse.core.commands.operations.IUndoContext;
16 import org.eclipse.core.commands.operations.IUndoableOperation;
17 import org.eclipse.core.commands.operations.LinearUndoViolationDetector;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchPart2;
25 import org.eclipse.ui.internal.Workbench;
26 import org.eclipse.ui.internal.WorkbenchMessages;
27
28 /**
29  * <p>
30  * An operation approver that prompts the user to see if linear undo violations
31  * are permitted. A linear undo violation is detected when an operation being
32  * undone or redone shares an undo context with another operation appearing more
33  * recently in the history.
34  * </p>
35  * <p>
36  * This class may be instantiated by clients.
37  * </p>
38  *
39  * @since 3.1
40  */

41 public final class LinearUndoViolationUserApprover extends
42         LinearUndoViolationDetector {
43
44     private IWorkbenchPart part;
45
46     private IUndoContext context;
47
48     /**
49      * Create a LinearUndoViolationUserApprover associated with the specified
50      * workbench part.
51      *
52      * @param context
53      * the undo context with the linear undo violation
54      * @param part
55      * the part that should be used for prompting the user
56      */

57     public LinearUndoViolationUserApprover(IUndoContext context,
58             IWorkbenchPart part) {
59         super();
60         this.part = part;
61         this.context = context;
62     }
63
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.eclipse.core.commands.operations.LinearUndoViolationDetector#allowLinearRedoViolation(org.eclipse.core.commands.operations.IUndoableOperation,
68      * org.eclipse.core.commands.operations.IUndoContext,
69      * org.eclipse.core.commands.operations.IOperationHistory,
70      * org.eclipse.core.runtime.IAdaptable)
71      */

72     protected IStatus allowLinearRedoViolation(IUndoableOperation operation,
73             IUndoContext context, IOperationHistory history, IAdaptable uiInfo) {
74
75         if (this.context != context) {
76             return Status.OK_STATUS;
77         }
78
79         final String JavaDoc message = NLS.bind(
80                 WorkbenchMessages.Operations_linearRedoViolation,
81                 getTitle(part), operation.getLabel());
82         final boolean [] proceed = new boolean[1];
83         Workbench.getInstance().getDisplay().syncExec(new Runnable JavaDoc() {
84             public void run() {
85                 // Show a dialog.
86
part.setFocus();
87                 proceed[0] = MessageDialog.openQuestion(part.getSite()
88                         .getShell(), getTitle(part), message);
89             }
90         });
91
92         if (proceed[0]) {
93             // redo the local changes first
94
while (operation != history.getRedoOperation(context)) {
95                 try {
96                     IStatus status = history.redo(context, null, uiInfo);
97                     if (!status.isOK()) {
98                         // flush the redo history because the operation
99
// failed
100
history.dispose(context, false, true, false);
101                         return Status.CANCEL_STATUS;
102                     }
103                 } catch (ExecutionException e) {
104                     // flush the redo history here because it failed.
105
history.dispose(context, false, true, false);
106                     return Status.CANCEL_STATUS;
107                 }
108             }
109             return Status.OK_STATUS;
110         }
111
112         return Status.CANCEL_STATUS;
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.eclipse.core.commands.operations.LinearUndoViolationDetector#allowLinearUndoViolation(org.eclipse.core.commands.operations.IUndoableOperation,
119      * org.eclipse.core.commands.operations.IUndoContext,
120      * org.eclipse.core.commands.operations.IOperationHistory,
121      * org.eclipse.core.runtime.IAdaptable)
122      */

123     protected IStatus allowLinearUndoViolation(IUndoableOperation operation,
124             IUndoContext context, IOperationHistory history, IAdaptable uiInfo) {
125
126         if (this.context != context) {
127             return Status.OK_STATUS;
128         }
129
130         final String JavaDoc message = NLS.bind(
131                 WorkbenchMessages.Operations_linearUndoViolation,
132                 getTitle(part), operation.getLabel());
133         final boolean [] proceed = new boolean[1];
134         Workbench.getInstance().getDisplay().syncExec(new Runnable JavaDoc() {
135             public void run() {
136                 // Show a dialog.
137
part.setFocus();
138                 proceed[0] = MessageDialog.openQuestion(part.getSite()
139                         .getShell(), getTitle(part), message);
140             }
141         });
142
143         if (proceed[0]) {
144             // redo the local changes first
145
while (operation != history.getUndoOperation(context)) {
146                 try {
147                     IStatus status = history.undo(context, null, uiInfo);
148                     if (!status.isOK()) {
149                         // flush the operation history because the operation
150
// failed.
151
history.dispose(context, true, false, false);
152                         return Status.CANCEL_STATUS;
153                     }
154                 } catch (ExecutionException e) {
155                     // flush the undo history here because something went wrong.
156
history.dispose(context, true, false, false);
157                     return Status.CANCEL_STATUS;
158                 }
159             }
160             return Status.OK_STATUS;
161         }
162         return Status.CANCEL_STATUS;
163     }
164
165     /*
166      * Get the title for the specified part. Use the newer interface
167      * IWorkbenchPart2 if available.
168      */

169     private String JavaDoc getTitle(IWorkbenchPart part) {
170         String JavaDoc title;
171         if (part instanceof IWorkbenchPart2) {
172             title = ((IWorkbenchPart2) part).getPartName();
173         } else {
174             title = part.getTitle();
175         }
176         // Null title is unexpected, but use an empty string if encountered.
177
if (title == null) {
178             title = ""; //$NON-NLS-1$
179
}
180         return title;
181     }
182 }
183
Popular Tags