1 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 33 public class ActionDescriptor implements IPluginContribution { 34 private PluginAction action; 35 36 private String toolbarId; 37 38 private String menuPath; 39 40 private String id; 41 42 private String pluginId; 43 44 private String menuGroup; 45 46 private String toolbarGroupId; 47 48 51 public static final int T_POPUP = 0x1; 52 53 56 public static final int T_VIEW = 0x2; 57 58 61 public static final int T_WORKBENCH = 0x3; 62 63 66 public static final int T_EDITOR = 0x4; 67 68 71 public static final int T_WORKBENCH_PULLDOWN = 0x5; 72 73 76 public static final String STYLE_PUSH = "push"; 78 81 public static final String STYLE_RADIO = "radio"; 83 86 public static final String STYLE_TOGGLE = "toggle"; 88 91 public static final String STYLE_PULLDOWN = "pulldown"; 93 99 public ActionDescriptor(IConfigurationElement actionElement, int targetType) { 100 this(actionElement, targetType, null); 101 } 102 103 111 public ActionDescriptor(IConfigurationElement actionElement, 112 int targetType, Object target) { 113 id = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 115 pluginId = actionElement.getNamespace(); 116 String label = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 117 String tooltip = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLTIP); 118 String helpContextId = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_HELP_CONTEXT_ID); 119 String mpath = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_MENUBAR_PATH); 120 String tpath = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_TOOLBAR_PATH); 121 String style = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_STYLE); 122 String icon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 123 String hoverIcon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_HOVERICON); 124 String disabledIcon = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_DISABLEDICON); 125 String description = actionElement.getAttribute(IWorkbenchRegistryConstants.TAG_DESCRIPTION); 126 String accelerator = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_ACCELERATOR); 127 128 if (label == null) { 130 WorkbenchPlugin 131 .log("Invalid action declaration (label == null): " + id); label = WorkbenchMessages.ActionDescriptor_invalidLabel; 133 } 134 135 String mgroup = null; 137 String 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")) { tpath = ""; } 166 toolbarId = tpath; 167 toolbarGroupId = tgroup; 168 169 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 fullID = helpContextId; 179 if (helpContextId.indexOf(".") == -1) { fullID = actionElement.getNamespace() 183 + "." + helpContextId; } 185 PlatformUI.getWorkbench().getHelpSystem().setHelp(action, fullID); 186 } 187 if (description != null) { 188 action.setDescription(description); 189 } 190 191 if (style != null) { 192 String 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")); } 199 } 200 } else { 201 String state = actionElement.getAttribute(IWorkbenchRegistryConstants.ATT_STATE); 204 if (state != null) { 205 action.setChecked(state.equals("true")); } 207 } 208 209 String 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 236 private PluginAction createAction(int targetType, 237 IConfigurationElement actionElement, Object target, String 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); return null; 270 } 271 } 272 273 278 public PluginAction getAction() { 279 return action; 280 } 281 282 287 public String getId() { 288 return id; 289 } 290 291 297 public String getMenuGroup() { 298 return menuGroup; 299 } 300 301 307 public String getMenuPath() { 308 return menuPath; 309 } 310 311 317 public String getToolbarGroupId() { 318 return toolbarGroupId; 319 } 320 321 327 public String getToolbarId() { 328 return toolbarId; 329 } 330 331 334 public String toString() { 335 return "ActionDescriptor(" + id + ")"; } 337 338 343 private void processAccelerator(IAction action, String acceleratorText) { 344 345 if (acceleratorText.length() == 0) { 346 return; 347 } 348 349 if (Character.isDigit(acceleratorText.charAt(0))) { 351 try { 352 action.setAccelerator(Integer.valueOf(acceleratorText) 353 .intValue()); 354 } catch (NumberFormatException e) { 355 WorkbenchPlugin.log("Invalid accelerator declaration for action: " + id, e); } 357 } else { 358 action.setAccelerator(Action.convertAccelerator(acceleratorText)); 359 } 360 } 361 362 365 public String getLocalId() { 366 return getId(); 367 } 368 369 372 public String getPluginId() { 373 return pluginId; 374 } 375 } 376
| Popular Tags
|