1 11 package org.eclipse.ui.internal.presentations.r33; 12 13 import org.eclipse.core.runtime.Assert; 14 import org.eclipse.jface.util.Geometry; 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.custom.CTabFolder; 17 import org.eclipse.swt.custom.CTabFolderEvent; 18 import org.eclipse.swt.custom.CTabItem; 19 import org.eclipse.swt.events.MouseAdapter; 20 import org.eclipse.swt.events.MouseEvent; 21 import org.eclipse.swt.events.SelectionAdapter; 22 import org.eclipse.swt.events.SelectionEvent; 23 import org.eclipse.swt.graphics.Font; 24 import org.eclipse.swt.graphics.GC; 25 import org.eclipse.swt.graphics.Image; 26 import org.eclipse.swt.graphics.Point; 27 import org.eclipse.swt.graphics.Rectangle; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Event; 31 import org.eclipse.swt.widgets.Label; 32 import org.eclipse.swt.widgets.Listener; 33 import org.eclipse.swt.widgets.ToolBar; 34 import org.eclipse.swt.widgets.ToolItem; 35 import org.eclipse.ui.internal.IWorkbenchGraphicConstants; 36 import org.eclipse.ui.internal.WorkbenchImages; 37 import org.eclipse.ui.internal.WorkbenchMessages; 38 import org.eclipse.ui.internal.dnd.DragUtil; 39 import org.eclipse.ui.internal.presentations.util.AbstractTabFolder; 40 import org.eclipse.ui.internal.presentations.util.AbstractTabItem; 41 import org.eclipse.ui.internal.presentations.util.PartInfo; 42 import org.eclipse.ui.internal.presentations.util.TabFolderEvent; 43 import org.eclipse.ui.internal.util.Util; 44 45 48 public class DefaultTabFolder extends AbstractTabFolder { 49 50 private PaneFolder paneFolder; 51 private Control viewToolBar; 52 private Label titleLabel; 53 54 private PaneFolderButtonListener buttonListener = new PaneFolderButtonListener() { 55 public void stateButtonPressed(int buttonId) { 56 fireEvent(TabFolderEvent.stackStateToEventId(buttonId)); 57 } 58 59 65 public void closeButtonPressed(CTabItem item) { 66 fireEvent(TabFolderEvent.EVENT_CLOSE, getTab(item)); 67 } 68 69 73 public void showList(CTabFolderEvent event) { 74 event.doit = false; 75 fireEvent(TabFolderEvent.EVENT_SHOW_LIST); 76 } 77 }; 78 79 private Listener selectionListener = new Listener() { 80 public void handleEvent(Event e) { 81 AbstractTabItem item = getTab((CTabItem) e.item); 82 83 if (item != null) { 84 fireEvent(TabFolderEvent.EVENT_TAB_SELECTED, item); 85 } 86 } 87 }; 88 89 private static DefaultTabFolderColors defaultColors = new DefaultTabFolderColors(); 90 91 private DefaultTabFolderColors[] activeShellColors = { defaultColors, 92 defaultColors, defaultColors }; 93 private DefaultTabFolderColors[] inactiveShellColors = { defaultColors, 94 defaultColors, defaultColors }; 95 private boolean shellActive = false; 96 97 105 public DefaultTabFolder(Composite parent, int flags, boolean allowMin, 106 boolean allowMax) { 107 paneFolder = new PaneFolder(parent, flags | SWT.NO_BACKGROUND); 108 paneFolder.addButtonListener(buttonListener); 109 paneFolder.setMinimizeVisible(allowMin); 110 paneFolder.setMaximizeVisible(allowMax); 111 paneFolder.getControl().addListener(SWT.Selection, selectionListener); 112 paneFolder.setTopRight(null); 113 114 { 116 ToolBar actualToolBar = new ToolBar(paneFolder.getControl(), 117 SWT.FLAT | SWT.NO_BACKGROUND); 118 viewToolBar = actualToolBar; 119 120 ToolItem pullDownButton = new ToolItem(actualToolBar, SWT.PUSH); 121 Image hoverImage = WorkbenchImages 122 .getImage(IWorkbenchGraphicConstants.IMG_LCL_RENDERED_VIEW_MENU); 123 pullDownButton.setDisabledImage(hoverImage); 124 pullDownButton.setImage(hoverImage); 125 pullDownButton.setToolTipText(WorkbenchMessages.Menu); 126 actualToolBar.addMouseListener(new MouseAdapter() { 127 public void mouseDown(MouseEvent e) { 128 fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), 129 getPaneMenuLocation()); 130 } 131 }); 132 pullDownButton.addSelectionListener(new SelectionAdapter() { 133 public void widgetSelected(SelectionEvent e) { 134 fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), 135 getPaneMenuLocation()); 136 137 super.widgetSelected(e); 138 } 139 }); 140 } 141 142 { 144 titleLabel = new Label(paneFolder.getControl(), SWT.NONE); 145 titleLabel.moveAbove(null); 146 titleLabel.setVisible(false); 147 attachListeners(titleLabel, false); 148 } 149 150 attachListeners(paneFolder.getControl(), false); 151 attachListeners(paneFolder.getViewForm(), false); 152 153 paneFolder.setTabHeight(computeTabHeight()); 154 155 viewToolBar.moveAbove(null); 156 } 157 158 168 public void setMinimumCharacters(int count) { 169 paneFolder.setMinimumCharacters(count); 170 } 171 172 public void setSimpleTabs(boolean simple) { 173 paneFolder.setSimpleTab(simple); 174 } 175 176 181 protected DefaultTabItem getTab(CTabItem item) { 182 return (DefaultTabItem) item.getData(); 183 } 184 185 191 public Point computeSize(int widthHint, int heightHint) { 192 return paneFolder.computeMinimumSize(); 193 } 194 195 PaneFolder getFolder() { 196 return paneFolder; 197 } 198 199 public AbstractTabItem getSelection() { 200 return getTab(paneFolder.getSelection()); 201 } 202 203 208 public AbstractTabItem add(int index, int flags) { 209 DefaultTabItem result = new DefaultTabItem((CTabFolder) getFolder() 210 .getControl(), index, flags); 211 212 result.getWidget().setData(result); 213 214 return result; 215 } 216 217 222 public Composite getContentParent() { 223 return paneFolder.getContentParent(); 224 } 225 226 231 public void setContent(Control newContent) { 232 paneFolder.setContent(newContent); 233 } 234 235 240 public AbstractTabItem[] getItems() { 241 CTabItem[] items = paneFolder.getItems(); 242 243 AbstractTabItem[] result = new AbstractTabItem[items.length]; 244 245 for (int i = 0; i < result.length; i++) { 246 result[i] = getTab(items[i]); 247 } 248 249 return result; 250 } 251 252 257 public int getItemCount() { 258 return paneFolder.getItemCount(); 260 } 261 262 267 public void setSelection(AbstractTabItem toSelect) { 268 paneFolder.setSelection(indexOf(toSelect)); 269 } 270 271 276 public Composite getToolbarParent() { 277 return paneFolder.getControl(); 278 } 279 280 285 public Control getControl() { 286 return paneFolder.getControl(); 287 } 288 289 public void setUnselectedCloseVisible(boolean visible) { 290 paneFolder.setUnselectedCloseVisible(visible); 291 } 292 293 public void setUnselectedImageVisible(boolean visible) { 294 paneFolder.setUnselectedImageVisible(visible); 295 } 296 297 302 public Rectangle getTabArea() { 303 return Geometry.toDisplay(paneFolder.getControl(), paneFolder 304 .getTitleArea()); 305 } 306 307 311 public void enablePaneMenu(boolean enabled) { 312 if (enabled) { 313 paneFolder.setTopRight(viewToolBar); 314 viewToolBar.setVisible(true); 315 } else { 316 paneFolder.setTopRight(null); 317 viewToolBar.setVisible(false); 318 } 319 } 320 321 326 public void setSelectedInfo(PartInfo info) { 327 String newTitle = DefaultTabItem 328 .escapeAmpersands(info.contentDescription); 329 330 if (!Util.equals(titleLabel.getText(), newTitle)) { 331 titleLabel.setText(newTitle); 332 } 333 334 if (!info.contentDescription.equals(Util.ZERO_LENGTH_STRING)) { 335 paneFolder.setTopLeft(titleLabel); 336 titleLabel.setVisible(true); 337 } else { 338 paneFolder.setTopLeft(null); 339 titleLabel.setVisible(false); 340 } 341 } 342 343 348 public Point getPaneMenuLocation() { 349 Point toolbarSize = viewToolBar.getSize(); 350 351 return viewToolBar.toDisplay(0, toolbarSize.y); 352 } 353 354 359 public Point getPartListLocation() { 360 return paneFolder.getControl().toDisplay( 361 paneFolder.getChevronLocation()); 362 } 363 364 369 public Point getSystemMenuLocation() { 370 Rectangle bounds = DragUtil.getDisplayBounds(paneFolder.getControl()); 371 372 int idx = paneFolder.getSelectionIndex(); 373 if (idx > -1) { 374 CTabItem item = paneFolder.getItem(idx); 375 Rectangle itemBounds = item.getBounds(); 376 377 bounds.x += itemBounds.x; 378 bounds.y += itemBounds.y; 379 } 380 381 Point location = new Point(bounds.x, bounds.y 382 + paneFolder.getTabHeight()); 383 384 return location; 385 } 386 387 392 public boolean isOnBorder(Point toTest) { 393 Control content = paneFolder.getContent(); 394 if (content != null) { 395 Rectangle displayBounds = DragUtil.getDisplayBounds(content); 396 397 if (paneFolder.getTabPosition() == SWT.TOP) { 398 return toTest.y >= displayBounds.y; 399 } 400 401 if (toTest.y >= displayBounds.y 402 && toTest.y < displayBounds.y + displayBounds.height) { 403 return true; 404 } 405 } 406 407 return super.isOnBorder(toTest); 408 } 409 410 415 public void layout(boolean flushCache) { 416 paneFolder.layout(flushCache); 417 super.layout(flushCache); 418 } 419 420 425 public void setState(int state) { 426 paneFolder.setState(state); 427 super.setState(state); 428 } 429 430 435 public void setActive(int activeState) { 436 super.setActive(activeState); 437 updateColors(activeState); 438 } 439 440 445 public void setTabPosition(int tabPosition) { 446 paneFolder.setTabPosition(tabPosition); 447 super.setTabPosition(tabPosition); 448 layout(true); 449 } 450 451 public void flushToolbarSize() { 452 paneFolder.flushTopCenterSize(); 453 } 454 455 460 public void setToolbar(Control toolbarControl) { 461 paneFolder.setTopCenter(toolbarControl); 462 super.setToolbar(toolbarControl); 463 } 464 465 public void setColors(DefaultTabFolderColors colors, int activationState, 466 boolean shellActivationState) { 467 Assert.isTrue(activationState < activeShellColors.length); 468 469 if (shellActivationState) { 470 activeShellColors[activationState] = colors; 471 } else { 472 inactiveShellColors[activationState] = colors; 473 } 474 475 if (activationState == getActive() 476 && shellActive == shellActivationState) { 477 updateColors(activationState); 478 } 479 } 480 481 485 private void updateColors(int activationState) { 486 DefaultTabFolderColors currentColors = shellActive ? activeShellColors[getActive()] 487 : inactiveShellColors[getActive()]; 488 489 paneFolder.setSelectionForeground(currentColors.foreground); 490 paneFolder.setSelectionBackground(currentColors.background, 491 currentColors.percentages, currentColors.vertical); 492 } 493 494 public void setColors(DefaultTabFolderColors colors, int activationState) { 495 setColors(colors, activationState, true); 496 setColors(colors, activationState, false); 497 } 498 499 504 public void shellActive(boolean isActive) { 505 this.shellActive = isActive; 506 super.shellActive(isActive); 507 508 updateColors(-1); 509 } 510 511 515 public void setFont(Font font) { 516 if (font != paneFolder.getControl().getFont()) { 517 paneFolder.getControl().setFont(font); 518 layout(true); 519 paneFolder.setTabHeight(computeTabHeight()); 520 } 521 } 522 523 526 protected int computeTabHeight() { 527 GC gc = new GC(getControl()); 528 529 int tabHeight = Math.max(viewToolBar.computeSize(SWT.DEFAULT, 531 SWT.DEFAULT).y, gc.getFontMetrics().getHeight()); 532 533 gc.dispose(); 534 535 return tabHeight; 536 } 537 538 542 public void setSingleTab(boolean b) { 543 paneFolder.setSingleTab(b); 544 AbstractTabItem[] items = getItems(); 545 546 for (int i = 0; i < items.length; i++) { 547 DefaultTabItem item = (DefaultTabItem) items[i]; 548 549 item.updateTabText(); 550 } 551 552 layout(true); 553 } 554 555 560 public void setVisible(boolean visible) { 561 super.setVisible(visible); 562 getFolder().setVisible(visible); 563 } 564 565 570 public void showMinMax(boolean show) { 571 paneFolder.showMinMax(show); 572 } 573 } 574 | Popular Tags |