KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.IConfigurationElement;
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.IPluginContribution;
18 import org.eclipse.ui.IViewPart;
19 import org.eclipse.ui.IWorkbenchActionConstants;
20 import org.eclipse.ui.IWorkbenchWindow;
21 import org.eclipse.ui.PlatformUI;
22 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
23 import org.eclipse.ui.plugin.AbstractUIPlugin;
24
25 /**
26  * When 'action' tag is found in the registry, an object of this
27  * class is created. It creates the appropriate action object
28  * and captures information that is later used to add this action
29  * object into menu/tool bar. This class is reused for
30  * global (workbench) menu/tool bar, popup menu actions,
31  * as well as view's pulldown and local tool bar.
32  */

33 public class ActionDescriptor implements IPluginContribution {
34     private PluginAction action;
35
36     private String JavaDoc toolbarId;
37
38     private String JavaDoc menuPath;
39
40     private String JavaDoc id;
41
42     private String JavaDoc pluginId;
43
44     private String JavaDoc menuGroup;
45
46     private String JavaDoc toolbarGroupId;
47
48     /**
49      * Popup constant. Value <code>0x1</code>.
50      */

51     public static final int T_POPUP = 0x1;
52
53     /**
54      * View constant. Value <code>0x2</code>.
55      */

56     public static final int T_VIEW = 0x2;
57
58     /**
59      * Workbench constant. Value <code>0x3</code>.
60      */

61     public static final int T_WORKBENCH = 0x3;
62
63     /**
64      * Editor constant. Value <code>0x4</code>.
65      */

66     public static final int T_EDITOR = 0x4;
67
68     /**
69      * Workbench pulldown constant. Value <code>0x5</code>.
70      */

71     public static final int T_WORKBENCH_PULLDOWN = 0x5;
72
73     /**
74      * Push style constant. Value <code>push</code>.
75      */

76     public static final String JavaDoc STYLE_PUSH = "push"; //$NON-NLS-1$
77

78     /**
79      * Radio style constant. Value <code>radio</code>.
80      */

81     public static final String JavaDoc STYLE_RADIO = "radio"; //$NON-NLS-1$
82

83     /***
84      * Toggle style constant. Value <code>toggle</code>.
85      */

86     public static final String JavaDoc STYLE_TOGGLE = "toggle"; //$NON-NLS-1$
87

88     /**
89      * Pulldown style constant. Value <code>pulldown</code>.
90      */

91     public static final String JavaDoc STYLE_PULLDOWN = "pulldown"; //$NON-NLS-1$
92

93     /**
94      * Creates a new descriptor with the specified target.
95      *
96      * @param actionElement the configuration element
97      * @param targetType the type of action
98      */

99     public ActionDescriptor(IConfigurationElement actionElement, int targetType) {
100         this(actionElement, targetType, null);
101     }
102
103     /**
104      * Creates a new descriptor with the target and destination workbench part
105      * it will go into.
106      *
107      * @param actionElement the configuration element
108      * @param targetType the type of action
109      * @param target the target object
110      */

111     public ActionDescriptor(IConfigurationElement actionElement,
112             int targetType, Object JavaDoc target) {
113         // Load attributes.
114
id = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID);
115         pluginId = actionElement.getNamespace();
116         String JavaDoc label = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL);
117         String JavaDoc tooltip = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLTIP);
118         String JavaDoc helpContextId = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_CONTEXT_ID);
119         String JavaDoc mpath = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_MENUBAR_PATH);
120         String JavaDoc tpath = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLBAR_PATH);
121         String JavaDoc style = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_STYLE);
122         String JavaDoc icon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
123         String JavaDoc hoverIcon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_HOVERICON);
124         String JavaDoc disabledIcon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_DISABLEDICON);
125         String JavaDoc description = actionElement.getAttribute(IWorkbenchRegistryConstants.TAG_DESCRIPTION);
126         String JavaDoc accelerator = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ACCELERATOR);
127
128         // Verify input.
129
if (label == null) {
130             WorkbenchPlugin
131                     .log("Invalid action declaration (label == null): " + id); //$NON-NLS-1$
132
label = WorkbenchMessages.ActionDescriptor_invalidLabel;
133         }
134
135         // Calculate menu and toolbar paths.
136
String JavaDoc mgroup = null;
137         String JavaDoc tgroup = null;
138         if (mpath != null) {
139             int loc = mpath.lastIndexOf('/');
140             if (loc != -1) {
141                 mgroup = mpath.substring(loc + 1);
142                 mpath = mpath.substring(0, loc);
143             } else {
144                 mgroup = mpath;
145                 mpath = null;
146             }
147         }
148         if (targetType == T_POPUP && mgroup == null) {
149             mgroup = IWorkbenchActionConstants.MB_ADDITIONS;
150         }
151         if (tpath != null) {
152             int loc = tpath.lastIndexOf('/');
153             if (loc != -1) {
154                 tgroup = tpath.substring(loc + 1);
155                 tpath = tpath.substring(0, loc);
156             } else {
157                 tgroup = tpath;
158                 tpath = null;
159             }
160         }
161         menuPath = mpath;
162         menuGroup = mgroup;
163         if ((tpath != null) && tpath.equals("Normal")) { //$NON-NLS-1$
164
tpath = ""; //$NON-NLS-1$
165
}
166         toolbarId = tpath;
167         toolbarGroupId = tgroup;
168
169         // Create action.
170
action = createAction(targetType, actionElement, target, style);
171         if (action.getText() == null) {
172             action.setText(label);
173         }
174         if (action.getToolTipText() == null && tooltip != null) {
175             action.setToolTipText(tooltip);
176         }
177         if (helpContextId != null) {
178             String JavaDoc fullID = helpContextId;
179             if (helpContextId.indexOf(".") == -1) { //$NON-NLS-1$
180
// For backward compatibility we auto qualify the id if it is not
181
// qualified)
182
fullID = actionElement.getNamespace()
183                         + "." + helpContextId;//$NON-NLS-1$
184
}
185             PlatformUI.getWorkbench().getHelpSystem().setHelp(action, fullID);
186         }
187         if (description != null) {
188             action.setDescription(description);
189         }
190
191         if (style != null) {
192             // Since 2.1, the "state" and "pulldown" attributes means something different
193
// when the new "style" attribute has been set. See doc for more info.
194
String JavaDoc state = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_STATE);
195             if (state != null) {
196                 if (style.equals(STYLE_RADIO) || style.equals(STYLE_TOGGLE)) {
197                     action.setChecked(state.equals("true"));//$NON-NLS-1$
198
}
199             }
200         } else {
201             // Keep for backward compatibility for actions not using the
202
// new style attribute.
203
String JavaDoc state = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_STATE);
204             if (state != null) {
205                 action.setChecked(state.equals("true"));//$NON-NLS-1$
206
}
207         }
208
209         String JavaDoc extendingPluginId = actionElement.getDeclaringExtension()
210                 .getNamespace();
211
212         if (icon != null) {
213             action.setImageDescriptor(AbstractUIPlugin
214                     .imageDescriptorFromPlugin(extendingPluginId, icon));
215         }
216         if (hoverIcon != null) {
217             action.setHoverImageDescriptor(AbstractUIPlugin
218                     .imageDescriptorFromPlugin(extendingPluginId, hoverIcon));
219         }
220         if (disabledIcon != null) {
221             action
222                     .setDisabledImageDescriptor(AbstractUIPlugin
223                             .imageDescriptorFromPlugin(extendingPluginId,
224                                     disabledIcon));
225         }
226
227         if (accelerator != null) {
228             processAccelerator(action, accelerator);
229         }
230     }
231
232     /**
233      * Creates an instance of PluginAction. Depending on the target part,
234      * subclasses of this class may be created.
235      */

236     private PluginAction createAction(int targetType,
237             IConfigurationElement actionElement, Object JavaDoc target, String JavaDoc style) {
238         int actionStyle = IAction.AS_UNSPECIFIED;
239         if (style != null) {
240             if (style.equals(STYLE_RADIO)) {
241                 actionStyle = IAction.AS_RADIO_BUTTON;
242             } else if (style.equals(STYLE_TOGGLE)) {
243                 actionStyle = IAction.AS_CHECK_BOX;
244             } else if (style.equals(STYLE_PULLDOWN)) {
245                 actionStyle = IAction.AS_DROP_DOWN_MENU;
246             } else if (style.equals(STYLE_PUSH)) {
247                 actionStyle = IAction.AS_PUSH_BUTTON;
248             }
249         }
250
251         switch (targetType) {
252         case T_VIEW:
253             return new ViewPluginAction(actionElement, (IViewPart) target, id,
254                     actionStyle);
255         case T_EDITOR:
256             return new EditorPluginAction(actionElement, (IEditorPart) target,
257                     id, actionStyle);
258         case T_WORKBENCH:
259             return new WWinPluginAction(actionElement,
260                     (IWorkbenchWindow) target, id, actionStyle);
261         case T_WORKBENCH_PULLDOWN:
262             actionStyle = IAction.AS_DROP_DOWN_MENU;
263             return new WWinPluginPulldown(actionElement,
264                     (IWorkbenchWindow) target, id, actionStyle);
265         case T_POPUP:
266             return new ObjectPluginAction(actionElement, id, actionStyle);
267         default:
268             WorkbenchPlugin.log("Unknown Action Type: " + targetType);//$NON-NLS-1$
269
return null;
270         }
271     }
272
273     /**
274      * Returns the action object held in this descriptor.
275      *
276      * @return the action
277      */

278     public PluginAction getAction() {
279         return action;
280     }
281
282     /**
283      * Returns action's id as defined in the registry.
284      *
285      * @return the id
286      */

287     public String JavaDoc getId() {
288         return id;
289     }
290
291     /**
292      * Returns named slot (group) in the menu where this action
293      * should be added.
294      *
295      * @return the menu group
296      */

297     public String JavaDoc getMenuGroup() {
298         return menuGroup;
299     }
300
301     /**
302      * Returns menu path where this action should be added. If null,
303      * the action will not be added into the menu.
304      *
305      * @return the menubar path
306      */

307     public String JavaDoc getMenuPath() {
308         return menuPath;
309     }
310
311     /**
312      * Returns the named slot (group) in the tool bar where this
313      * action should be added.
314      *
315      * @return the toolbar group id
316      */

317     public String JavaDoc getToolbarGroupId() {
318         return toolbarGroupId;
319     }
320
321     /**
322      * Returns id of the tool bar where this action should be added.
323      * If null, action will not be added to the tool bar.
324      *
325      * @return the toolbar id
326      */

327     public String JavaDoc getToolbarId() {
328         return toolbarId;
329     }
330
331     /**
332      * For debugging only.
333      */

334     public String JavaDoc toString() {
335         return "ActionDescriptor(" + id + ")";//$NON-NLS-2$//$NON-NLS-1$
336
}
337
338     /**
339      * Process the accelerator definition. If it is a number
340      * then process the code directly - if not then parse it
341      * and create the code
342      */

343     private void processAccelerator(IAction action, String JavaDoc acceleratorText) {
344
345         if (acceleratorText.length() == 0) {
346             return;
347         }
348
349         //Is it a numeric definition?
350
if (Character.isDigit(acceleratorText.charAt(0))) {
351             try {
352                 action.setAccelerator(Integer.valueOf(acceleratorText)
353                         .intValue());
354             } catch (NumberFormatException JavaDoc e) {
355                 WorkbenchPlugin.log("Invalid accelerator declaration for action: " + id, e); //$NON-NLS-1$
356
}
357         } else {
358             action.setAccelerator(Action.convertAccelerator(acceleratorText));
359         }
360     }
361
362     /* (non-Javadoc)
363      * @see org.eclipse.ui.IPluginContribution#getLocalId()
364      */

365     public String JavaDoc getLocalId() {
366         return getId();
367     }
368
369     /* (non-Javadoc)
370      * @see org.eclipse.ui.IPluginContribution#getPluginId()
371      */

372     public String JavaDoc getPluginId() {
373         return pluginId;
374     }
375 }
376
Free Books   Free Magazines  
Popular Tags