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.swt.widgets.Composite; 19 import org.eclipse.swt.widgets.CoolBar; 20 import org.eclipse.swt.widgets.Menu; 21 import org.eclipse.swt.widgets.ToolBar; 22 import org.eclipse.ui.IWorkbenchWindow; 23 import org.eclipse.ui.internal.WorkbenchPlugin; 24 import org.eclipse.ui.menus.AbstractWorkbenchTrimWidget; 25 import org.eclipse.ui.menus.IWorkbenchWidget; 26 27 36 final class WidgetProxy implements IWorkbenchWidget { 37 38 42 private boolean firstLoad = true; 43 44 49 private IConfigurationElement configurationElement; 50 51 56 private IWorkbenchWidget widget = null; 57 58 62 private final String widgetAttributeName; 63 64 75 public WidgetProxy(final IConfigurationElement configurationElement, 76 final String widgetAttributeName) { 77 if (configurationElement == null) { 78 throw new NullPointerException ( 79 "The configuration element backing a widget proxy cannot be null"); } 81 82 if (widgetAttributeName == null) { 83 throw new NullPointerException ( 84 "The attribute containing the widget class must be known"); } 86 87 this.configurationElement = configurationElement; 88 this.widgetAttributeName = widgetAttributeName; 89 } 90 91 public final void dispose() { 92 if (loadWidget()) { 93 widget.dispose(); 94 } 95 } 96 97 public final void fill(final Composite parent) { 98 if (loadWidget()) { 99 widget.fill(parent); 100 } 101 } 102 103 public final void fill(final CoolBar parent, final int index) { 104 if (loadWidget()) { 105 widget.fill(parent, index); 106 } 107 } 108 109 public final void fill(final Menu parent, final int index) { 110 if (loadWidget()) { 111 widget.fill(parent, index); 112 } 113 } 114 115 public final void fill(final ToolBar parent, final int index) { 116 if (loadWidget()) { 117 widget.fill(parent, index); 118 } 119 } 120 121 124 public void init(IWorkbenchWindow workbenchWindow) { 125 if (loadWidget()) { 126 widget.init(workbenchWindow); 127 } 128 } 129 130 140 public final void fill(Composite parent, int oldSide, int newSide) { 141 if (loadWidget()) { 142 if (isMoveableTrimWidget()) { 143 ((AbstractWorkbenchTrimWidget) widget).fill(parent, oldSide, newSide); 144 } else { 145 widget.fill(parent); 146 } 147 } 148 } 149 150 157 private final boolean loadWidget() { 158 if (firstLoad) { 159 try { 161 widget = (IWorkbenchWidget) configurationElement 162 .createExecutableExtension(widgetAttributeName); 163 configurationElement = null; 164 } catch (final ClassCastException e) { 165 final String message = "The proxied widget was the wrong class"; final IStatus status = new Status(IStatus.ERROR, 167 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 168 WorkbenchPlugin.log(message, status); 169 170 } catch (final CoreException e) { 171 final String message = "The proxied widget for '" + configurationElement.getAttribute(widgetAttributeName) + "' could not be loaded"; IStatus status = new Status(IStatus.ERROR, 174 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 175 WorkbenchPlugin.log(message, status); 176 } 177 } 178 179 firstLoad = false; 181 182 return widget != null; 184 } 185 186 193 private final boolean isMoveableTrimWidget() { 194 if (loadWidget()) { 195 return widget instanceof AbstractWorkbenchTrimWidget; 196 } 197 198 return false; 199 } 200 201 public final String toString() { 202 if (widget == null) { 203 return configurationElement.getAttribute(widgetAttributeName); 204 } 205 206 return widget.toString(); 207 } 208 } 209 | Popular Tags |