KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > RetargetAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.actions;
12
13 import org.eclipse.core.commands.IHandlerAttributes;
14 import org.eclipse.jface.action.IAction;
15 import org.eclipse.jface.util.IPropertyChangeListener;
16 import org.eclipse.jface.util.PropertyChangeEvent;
17 import org.eclipse.swt.events.HelpEvent;
18 import org.eclipse.swt.events.HelpListener;
19 import org.eclipse.swt.widgets.Event;
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.IWorkbenchPartSite;
23 import org.eclipse.ui.SubActionBars;
24 import org.eclipse.ui.internal.PartSite;
25
26 /**
27  * A <code>RetargetAction</code> tracks the active part in the workbench.
28  * Each RetargetAction has an ID. If the active part provides an action
29  * handler for the ID the enable and check state of the RetargetAction
30  * is determined from the enable and check state of the handler. If the
31  * active part does not provide an action handler then this action is
32  * disabled.
33  * </p>
34  * <p>
35  * <b>Note:</b> instances of this class add themselves as listeners to their
36  * action handler. It is important for the creator of a retarget action to call
37  * dispose when the action is no longer needed. This will ensure that the
38  * listener is removed.
39  * </p>
40  * <p>
41  * This class may be instantiated. It is not intented to be subclassed.
42  * </p>
43  *
44  * @since 2.0
45  */

46 public class RetargetAction extends PartEventAction implements
47         ActionFactory.IWorkbenchAction {
48
49     /**
50      * The help listener assigned to this action, or <code>null</code> if none.
51      */

52     private HelpListener localHelpListener;
53
54     private boolean enableAccelerator = true;
55
56     private IAction handler;
57
58     private IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener() {
59         public void propertyChange(PropertyChangeEvent event) {
60             RetargetAction.this.propagateChange(event);
61         }
62     };
63
64     /**
65      * Constructs a RetargetAction with the given action id and text.
66      *
67      * @param actionID the retargetable action id
68      * @param text the action's text, or <code>null</code> if there is no text
69      */

70     public RetargetAction(String JavaDoc actionID, String JavaDoc text) {
71         this(actionID, text, IAction.AS_UNSPECIFIED);
72     }
73
74     /**
75      * Constructs a RetargetAction with the given action id, text and style.
76      *
77      * @param actionID the retargetable action id
78      * @param text the action's text, or <code>null</code> if there is no text
79      * @param style one of <code>AS_PUSH_BUTTON</code>, <code>AS_CHECK_BOX</code>,
80      * <code>AS_DROP_DOWN_MENU</code>, <code>AS_RADIO_BUTTON</code>, and
81      * <code>AS_UNSPECIFIED</code>
82      * @since 3.0
83      */

84     public RetargetAction(String JavaDoc actionID, String JavaDoc text, int style) {
85         super(text, style);
86         setId(actionID);
87         setEnabled(false);
88         super.setHelpListener(new HelpListener() {
89             public void helpRequested(HelpEvent e) {
90                 HelpListener listener = null;
91                 if (handler != null) {
92                     // if we have a handler, see if it has a help listener
93
listener = handler.getHelpListener();
94                     if (listener == null) {
95                         // use our own help listener
96
listener = localHelpListener;
97                     }
98                 }
99                 if (listener != null) {
100                     // pass on the event
101
listener.helpRequested(e);
102                 }
103             }
104         });
105     }
106
107     /**
108      * Disposes of the action and any resources held.
109      */

110     public void dispose() {
111         if (handler != null) {
112             handler.removePropertyChangeListener(propertyChangeListener);
113             handler = null;
114         }
115         IWorkbenchPart part = getActivePart();
116         if (part != null) {
117             IWorkbenchPartSite site = part.getSite();
118             SubActionBars bars = (SubActionBars) ((PartSite) site).getActionBars();
119             bars.removePropertyChangeListener(propertyChangeListener);
120         }
121     }
122
123     /**
124      * Enables the accelerator for this action.
125      *
126      * @param b the new enable state
127      */

128     public void enableAccelerator(boolean b) {
129         enableAccelerator = b;
130     }
131
132     /* (non-Javadoc)
133      * Retaget actions do not have accelerators. It is up to the
134      * part to hook the accelerator.
135      */

136     public int getAccelerator() {
137         if (enableAccelerator) {
138             return super.getAccelerator();
139         }
140         return 0;
141     }
142
143     /**
144      * A workbench part has been activated. Try to connect
145      * to it.
146      *
147      * @param part the workbench part that has been activated
148      */

149     public void partActivated(IWorkbenchPart part) {
150         super.partActivated(part);
151         IWorkbenchPartSite site = part.getSite();
152         SubActionBars bars = (SubActionBars) ((PartSite) site).getActionBars();
153         bars.addPropertyChangeListener(propertyChangeListener);
154         setActionHandler(bars.getGlobalActionHandler(getId()));
155     }
156
157     /**
158      * A workbench part has been closed.
159      *
160      * @param part the workbench part that has been closed
161      */

162     public void partClosed(IWorkbenchPart part) {
163         IWorkbenchPart activePart = part.getSite().getPage().getActivePart();
164         if (activePart != null) {
165             // We are going to get a part activated message so don't bother setting the
166
// action handler to null. This prevents enablement flash in the toolbar
167
return;
168         }
169         if (part == getActivePart()) {
170             setActionHandler(null);
171         }
172         super.partClosed(part);
173     }
174
175     /**
176      * A workbench part has been deactivated. Disconnect from it.
177      *
178      * @param part the workbench part that has been deactivated
179      */

180     public void partDeactivated(IWorkbenchPart part) {
181         super.partDeactivated(part);
182         IWorkbenchPartSite site = part.getSite();
183         SubActionBars bars = (SubActionBars) ((PartSite) site).getActionBars();
184         bars.removePropertyChangeListener(propertyChangeListener);
185
186         IWorkbenchPart activePart = part.getSite().getPage().getActivePart();
187         if (activePart != null) {
188             // We are going to get a part activated message so don't bother setting the
189
// action handler to null. This prevents enablement flash in the toolbar
190
return;
191         }
192
193         setActionHandler(null);
194     }
195
196     /**
197      * Either the action handler itself has changed, or the configured action
198      * handlers on the action bars have changed. Update self.
199      */

200     protected void propagateChange(PropertyChangeEvent event) {
201         if (event.getProperty().equals(IAction.ENABLED)) {
202             Boolean JavaDoc bool = (Boolean JavaDoc) event.getNewValue();
203             setEnabled(bool.booleanValue());
204         } else if (event.getProperty().equals(IAction.CHECKED)) {
205             Boolean JavaDoc bool = (Boolean JavaDoc) event.getNewValue();
206             setChecked(bool.booleanValue());
207         } else if (event.getProperty().equals(SubActionBars.P_ACTION_HANDLERS)) {
208             if (event.getSource() instanceof IActionBars) {
209                 IActionBars bars = (IActionBars) event.getSource();
210                 setActionHandler(bars.getGlobalActionHandler(getId()));
211             }
212         }
213     }
214
215     /**
216      * Invoked when an action occurs.
217      */

218     public void run() {
219         if (handler != null) {
220             handler.run();
221         }
222     }
223
224     /**
225      * Invoked when an action occurs.
226      */

227     public void runWithEvent(Event event) {
228         if (handler != null) {
229             handler.runWithEvent(event);
230         }
231     }
232
233     /**
234      * Returns the action handler. This method was made public in 3.0.
235      *
236      * @return The current action handling this retargettable action. This
237      * handler will be <code>null</code> if there is no current
238      * handler.
239      */

240     public IAction getActionHandler() {
241         return handler;
242     }
243     
244     public final boolean isHandled() {
245         return (handler != null);
246     }
247
248     /**
249      * Sets the action handler.
250      */

251     protected void setActionHandler(IAction newHandler) {
252         // Optimize.
253
if (newHandler == handler) {
254             return;
255         }
256
257         // Clear old action.
258
if (handler != null) {
259             handler.removePropertyChangeListener(propertyChangeListener);
260             handler = null;
261         }
262
263         // Set new action.
264
IAction oldHandler = handler;
265         handler = newHandler;
266         if (handler == null) {
267             setEnabled(false);
268             if (getStyle() == AS_CHECK_BOX || getStyle() == AS_RADIO_BUTTON) {
269                 setChecked(false);
270             }
271         } else {
272             setEnabled(handler.isEnabled());
273             if (getStyle() == AS_CHECK_BOX || getStyle() == AS_RADIO_BUTTON) {
274                 setChecked(handler.isChecked());
275             }
276             handler.addPropertyChangeListener(propertyChangeListener);
277         }
278         
279         // Notify listeners that the handler has changed.
280
firePropertyChange(IHandlerAttributes.ATTRIBUTE_HANDLED, oldHandler,
281                 newHandler);
282     }
283
284     /* (non-Javadoc)
285      * Method declared on IAction.
286      */

287     public void setChecked(boolean checked) {
288         super.setChecked(checked);
289         // This call may come from the SWT control event handler
290
// itself, so notify the handler action to keep things
291
// in sync.
292
if (handler != null) {
293             handler.setChecked(checked);
294         }
295     }
296
297     /**
298      * The <code>RetargetAction</code> implementation of this method declared on
299      * <code>IAction</code> stores the help listener in a local field. The
300      * supplied listener is only used if there is no hanlder.
301      */

302     public void setHelpListener(HelpListener listener) {
303         localHelpListener = listener;
304     }
305
306     /**
307      * Returns a string representation of this action.
308      *
309      * @return A string representation of this action.
310      *
311      * @since 3.2
312      */

313     public final String JavaDoc toString() {
314         final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
315
316         buffer.append("RetargetAction("); //$NON-NLS-1$
317
buffer.append(getId());
318         buffer.append(')');
319
320         return buffer.toString();
321     }
322 }
323
Popular Tags