1 11 package org.eclipse.ui.internal.presentations.defaultpresentation; 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.PaneFolder; 40 import org.eclipse.ui.internal.presentations.PaneFolderButtonListener; 41 import org.eclipse.ui.internal.presentations.util.AbstractTabFolder; 42 import org.eclipse.ui.internal.presentations.util.AbstractTabItem; 43 import org.eclipse.ui.internal.presentations.util.PartInfo; 44 import org.eclipse.ui.internal.presentations.util.TabFolderEvent; 45 import org.eclipse.ui.internal.util.Util; 46 47 50 public class DefaultTabFolder extends AbstractTabFolder { 51 52 private PaneFolder paneFolder; 53 private Control viewToolBar; 54 private Label titleLabel; 55 56 private PaneFolderButtonListener buttonListener = new PaneFolderButtonListener() { 57 public void stateButtonPressed(int buttonId) { 58 fireEvent(TabFolderEvent.stackStateToEventId(buttonId)); 59 } 60 61 66 public void closeButtonPressed(CTabItem item) { 67 fireEvent(TabFolderEvent.EVENT_CLOSE, getTab(item)); 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, defaultColors, defaultColors}; 92 private DefaultTabFolderColors[] inactiveShellColors = {defaultColors, defaultColors, defaultColors}; 93 private boolean shellActive = false; 94 95 103 public DefaultTabFolder(Composite parent, int flags, boolean allowMin, boolean allowMax) { 104 paneFolder = new PaneFolder(parent, flags | SWT.NO_BACKGROUND); 105 paneFolder.addButtonListener(buttonListener); 106 paneFolder.setMinimizeVisible(allowMin); 107 paneFolder.setMaximizeVisible(allowMax); 108 paneFolder.getControl().addListener(SWT.Selection, selectionListener); 109 paneFolder.setTopRight(null); 110 111 { 113 ToolBar actualToolBar = new ToolBar(paneFolder.getControl(), SWT.FLAT | SWT.NO_BACKGROUND); 114 viewToolBar = actualToolBar; 115 116 ToolItem pullDownButton = new ToolItem(actualToolBar, SWT.PUSH); 117 Image hoverImage = WorkbenchImages 118 .getImage(IWorkbenchGraphicConstants.IMG_LCL_RENDERED_VIEW_MENU); 119 pullDownButton.setDisabledImage(hoverImage); 120 pullDownButton.setImage(hoverImage); 121 pullDownButton.setToolTipText(WorkbenchMessages.Menu); 122 actualToolBar.addMouseListener(new MouseAdapter() { 123 public void mouseDown(MouseEvent e) { 124 fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), getPaneMenuLocation()); 125 } 126 }); 127 pullDownButton.addSelectionListener(new SelectionAdapter() { 128 public void widgetSelected(SelectionEvent e) { 129 fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), getPaneMenuLocation()); 130 131 super.widgetSelected(e); 132 } 133 }); 134 } 135 136 { 138 titleLabel = new Label(paneFolder.getControl(), SWT.NONE); 139 titleLabel.moveAbove(null); 140 titleLabel.setVisible(false); 141 attachListeners(titleLabel, false); 142 } 143 144 attachListeners(paneFolder.getControl(), false); 145 attachListeners(paneFolder.getViewForm(), false); 146 147 paneFolder.setTabHeight(computeTabHeight()); 148 149 viewToolBar.moveAbove(null); 150 } 151 152 162 public void setMinimumCharacters(int count) { 163 paneFolder.setMinimumCharacters(count); 164 } 165 166 public void setSimpleTabs(boolean simple) { 167 paneFolder.setSimpleTab(simple); 168 } 169 170 175 protected DefaultTabItem getTab(CTabItem item) { 176 return (DefaultTabItem)item.getData(); 177 } 178 179 182 public Point computeSize(int widthHint, int heightHint) { 183 return paneFolder.computeMinimumSize(); 184 } 185 186 PaneFolder getFolder() { 187 return paneFolder; 188 } 189 190 public AbstractTabItem getSelection() { 191 return getTab(paneFolder.getSelection()); 192 } 193 194 197 public AbstractTabItem add(int index, int flags) { 198 DefaultTabItem result = new DefaultTabItem((CTabFolder)getFolder().getControl(), index, flags); 199 200 result.getWidget().setData(result); 201 202 return result; 203 } 204 205 208 public Composite getContentParent() { 209 return paneFolder.getContentParent(); 210 } 211 212 215 public void setContent(Control newContent) { 216 paneFolder.setContent(newContent); 217 } 218 219 222 public AbstractTabItem[] getItems() { 223 CTabItem[] items = paneFolder.getItems(); 224 225 AbstractTabItem[] result = new AbstractTabItem[items.length]; 226 227 for (int i = 0; i < result.length; i++) { 228 result[i] = getTab(items[i]); 229 } 230 231 return result; 232 } 233 234 237 public int getItemCount() { 238 return paneFolder.getItemCount(); 240 } 241 242 245 public void setSelection(AbstractTabItem toSelect) { 246 paneFolder.setSelection(indexOf(toSelect)); 247 } 248 249 252 public Composite getToolbarParent() { 253 return paneFolder.getControl(); 254 } 255 256 259 public Control getControl() { 260 return paneFolder.getControl(); 261 } 262 263 public void setUnselectedCloseVisible(boolean visible) { 264 paneFolder.setUnselectedCloseVisible(visible); 265 } 266 267 public void setUnselectedImageVisible(boolean visible) { 268 paneFolder.setUnselectedImageVisible(visible); 269 } 270 271 274 public Rectangle getTabArea() { 275 return Geometry.toDisplay(paneFolder.getControl(), paneFolder.getTitleArea()); 276 } 277 278 282 public void enablePaneMenu(boolean enabled) { 283 if (enabled) { 284 paneFolder.setTopRight(viewToolBar); 285 viewToolBar.setVisible(true); 286 } else { 287 paneFolder.setTopRight(null); 288 viewToolBar.setVisible(false); 289 } 290 } 291 292 295 public void setSelectedInfo(PartInfo info) { 296 String newTitle = DefaultTabItem.escapeAmpersands(info.contentDescription); 297 298 if (!Util.equals(titleLabel.getText(), newTitle)) { 299 titleLabel.setText(newTitle); 300 } 301 302 if (!info.contentDescription.equals(Util.ZERO_LENGTH_STRING)) { 303 paneFolder.setTopLeft(titleLabel); 304 titleLabel.setVisible(true); 305 } else { 306 paneFolder.setTopLeft(null); 307 titleLabel.setVisible(false); 308 } 309 } 310 311 314 public Point getPaneMenuLocation() { 315 Point toolbarSize = viewToolBar.getSize(); 316 317 return viewToolBar.toDisplay(0,toolbarSize.y); 318 } 319 320 323 public Point getPartListLocation() { 324 return paneFolder.getControl().toDisplay(paneFolder.getChevronLocation()); 325 } 326 327 330 public Point getSystemMenuLocation() { 331 Rectangle bounds = DragUtil.getDisplayBounds(paneFolder.getControl()); 332 333 int idx = paneFolder.getSelectionIndex(); 334 if (idx > -1) { 335 CTabItem item = paneFolder.getItem(idx); 336 Rectangle itemBounds = item.getBounds(); 337 338 bounds.x += itemBounds.x; 339 bounds.y += itemBounds.y; 340 } 341 342 Point location = new Point(bounds.x, bounds.y 343 + paneFolder.getTabHeight()); 344 345 return location; 346 } 347 348 351 public boolean isOnBorder(Point toTest) { 352 Control content = paneFolder.getContent(); 353 if (content != null) { 354 Rectangle displayBounds = DragUtil.getDisplayBounds(content); 355 356 if (paneFolder.getTabPosition() == SWT.TOP) { 357 return toTest.y >= displayBounds.y; 358 } 359 360 if (toTest.y >= displayBounds.y && toTest.y < displayBounds.y + displayBounds.height) { 361 return true; 362 } 363 } 364 365 return super.isOnBorder(toTest); 366 } 367 368 371 public void layout(boolean flushCache) { 372 paneFolder.layout(flushCache); 373 super.layout(flushCache); 374 } 375 376 379 public void setState(int state) { 380 paneFolder.setState(state); 381 super.setState(state); 382 } 383 384 387 public void setActive(int activeState) { 388 super.setActive(activeState); 389 updateColors(); 390 } 391 392 395 public void setTabPosition(int tabPosition) { 396 paneFolder.setTabPosition(tabPosition); 397 super.setTabPosition(tabPosition); 398 layout(true); 399 } 400 401 public void flushToolbarSize() { 402 paneFolder.flushTopCenterSize(); 403 } 404 405 408 public void setToolbar(Control toolbarControl) { 409 paneFolder.setTopCenter(toolbarControl); 410 super.setToolbar(toolbarControl); 411 } 412 413 public void setColors(DefaultTabFolderColors colors, int activationState, boolean shellActivationState) { 414 Assert.isTrue(activationState < activeShellColors.length); 415 416 if (shellActivationState) { 417 activeShellColors[activationState] = colors; 418 } else { 419 inactiveShellColors[activationState] = colors; 420 } 421 422 if (activationState == getActive() && shellActive == shellActivationState) { 423 updateColors(); 424 } 425 } 426 427 431 private void updateColors() { 432 DefaultTabFolderColors currentColors = shellActive ? 433 activeShellColors[getActive()] 434 : inactiveShellColors[getActive()]; 435 436 paneFolder.setSelectionForeground(currentColors.foreground); 437 paneFolder.setSelectionBackground(currentColors.background, currentColors.percentages, currentColors.vertical); 438 } 439 440 public void setColors(DefaultTabFolderColors colors, int activationState) { 441 setColors(colors, activationState, true); 442 setColors(colors, activationState, false); 443 } 444 445 448 public void shellActive(boolean isActive) { 449 this.shellActive = isActive; 450 super.shellActive(isActive); 451 452 updateColors(); 453 } 454 455 459 public void setFont(Font font) { 460 if (font != paneFolder.getControl().getFont()) { 461 paneFolder.getControl().setFont(font); 462 layout(true); 463 paneFolder.setTabHeight(computeTabHeight()); 464 } 465 } 466 467 470 protected int computeTabHeight() { 471 GC gc = new GC(getControl()); 472 473 int tabHeight = Math.max(viewToolBar.computeSize(SWT.DEFAULT, 475 SWT.DEFAULT).y, gc.getFontMetrics().getHeight()); 476 477 gc.dispose(); 478 479 return tabHeight; 480 } 481 482 486 public void setSingleTab(boolean b) { 487 paneFolder.setSingleTab(b); 488 AbstractTabItem[] items = getItems(); 489 490 for (int i = 0; i < items.length; i++) { 491 DefaultTabItem item = (DefaultTabItem)items[i]; 492 493 item.updateTabText(); 494 } 495 496 layout(true); 497 } 498 499 502 public void setVisible(boolean visible) { 503 super.setVisible(visible); 504 getFolder().setVisible(visible); 505 } 506 507 510 public void showMinMax(boolean show) { 511 paneFolder.showMinMax(show); 512 } 513 } 514 | Popular Tags |