Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/eclipse/ui/internal/ActionDescriptor.java


1   /*******************************************************************************
2    * Copyright (c) 2000, 2004 IBM Corporation and others.
3    * All rights reserved. This program and the accompanying materials 
4    * are made available under the terms of the Common Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/cpl-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.help.WorkbenchHelp;
22  import org.eclipse.ui.plugin.AbstractUIPlugin;
23  
24  /**
25   * When 'action' tag is found in the registry, an object of this
26   * class is created. It creates the appropriate action object
27   * and captures information that is later used to add this action
28   * object into menu/tool bar. This class is reused for
29   * global (workbench) menu/tool bar, popup menu actions,
30   * as well as view's pulldown and local tool bar.
31   */
32  public class ActionDescriptor implements IPluginContribution {
33    private PluginAction action;
34    private String toolbarId;
35    private String menuPath;
36    private String id;
37    private String pluginId;
38    private String menuGroup;
39    private String toolbarGroupId;
40    
41    public static final int T_POPUP=0x1;
42    public static final int T_VIEW=0x2;
43    public static final int T_WORKBENCH=0x3;
44    public static final int T_EDITOR=0x4;
45    public static final int T_WORKBENCH_PULLDOWN=0x5;
46    
47    public static final String ATT_ID = "id";//$NON-NLS-1$
48    public static final String ATT_DEFINITION_ID = "definitionId";//$NON-NLS-1$
49    public static final String ATT_HELP_CONTEXT_ID = "helpContextId";//$NON-NLS-1$
50    public static final String ATT_LABEL = "label";//$NON-NLS-1$
51    public static final String ATT_STYLE = "style";//$NON-NLS-1$
52    public static final String ATT_STATE = "state";//$NON-NLS-1$
53    public static final String ATT_DESCRIPTION = "description";//$NON-NLS-1$
54    public static final String ATT_TOOLTIP = "tooltip";//$NON-NLS-1$
55    public static final String ATT_MENUBAR_PATH = "menubarPath";//$NON-NLS-1$
56    public static final String ATT_TOOLBAR_PATH = "toolbarPath";//$NON-NLS-1$
57    public static final String ATT_ICON = "icon";//$NON-NLS-1$
58    public static final String ATT_HOVERICON = "hoverIcon";//$NON-NLS-1$
59    public static final String ATT_DISABLEDICON = "disabledIcon";//$NON-NLS-1$
60    public static final String ATT_CLASS = "class";//$NON-NLS-1$
61    public static final String ATT_RETARGET = "retarget";//$NON-NLS-1$
62    public static final String ATT_ALLOW_LABEL_UPDATE = "allowLabelUpdate";//$NON-NLS-1$
63    public static final String ATT_ACCELERATOR = "accelerator";//$NON-NLS-1$
64    
65    public static final String STYLE_PUSH = "push"; //$NON-NLS-1$
66    public static final String STYLE_RADIO = "radio"; //$NON-NLS-1$
67    public static final String STYLE_TOGGLE = "toggle"; //$NON-NLS-1$
68    public static final String STYLE_PULLDOWN = "pulldown"; //$NON-NLS-1$
69    
70  /**
71   * Creates a new descriptor with the specified target.
72   */
73  public ActionDescriptor(IConfigurationElement actionElement, int targetType) {
74    this(actionElement, targetType, null);
75  }
76  /**
77   * Creates a new descriptor with the target and destination workbench part
78   * it will go into.
79   */
80  public ActionDescriptor(IConfigurationElement actionElement, int targetType, Object target) {
81    // Load attributes.
82    id = actionElement.getAttribute(ATT_ID);
83    pluginId = actionElement.getDeclaringExtension().getNamespace();
84    String label = actionElement.getAttribute(ATT_LABEL);
85    String tooltip = actionElement.getAttribute(ATT_TOOLTIP);
86    String helpContextId = actionElement.getAttribute(ATT_HELP_CONTEXT_ID);
87    String mpath = actionElement.getAttribute(ATT_MENUBAR_PATH);
88    String tpath = actionElement.getAttribute(ATT_TOOLBAR_PATH);
89    String style = actionElement.getAttribute(ATT_STYLE);
90    String icon = actionElement.getAttribute(ATT_ICON);
91    String hoverIcon = actionElement.getAttribute(ATT_HOVERICON);
92    String disabledIcon = actionElement.getAttribute(ATT_DISABLEDICON);
93    String description = actionElement.getAttribute(ATT_DESCRIPTION);
94    String accelerator = actionElement.getAttribute(ATT_ACCELERATOR);
95  
96    // Verify input.
97    if (label == null) {
98      WorkbenchPlugin.log("Invalid action declaration (label == null): " + id); //$NON-NLS-1$
99      label = WorkbenchMessages.getString("ActionDescriptor.invalidLabel"); //$NON-NLS-1$
100   }
101 
102   // Calculate menu and toolbar paths.
103   String mgroup = null;
104   String tgroup = null;
105   if (mpath != null) {
106     int loc = mpath.lastIndexOf('/');
107     if (loc != -1) {
108       mgroup = mpath.substring(loc + 1);
109       mpath = mpath.substring(0, loc);
110     } else {
111       mgroup = mpath;
112       mpath = null;
113     }
114   }
115   if (targetType == T_POPUP && mgroup == null)
116     mgroup = IWorkbenchActionConstants.MB_ADDITIONS;
117   if (tpath != null) {
118     int loc = tpath.lastIndexOf('/');
119     if (loc != -1) {
120       tgroup = tpath.substring(loc + 1);
121       tpath = tpath.substring(0, loc);
122     } else {
123       tgroup = tpath;
124       tpath = null;
125     }
126   }
127   menuPath = mpath;
128   menuGroup = mgroup;
129   if ((tpath != null) && tpath.equals("Normal")) //$NON-NLS-1$
130     tpath = ""; //$NON-NLS-1$
131   toolbarId = tpath;
132   toolbarGroupId = tgroup;
133 
134   // Create action.
135   action = createAction(targetType, actionElement, target, style);
136   if (action.getText() == null) // may have been set by delegate
137     action.setText(label);
138   if (action.getToolTipText() == null && tooltip != null) // may have been set by delegate
139     action.setToolTipText(tooltip);
140   if (helpContextId != null) {
141     String fullID = helpContextId;
142     if (helpContextId.indexOf(".") == -1) //$NON-NLS-1$
143       // For backward compatibility we auto qualify the id if it is not
144       // qualified)
145       fullID = actionElement.getDeclaringExtension().getNamespace()
146             + "." + helpContextId;//$NON-NLS-1$
147     WorkbenchHelp.setHelp(action, fullID);
148   }
149   if (description != null)
150     action.setDescription(description);
151     
152   if (style != null) {
153     // Since 2.1, the "state" and "pulldown" attributes means something different
154     // when the new "style" attribute has been set. See doc for more info.
155     String state = actionElement.getAttribute(ATT_STATE);
156     if (state != null) {
157       if (style.equals(STYLE_RADIO) || style.equals(STYLE_TOGGLE))
158         action.setChecked(state.equals("true"));//$NON-NLS-1$
159     }
160   } else {
161     // Keep for backward compatibility for actions not using the
162     // new style attribute.
163     String state = actionElement.getAttribute(ATT_STATE);
164     if (state != null) {
165       action.setChecked(state.equals("true"));//$NON-NLS-1$
166     }
167   }
168   
169   String extendingPluginId = actionElement.getDeclaringExtension()
170         .getNamespace();
171   
172   if (icon != null) {
173     action.setImageDescriptor(
174       AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, icon));
175   }
176   if (hoverIcon != null) {
177     action.setHoverImageDescriptor(
178       AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, hoverIcon));
179   }
180   if (disabledIcon != null) {
181     action.setDisabledImageDescriptor(
182       AbstractUIPlugin.imageDescriptorFromPlugin(extendingPluginId, disabledIcon));
183   }
184   
185   if(accelerator != null)
186     processAccelerator(action,accelerator);
187 }
188 /**
189  * Creates an instance of PluginAction. Depending on the target part,
190  * subclasses of this class may be created.
191  */
192 private PluginAction createAction(int targetType, IConfigurationElement actionElement, Object target, String style) {
193   int actionStyle = IAction.AS_UNSPECIFIED;
194   if (style != null) {
195     if (style.equals(STYLE_RADIO)) {
196       actionStyle = IAction.AS_RADIO_BUTTON;
197     } else if (style.equals(STYLE_TOGGLE)) {
198       actionStyle = IAction.AS_CHECK_BOX;
199     } else if (style.equals(STYLE_PULLDOWN)) {
200       actionStyle = IAction.AS_DROP_DOWN_MENU;
201     } else if (style.equals(STYLE_PUSH)) {
202       actionStyle = IAction.AS_PUSH_BUTTON;
203     }
204   }
205   
206   switch (targetType) {
207     case T_VIEW:
208       return new ViewPluginAction(actionElement, (IViewPart)target, id, actionStyle);
209     case T_EDITOR:
210       return new EditorPluginAction(actionElement, (IEditorPart)target, id, actionStyle);
211     case T_WORKBENCH:
212       return new WWinPluginAction(actionElement, (IWorkbenchWindow)target, id, actionStyle);
213     case T_WORKBENCH_PULLDOWN:
214       actionStyle = IAction.AS_DROP_DOWN_MENU;
215       return new WWinPluginPulldown(actionElement, (IWorkbenchWindow)target, id, actionStyle);
216     case T_POPUP:
217       return new ObjectPluginAction(actionElement, id, actionStyle);
218     default:
219       WorkbenchPlugin.log("Unknown Action Type: " + targetType);//$NON-NLS-1$
220       return null;
221   }
222 }
223 /**
224  * Returns the action object held in this descriptor.
225  */
226 public PluginAction getAction() {
227   return action;
228 }
229 /**
230  * Returns action's id as defined in the registry.
231  */
232 public String getId() {
233   return id;
234 }
235 /**
236  * Returns named slot (group) in the menu where this action
237  * should be added.
238  */
239 public String getMenuGroup() {
240   return menuGroup;
241 }
242 /**
243  * Returns menu path where this action should be added. If null,
244  * the action will not be added into the menu.
245  */
246 
247 public String getMenuPath() {
248   return menuPath;
249 }
250 /**
251  * Returns the named slot (group) in the tool bar where this
252  * action should be added.
253  */
254 
255 public String getToolbarGroupId() {
256   return toolbarGroupId;
257 }
258 /**
259  * Returns id of the tool bar where this action should be added.
260  * If null, action will not be added to the tool bar.
261  */
262 public String getToolbarId() {
263   return toolbarId;
264 }
265 /**
266  * For debugging only.
267  */
268 public String toString() {
269   return "ActionDescriptor(" + id + ")";//$NON-NLS-2$//$NON-NLS-1$
270 }
271 
272 /**
273  * Process the accelerator definition. If it is a number
274  * then process the code directly - if not then parse it
275  * and create the code
276  */
277 private void processAccelerator(IAction action, String acceleratorText){
278   
279   if(acceleratorText.length() == 0)
280     return;
281   
282   //Is it a numeric definition?
283   if(Character.isDigit(acceleratorText.charAt(0))){
284     try{
285       action.setAccelerator(Integer.valueOf(acceleratorText).intValue());
286     }
287     catch (NumberFormatException exception){
288       WorkbenchPlugin.log("Invalid accelerator declaration: " + id); //$NON-NLS-1$
289     }
290   }
291   else
292     action.setAccelerator(Action.convertAccelerator(acceleratorText));
293 }
294 /* (non-Javadoc)
295  * @see org.eclipse.ui.IPluginContribution#getLocalId()
296  */
297 public String getLocalId() {
298     return getId();
299 }
300 /* (non-Javadoc)
301  * @see org.eclipse.ui.IPluginContribution#getPluginId()
302  */
303 public String getPluginId() { 
304     return pluginId;
305 }
306 }