1 11 12 package org.eclipse.ui.internal.menus; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IConfigurationElement; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.jface.menus.IWidget; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.layout.RowLayout; 26 import org.eclipse.swt.widgets.Composite; 27 import org.eclipse.ui.internal.WindowTrimProxy; 28 import org.eclipse.ui.internal.WorkbenchWindow; 29 import org.eclipse.ui.internal.layout.IWindowTrim; 30 import org.eclipse.ui.internal.layout.TrimLayout; 31 import org.eclipse.ui.internal.misc.StatusUtil; 32 import org.eclipse.ui.menus.AbstractWorkbenchTrimWidget; 33 import org.eclipse.ui.menus.IMenuService; 34 import org.eclipse.ui.menus.IWorkbenchWidget; 35 import org.eclipse.ui.statushandlers.StatusManager; 36 37 49 public class TrimBarManager2 { 50 51 54 private MenuLocationURI[] trimAreaURIs = { 55 new MenuLocationURI("toolbar:command1"), new MenuLocationURI("toolbar:command2"), new MenuLocationURI("toolbar:vertical1"), new MenuLocationURI("toolbar:vertical2"), new MenuLocationURI("toolbar:status"), }; 61 62 65 int[] swtSides = { SWT.TOP, SWT.TOP, SWT.LEFT, SWT.RIGHT, SWT.BOTTOM }; 66 69 private STrimBuilder fTrimBuilder; 70 71 private WorkbenchMenuService fMenuService; 72 73 private boolean fDirty; 74 75 82 public TrimBarManager2(final WorkbenchWindow window) { 83 if (window == null) { 84 throw new IllegalArgumentException ("The window cannot be null"); } 86 87 fMenuService = (WorkbenchMenuService) window.getWorkbench().getService( 89 IMenuService.class); 90 fTrimBuilder = new STrimBuilder(window); 91 92 fDirty = true; 94 } 95 96 106 public void update(boolean force, boolean recursive, boolean hideTopTrim) { 107 if (force || isDirty()) { 108 fTrimBuilder.build(hideTopTrim); 110 setDirty(false); 111 } 112 } 113 114 124 public void update(boolean force, boolean recursive) { 125 update(force, recursive, false); 126 } 127 128 133 private void setDirty(boolean isDirty) { 134 fDirty = isDirty; 135 } 136 137 142 private boolean isDirty() { 143 return fDirty; 144 } 145 146 154 private class TrimWidgetProxy extends WindowTrimProxy { 155 156 private List widgets; 157 private TrimAdditionCacheEntry cacheEntry; 158 private int originalSide; 159 private int curSide; 160 161 private Composite parent; 162 163 169 public TrimWidgetProxy(List widgets, int side, Composite parent, 170 TrimAdditionCacheEntry entry, boolean resizeable) { 171 super(parent, entry.getId(), entry.getId(), SWT.TOP | SWT.BOTTOM | SWT.LEFT 172 | SWT.RIGHT, resizeable); 173 174 this.widgets = widgets; 176 this.curSide = side; 177 this.originalSide = side; 178 this.parent = parent; 179 this.cacheEntry = entry; 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 cacheEntry.removeWidget(widget); 194 } 195 196 for (Iterator iter = widgets.iterator(); iter.hasNext();) { 198 IWorkbenchWidget widget = (IWorkbenchWidget) iter.next(); 199 if (widget instanceof AbstractWorkbenchTrimWidget) 200 ((AbstractWorkbenchTrimWidget)widget).fill(parent, curSide, newSide); 201 else 202 widget.fill(parent); 203 } 204 205 curSide = newSide; 206 parent.layout(); 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 cacheEntry.removeWidget(widget); 220 } 221 222 getControl().dispose(); 223 } 224 225 228 public int getSide() { 229 return originalSide; 230 } 231 232 236 public boolean isAtStart() { 237 return cacheEntry.isAtStart(); 239 } 240 } 241 242 249 private class STrimBuilder { 250 253 private WorkbenchWindow fWindow; 254 255 261 private List curGroups = new ArrayList (); 262 263 266 private Map initializedTrim = new HashMap (); 267 268 274 public STrimBuilder(WorkbenchWindow window) { 275 fWindow = window; 276 } 277 278 282 public void tearDown() { 283 for (Iterator iter = curGroups.iterator(); iter.hasNext();) { 285 TrimWidgetProxy proxy = (TrimWidgetProxy) iter.next(); 286 fWindow.getTrimManager().removeTrim(proxy); 287 288 try { 289 proxy.dispose(); 290 } catch (Throwable e) { 291 IStatus status = null; 292 if (e instanceof CoreException) { 293 status = ((CoreException) e).getStatus(); 294 } else { 295 status = StatusUtil 296 .newStatus( 297 IStatus.ERROR, 298 "Internal plug-in widget delegate error on dispose.", e); } 300 StatusUtil 301 .handleStatus( 302 status, 303 "widget delegate failed on dispose: id = " + proxy.getId(), StatusManager.LOG); } 305 } 306 307 curGroups.clear(); 309 } 310 311 326 public void build(boolean hideTopTrim) { 327 tearDown(); 328 329 for (int i = 0; i < trimAreaURIs.length; i++) { 330 processAdditions(trimAreaURIs[i], hideTopTrim); 331 } 332 } 333 334 338 private void processAdditions(MenuLocationURI trimURI, boolean hideTopTrim) { 339 List additions = fMenuService.getAdditionsForURI(trimURI); 340 if (additions.size() == 0) 341 return; 342 343 int swtSide = getSide(trimURI); 344 345 if (swtSide == SWT.TOP && hideTopTrim) 347 return; 348 349 for (Iterator iterator = additions.iterator(); iterator.hasNext();) { 352 TrimAdditionCacheEntry trimEntry = (TrimAdditionCacheEntry) iterator.next(); 353 String groupId = trimEntry.getId(); 354 355 List widgets = trimEntry.getWidgets(); 358 if (widgets.size() == 0) 359 continue; 360 361 Composite grpComposite = new Composite(fWindow.getShell(), SWT.NONE); 363 grpComposite.setToolTipText(groupId); 364 365 RowLayout rl = new RowLayout(); 367 rl.marginBottom = rl.marginHeight = rl.marginLeft = rl.marginRight = rl.marginTop = rl.marginWidth = 0; 368 grpComposite.setLayout(rl); 369 370 boolean resizeable = false; 372 373 for (Iterator widgetIter = widgets.iterator(); widgetIter.hasNext();) { 374 IWorkbenchWidget widget = (IWorkbenchWidget) widgetIter.next(); 375 IConfigurationElement widgetElement = trimEntry.getElement(widget); 376 if (widget != null) { 377 resizeable |= trimEntry.fillMajor(widgetElement); 378 renderTrim(grpComposite, widget, swtSide); 379 } 380 } 381 382 TrimWidgetProxy groupTrimProxy = new TrimWidgetProxy(widgets, 384 swtSide, grpComposite, trimEntry, resizeable); 385 curGroups.add(groupTrimProxy); 386 387 placeGroup(groupTrimProxy); 389 } 390 } 391 392 private void placeGroup(final TrimWidgetProxy proxy) { 393 final int side = proxy.getSide(); 395 boolean atStart = proxy.isAtStart(); 396 397 IWindowTrim beforeMe = null; 400 if (atStart) { 401 List trim = fWindow.getTrimManager().getAreaTrim(side); 402 if (trim.size() > 0) 403 beforeMe = (IWindowTrim) trim.get(0); 404 } 405 406 try { 408 proxy.dock(side); TrimLayout tl = (TrimLayout) fWindow.getShell().getLayout(); 410 tl.addTrim(side, proxy, beforeMe); 411 } catch (Throwable e) { 412 IStatus status = null; 413 if (e instanceof CoreException) { 414 status = ((CoreException) e).getStatus(); 415 } else { 416 status = StatusUtil 417 .newStatus( 418 IStatus.ERROR, 419 "Internal plug-in widget delegate error on dock.", e); } 421 StatusUtil.handleStatus(status, "widget delegate failed on dock: id = " + proxy.getId(), StatusManager.LOG); 423 } 424 } 425 426 435 private void renderTrim(final Composite groupComposite, IWidget iw, 436 final int side) { 437 if (iw != null) { 439 if (iw instanceof IWorkbenchWidget && initializedTrim.get(iw) == null) { 441 IWorkbenchWidget iww = (IWorkbenchWidget) iw; 442 iww.init(fWindow); 443 initializedTrim.put(iw, iw); 444 } 445 446 if (iw instanceof AbstractWorkbenchTrimWidget) 447 ((AbstractWorkbenchTrimWidget)iw).fill(groupComposite, SWT.DEFAULT, side); 448 else 449 iw.fill(groupComposite); 450 } 451 } 452 453 private int getSide(MenuLocationURI uri) { 454 for (int i = 0; i < trimAreaURIs.length; i++) { 455 if (trimAreaURIs[i].getRawString().equals(uri.getRawString())) 456 return swtSides[i]; 457 } 458 return SWT.BOTTOM; 459 } 460 461 462 473 public void updateLocations(List knownIds) { 474 for (Iterator iter = curGroups.iterator(); iter.hasNext();) { 475 TrimWidgetProxy proxy = (TrimWidgetProxy) iter.next(); 476 if (!knownIds.contains(proxy.getId())) { 477 placeGroup(proxy); 478 } 479 } 480 } 481 } 482 483 490 public void updateLocations(List knownIds) { 491 fTrimBuilder.updateLocations(knownIds); 492 } 493 494 497 public void dispose() { 498 fMenuService = null; 499 fTrimBuilder = null; 500 } 501 } 502 503 | Popular Tags |