KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WWinPluginAction


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.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.runtime.IConfigurationElement;
17 import org.eclipse.jface.action.IAction;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.swt.events.HelpEvent;
22 import org.eclipse.swt.events.HelpListener;
23 import org.eclipse.swt.widgets.Event;
24 import org.eclipse.ui.IActionDelegate;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.IWorkbenchWindow;
27 import org.eclipse.ui.IWorkbenchWindowActionDelegate;
28 import org.eclipse.ui.WorkbenchException;
29 import org.eclipse.ui.actions.LabelRetargetAction;
30 import org.eclipse.ui.actions.RetargetAction;
31 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
32
33 /**
34  * This class extends regular plugin action with the additional requirement that
35  * the delegate has to implement interface
36  * {@link org.eclipse.ui.IWorkbenchWindowActionDelegate}. This interface has
37  * one additional method (init) whose purpose is to initialize the delegate with
38  * the window in which the action is intended to run.
39  */

40 public class WWinPluginAction extends PluginAction implements
41         IActionSetContributionItem {
42     /**
43      * The help listener assigned to this action, or <code>null</code> if none.
44      */

45     private HelpListener localHelpListener;
46
47     private IWorkbenchWindow window;
48
49     private String JavaDoc actionSetId;
50
51     private RetargetAction retargetAction;
52
53     private static ArrayList JavaDoc staticActionList = new ArrayList JavaDoc(50);
54
55     /**
56      * Constructs a new <code>WWinPluginAction</code> object.
57      *
58      * @param actionElement the configuration element
59      * @param window the window to contribute to
60      * @param id the identifier
61      * @param style the style
62      */

63     public WWinPluginAction(IConfigurationElement actionElement,
64             IWorkbenchWindow window, String JavaDoc id, int style) {
65         super(actionElement, id, style);
66         this.window = window;
67
68         // If config specifies a retarget action, create it now
69
String JavaDoc retarget = actionElement
70                 .getAttribute(IWorkbenchRegistryConstants.ATT_RETARGET);
71         if (retarget != null && Boolean.valueOf(retarget).booleanValue()) {
72             // create a retarget action
73
String JavaDoc allowLabelUpdate = actionElement
74                     .getAttribute(IWorkbenchRegistryConstants.ATT_ALLOW_LABEL_UPDATE);
75             String JavaDoc label = actionElement
76                     .getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
77
78             if (allowLabelUpdate != null && Boolean.valueOf(allowLabelUpdate).booleanValue()) {
79                 retargetAction = new LabelRetargetAction(id, label, style);
80             } else {
81                 retargetAction = new RetargetAction(id, label, style);
82             }
83             retargetAction
84                     .addPropertyChangeListener(new IPropertyChangeListener() {
85                         public void propertyChange(PropertyChangeEvent event) {
86                             if (event.getProperty().equals(IAction.ENABLED)) {
87                                 Object JavaDoc val = event.getNewValue();
88                                 if (val instanceof Boolean JavaDoc) {
89                                     setEnabled(((Boolean JavaDoc) val).booleanValue());
90                                 }
91                             } else if (event.getProperty().equals(
92                                     IAction.CHECKED)) {
93                                 Object JavaDoc val = event.getNewValue();
94                                 if (val instanceof Boolean JavaDoc) {
95                                     setChecked(((Boolean JavaDoc) val).booleanValue());
96                                 }
97                             } else if (event.getProperty().equals(IAction.TEXT)) {
98                                 Object JavaDoc val = event.getNewValue();
99                                 if (val instanceof String JavaDoc) {
100                                     setText((String JavaDoc) val);
101                                 }
102                             } else if (event.getProperty().equals(
103                                     IAction.TOOL_TIP_TEXT)) {
104                                 Object JavaDoc val = event.getNewValue();
105                                 if (val instanceof String JavaDoc) {
106                                     setToolTipText((String JavaDoc) val);
107                                 }
108                             }
109                         }
110                     });
111             retargetAction.setEnabled(false);
112             setEnabled(false);
113             window.getPartService().addPartListener(retargetAction);
114             IWorkbenchPart activePart = window.getPartService().getActivePart();
115             if (activePart != null) {
116                 retargetAction.partActivated(activePart);
117             }
118         } else {
119             // if we retarget the handler will look after selection changes
120
window.getSelectionService().addSelectionListener(this);
121             refreshSelection();
122         }
123         addToActionList(this);
124
125         super.setHelpListener(new HelpListener() {
126             public void helpRequested(HelpEvent e) {
127                 HelpListener listener = null;
128                 if (retargetAction != null) {
129                     listener = retargetAction.getHelpListener();
130                 }
131                 if (listener == null) {
132                     // use our own help listener
133
listener = localHelpListener;
134                 }
135                 if (listener != null) {
136                     // pass on the event
137
listener.helpRequested(e);
138                 }
139             }
140         });
141     }
142
143     /**
144      * Adds an item to the action list.
145      */

146     private static void addToActionList(WWinPluginAction action) {
147         staticActionList.add(action);
148     }
149
150     /**
151      * Removes an item from the action list.
152      */

153     private static void removeFromActionList(WWinPluginAction action) {
154         staticActionList.remove(action);
155     }
156
157     /**
158      * Creates any actions which belong to an activated plugin.
159      */

160     public static void refreshActionList() {
161         Iterator JavaDoc iter = staticActionList.iterator();
162         while (iter.hasNext()) {
163             WWinPluginAction action = (WWinPluginAction) iter.next();
164             if ((action.getDelegate() == null) && action.isOkToCreateDelegate()) {
165                 action.createDelegate();
166                 // creating the delegate also refreshes its enablement
167
}
168         }
169     }
170
171     /* (non-Javadoc)
172      * Method declared on PluginAction.
173      */

174     protected IActionDelegate validateDelegate(Object JavaDoc obj)
175             throws WorkbenchException {
176         if (obj instanceof IWorkbenchWindowActionDelegate) {
177             return (IWorkbenchWindowActionDelegate) obj;
178         }
179         
180         throw new WorkbenchException(
181                 "Action must implement IWorkbenchWindowActionDelegate"); //$NON-NLS-1$
182
}
183
184     /* (non-Javadoc)
185      * Method declared on PluginAction.
186      */

187     protected void initDelegate() {
188         super.initDelegate();
189         ((IWorkbenchWindowActionDelegate) getDelegate()).init(window);
190     }
191
192     /**
193      * Disposes of the action and any resources held.
194      */

195     public void dispose() {
196         removeFromActionList(this);
197         if (retargetAction != null) {
198             window.getPartService().removePartListener(retargetAction);
199             retargetAction.dispose();
200             retargetAction = null;
201         }
202         window.getSelectionService().removeSelectionListener(this);
203         super.dispose();
204     }
205
206     /**
207      * Returns the action set id.
208      */

209     public String JavaDoc getActionSetId() {
210         return actionSetId;
211     }
212
213     /**
214      * Returns true if the window has been set.
215      * The window may be null after the constructor is called and
216      * before the window is stored. We cannot create the delegate
217      * at that time.
218      */

219     public boolean isOkToCreateDelegate() {
220         return super.isOkToCreateDelegate() && window != null
221                 && retargetAction == null;
222     }
223
224     /* (non-Javadoc)
225      * Method declared on IActionDelegate2.
226      */

227     public void runWithEvent(Event event) {
228         if (retargetAction == null) {
229             super.runWithEvent(event);
230             return;
231         }
232
233         if (event != null) {
234             retargetAction.runWithEvent(event);
235         } else {
236             retargetAction.run();
237         }
238     }
239
240     /**
241      * Sets the action set id.
242      */

243     public void setActionSetId(String JavaDoc newActionSetId) {
244         actionSetId = newActionSetId;
245     }
246
247     /**
248      * The <code>WWinPluginAction</code> implementation of this method
249      * declared on <code>IAction</code> stores the help listener in
250      * a local field. The supplied listener is only used if there is
251      * no retarget action.
252      */

253     public void setHelpListener(HelpListener listener) {
254         localHelpListener = listener;
255     }
256
257     /* (non-Javadoc)
258      * Method declared on IAction.
259      */

260     public void setChecked(boolean checked) {
261         super.setChecked(checked);
262         // This call may come from the SWT control event handler
263
// itself, so notify the retarget action to keep things
264
// in sync.
265
if (retargetAction != null) {
266             retargetAction.setChecked(checked);
267         }
268     }
269
270     /**
271      * Refresh the selection for the action.
272      */

273     protected void refreshSelection() {
274         ISelection selection = window.getSelectionService().getSelection();
275         selectionChanged(selection);
276     }
277 }
278
Popular Tags