1 11 12 package org.eclipse.ui.internal.menus; 13 14 import org.eclipse.core.commands.ParameterizedCommand; 15 import org.eclipse.core.commands.common.CommandException; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.ISafeRunnable; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.SafeRunner; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.jface.menus.IWidget; 23 import org.eclipse.swt.SWT; 24 import org.eclipse.swt.events.DisposeEvent; 25 import org.eclipse.swt.events.DisposeListener; 26 import org.eclipse.swt.graphics.Point; 27 import org.eclipse.swt.graphics.Rectangle; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.CoolBar; 31 import org.eclipse.swt.widgets.Event; 32 import org.eclipse.swt.widgets.Listener; 33 import org.eclipse.swt.widgets.Menu; 34 import org.eclipse.swt.widgets.MenuItem; 35 import org.eclipse.swt.widgets.ToolBar; 36 import org.eclipse.swt.widgets.ToolItem; 37 import org.eclipse.swt.widgets.Widget; 38 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate; 39 import org.eclipse.ui.IWorkbenchWindowPulldownDelegate2; 40 import org.eclipse.ui.handlers.IHandlerService; 41 import org.eclipse.ui.internal.WorkbenchPlugin; 42 import org.eclipse.ui.services.IServiceLocator; 43 44 58 final class PulldownDelegateWidgetProxy implements IWidget { 59 60 64 private static final class MenuLoader implements ISafeRunnable { 65 66 70 private final Control control; 71 72 75 private final IWorkbenchWindowPulldownDelegate delegate; 76 77 81 private Menu menu = null; 82 83 87 private final Menu parent; 88 89 99 private MenuLoader(final IWorkbenchWindowPulldownDelegate delegate, 100 final Control parent) { 101 this.delegate = delegate; 102 this.parent = null; 103 this.control = parent; 104 } 105 106 116 private MenuLoader(final IWorkbenchWindowPulldownDelegate2 delegate, 117 final Menu parent) { 118 this.delegate = delegate; 119 this.parent = parent; 120 this.control = null; 121 } 122 123 128 private Menu getMenu() { 129 return menu; 130 } 131 132 135 public void handleException(Throwable exception) { 136 } 138 139 142 public void run() throws Exception { 143 if (parent == null) { 144 menu = delegate.getMenu(control); 145 } else { 146 menu = ((IWorkbenchWindowPulldownDelegate2) delegate) 147 .getMenu(parent); 148 } 149 } 150 } 151 152 157 private final ParameterizedCommand command; 158 159 164 private IConfigurationElement configurationElement; 165 166 171 private IWorkbenchWindowPulldownDelegate delegate = null; 172 173 177 private final String delegateAttributeName; 178 179 private final DisposeListener disposeListener = new DisposeListener() { 180 public void widgetDisposed(DisposeEvent e) { 181 if (e.widget == widget) { 182 dispose(); 183 widget = null; 184 185 } 188 } 189 }; 190 191 196 private final IServiceLocator locator; 197 198 private final Listener selectionListener = new Listener() { 199 public final void handleEvent(final Event event) { 200 final Widget item = event.widget; 201 if (item == null) { 202 return; 203 } 204 205 final int style = item.getStyle(); 206 if (((style & SWT.DROP_DOWN) != 0) && (event.detail == SWT.ARROW) 207 && (item instanceof ToolItem)) { 208 final ToolItem toolItem = (ToolItem) item; 210 final ToolBar toolBar = toolItem.getParent(); 211 if (loadDelegate() 212 && (delegate instanceof IWorkbenchWindowPulldownDelegate2)) { 213 final IWorkbenchWindowPulldownDelegate2 delegate2 = (IWorkbenchWindowPulldownDelegate2) delegate; 214 final MenuLoader loader = new MenuLoader(delegate2, toolBar); 215 SafeRunner.run(loader); 216 final Menu subMenu = loader.getMenu(); 217 if (subMenu != null) { 218 final Rectangle bounds = toolItem.getBounds(); 220 final Point location = toolBar.toDisplay(new Point( 221 bounds.x, bounds.y + bounds.height)); 222 subMenu.setLocation(location); 223 subMenu.setVisible(true); 224 return; } 226 } 227 } 228 229 final IHandlerService service = (IHandlerService) locator 230 .getService(IHandlerService.class); 231 try { 232 service.executeCommand(command, event); 233 } catch (final CommandException e) { 234 238 } 239 } 240 241 }; 242 243 248 private Widget widget = null; 249 250 268 public PulldownDelegateWidgetProxy( 269 final IConfigurationElement configurationElement, 270 final String delegateAttributeName, 271 final ParameterizedCommand command, final IServiceLocator locator) { 272 if (configurationElement == null) { 273 throw new NullPointerException ( 274 "The configuration element backing a handler proxy cannot be null"); } 276 277 if (delegateAttributeName == null) { 278 throw new NullPointerException ( 279 "The attribute containing the handler class must be known"); } 281 282 if (command == null) { 283 throw new NullPointerException ("The command cannot be null"); } 285 286 this.configurationElement = configurationElement; 287 this.delegateAttributeName = delegateAttributeName; 288 this.command = command; 289 this.locator = locator; 290 } 291 292 295 public final void dispose() { 296 if (delegate != null) { 297 delegate.dispose(); 298 } 299 } 300 301 public final void fill(final Composite parent) { 302 } 304 305 public final void fill(CoolBar parent, final int index) { 306 } 308 309 public final void fill(final Menu parent, final int index) { 310 if ((widget != null) || (parent == null)) { 311 return; 312 } 313 314 final MenuItem menuItem; 316 if (index >= 0) { 317 menuItem = new MenuItem(parent, SWT.CASCADE, index); 318 } else { 319 menuItem = new MenuItem(parent, SWT.CASCADE); 320 } 321 menuItem.setData(this); 322 widget = menuItem; 323 324 if (loadDelegate() 326 && (delegate instanceof IWorkbenchWindowPulldownDelegate2)) { 327 final IWorkbenchWindowPulldownDelegate2 delegate2 = (IWorkbenchWindowPulldownDelegate2) delegate; 328 final MenuLoader loader = new MenuLoader(delegate2, parent); 329 SafeRunner.run(loader); 330 final Menu subMenu = loader.getMenu(); 331 if (subMenu != null) { 332 menuItem.setMenu(subMenu); 333 } 334 } 335 336 menuItem.addDisposeListener(disposeListener); 337 menuItem.addListener(SWT.Selection, selectionListener); 338 339 343 } 346 347 public final void fill(final ToolBar parent, final int index) { 348 if ((widget != null) && (parent == null)) { 349 return; 350 } 351 352 final ToolItem toolItem; 353 if (index >= 0) { 354 toolItem = new ToolItem(parent, SWT.DROP_DOWN, index); 355 } else { 356 toolItem = new ToolItem(parent, SWT.DROP_DOWN); 357 } 358 toolItem.setData(this); 359 widget = toolItem; 360 361 toolItem.addDisposeListener(disposeListener); 363 toolItem.addListener(SWT.Selection, selectionListener); 364 365 379 } 382 383 390 private final boolean loadDelegate() { 391 if (delegate == null) { 392 try { 394 delegate = (IWorkbenchWindowPulldownDelegate) configurationElement 395 .createExecutableExtension(delegateAttributeName); 396 configurationElement = null; 397 return true; 398 399 } catch (final ClassCastException e) { 400 final String message = "The proxied delegate was the wrong class"; final IStatus status = new Status(IStatus.ERROR, 402 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 403 WorkbenchPlugin.log(message, status); 404 return false; 405 406 } catch (final CoreException e) { 407 final String message = "The proxied delegate for '" + configurationElement.getAttribute(delegateAttributeName) + "' could not be loaded"; IStatus status = new Status(IStatus.ERROR, 410 WorkbenchPlugin.PI_WORKBENCH, 0, message, e); 411 WorkbenchPlugin.log(message, status); 412 return false; 413 } 414 } 415 416 return true; 417 } 418 419 public final String toString() { 420 if (delegate == null) { 421 return configurationElement.getAttribute(delegateAttributeName); 422 } 423 424 return delegate.toString(); 425 } 426 } 427 | Popular Tags |