1 11 12 package org.eclipse.ui.internal.menus; 13 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IConfigurationElement; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.Status; 18 import org.eclipse.ui.internal.WorkbenchPlugin; 19 20 34 final class DynamicMenuProxy implements IDynamicMenu { 35 36 41 private IConfigurationElement configurationElement; 42 43 49 private IDynamicMenu dynamicMenu = null; 50 51 55 private final String dynamicMenuAttributeName; 56 57 68 public DynamicMenuProxy(final IConfigurationElement configurationElement, 69 final String dynamicMenuAttributeName) { 70 if (configurationElement == null) { 71 throw new NullPointerException ( 72 "The configuration element backing a dynamic menu proxy cannot be null"); } 74 75 if (dynamicMenuAttributeName == null) { 76 throw new NullPointerException ( 77 "The attribute containing the dynamic menu class must be known"); } 79 80 this.configurationElement = configurationElement; 81 this.dynamicMenuAttributeName = dynamicMenuAttributeName; 82 } 83 84 public final void aboutToShow(final IMenuCollection menu) { 85 if (loadDynamicMenu()) { 86 dynamicMenu.aboutToShow(menu); 87 } 88 } 89 90 97 private final boolean loadDynamicMenu() { 98 if (dynamicMenu == null) { 99 try { 101 dynamicMenu = (IDynamicMenu) configurationElement 102 .createExecutableExtension(dynamicMenuAttributeName); 103 configurationElement = null; 104 return true; 105 106 } catch (final ClassCastException e) { 107 final String message = "The proxied dynamic menu was the wrong class"; final IStatus status = new Status(IStatus.ERROR, 109 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 110 WorkbenchPlugin.log(message, status); 111 return false; 112 113 } catch (final CoreException e) { 114 final String message = "The proxied dynamic menu for '" + configurationElement.getAttribute(dynamicMenuAttributeName) + "' could not be loaded"; final IStatus status = new Status(IStatus.ERROR, 117 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 118 WorkbenchPlugin.log(message, status); 119 return false; 120 121 } 122 } 123 124 return true; 125 } 126 127 public final String toString() { 128 if (dynamicMenu == null) { 129 return configurationElement.getAttribute(dynamicMenuAttributeName); 130 } 131 132 return dynamicMenu.toString(); 133 } 134 } 135 | Popular Tags |