1 11 12 package org.eclipse.jface.action; 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 17 import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem; 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.jface.util.Policy; 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.events.DisposeEvent; 22 import org.eclipse.swt.events.DisposeListener; 23 import org.eclipse.swt.events.SelectionAdapter; 24 import org.eclipse.swt.events.SelectionEvent; 25 import org.eclipse.swt.graphics.Point; 26 import org.eclipse.swt.graphics.Rectangle; 27 import org.eclipse.swt.widgets.Control; 28 import org.eclipse.swt.widgets.CoolBar; 29 import org.eclipse.swt.widgets.CoolItem; 30 import org.eclipse.swt.widgets.Event; 31 import org.eclipse.swt.widgets.Listener; 32 import org.eclipse.swt.widgets.Menu; 33 import org.eclipse.swt.widgets.ToolBar; 34 import org.eclipse.swt.widgets.ToolItem; 35 36 47 public class ToolBarContributionItem extends ContributionItem implements IToolBarContributionItem { 48 49 53 public static final int SHOW_ALL_ITEMS = -1; 54 55 59 private MenuManager chevronMenuManager = null; 60 61 65 private CoolItem coolItem = null; 66 67 70 private int currentHeight = -1; 71 72 75 private int currentWidth = -1; 76 77 81 private boolean disposed = false; 82 83 86 private int minimumItemsToShow = SHOW_ALL_ITEMS; 87 88 92 private ToolBarManager toolBarManager = null; 93 94 97 private boolean useChevron = true; 98 99 102 public ToolBarContributionItem() { 103 this(new ToolBarManager(), null); 104 } 105 106 112 public ToolBarContributionItem(IToolBarManager toolBarManager) { 113 this(toolBarManager, null); 114 } 115 116 124 public ToolBarContributionItem(IToolBarManager toolBarManager, String id) { 125 super(id); 126 Assert.isTrue(toolBarManager instanceof ToolBarManager); 127 this.toolBarManager = (ToolBarManager) toolBarManager; 128 } 129 130 139 private final boolean checkDisposed() { 140 if (disposed) { 141 if (Policy.TRACE_TOOLBAR) { 142 System.out 143 .println("Method invocation on a disposed tool bar contribution item."); new Exception ().printStackTrace(System.out); 145 } 146 147 return true; 148 } 149 150 return false; 151 } 152 153 158 public void dispose() { 159 if (toolBarManager != null) { 161 toolBarManager.dispose(); 162 toolBarManager = null; 163 } 164 165 169 if ((coolItem != null) && (!coolItem.isDisposed())) { 170 coolItem.dispose(); 171 coolItem = null; 172 } 173 174 disposed = true; 176 } 177 178 184 public void fill(CoolBar coolBar, int index) { 185 if (checkDisposed()) { 186 return; 187 } 188 189 if (coolItem == null && coolBar != null) { 190 ToolBar oldToolBar = toolBarManager.getControl(); 191 ToolBar toolBar = toolBarManager.createControl(coolBar); 192 if ((oldToolBar != null) && (oldToolBar.equals(toolBar))) { 193 toolBarManager.update(true); 195 } 196 197 if (toolBar.getItemCount() < 1) { 199 return; 200 } 201 int flags = SWT.DROP_DOWN; 202 if (index >= 0) { 203 coolItem = new CoolItem(coolBar, flags, index); 204 } else { 205 coolItem = new CoolItem(coolBar, flags); 206 } 207 coolItem.setData(this); 209 coolItem.setControl(toolBar); 211 212 if (oldToolBar != toolBar) { 216 toolBar.addListener(SWT.MenuDetect, new Listener() { 217 218 public void handleEvent(Event event) { 219 if (toolBarManager.getContextMenuManager() == null) { 222 handleContextMenu(event); 223 } 224 } 225 }); 226 } 227 228 if (getUseChevron()) { 230 coolItem.addSelectionListener(new SelectionAdapter() { 232 233 public void widgetSelected(SelectionEvent event) { 234 if (event.detail == SWT.ARROW) { 235 handleChevron(event); 236 } 237 } 238 }); 239 } 240 241 coolItem.addDisposeListener(new DisposeListener() { 243 244 public void widgetDisposed(DisposeEvent event) { 245 handleWidgetDispose(event); 246 } 247 }); 248 249 updateSize(true); 251 } 252 } 253 254 260 private int[] getAdjustedWrapIndices(int[] wraps) { 261 int[] adjustedWrapIndices; 262 if (wraps.length == 0) { 263 adjustedWrapIndices = new int[] { 0 }; 264 } else { 265 if (wraps[0] != 0) { 266 adjustedWrapIndices = new int[wraps.length + 1]; 267 adjustedWrapIndices[0] = 0; 268 for (int i = 0; i < wraps.length; i++) { 269 adjustedWrapIndices[i + 1] = wraps[i]; 270 } 271 } else { 272 adjustedWrapIndices = wraps; 273 } 274 } 275 return adjustedWrapIndices; 276 } 277 278 283 public int getCurrentHeight() { 284 if (checkDisposed()) { 285 return -1; 286 } 287 return currentHeight; 288 } 289 290 295 public int getCurrentWidth() { 296 if (checkDisposed()) { 297 return -1; 298 } 299 return currentWidth; 300 } 301 302 309 public int getMinimumItemsToShow() { 310 if (checkDisposed()) { 311 return -1; 312 } 313 return minimumItemsToShow; 314 } 315 316 323 public IToolBarManager getToolBarManager() { 324 if (checkDisposed()) { 325 return null; 326 } 327 return toolBarManager; 328 } 329 330 336 public boolean getUseChevron() { 337 if (checkDisposed()) { 338 return false; 339 } 340 return useChevron; 341 } 342 343 346 private void handleChevron(SelectionEvent event) { 347 CoolItem item = (CoolItem) event.widget; 348 Control control = item.getControl(); 349 if ((control instanceof ToolBar) == false) { 350 return; 351 } 352 CoolBar coolBar = item.getParent(); 353 ToolBar toolBar = (ToolBar) control; 354 Rectangle toolBarBounds = toolBar.getBounds(); 355 ToolItem[] items = toolBar.getItems(); 356 ArrayList hidden = new ArrayList (); 357 for (int i = 0; i < items.length; ++i) { 358 Rectangle itemBounds = items[i].getBounds(); 359 if (!((itemBounds.x + itemBounds.width <= toolBarBounds.width) && (itemBounds.y 360 + itemBounds.height <= toolBarBounds.height))) { 361 hidden.add(items[i]); 362 } 363 } 364 365 if (chevronMenuManager != null) { 367 chevronMenuManager.dispose(); 368 } 369 chevronMenuManager = new MenuManager(); 370 for (Iterator i = hidden.iterator(); i.hasNext();) { 371 ToolItem toolItem = (ToolItem) i.next(); 372 IContributionItem data = (IContributionItem) toolItem.getData(); 373 if (data instanceof ActionContributionItem) { 374 ActionContributionItem contribution = new ActionContributionItem( 375 ((ActionContributionItem) data).getAction()); 376 chevronMenuManager.add(contribution); 377 } else if (data instanceof SubContributionItem) { 378 IContributionItem innerData = ((SubContributionItem) data) 379 .getInnerItem(); 380 if (innerData instanceof ActionContributionItem) { 381 ActionContributionItem contribution = new ActionContributionItem( 382 ((ActionContributionItem) innerData).getAction()); 383 chevronMenuManager.add(contribution); 384 } 385 } else if (data.isSeparator()) { 386 chevronMenuManager.add(new Separator()); 387 } 388 } 389 Menu popup = chevronMenuManager.createContextMenu(coolBar); 390 Point chevronPosition = coolBar.toDisplay(event.x, event.y); 391 popup.setLocation(chevronPosition.x, chevronPosition.y); 392 popup.setVisible(true); 393 } 394 395 402 private void handleContextMenu(Event event) { 403 ToolBar toolBar = toolBarManager.getControl(); 404 Menu parentMenu = toolBar.getParent().getMenu(); 406 if ((parentMenu != null) && (!parentMenu.isDisposed())) { 407 toolBar.setMenu(parentMenu); 408 parentMenu.addListener(SWT.Hide, new Listener() { 410 411 public void handleEvent(Event innerEvent) { 412 ToolBar innerToolBar = toolBarManager.getControl(); 413 if (innerToolBar != null) { 414 innerToolBar.setMenu(null); 415 Menu innerParentMenu = innerToolBar.getParent() 416 .getMenu(); 417 if (innerParentMenu != null) { 418 innerParentMenu.removeListener(SWT.Hide, this); 419 } 420 } 421 } 422 }); 423 } 424 } 425 426 432 private void handleWidgetDispose(DisposeEvent event) { 433 coolItem = null; 434 } 435 436 445 public boolean isVisible() { 446 if (checkDisposed()) { 447 return false; 448 } 449 450 boolean visibleItem = false; 451 if (toolBarManager != null) { 452 IContributionItem[] contributionItems = toolBarManager.getItems(); 453 for (int i = 0; i < contributionItems.length; i++) { 454 IContributionItem contributionItem = contributionItems[i]; 455 if ((!contributionItem.isGroupMarker()) 456 && (!contributionItem.isSeparator())) { 457 visibleItem = true; 458 break; 459 } 460 } 461 } 462 463 return (visibleItem || super.isVisible()); 464 } 465 466 471 public void saveWidgetState() { 472 if (checkDisposed()) { 473 return; 474 } 475 if (coolItem == null) { 476 return; 477 } 478 479 CoolBar coolBar = coolItem.getParent(); 481 boolean isLastOnRow = false; 482 int lastIndex = coolBar.getItemCount() - 1; 483 int coolItemIndex = coolBar.indexOf(coolItem); 484 int[] wrapIndicies = getAdjustedWrapIndices(coolBar.getWrapIndices()); 485 for (int row = wrapIndicies.length - 1; row >= 0; row--) { 487 if (wrapIndicies[row] <= coolItemIndex) { 488 489 int nextRow = row + 1; 490 int nextRowStartIndex; 491 if (nextRow > (wrapIndicies.length - 1)) { 492 nextRowStartIndex = lastIndex + 1; 493 } else { 494 nextRowStartIndex = wrapIndicies[nextRow]; 495 } 496 497 if (coolItemIndex == (nextRowStartIndex - 1)) { 499 isLastOnRow = true; 500 } 501 break; 502 } 503 } 504 505 int nCurrentWidth; 507 if (isLastOnRow) { 508 nCurrentWidth = coolItem.getPreferredSize().x; 509 } else { 510 nCurrentWidth = coolItem.getSize().x; 511 } 512 setCurrentWidth(nCurrentWidth); 513 setCurrentHeight(coolItem.getSize().y); 514 } 515 516 523 public void setCurrentHeight(int currentHeight) { 524 if (checkDisposed()) { 525 return; 526 } 527 this.currentHeight = currentHeight; 528 } 529 530 537 public void setCurrentWidth(int currentWidth) { 538 if (checkDisposed()) { 539 return; 540 } 541 this.currentWidth = currentWidth; 542 } 543 544 555 public void setMinimumItemsToShow(int minimumItemsToShow) { 556 if (checkDisposed()) { 557 return; 558 } 559 this.minimumItemsToShow = minimumItemsToShow; 560 } 561 562 570 public void setUseChevron(boolean value) { 571 if (checkDisposed()) { 572 return; 573 } 574 useChevron = value; 575 } 576 577 582 public void update(String propertyName) { 583 if (checkDisposed()) { 584 return; 585 } 586 if (coolItem != null) { 587 IToolBarManager manager = getToolBarManager(); 588 if (manager != null) { 589 manager.update(true); 590 } 591 592 if ((propertyName == null) 593 || propertyName.equals(ICoolBarManager.SIZE)) { 594 updateSize(true); 595 } 596 } 597 } 598 599 608 private void updateSize(boolean changeCurrentSize) { 609 if (checkDisposed()) { 610 return; 611 } 612 if (coolItem == null || coolItem.isDisposed()) { 614 return; 615 } 616 boolean locked = false; 617 CoolBar coolBar = coolItem.getParent(); 618 try { 619 if (coolBar != null) { 621 if (coolBar.getLocked()) { 622 coolBar.setLocked(false); 623 locked = true; 624 } 625 } 626 ToolBar toolBar = (ToolBar) coolItem.getControl(); 627 if ((toolBar == null) || (toolBar.isDisposed()) 628 || (toolBar.getItemCount() <= 0)) { 629 coolItem.setData(null); 632 Control control = coolItem.getControl(); 633 if ((control != null) && !control.isDisposed()) { 634 control.dispose(); 635 coolItem.setControl(null); 636 } 637 if (!coolItem.isDisposed()) { 638 coolItem.dispose(); 639 } 640 } else { 641 Point toolBarSize = toolBar.computeSize(SWT.DEFAULT, 644 SWT.DEFAULT); 645 Point preferredSize = coolItem.computeSize(toolBarSize.x, 647 toolBarSize.y); 648 coolItem.setPreferredSize(preferredSize); 649 if (getMinimumItemsToShow() != SHOW_ALL_ITEMS) { 653 int toolItemWidth = toolBar.getItems()[0].getWidth(); 654 int minimumWidth = toolItemWidth * getMinimumItemsToShow(); 655 coolItem.setMinimumSize(minimumWidth, toolBarSize.y); 656 } else { 657 coolItem.setMinimumSize(toolBarSize.x, toolBarSize.y); 658 } 659 if (changeCurrentSize) { 660 coolItem.setSize(preferredSize); 662 } 663 } 664 } finally { 665 if ((locked) && (coolBar != null)) { 667 coolBar.setLocked(true); 668 } 669 } 670 } 671 672 } 673 | Popular Tags |