Debian Codegraph @

plugins/org.eclipse.emf.edit.ui/src/org/eclipse/emf/edit/ui/action/DelegatingCommandAction.java

raw
1 /**
2 * Copyright (c) 2002-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 v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.emf.edit.ui.action;
12
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.ISelectionProvider;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.ui.IActionDelegate;
20 import org.eclipse.ui.IActionDelegate2;
21 import org.eclipse.ui.IEditorActionDelegate;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.ISelectionListener;
24 import org.eclipse.ui.IViewActionDelegate;
25 import org.eclipse.ui.IViewPart;
26 import org.eclipse.ui.IWorkbenchPart;
27
28
29 /**
30 * This class wraps an {@link IActionDelegate}, e.g., a {@link CommandAction}, to make it into an {@link Action}.
31 * Even if the action delegate implements {@link IActionDelegate2}, this class will still only use the older interface
32 * (i.e. it will not call {@link IActionDelegate2#init(org.eclipse.jface.action.IAction) init}, {@link IActionDelegate2#runWithEvent
33 * runWithEvent}, or {@link IActionDelegate2#dispose dispose}, since it does not have the information required
34 * to do so).
35 */
36 public class DelegatingCommandAction extends Action implements ISelectionListener, ISelectionChangedListener
37 {
38 /**
39 * This is the action delegate we're wrapping.
40 * @since 2.1.0
41 */
42 protected IActionDelegate actionDelegate;
43
44 /**
45 * If the action delegate is associated with an editor, is also recorded here.
46 * This field was retained for backwards compatibility.
47 * @deprecated As of EMF 2.1.0, replaced by {@link #actionDelegate}.
48 */
49 @Deprecated
50 protected IEditorActionDelegate editorActionDelegate;
51
52 /**
53 * This is the current workbench part.
54 */
55 protected IWorkbenchPart workbenchPart;
56
57 /**
58 * If the workbench part is an editor, it is also recorded here.
59 * This field was retained for backwards compatibility.
60 * @deprecated As of EMF 2.1.0, replaced by {@link #workbenchPart}.
61 */
62 @Deprecated
63 protected IEditorPart editorPart;
64
65 /**
66 * This constructs an instance.
67 * @since 2.1.0
68 */
69 public DelegatingCommandAction(IActionDelegate actionDelegate)
70 {
71 this.actionDelegate = actionDelegate;
72 if (actionDelegate instanceof IEditorActionDelegate)
73 {
74 editorActionDelegate = (IEditorActionDelegate)actionDelegate;
75 }
76 }
77
78 /**
79 * This constructor is simply retained for binary compatibility.
80 * It just calls the {@link #DelegatingCommandAction(IActionDelegate) new form}.
81 */
82 public DelegatingCommandAction(IEditorActionDelegate editorActionDelegate)
83 {
84 this((IActionDelegate)editorActionDelegate);
85 }
86
87 public void selectionChanged(SelectionChangedEvent event)
88 {
89 handleSelection(event.getSelection());
90 }
91
92 public void selectionChanged(IWorkbenchPart part, ISelection selection)
93 {
94 handleSelection(selection);
95 }
96
97 protected void selectionChanged(ISelection selection)
98 {
99 if (actionDelegate != null)
100 {
101 // This is for backwards compatibility, since the constructor may have been overridden before it was expected
102 // to set actionDelegate.
103 //
104 editorActionDelegate.selectionChanged(this, selection);
105 }
106 else
107 {
108 actionDelegate.selectionChanged(this, selection);
109 }
110 }
111
112 protected void handleSelection(ISelection selection)
113 {
114 selectionChanged(selection);
115 }
116
117 /**
118 * @since 2.1.0
119 */
120 protected void registerSelectionListener(IWorkbenchPart workbenchPart)
121 {
122 ISelectionProvider selectionProvider = workbenchPart.getSite().getSelectionProvider();
123 if (selectionProvider != null)
124 {
125 selectionProvider.addSelectionChangedListener(this);
126 handleSelection(selectionProvider.getSelection());
127 }
128 }
129
130 /**
131 * @deprecated As of EMF 2.1.0, replaced by {@link #registerSelectionListener(IWorkbenchPart) registerSelectionListener}.
132 */
133 @Deprecated
134 protected void registerSelectionListener(IEditorPart editorPart)
135 {
136 registerSelectionListener((IWorkbenchPart)editorPart);
137 }
138
139 /**
140 * @since 2.1.0
141 */
142 protected void unregisterSelectionListener(IWorkbenchPart workbenchPart)
143 {
144 ISelectionProvider selectionProvider = workbenchPart.getSite().getSelectionProvider();
145 if (selectionProvider != null)
146 {
147 selectionProvider.removeSelectionChangedListener(this);
148 }
149 }
150
151 /**
152 * @deprecated As of EMF 2.1.0, replaced by {@link #unregisterSelectionListener(IWorkbenchPart) unregisterSelectionListener}.
153 */
154 @Deprecated
155 protected void unregisterSelectionListener(IEditorPart editorPart)
156 {
157 unregisterSelectionListener((IWorkbenchPart)editorPart);
158 }
159
160 /**
161 * @deprecated As of EMF 2.1.0, replaced by {@link #setActiveWorkbenchPart}.
162 */
163 @Deprecated
164 public void setActiveEditor(IEditorPart editorPart)
165 {
166 setActiveWorkbenchPart(editorPart);
167 this.editorPart = editorPart;
168 }
169
170 public void setActiveWorkbenchPart(IWorkbenchPart workbenchPart)
171 {
172 if (this.workbenchPart != workbenchPart)
173 {
174 if (this.workbenchPart != null)
175 {
176 unregisterSelectionListener(this.workbenchPart);
177 }
178 this.workbenchPart = workbenchPart;
179
180 if (actionDelegate == null)
181 {
182 // This is for backwards compatibility, since the constructor may have been overridden before it was expected
183 // to set actionDelegate.
184 //
185 editorActionDelegate.setActiveEditor(this, (IEditorPart)workbenchPart);
186 }
187 else if (actionDelegate instanceof IEditorActionDelegate)
188 {
189 ((IEditorActionDelegate)actionDelegate).setActiveEditor(this, (IEditorPart)workbenchPart);
190 }
191 else
192 {
193 ((IViewActionDelegate)actionDelegate).init((IViewPart)workbenchPart);
194 }
195
196 if (workbenchPart != null)
197 {
198 registerSelectionListener(workbenchPart);
199 }
200 }
201 }
202
203 @Override
204 public void run()
205 {
206 actionDelegate.run(this);
207 }
208 }