1 11 package org.eclipse.ui.internal.layout; 12 13 import org.eclipse.jface.action.ContributionItem; 14 import org.eclipse.jface.action.MenuManager; 15 import org.eclipse.jface.action.ToolBarManager; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.events.SelectionListener; 19 import org.eclipse.swt.graphics.Cursor; 20 import org.eclipse.swt.graphics.Point; 21 import org.eclipse.swt.graphics.Rectangle; 22 import org.eclipse.swt.widgets.Control; 23 import org.eclipse.swt.widgets.CoolBar; 24 import org.eclipse.swt.widgets.CoolItem; 25 import org.eclipse.swt.widgets.Event; 26 import org.eclipse.swt.widgets.Listener; 27 import org.eclipse.swt.widgets.Menu; 28 import org.eclipse.swt.widgets.MenuItem; 29 import org.eclipse.swt.widgets.ToolBar; 30 import org.eclipse.swt.widgets.ToolItem; 31 import org.eclipse.ui.internal.IChangeListener; 32 import org.eclipse.ui.internal.IntModel; 33 import org.eclipse.ui.internal.RadioMenu; 34 import org.eclipse.ui.internal.TrimFrame; 35 import org.eclipse.ui.internal.WorkbenchMessages; 36 import org.eclipse.ui.internal.WorkbenchWindow; 37 import org.eclipse.ui.internal.dnd.DragUtil; 38 import org.eclipse.ui.presentations.PresentationUtil; 39 40 67 public abstract class TrimToolBarBase implements IWindowTrim { 68 protected String id; 70 protected int orientation; 71 protected WorkbenchWindow wbw; 72 protected TrimLayout layout; 73 protected ToolBarManager tbMgr = null; 74 protected ToolItem contextToolItem = null; 75 76 private TrimFrame frame = null; 78 private CoolBar cb = null; 79 private CoolItem ci = null; 80 81 private MenuManager dockMenuManager; 83 private ContributionItem dockContributionItem = null; 84 private Menu sidesMenu; 85 private MenuItem dockCascade; 86 private RadioMenu radioButtons; 87 private IntModel radioVal = new IntModel(0); 88 91 94 95 private Listener tbListener = new Listener() { 96 public void handleEvent(Event event) { 97 Point loc = new Point(event.x, event.y); 98 if (event.type == SWT.MenuDetect) { 99 showToolBarPopup(loc); 100 } 101 } 102 }; 103 104 107 private Listener cbListener = new Listener() { 108 public void handleEvent(Event event) { 109 Point loc = new Point(event.x, event.y); 110 if (event.type == SWT.MenuDetect) { 111 showDockTrimPopup(loc); 112 } 113 } 114 }; 115 116 120 private Listener dragListener = new Listener() { 121 public void handleEvent(Event event) { 122 if (event.button != 3) { 124 Point position = DragUtil.getEventLoc(event); 125 startDraggingTrim(position); 126 } 127 } 128 }; 129 130 137 protected TrimToolBarBase(String id, int curSide, WorkbenchWindow wbw) { 138 this.id = id; 139 this.wbw = wbw; 140 this.layout = (TrimLayout) wbw.getTrimManager(); 141 } 142 143 146 private void showToolBarPopup(Point loc) { 147 Point tbLoc = tbMgr.getControl().toControl(loc); 148 contextToolItem = tbMgr.getControl().getItem(tbLoc); 149 MenuManager mm = tbMgr.getContextMenuManager(); 150 if (mm != null) { 151 Menu menu = mm.createContextMenu(wbw.getShell()); 152 menu.setLocation(loc.x, loc.y); 153 menu.setVisible(true); 154 } 155 } 156 157 165 public abstract void initToolBarManager(ToolBarManager mgr); 166 167 175 public abstract void hookControl(ToolBarManager mgr); 176 177 182 private void createControl(int curSide) { 183 dispose(); 185 186 this.radioVal.set(curSide); 187 188 orientation = (curSide == SWT.LEFT || curSide == SWT.RIGHT) ? SWT.VERTICAL : SWT.HORIZONTAL; 190 191 frame = new TrimFrame(wbw.getShell()); 192 193 cb = new CoolBar(frame.getComposite(), orientation | SWT.FLAT); 195 ci = new CoolItem(cb, SWT.FLAT); 196 197 tbMgr = new ToolBarManager(orientation | SWT.FLAT); 199 200 initToolBarManager(tbMgr); 202 203 ToolBar tb = tbMgr.createControl(cb); 205 ci.setControl(tb); 206 207 hookControl(tbMgr); 209 210 update(true); 212 213 Cursor dragCursor = getControl().getDisplay().getSystemCursor(SWT.CURSOR_SIZEALL); 215 cb.setCursor(dragCursor); 216 217 Cursor tbCursor = getControl().getDisplay().getSystemCursor(SWT.CURSOR_ARROW); 219 tb.setCursor(tbCursor); 220 221 223 PresentationUtil.addDragListener(cb, dragListener); 225 226 dockMenuManager = new MenuManager(); 228 dockContributionItem = getDockingContribution(); 229 dockMenuManager.add(dockContributionItem); 230 231 tb.addListener(SWT.MenuDetect, tbListener); 232 cb.addListener(SWT.MenuDetect, cbListener); 233 234 cb.pack(true); 237 cb.setVisible(true); 238 239 tbMgr.getControl().setVisible(true); 240 cb.setVisible(true); 241 frame.getComposite().setVisible(true); 242 } 243 244 248 private void handleShowOnChange() { 249 if (getControl() == null) 250 return; 251 252 layout.removeTrim(this); 253 dock(radioVal.get()); 254 layout.addTrim(radioVal.get(), this, null); 255 256 LayoutUtil.resize(getControl()); 258 } 259 260 264 public void update(boolean changed) { 265 tbMgr.update(changed); 266 267 tbMgr.getControl().pack(); 269 Point size = tbMgr.getControl().getSize(); 270 Point ps = ci.computeSize (size.x, size.y); 272 ci.setPreferredSize (ps); 273 ci.setSize(ps); 274 cb.pack(); 275 cb.update(); 276 LayoutUtil.resize(getControl()); 277 } 278 279 286 private ContributionItem getDockingContribution() { 287 if (dockContributionItem == null) { 288 dockContributionItem = new ContributionItem() { 289 public void fill(Menu menu, int index) { 290 super.fill(menu, index); 292 293 if (isCloseable()) { 295 MenuItem closeItem = new MenuItem(menu, SWT.PUSH, index++); 296 closeItem.setText(WorkbenchMessages.TrimCommon_Close); 297 298 closeItem.addSelectionListener(new SelectionListener() { 299 public void widgetSelected(SelectionEvent e) { 300 handleCloseTrim(); 301 } 302 303 public void widgetDefaultSelected(SelectionEvent e) { 304 } 305 }); 306 307 new MenuItem(menu, SWT.SEPARATOR, index++); 308 } 309 310 326 dockCascade = new MenuItem(menu, SWT.CASCADE, index++); 328 { 329 dockCascade.setText(WorkbenchMessages.TrimCommon_DockOn); 330 331 sidesMenu = new Menu(dockCascade); 332 radioButtons = new RadioMenu(sidesMenu, radioVal); 333 334 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Top, new Integer (SWT.TOP)); 335 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Bottom, new Integer (SWT.BOTTOM)); 336 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Left, new Integer (SWT.LEFT)); 337 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Right, new Integer (SWT.RIGHT)); 338 339 dockCascade.setMenu(sidesMenu); 340 } 341 342 radioVal.addChangeListener(new IChangeListener() { 344 public void update(boolean changed) { 345 if (changed) { 346 handleShowOnChange(); 347 } 348 } 349 }); 350 351 } 387 }; 388 } 389 return dockContributionItem; 390 } 391 392 395 public int getCurrentSide() { 396 return radioVal.get(); 397 } 398 399 403 408 414 private void handleCloseTrim() { 415 handleClose(); 416 } 417 418 421 public void dispose() { 422 if (getControl() == null || getControl().isDisposed()) 423 return; 424 425 if (radioButtons != null) { 426 radioButtons.dispose(); 427 } 428 429 getControl().removeListener(SWT.MenuDetect, cbListener); 431 432 tbMgr.dispose(); 433 tbMgr = null; 434 435 getControl().dispose(); 436 frame = null; 437 } 438 439 444 private void startDraggingTrim(Point position) { 445 Rectangle fakeBounds = new Rectangle(100000, 0,0,0); 446 DragUtil.performDrag(this, fakeBounds, position, true); 447 } 448 449 452 private void showDockTrimPopup(Point pt) { 453 Menu menu = dockMenuManager.createContextMenu(this.getControl()); 454 menu.setLocation(pt.x, pt.y); 455 menu.setVisible(true); 456 } 457 458 461 public void dock(int dropSide) { 462 createControl(dropSide); 463 } 464 465 468 public Control getControl() { 469 if (frame == null) 470 return null; 471 472 return frame.getComposite(); 473 } 474 475 478 public String getDisplayName() { 479 return id; 480 } 481 482 485 public int getHeightHint() { 486 return getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT, true).y; 487 } 488 489 492 public String getId() { 493 return id; 494 } 495 496 499 public int getValidSides() { 500 return SWT.BOTTOM | SWT.TOP | SWT.LEFT | SWT.RIGHT; 501 } 502 503 506 public int getWidthHint() { 507 return getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x; 508 } 509 510 513 public void handleClose() { 514 } 515 516 519 public boolean isCloseable() { 520 return false; 521 } 522 523 526 public boolean isResizeable() { 527 return false; 528 } 529 } 530 | Popular Tags |