1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.core.commands.common.NotDefinedException; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IStatus; 25 import org.eclipse.jface.menus.IWidget; 26 import org.eclipse.swt.SWT; 27 import org.eclipse.swt.layout.RowLayout; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.ui.internal.WindowTrimProxy; 30 import org.eclipse.ui.internal.WorkbenchPlugin; 31 import org.eclipse.ui.internal.WorkbenchWindow; 32 import org.eclipse.ui.internal.layout.IWindowTrim; 33 import org.eclipse.ui.internal.layout.TrimLayout; 34 import org.eclipse.ui.internal.misc.StatusUtil; 35 import org.eclipse.ui.menus.IWorkbenchWidget; 36 37 49 public class TrimBarManager { 50 51 54 private STrimBuilder fTrimBuilder; 55 56 private IMenuService fMenuService; 57 58 private boolean fDirty; 59 60 67 public TrimBarManager(final WorkbenchWindow window) { 68 if (window == null) { 69 throw new IllegalArgumentException ("The window cannot be null"); } 71 72 fMenuService = (IMenuService) window.getService(IMenuService.class); 74 fTrimBuilder = new STrimBuilder(window); 75 76 fDirty = true; 78 } 79 80 90 public void update(boolean force, boolean recursive, boolean hideTopTrim) { 91 if (force || isDirty()) { 92 SMenuLayout layout = fMenuService.getLayout(); 94 fTrimBuilder.build(layout, hideTopTrim); 95 setDirty(false); 96 } 97 } 98 99 109 public void update(boolean force, boolean recursive) { 110 update(force, recursive, false); 111 } 112 113 118 private void setDirty(boolean isDirty) { 119 fDirty = isDirty; 120 } 121 122 127 private boolean isDirty() { 128 return fDirty; 129 } 130 131 139 private class TrimWidgetProxy extends WindowTrimProxy { 140 141 private List widgets; 142 143 private int curSide; 144 145 private Composite parent; 146 147 private SGroup group; 148 149 171 public TrimWidgetProxy(List widgets, int side, Composite parent, 172 SGroup group, int validSides, boolean resizeable) { 173 super(parent, group.getId(), group.getId(), validSides, resizeable); 174 175 this.widgets = widgets; 177 this.curSide = side; 178 this.parent = parent; 179 this.group = group; 180 } 181 182 187 public void dock(int newSide) { 188 for (Iterator iter = widgets.iterator(); iter.hasNext();) { 190 IWidget widget = (IWidget) iter.next(); 191 widget.dispose(); 192 } 193 194 for (Iterator iter = widgets.iterator(); iter.hasNext();) { 196 WidgetProxy widget = (WidgetProxy) iter.next(); 197 widget.fill(parent, curSide, newSide); 198 } 199 200 curSide = newSide; 201 202 parent.layout(); 203 } 204 205 public SGroup getGroup() { 206 return group; 207 } 208 209 213 public void dispose() { 214 for (Iterator iter = widgets.iterator(); iter.hasNext();) { 215 IWidget widget = (IWidget) iter.next(); 216 widget.dispose(); 217 } 218 219 getControl().dispose(); 220 } 221 } 222 223 230 private class STrimBuilder { 231 234 private WorkbenchWindow fWindow; 235 236 241 private Set bogusTrim = new HashSet (); 242 243 249 private List curGroups = new ArrayList (); 250 251 254 private Map initializedTrim = new HashMap (); 255 256 262 public STrimBuilder(WorkbenchWindow window) { 263 fWindow = window; 264 } 265 266 270 public void tearDown() { 271 for (Iterator iter = curGroups.iterator(); iter.hasNext();) { 273 TrimWidgetProxy proxy = (TrimWidgetProxy) iter.next(); 274 fWindow.getTrimManager().removeTrim(proxy); 275 276 proxy.dispose(); 277 } 278 279 curGroups.clear(); 281 } 282 283 298 public void build(SMenuLayout layout, boolean hideTopTrim) { 299 tearDown(); 300 301 ILayoutNode trimInfo = layout.getBar(SBar.TYPE_TRIM); 303 304 List kids = trimInfo.getChildrenSorted(); 306 for (Iterator iter = kids.iterator(); iter.hasNext();) { 307 ILayoutNode node = (ILayoutNode) iter.next(); 308 MenuElement element = node.getMenuElement(); 309 if (element instanceof SWidget) { 310 } else if (element instanceof SGroup) { 313 renderGroup(node, hideTopTrim); 315 } 316 } 317 } 318 319 326 private void renderGroup(ILayoutNode groupNode, boolean hideTopTrim) { 327 SGroup group = (SGroup) groupNode.getMenuElement(); 328 if ("testGroup".equals(group.getId())) { int i = 0; 330 i++; 331 } 332 List kids = groupNode.getChildrenSorted(); 333 334 if (kids.size() == 0) 336 return; 337 338 int side = getSide(group); 339 340 if (hideTopTrim && side == SWT.TOP) 341 return; 342 343 Composite grpComposite = new Composite(fWindow.getShell(), SWT.NONE); 345 grpComposite.setToolTipText(group.getId()); 346 347 RowLayout rl = new RowLayout(); 349 rl.marginBottom = rl.marginHeight = rl.marginLeft = rl.marginRight = rl.marginTop = rl.marginWidth = 0; 350 grpComposite.setLayout(rl); 351 352 boolean resizeable = false; 354 355 List groupWidgets = new ArrayList (); 357 for (Iterator iter = kids.iterator(); iter.hasNext();) { 358 ILayoutNode node = (ILayoutNode) iter.next(); 359 MenuElement element = node.getMenuElement(); 360 if (element instanceof SWidget) { 361 SWidget sWidget = (SWidget) element; 362 363 resizeable = resizeable || isResizeable(sWidget); 365 366 IWidget iw = renderTrim(grpComposite, sWidget, side); 368 if (iw != null) 369 groupWidgets.add(iw); 370 } else if (element instanceof SGroup) { 371 } 373 } 374 375 TrimWidgetProxy groupTrimProxy = new TrimWidgetProxy(groupWidgets, 377 side, grpComposite, group, SWT.TOP | SWT.BOTTOM | SWT.LEFT 378 | SWT.RIGHT, resizeable); 379 curGroups.add(groupTrimProxy); 380 381 placeGroup(groupTrimProxy); 383 } 384 385 private void placeGroup(final TrimWidgetProxy proxy) { 386 final int side = getSide(proxy.getGroup()); 388 boolean atStart = isAtStart(proxy.getGroup()); 389 390 IWindowTrim beforeMe = null; 393 if (atStart) { 394 List trim = fWindow.getTrimManager().getAreaTrim(side); 395 if (trim.size() > 0) 396 beforeMe = (IWindowTrim) trim.get(0); 397 } 398 399 try { 401 proxy.dock(side); TrimLayout tl = (TrimLayout) fWindow.getShell().getLayout(); 403 tl.addTrim(side, proxy, beforeMe); 404 } catch (Throwable e) { 405 IStatus status = null; 406 if (e instanceof CoreException) { 407 status = ((CoreException) e).getStatus(); 408 } else { 409 status = StatusUtil 410 .newStatus( 411 IStatus.ERROR, 412 "Internal plug-in widget delegate error on dock.", e); } 414 WorkbenchPlugin 415 .log( 416 "widget delegate failed on dock: id = " + proxy.getId(), status); } 418 } 419 420 429 private IWidget renderTrim(final Composite groupComposite, final SWidget widget, 430 final int side) { 431 if (bogusTrim.contains(widget)) 433 return null; 434 435 IWidget iw = null; 437 try { 438 iw = widget.getWidget(); 439 if (iw != null) { 440 if (iw instanceof IWorkbenchWidget && initializedTrim.get(iw) == null) { 442 IWorkbenchWidget iww = (IWorkbenchWidget) iw; 443 iww.init(fWindow); 444 initializedTrim.put(iw, iw); 445 } 446 447 if (iw instanceof WidgetProxy) 448 ((WidgetProxy) iw).fill(groupComposite, SWT.DEFAULT, side); 449 else 450 iw.fill(groupComposite); 451 } 452 } catch (Throwable e) { 453 bogusTrim.add(widget); 455 456 IStatus status = null; 457 if (e instanceof CoreException) { 458 status = ((CoreException) e).getStatus(); 459 } else { 460 status = StatusUtil 461 .newStatus( 462 IStatus.ERROR, 463 "Internal plug-in widget delegate error on creation.", e); } 465 WorkbenchPlugin 466 .log( 467 "Could not create widget delegate for id: " + widget.getId(), status); 469 return null; 470 } 471 472 return iw; 473 } 474 475 482 private boolean isResizeable(SWidget widget) { 483 try { 484 SLayout layout = widget.getLayout(); 485 return layout.fillMajor(); 486 } catch (NotDefinedException e) { 487 e.printStackTrace(); 489 } 490 491 return false; 492 } 493 494 505 private int getSide(SGroup group) { 506 int side = SWT.BOTTOM; try { 508 SLocation[] locs = group.getLocations(); 509 if (locs.length == 0) 510 return side; 511 512 SOrder order = locs[0].getOrdering(); 514 String relTo = order.getRelativeTo(); 515 if ("command1".equals(relTo)) side = SWT.TOP; 517 else if ("command2".equals(relTo)) side = SWT.TOP; 519 else if ("vertical1".equals(relTo)) side = SWT.LEFT; 521 else if ("vertical2".equals(relTo)) side = SWT.RIGHT; 523 else if ("status".equals(relTo)) side = SWT.BOTTOM; 525 else { 526 } 529 } catch (NotDefinedException e) { 530 e.printStackTrace(); 532 } 533 534 return side; 535 } 536 537 544 private boolean isAtStart(SGroup group) { 545 boolean atStart = false; 546 try { 547 SLocation[] locs = group.getLocations(); 548 if (locs.length == 0) 549 return atStart; 550 551 SOrder order = locs[0].getOrdering(); 553 atStart = SOrder.POSITION_BEFORE == order.getPosition(); 554 } catch (NotDefinedException e) { 555 e.printStackTrace(); 557 } 558 559 return atStart; 560 } 561 562 573 public void updateLocations(List knownIds) { 574 for (Iterator iter = curGroups.iterator(); iter.hasNext();) { 575 TrimWidgetProxy proxy = (TrimWidgetProxy) iter.next(); 576 if (!knownIds.contains(proxy.getGroup().getId())) { 577 placeGroup(proxy); 578 } 579 } 580 } 581 } 582 583 590 public void updateLocations(List knownIds) { 591 fTrimBuilder.updateLocations(knownIds); 592 } 593 594 597 public void dispose() { 598 fMenuService = null; 599 fTrimBuilder = null; 600 } 601 } 602 | Popular Tags |