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.swt.SWT; 16 import org.eclipse.swt.events.ControlEvent; 17 import org.eclipse.swt.events.ControlListener; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.events.SelectionListener; 20 import org.eclipse.swt.graphics.Cursor; 21 import org.eclipse.swt.graphics.Point; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.CoolBar; 26 import org.eclipse.swt.widgets.CoolItem; 27 import org.eclipse.swt.widgets.Event; 28 import org.eclipse.swt.widgets.Label; 29 import org.eclipse.swt.widgets.Listener; 30 import org.eclipse.swt.widgets.Menu; 31 import org.eclipse.swt.widgets.MenuItem; 32 import org.eclipse.ui.internal.IChangeListener; 33 import org.eclipse.ui.internal.IntModel; 34 import org.eclipse.ui.internal.RadioMenu; 35 import org.eclipse.ui.internal.WindowTrimProxy; 36 import org.eclipse.ui.internal.WorkbenchMessages; 37 import org.eclipse.ui.internal.dnd.DragUtil; 38 import org.eclipse.ui.presentations.PresentationUtil; 39 40 68 public class TrimCommonUIHandle extends Composite { 69 72 private TrimLayout layout; 73 private IWindowTrim trim; 74 private Control toDrag; 75 private int orientation; 76 77 private CoolBar cb = null; 79 private CoolItem ci = null; 80 private static int horizontalHandleSize = -1; 81 private static int verticalHandleSize = -1; 82 83 86 private MenuManager dockMenuManager; 87 private ContributionItem dockContributionItem = null; 88 private Menu sidesMenu; 89 private MenuItem dockCascade; 90 private RadioMenu radioButtons; 91 private IntModel radioVal = new IntModel(0); 92 95 98 99 103 private Listener dragListener = new Listener() { 104 public void handleEvent(Event event) { 105 if (event.button != 3) { 107 Point position = DragUtil.getEventLoc(event); 108 startDraggingTrim(position); 109 } 110 } 111 }; 112 113 116 private Listener menuListener = new Listener() { 117 public void handleEvent(Event event) { 118 Point loc = new Point(event.x, event.y); 119 if (event.type == SWT.MenuDetect) { 120 showDockTrimPopup(loc); 121 } 122 } 123 }; 124 125 129 private ControlListener controlListener = new ControlListener() { 130 public void controlMoved(ControlEvent e) { 131 } 132 133 public void controlResized(ControlEvent e) { 134 if (e.widget instanceof TrimCommonUIHandle) { 135 TrimCommonUIHandle ctrl = (TrimCommonUIHandle) e.widget; 136 Point size = ctrl.getSize(); 137 138 cb.setSize(size); 140 ci.setSize(size); 141 ci.setPreferredSize(size); 142 cb.layout(true); 143 } 144 } 145 }; 146 147 154 public TrimCommonUIHandle(TrimLayout layout, IWindowTrim trim, int curSide) { 155 super(trim.getControl().getParent(), SWT.NONE); 156 157 setup(layout, trim, curSide); 159 160 addControlListener(controlListener); 162 } 163 164 169 public void setup(TrimLayout layout, IWindowTrim trim, int curSide) { 170 this.layout = layout; 171 this.trim = trim; 172 this.toDrag = trim.getControl(); 173 this.radioVal.set(curSide); 174 175 orientation = (curSide == SWT.LEFT || curSide == SWT.RIGHT) ? SWT.VERTICAL : SWT.HORIZONTAL; 177 178 insertCoolBar(orientation); 180 181 createWindowTrimProxy(); 183 184 setDragCursor(); 186 187 PresentationUtil.addDragListener(cb, dragListener); 189 190 dockMenuManager = new MenuManager(); 192 dockContributionItem = getDockingContribution(); 193 dockMenuManager.add(dockContributionItem); 194 195 cb.addListener(SWT.MenuDetect, menuListener); 196 197 setVisible(true); 198 } 199 200 204 private void handleShowOnChange() { 205 layout.removeTrim(trim); 206 trim.dock(radioVal.get()); 207 layout.addTrim(radioVal.get(), trim, null); 208 209 LayoutUtil.resize(trim.getControl()); 211 } 212 213 217 private void createWindowTrimProxy() { 218 WindowTrimProxy proxy = new WindowTrimProxy(this, "NONE", "NONE", SWT.TOP | SWT.BOTTOM | SWT.LEFT | SWT.RIGHT, false); 221 222 if (orientation == SWT.HORIZONTAL) { 227 proxy.setWidthHint(getHandleSize()); 228 proxy.setHeightHint(0); 229 } 230 else { 231 proxy.setWidthHint(0); 232 proxy.setHeightHint(getHandleSize()); 233 } 234 235 setLayoutData(proxy); 236 } 237 238 244 private int getHandleSize() { 245 if (orientation == SWT.HORIZONTAL && horizontalHandleSize != -1) { 247 return horizontalHandleSize; 248 } 249 250 if (orientation == SWT.VERTICAL && verticalHandleSize != -1) { 251 return verticalHandleSize; 252 } 253 254 CoolBar bar = new CoolBar (trim.getControl().getParent(), orientation); 256 257 CoolItem item = new CoolItem (bar, SWT.NONE); 258 259 Label ctrl = new Label (bar, SWT.PUSH); 260 ctrl.setText ("Button 1"); Point size = ctrl.computeSize (SWT.DEFAULT, SWT.DEFAULT); 262 263 Point ps = item.computeSize (size.x, size.y); 264 item.setPreferredSize (ps); 265 item.setControl (ctrl); 266 267 bar.pack (); 268 269 Point bl = ctrl.getLocation(); 272 Point cl = bar.getLocation(); 273 274 ctrl.dispose(); 276 item.dispose(); 277 bar.dispose(); 278 279 int length; 282 if (orientation == SWT.HORIZONTAL) { 283 length = bl.x - cl.x; 284 horizontalHandleSize = length; 285 } 286 else { 287 length = bl.y - cl.y; 288 verticalHandleSize = length; 289 } 290 291 return length; 292 } 293 294 302 public void insertCoolBar(int orientation) { 303 if (cb != null) { 305 ci.dispose(); 306 PresentationUtil.removeDragListener(cb, dragListener); 307 cb.dispose(); 308 } 309 310 cb = new CoolBar(this, orientation | SWT.FLAT); 312 cb.setLocation(0,0); 313 ci = new CoolItem(cb, SWT.FLAT); 314 315 Composite comp = new Composite(cb, SWT.NONE); 317 ci.setControl(comp); 318 } 319 320 324 private void setDragCursor() { 325 Cursor dragCursor = toDrag.getDisplay().getSystemCursor(SWT.CURSOR_SIZEALL); 326 setCursor(dragCursor); 327 } 328 329 332 public Point computeSize(int wHint, int hHint, boolean changed) { 333 Point ctrlPrefSize = trim.getControl().computeSize(wHint, hHint); 334 if (orientation == SWT.HORIZONTAL) { 335 return new Point(getHandleSize(), ctrlPrefSize.y); 336 } 337 338 return new Point(ctrlPrefSize.x, getHandleSize()); 340 } 341 342 349 public ContributionItem getDockingContribution() { 350 if (dockContributionItem == null) { 351 dockContributionItem = new ContributionItem() { 352 public void fill(Menu menu, int index) { 353 super.fill(menu, index); 355 356 if (trim.isCloseable()) { 358 MenuItem closeItem = new MenuItem(menu, SWT.PUSH, index++); 359 closeItem.setText(WorkbenchMessages.TrimCommon_Close); 360 361 closeItem.addSelectionListener(new SelectionListener() { 362 public void widgetSelected(SelectionEvent e) { 363 handleCloseTrim(); 364 } 365 366 public void widgetDefaultSelected(SelectionEvent e) { 367 } 368 }); 369 370 new MenuItem(menu, SWT.SEPARATOR, index++); 371 } 372 373 389 dockCascade = new MenuItem(menu, SWT.CASCADE, index++); 391 { 392 dockCascade.setText(WorkbenchMessages.TrimCommon_DockOn); 393 394 sidesMenu = new Menu(dockCascade); 395 radioButtons = new RadioMenu(sidesMenu, radioVal); 396 397 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Top, new Integer (SWT.TOP)); 398 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Bottom, new Integer (SWT.BOTTOM)); 399 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Left, new Integer (SWT.LEFT)); 400 radioButtons.addMenuItem(WorkbenchMessages.TrimCommon_Right, new Integer (SWT.RIGHT)); 401 402 dockCascade.setMenu(sidesMenu); 403 } 404 405 radioVal.addChangeListener(new IChangeListener() { 407 public void update(boolean changed) { 408 if (changed) { 409 handleShowOnChange(); 410 } 411 } 412 }); 413 414 } 450 }; 451 } 452 return dockContributionItem; 453 } 454 455 459 464 470 private void handleCloseTrim() { 471 layout.removeTrim(trim); 472 trim.handleClose(); 473 } 474 475 478 public void dispose() { 479 if (radioButtons != null) { 480 radioButtons.dispose(); 481 } 482 483 removeControlListener(controlListener); 485 removeListener(SWT.MenuDetect, menuListener); 486 487 super.dispose(); 488 } 489 490 495 protected void startDraggingTrim(Point position) { 496 Rectangle fakeBounds = new Rectangle(100000, 0,0,0); 497 DragUtil.performDrag(trim, fakeBounds, position, true); 498 } 499 500 503 private void showDockTrimPopup(Point pt) { 504 Menu menu = dockMenuManager.createContextMenu(toDrag); 505 menu.setLocation(pt.x, pt.y); 506 menu.setVisible(true); 507 } 508 } 509 | Popular Tags |