Lines 15-95
Link Here
|
15 |
</HEAD> |
15 |
</HEAD> |
16 |
<BODY BGCOLOR="#ffffff"> |
16 |
<BODY BGCOLOR="#ffffff"> |
17 |
<h2>Undoable operations</h2> |
17 |
<h2>Undoable operations</h2> |
18 |
<p>We've looked at many different ways to contribute actions to the workbench, but we |
18 |
<p>We've looked at many different ways to contribute commands to the workbench, but we |
19 |
haven't focused on the implementation of an action's <tt>run()</tt> method. The mechanics |
19 |
haven't focused on the implementation of a command's <tt>execute(ExecutionEvent)</tt> method. |
20 |
of the method depend on the specific action in question, but structuring the code as an |
20 |
The mechanics |
21 |
<b>undoable operation</b> allows the action to participate in the platform undo and redo |
21 |
of the method depend on the specific command in question, but structuring the code as an |
|
|
22 |
<b>undoable operation</b> allows the command to participate in the platform undo and redo |
22 |
support.</p> |
23 |
support.</p> |
23 |
<p> |
24 |
<p> |
24 |
The platform provides an <b>undoable operations framework</b> in the package |
25 |
The platform provides an <b>undoable operations framework</b> in the package |
25 |
<b><a href="../reference/api/org/eclipse/core/commands/operations/package-summary.html">org.eclipse.core.commands.operations</a></b>. |
26 |
<b><a href="../reference/api/org/eclipse/core/commands/operations/package-summary.html">org.eclipse.core.commands.operations</a></b>. |
26 |
By implementing the code inside a <tt>run()</tt> method to create an |
27 |
By implementing the code inside a <tt>execute(ExecutionEvent)</tt> method to create an |
27 |
<a href="../reference/api/org/eclipse/core/commands/operations/IUndoableOperation.html"><b>IUndoableOperation</b></a>, |
28 |
<a href="../reference/api/org/eclipse/core/commands/operations/IUndoableOperation.html"><b>IUndoableOperation</b></a>, |
28 |
the operation can be made available for undo and redo. Converting an action to use operations is straightforward, |
29 |
the operation can be made available for undo and redo. Converting an command or action to use operations is straightforward, |
29 |
apart from implementing the undo and redo behavior itself.</p> |
30 |
apart from implementing the undo and redo behavior itself.</p> |
30 |
<h3>Writing an undoable operation</h3> |
31 |
<h3>Writing an undoable operation</h3> |
31 |
<p> |
32 |
<p> |
32 |
We'll start by looking at a very simple example. Recall the simple |
33 |
We'll start by looking at a very simple example. Recall the simple |
33 |
<b>ViewActionDelegate</b> provided in the readme example plug-in. When |
34 |
example in <b>org.eclipse.ui.examples.contributions.editor.DeltaInfoHandler</b>. |
34 |
invoked, the action simply launches a dialog that announces it was executed.</p> |
35 |
It builds a string and then opens a dialog.</p> |
35 |
<pre> |
36 |
<pre> |
36 |
public void run(org.eclipse.jface.action.IAction action) { |
37 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
37 |
MessageDialog.openInformation(view.getSite().getShell(), |
38 |
// Build the string buffer "buf" |
38 |
MessageUtil.getString("Readme_Editor"), |
39 |
MessageDialog.openInformation(editor.getSite().getShell(), |
39 |
MessageUtil.getString("View_Action_executed")); |
40 |
ContributionMessages.DeltaInfoHandler_shellTitle, buf |
40 |
}</pre> |
41 |
.toString()); |
|
|
42 |
return null; |
43 |
} |
44 |
</pre> |
41 |
|
45 |
|
42 |
Using operations, the run method is responsible for creating an operation that does |
46 |
Using operations, the execute method is responsible for creating an operation that does |
43 |
the work formerly done in the run method, and requesting that an <b>operations history</b> |
47 |
the work formerly done in the execute method, and requesting that an <b>operations history</b> |
44 |
execute the operation, so that it can be remembered for undo and redo. |
48 |
execute the operation, so that it can be remembered for undo and redo. |
45 |
<pre> |
49 |
<pre> |
46 |
public void run(org.eclipse.jface.action.IAction action) { |
50 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
47 |
IUndoableOperation operation = new ReadmeOperation( |
51 |
IUndoableOperation operation = new DeltaInfoOperation( |
48 |
view.getSite().getShell()); |
52 |
editor.getSite().getShell()); |
49 |
... |
53 |
operationHistory.execute(operation, null, null); |
50 |
operationHistory.execute(operation, null, null); |
54 |
return null; |
51 |
} |
55 |
} |
52 |
</pre> |
56 |
</pre> |
53 |
The operation encapsulates the old behavior from the run method, as well as the undo |
57 |
The operation encapsulates the old behavior from the run method, as well as the undo |
54 |
and redo for the operation. |
58 |
and redo for the operation. |
55 |
<pre> |
59 |
<pre> |
56 |
class ReadmeOperation extends AbstractOperation { |
60 |
class DeltaInfoOperation extends AbstractOperation { |
57 |
Shell shell; |
61 |
Shell shell; |
58 |
public ReadmeOperation(Shell shell) { |
62 |
public DeltaInfoOperation(Shell shell) { |
59 |
super("Readme Operation"); |
63 |
super("Delta Operation"); |
60 |
this.shell = shell; |
64 |
this.shell = shell; |
61 |
} |
65 |
} |
62 |
public IStatus execute(IProgressMonitor monitor, IAdaptable info) { |
66 |
public IStatus execute(IProgressMonitor monitor, IAdaptable info) { |
63 |
MessageDialog.openInformation(shell, |
67 |
// Build the string buffer "buf" |
64 |
MessageUtil.getString("Readme_Editor"), |
68 |
MessageDialog.openInformation(shell, |
65 |
MessageUtil.getString("View_Action_executed")); |
69 |
ContributionMessages.DeltaInfoHandler_shellTitle, buf |
|
|
70 |
.toString()); |
66 |
return Status.OK_STATUS; |
71 |
return Status.OK_STATUS; |
67 |
} |
72 |
} |
68 |
public IStatus undo(IProgressMonitor monitor, IAdaptable info) { |
73 |
public IStatus undo(IProgressMonitor monitor, IAdaptable info) { |
69 |
MessageDialog.openInformation(shell, |
74 |
// Build the string buffer "buf" |
70 |
MessageUtil.getString("Readme_Editor"), |
75 |
MessageDialog.openInformation(shell, |
71 |
"Undoing view action"); |
76 |
ContributionMessages.DeltaInfoHandler_shellTitle, |
|
|
77 |
"Undoing delta calculation"); |
72 |
return Status.OK_STATUS; |
78 |
return Status.OK_STATUS; |
73 |
} |
79 |
} |
74 |
public IStatus redo(IProgressMonitor monitor, IAdaptable info) { |
80 |
public IStatus redo(IProgressMonitor monitor, IAdaptable info) { |
75 |
MessageDialog.openInformation(shell, |
81 |
// Build the string buffer "buf" |
76 |
MessageUtil.getString("Readme_Editor"), |
82 |
// simply re-calculate the delta |
77 |
"Redoing view action"); |
83 |
MessageDialog.openInformation(shell, |
|
|
84 |
ContributionMessages.DeltaInfoHandler_shellTitle, buf |
85 |
.toString()); |
78 |
return Status.OK_STATUS; |
86 |
return Status.OK_STATUS; |
79 |
} |
87 |
} |
80 |
} |
88 |
} |
81 |
</pre> |
89 |
</pre> |
82 |
|
90 |
|
83 |
<p> |
91 |
<p> |
84 |
For simple actions, it may be possible to move all of the nuts and bolt work into the operation class. |
92 |
For simple commands, it may be possible to move all of the nuts and bolt work into the operation class. |
85 |
In this case, it may be appropriate to collapse the former action classes into a single action class |
93 |
In this case, it may be appropriate to collapse the former handler classes into a single handler class |
86 |
that is parameterized. The action would simply execute the supplied operation when it is time to run. |
94 |
that is parameterized. The handler would simply execute the supplied operation when it is time to execute. |
87 |
This is largely an application design decision. |
95 |
This is largely an application design decision. |
88 |
</p> |
96 |
</p> |
89 |
<p> |
97 |
<p> |
90 |
When an action launches a wizard, then the operation is typically created as part of the wizard's |
98 |
When a command launches a wizard, then the operation is typically created as part of the wizard's |
91 |
<tt>performFinish()</tt> method or a wizard page's <tt>finish()</tt> method. Converting the <tt>finish</tt> method |
99 |
<tt>performFinish()</tt> method or a wizard page's <tt>finish()</tt> method. Converting the <tt>finish</tt> method |
92 |
to use operations is similar to converting a <tt>run</tt> method. The method is responsible for creating |
100 |
to use operations is similar to converting an <tt>execute</tt> method. The method is responsible for creating |
93 |
and executing an operation that does the work previously done inline. |
101 |
and executing an operation that does the work previously done inline. |
94 |
</p> |
102 |
</p> |
95 |
<h3>Operation history</h3> |
103 |
<h3>Operation history</h3> |
Lines 97-107
Link Here
|
97 |
So far we've used an <b>operations history</b> without really explaining it. |
105 |
So far we've used an <b>operations history</b> without really explaining it. |
98 |
Let's look again at the code that creates our example operation.</p> |
106 |
Let's look again at the code that creates our example operation.</p> |
99 |
<pre> |
107 |
<pre> |
100 |
public void run(org.eclipse.jface.action.IAction action) { |
108 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
101 |
IUndoableOperation operation = new ReadmeOperation( |
109 |
IUndoableOperation operation = new DeltaInfoOperation( |
102 |
view.getSite().getShell()); |
110 |
editor.getSite().getShell()); |
103 |
... |
111 |
... |
104 |
<b>operationHistory.execute(operation, null, null);</b> |
112 |
operationHistory.execute(operation, null, null); |
|
|
113 |
return null; |
105 |
} |
114 |
} |
106 |
</pre> |
115 |
</pre> |
107 |
What is the <b>operation history</b> all about? |
116 |
What is the <b>operation history</b> all about? |
Lines 123-129
Link Here
|
123 |
operation history and also provides protocol to access it. The following snippet demonstrates how to |
132 |
operation history and also provides protocol to access it. The following snippet demonstrates how to |
124 |
obtain the operation history from the workbench. </p> |
133 |
obtain the operation history from the workbench. </p> |
125 |
<pre> |
134 |
<pre> |
126 |
IWorkbench workbench = view.getSite().getWorkbenchWindow().getWorkbench(); |
135 |
IWorkbench workbench = editor.getSite().getWorkbenchWindow().getWorkbench(); |
127 |
IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory(); |
136 |
IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory(); |
128 |
</pre> |
137 |
</pre> |
129 |
Once an operation history is obtained, it can be used to query the undo or redo history, find out which operation |
138 |
Once an operation history is obtained, it can be used to query the undo or redo history, find out which operation |
Lines 152-168
Link Here
|
152 |
Undo contexts can be created by the plug-in that is creating the undoable operations, or accessed through API. For |
161 |
Undo contexts can be created by the plug-in that is creating the undoable operations, or accessed through API. For |
153 |
example, the workbench provides access to an undo context that can be used for workbench-wide operations. However they |
162 |
example, the workbench provides access to an undo context that can be used for workbench-wide operations. However they |
154 |
are obtained, undo contexts should be assigned when an operation is created. The following snippet shows how the |
163 |
are obtained, undo contexts should be assigned when an operation is created. The following snippet shows how the |
155 |
readme plug-in's <b>ViewActionDelegate</b> could assign a workbench-wide context to its operations. |
164 |
<tt>execute</tt> method could assign a workbench-wide context to its operations. |
156 |
</p> |
165 |
</p> |
157 |
<pre> |
166 |
<pre> |
158 |
public void run(org.eclipse.jface.action.IAction action) { |
167 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
159 |
IUndoableOperation operation = new ReadmeOperation( |
168 |
IUndoableOperation operation = new DeltaInfoOperation( |
160 |
view.getSite().getShell()); |
169 |
editor.getSite().getShell()); |
161 |
IWorkbench workbench = view.getSite().getWorkbenchWindow().getWorkbench(); |
170 |
... |
162 |
IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory(); |
171 |
IWorkbench workbench = editor.getSite().getWorkbenchWindow().getWorkbench(); |
163 |
IUndoContext undoContext = workbench.getOperationSupport().getUndoContext(); |
172 |
IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory(); |
164 |
operation.addContext(undoContext); |
173 |
IUndoContext undoContext = workbench.getOperationSupport().getUndoContext(); |
165 |
operationHistory.execute(operation, null, null); |
174 |
operation.addContext(undoContext); |
|
|
175 |
operationHistory.execute(operation, null, null); |
176 |
return null; |
166 |
} |
177 |
} |
167 |
</pre> |
178 |
</pre> |
168 |
<p> |
179 |
<p> |
Lines 214-227
Link Here
|
214 |
Whenever an operation is executed, undone, or redone using operation history protocol, clients can |
225 |
Whenever an operation is executed, undone, or redone using operation history protocol, clients can |
215 |
provide a progress monitor and any additional UI info that may be needed for performing the operation. |
226 |
provide a progress monitor and any additional UI info that may be needed for performing the operation. |
216 |
This information is passed to the operation itself. In our original |
227 |
This information is passed to the operation itself. In our original |
217 |
example, the readme action constructed an operation with a shell parameter that could be used to open |
228 |
example, the <tt>execute</tt> method constructed an operation with a shell parameter that could be used to open |
218 |
the dialog. Instead of storing the shell in the operation, a better approach is to pass parameters |
229 |
the dialog. Instead of storing the shell in the operation, a better approach is to pass parameters |
219 |
to the execute, undo, and redo methods that provide any UI information needed to run the operation. These |
230 |
to the execute, undo, and redo methods that provide any UI information needed to run the operation. These |
220 |
parameters will be passed on to the operation itself.</p> |
231 |
parameters will be passed on to the operation itself.</p> |
221 |
<pre>public void run(org.eclipse.jface.action.IAction action) { |
232 |
<pre>public Object execute(ExecutionEvent event) throws ExecutionException { |
222 |
IUndoableOperation operation = new ReadmeOperation(); |
233 |
IUndoableOperation operation = new DeltaInfoOperation( |
223 |
... |
234 |
editor.getSite().getShell()); |
224 |
<b>operationHistory.execute(operation, null, infoAdapter);</b> |
235 |
... |
|
|
236 |
<b>operationHistory.execute(operation, null, infoAdapter);</b> |
237 |
return null; |
225 |
} |
238 |
} |
226 |
</pre> |
239 |
</pre> |
227 |
The <b>infoAdapter</b> is an <a href="../reference/api/org/eclipse/core/runtime/IAdaptable.html"><b>IAdaptable</b></a> |
240 |
The <b>infoAdapter</b> is an <a href="../reference/api/org/eclipse/core/runtime/IAdaptable.html"><b>IAdaptable</b></a> |
Lines 232-240
Link Here
|
232 |
if (info != null) { |
245 |
if (info != null) { |
233 |
Shell shell = (Shell)info.getAdapter(Shell.class); |
246 |
Shell shell = (Shell)info.getAdapter(Shell.class); |
234 |
if (shell != null) { |
247 |
if (shell != null) { |
|
|
248 |
// Build the string buffer "buf" |
235 |
MessageDialog.openInformation(shell, |
249 |
MessageDialog.openInformation(shell, |
236 |
MessageUtil.getString("Readme_Editor"), |
250 |
ContributionMessages.DeltaInfoHandler_shellTitle, buf |
237 |
MessageUtil.getString("View_Action_executed")); |
251 |
.toString()); |
238 |
return Status.OK_STATUS; |
252 |
return Status.OK_STATUS; |
239 |
} |
253 |
} |
240 |
} |
254 |
} |
Lines 242-249
Link Here
|
242 |
} |
256 |
} |
243 |
</pre> |
257 |
</pre> |
244 |
|
258 |
|
245 |
<h3>Undo and redo action handlers</h3> |
259 |
<h3>Undo and redo action handlers (Deprecated)</h3> |
246 |
<p>The platform provides standard undo and redo <a href="wrkAdv_retarget.htm"><b>retargetable action handlers</b></a> |
260 |
<p>The platform provides standard undo and redo <b>retargetable action handlers</b> |
247 |
that can be configured by views and editors to provide undo and redo support for their particular context. When the |
261 |
that can be configured by views and editors to provide undo and redo support for their particular context. When the |
248 |
action handler is created, a context is assigned to it so that the operations history is filtered in a way appropriate for |
262 |
action handler is created, a context is assigned to it so that the operations history is filtered in a way appropriate for |
249 |
that particular view. The action handlers take care of updating the undo and redo labels to show the current |
263 |
that particular view. The action handlers take care of updating the undo and redo labels to show the current |