1 11 package org.eclipse.ui.internal; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.IConfigurationElement; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.jface.action.AbstractGroupMarker; 18 import org.eclipse.jface.action.GroupMarker; 19 import org.eclipse.jface.action.IContributionItem; 20 import org.eclipse.jface.action.IContributionManager; 21 import org.eclipse.jface.action.IMenuManager; 22 import org.eclipse.jface.action.IToolBarManager; 23 import org.eclipse.jface.action.MenuManager; 24 import org.eclipse.jface.action.Separator; 25 import org.eclipse.ui.IWorkbenchActionConstants; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 28 import org.eclipse.ui.internal.registry.RegistryReader; 29 30 34 public abstract class PluginActionBuilder extends RegistryReader { 35 protected String targetID; 36 37 protected String targetContributionTag; 38 39 protected BasicContribution currentContribution; 40 41 protected ArrayList cache; 42 43 46 public PluginActionBuilder() { 47 } 48 49 57 public final void contribute(IMenuManager menu, IToolBarManager toolbar, 58 boolean appendIfMissing) { 59 if (cache == null) { 60 return; 61 } 62 63 for (int i = 0; i < cache.size(); i++) { 64 BasicContribution contribution = (BasicContribution) cache.get(i); 65 contribution.contribute(menu, appendIfMissing, toolbar, 66 appendIfMissing); 67 } 68 } 69 70 74 protected abstract ActionDescriptor createActionDescriptor( 75 IConfigurationElement element); 76 77 81 protected BasicContribution createContribution() { 82 return new BasicContribution(); 83 } 84 85 89 protected String getTargetID(IConfigurationElement element) { 90 String value = element.getAttribute(IWorkbenchRegistryConstants.ATT_TARGET_ID); 91 return value != null ? value : "???"; } 93 94 97 protected String getID(IConfigurationElement element) { 98 String value = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 99 return value != null ? value : "???"; } 101 102 106 protected void readContributions(String id, String tag, 107 String extensionPoint) { 108 cache = null; 109 currentContribution = null; 110 targetID = id; 111 targetContributionTag = tag; 112 readRegistry(Platform.getExtensionRegistry(), PlatformUI.PLUGIN_ID, 113 extensionPoint); 114 } 115 116 120 protected boolean readElement(IConfigurationElement element) { 121 String tag = element.getName(); 122 123 if (tag.equals(IWorkbenchRegistryConstants.TAG_OBJECT_CONTRIBUTION)) { 126 return true; 127 } 128 129 if (tag.equals(targetContributionTag)) { 131 if (targetID != null) { 132 String id = getTargetID(element); 134 if (id == null || !id.equals(targetID)) { 135 return true; 136 } 137 } 138 139 currentContribution = createContribution(); 141 readElementChildren(element); 142 if (cache == null) { 143 cache = new ArrayList (4); 144 } 145 cache.add(currentContribution); 146 currentContribution = null; 147 return true; 148 } 149 150 if (tag.equals(IWorkbenchRegistryConstants.TAG_MENU)) { 152 currentContribution.addMenu(element); 153 return true; 154 } 155 156 if (tag.equals(IWorkbenchRegistryConstants.TAG_ACTION)) { 158 currentContribution.addAction(createActionDescriptor(element)); 159 return true; 160 } 161 162 return false; 163 } 164 165 169 protected static class BasicContribution { 170 protected ArrayList menus; 171 172 protected ArrayList actions; 173 174 179 public void addMenu(IConfigurationElement element) { 180 if (menus == null) { 181 menus = new ArrayList (1); 182 } 183 menus.add(element); 184 } 185 186 191 public void addAction(ActionDescriptor desc) { 192 if (actions == null) { 193 actions = new ArrayList (3); 194 } 195 actions.add(desc); 196 } 197 198 208 public void contribute(IMenuManager menu, boolean menuAppendIfMissing, 209 IToolBarManager toolbar, boolean toolAppendIfMissing) { 210 if (menus != null && menu != null) { 211 for (int i = 0; i < menus.size(); i++) { 212 IConfigurationElement menuElement = (IConfigurationElement) menus 213 .get(i); 214 contributeMenu(menuElement, menu, menuAppendIfMissing); 215 } 216 } 217 218 if (actions != null) { 219 for (int i = 0; i < actions.size(); i++) { 220 ActionDescriptor ad = (ActionDescriptor) actions.get(i); 221 if (menu != null) { 222 contributeMenuAction(ad, menu, menuAppendIfMissing); 223 } 224 if (toolbar != null) { 225 contributeToolbarAction(ad, toolbar, 226 toolAppendIfMissing); 227 } 228 } 229 } 230 } 231 232 238 protected void contributeMenu(IConfigurationElement menuElement, 239 IMenuManager mng, boolean appendIfMissing) { 240 String id = menuElement.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 242 String label = menuElement.getAttribute(IWorkbenchRegistryConstants.ATT_LABEL); 243 String path = menuElement.getAttribute(IWorkbenchRegistryConstants.ATT_PATH); 244 if (label == null) { 245 WorkbenchPlugin.log("Plugin \'" + menuElement.getContributor().getName() 247 + "\' invalid Menu Extension (label == null): " + id); return; 249 } 250 251 String group = null; 253 if (path != null) { 254 int loc = path.lastIndexOf('/'); 255 if (loc != -1) { 256 group = path.substring(loc + 1); 257 path = path.substring(0, loc); 258 } else { 259 group = path; 262 path = null; 263 } 264 } 265 266 IMenuManager parent = mng; 268 if (path != null) { 269 parent = mng.findMenuUsingPath(path); 270 if (parent == null) { 271 ideLog("Plugin \'" + menuElement.getContributor().getName() 273 + "\' invalid Menu Extension (Path \'" + path + "\' is invalid): " + id); return; 276 } 277 } 278 279 if (group == null) { 281 group = IWorkbenchActionConstants.MB_ADDITIONS; 282 } 283 IContributionItem sep = parent.find(group); 284 if (sep == null) { 285 if (appendIfMissing) { 286 addGroup(parent, group); 287 } else { 288 WorkbenchPlugin 289 .log("Plugin \'" + menuElement.getContributor().getName() 291 + "\' invalid Menu Extension (Group \'" + group + "\' is invalid): " + id); return; 294 } 295 } 296 297 IMenuManager newMenu = parent.findMenuUsingPath(id); 299 if (newMenu == null) { 300 newMenu = new MenuManager(label, id); 301 } 302 303 try { 305 insertAfter(parent, group, newMenu); 306 } catch (IllegalArgumentException e) { 307 WorkbenchPlugin 308 .log("Plugin \'" + menuElement.getContributor().getName() 310 + "\' invalid Menu Extension (Group \'" + group + "\' is missing): " + id); } 313 314 newMenu = parent.findMenuUsingPath(id); 318 if (newMenu == null) { 319 WorkbenchPlugin.log("Could not find new menu: " + id); } 321 322 IConfigurationElement[] children = menuElement.getChildren(); 324 for (int i = 0; i < children.length; i++) { 325 String childName = children[i].getName(); 326 if (childName.equals(IWorkbenchRegistryConstants.TAG_SEPARATOR)) { 327 contributeSeparator(newMenu, children[i]); 328 } else if (childName.equals(IWorkbenchRegistryConstants.TAG_GROUP_MARKER)) { 329 contributeGroupMarker(newMenu, children[i]); 330 } 331 } 332 } 333 334 337 protected void contributeMenuAction(ActionDescriptor ad, 338 IMenuManager menu, boolean appendIfMissing) { 339 String mpath = ad.getMenuPath(); 341 String mgroup = ad.getMenuGroup(); 342 if (mpath == null && mgroup == null) { 343 return; 344 } 345 IMenuManager parent = menu; 347 if (mpath != null) { 348 parent = parent.findMenuUsingPath(mpath); 349 if (parent == null) { 350 ideLog("Plug-in '" + ad.getPluginId() + "' contributed an invalid Menu Extension (Path: '" + mpath + "' is invalid): " + ad.getId()); return; 352 } 353 } 354 355 if (mgroup == null) { 357 mgroup = IWorkbenchActionConstants.MB_ADDITIONS; 358 } 359 IContributionItem sep = parent.find(mgroup); 360 if (sep == null) { 361 if (appendIfMissing) { 362 addGroup(parent, mgroup); 363 } else { 364 WorkbenchPlugin 365 .log("Plug-in '" + ad.getPluginId() + "' contributed an invalid Menu Extension (Group: '" + mgroup + "' is invalid): " + ad.getId()); return; 367 } 368 } 369 370 try { 372 insertAfter(parent, mgroup, ad.getAction()); 373 } catch (IllegalArgumentException e) { 374 WorkbenchPlugin 375 .log("Plug-in '" + ad.getPluginId() + "' contributed an invalid Menu Extension (Group: '" + mgroup + "' is missing): " + ad.getId()); } 377 } 378 379 383 protected void contributeSeparator(IMenuManager menu, 384 IConfigurationElement element) { 385 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 386 if (id == null || id.length() <= 0) { 387 return; 388 } 389 IContributionItem sep = menu.find(id); 390 if (sep != null) { 391 return; 392 } 393 insertMenuGroup(menu, new Separator(id)); 394 } 395 396 400 protected void contributeGroupMarker(IMenuManager menu, 401 IConfigurationElement element) { 402 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_NAME); 403 if (id == null || id.length() <= 0) { 404 return; 405 } 406 IContributionItem marker = menu.find(id); 407 if (marker != null) { 408 return; 409 } 410 insertMenuGroup(menu, new GroupMarker(id)); 411 } 412 413 416 protected void contributeToolbarAction(ActionDescriptor ad, 417 IToolBarManager toolbar, boolean appendIfMissing) { 418 String tId = ad.getToolbarId(); 420 String tgroup = ad.getToolbarGroupId(); 421 if (tId == null && tgroup == null) { 422 return; 423 } 424 425 if (tgroup == null) { 427 tgroup = IWorkbenchActionConstants.MB_ADDITIONS; 428 } 429 IContributionItem sep = null; 430 sep = toolbar.find(tgroup); 431 if (sep == null) { 432 if (appendIfMissing) { 433 addGroup(toolbar, tgroup); 434 } else { 435 WorkbenchPlugin 436 .log("Plug-in '" + ad.getPluginId() + "' invalid Toolbar Extension (Group \'" + tgroup + "\' is invalid): " + ad.getId()); return; 440 } 441 } 442 try { 444 insertAfter(toolbar, tgroup, ad.getAction()); 445 } catch (IllegalArgumentException e) { 446 WorkbenchPlugin 447 .log("Plug-in '" + ad.getPluginId() + "' invalid Toolbar Extension (Group \'" + tgroup + "\' is missing): " + ad.getId()); } 451 } 452 453 456 protected void insertMenuGroup(IMenuManager menu, 457 AbstractGroupMarker marker) { 458 menu.add(marker); 459 } 460 461 465 protected void insertAfter(IContributionManager mgr, String refId, 466 PluginAction action) { 467 insertAfter(mgr, refId, new PluginActionContributionItem(action)); 468 } 469 470 474 protected void insertAfter(IContributionManager mgr, String refId, 475 IContributionItem item) { 476 mgr.insertAfter(refId, item); 477 } 478 479 483 protected void addGroup(IContributionManager mgr, String name) { 484 mgr.add(new Separator(name)); 485 } 486 487 492 public void dispose() { 493 } 495 496 501 protected void disposeActions() { 502 if (actions != null) { 503 for (int i = 0; i < actions.size(); i++) { 504 PluginAction proxy = ((ActionDescriptor) actions.get(i)) 505 .getAction(); 506 proxy.dispose(); 507 } 508 actions = null; 509 } 510 } 511 } 512 513 private static boolean allowIdeLogging = false; 514 515 523 public static void setAllowIdeLogging(boolean b) { 524 allowIdeLogging = b; 525 } 526 527 534 private static void ideLog(String msg) { 535 if (allowIdeLogging) { 536 WorkbenchPlugin.log(msg); 537 } 538 } 539 } 540 | Popular Tags |