1 11 package org.eclipse.ui.internal.presentations.util; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.Plugin; 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.graphics.Point; 24 import org.eclipse.swt.graphics.Rectangle; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.ui.IMemento; 27 import org.eclipse.ui.PlatformUI; 28 import org.eclipse.ui.internal.preferences.IDynamicPropertyMap; 29 import org.eclipse.ui.internal.preferences.PreferenceStoreAdapter; 30 import org.eclipse.ui.internal.preferences.PreferencesAdapter; 31 import org.eclipse.ui.internal.preferences.PropertyMapAdapter; 32 import org.eclipse.ui.internal.preferences.ThemeManagerAdapter; 33 import org.eclipse.ui.internal.presentations.defaultpresentation.DefaultPartList; 34 import org.eclipse.ui.internal.util.PrefUtil; 35 import org.eclipse.ui.presentations.IPartMenu; 36 import org.eclipse.ui.presentations.IPresentablePart; 37 import org.eclipse.ui.presentations.IPresentationSerializer; 38 import org.eclipse.ui.presentations.IStackPresentationSite; 39 import org.eclipse.ui.presentations.StackDropResult; 40 import org.eclipse.ui.presentations.StackPresentation; 41 42 45 public final class TabbedStackPresentation extends StackPresentation { 46 47 private PresentablePartFolder folder; 48 private ISystemMenu systemMenu; 49 private ISystemMenu partList; 50 private PreferenceStoreAdapter apiPreferences = new PreferenceStoreAdapter(PrefUtil 51 .getAPIPreferenceStore()); 52 private ThemeManagerAdapter themePreferences = new ThemeManagerAdapter( 53 PlatformUI.getWorkbench().getThemeManager()); 54 55 private TabOrder tabs; 56 57 private TabDragHandler dragBehavior; 58 59 private boolean initializing = true; 60 private int ignoreSelectionChanges = 0; 61 62 private TabFolderListener tabFolderListener = new TabFolderListener() { 63 public void handleEvent(TabFolderEvent e) { 64 switch (e.type) { 65 case TabFolderEvent.EVENT_MINIMIZE: { 66 getSite().setState(IStackPresentationSite.STATE_MINIMIZED); 67 break; 68 } 69 case TabFolderEvent.EVENT_MAXIMIZE: { 70 getSite().setState(IStackPresentationSite.STATE_MAXIMIZED); 71 break; 72 } 73 case TabFolderEvent.EVENT_RESTORE: { 74 getSite().setState(IStackPresentationSite.STATE_RESTORED); 75 break; 76 } 77 case TabFolderEvent.EVENT_CLOSE: { 78 IPresentablePart part = folder.getPartForTab(e.tab); 79 80 if (part != null) { 81 getSite().close(new IPresentablePart[] { part }); 82 } 83 break; 84 } 85 case TabFolderEvent.EVENT_SHOW_LIST: { 86 showPartList(); 87 break; 88 } 89 case TabFolderEvent.EVENT_GIVE_FOCUS_TO_PART: { 90 IPresentablePart part = getSite().getSelectedPart(); 91 if (part != null) { 92 part.setFocus(); 93 } 94 break; 95 } 96 case TabFolderEvent.EVENT_PANE_MENU: { 97 IPresentablePart part = getSite().getSelectedPart(); 98 if (part != null) { 99 part.setFocus(); 100 } 101 TabbedStackPresentation.this.showPaneMenu(folder 102 .getPartForTab(e.tab), new Point(e.x, e.y)); 103 break; 104 } 105 case TabFolderEvent.EVENT_DRAG_START: { 106 AbstractTabItem beingDragged = e.tab; 107 Point initialLocation = new Point(e.x, e.y); 108 109 if (beingDragged == null) { 110 getSite().dragStart(initialLocation, false); 111 } else { 112 IPresentablePart part = folder.getPartForTab(beingDragged); 113 114 try { 115 dragStart = folder.indexOf(part); 116 getSite().dragStart(part, initialLocation, false); 117 } finally { 118 dragStart = -1; 119 } 120 } 121 break; 122 } 123 case TabFolderEvent.EVENT_TAB_SELECTED: { 124 if (ignoreSelectionChanges > 0) { 125 return; 126 } 127 128 IPresentablePart part = folder.getPartForTab(e.tab); 129 130 if (part != null) { 131 getSite().selectPart(part); 132 } 133 break; 134 } 135 case TabFolderEvent.EVENT_SYSTEM_MENU: { 136 IPresentablePart part = folder.getPartForTab(e.tab); 137 138 if (part == null) { 139 part = getSite().getSelectedPart(); 140 } 141 142 if (part != null) { 143 showSystemMenu(new Point(e.x, e.y), part); 144 } 145 break; 146 } 147 148 } 149 } 150 }; 151 152 private int dragStart = -1; 153 private Map prefs = new HashMap (); 154 155 public TabbedStackPresentation(IStackPresentationSite site, AbstractTabFolder widget, ISystemMenu systemMenu) { 156 this(site, new PresentablePartFolder(widget), systemMenu); 157 } 158 159 public TabbedStackPresentation(IStackPresentationSite site, PresentablePartFolder folder, ISystemMenu systemMenu) { 160 this(site, folder, new LeftToRightTabOrder(folder), new ReplaceDragHandler(folder.getTabFolder()), systemMenu); 161 } 162 163 public TabbedStackPresentation(IStackPresentationSite site, 164 PresentablePartFolder newFolder, TabOrder tabs, TabDragHandler dragBehavior, ISystemMenu systemMenu) { 165 super(site); 166 this.systemMenu = systemMenu; 167 168 this.folder = newFolder; 169 this.tabs = tabs; 170 this.dragBehavior = dragBehavior; 171 172 folder.getTabFolder().getControl().addDisposeListener(new DisposeListener() { 175 public void widgetDisposed(DisposeEvent e) { 176 presentationDisposed(); 177 } 178 }); 179 180 folder.getTabFolder().addListener(tabFolderListener); 181 182 this.partList = new DefaultPartList(site, newFolder); 183 } 184 185 191 public void restoreState(IPresentationSerializer serializer, 192 IMemento savedState) { 193 tabs.restoreState(serializer, savedState); 194 } 195 196 199 public void saveState(IPresentationSerializer context, IMemento memento) { 200 super.saveState(context, memento); 201 202 tabs.saveState(context, memento); 203 } 204 205 210 private boolean isDisposed() { 211 return folder == null || folder.isDisposed(); 212 } 213 214 217 public void setBounds(Rectangle bounds) { 218 folder.setBounds(bounds); 219 } 220 221 224 public Point computeMinimumSize() { 225 return folder.getTabFolder().computeSize(SWT.DEFAULT, SWT.DEFAULT); 226 } 227 228 231 public int computePreferredSize(boolean width, int availableParallel, 232 int availablePerpendicular, int preferredResult) { 233 234 if (preferredResult != INFINITE || getSite().getState() == IStackPresentationSite.STATE_MINIMIZED) { 235 int minSize = 0; 236 if (width) { 237 int heightHint = availablePerpendicular == INFINITE ? SWT.DEFAULT : availablePerpendicular; 238 minSize = folder.getTabFolder().computeSize(SWT.DEFAULT, heightHint).x; 239 } else { 240 int widthHint = availablePerpendicular == INFINITE ? SWT.DEFAULT : availablePerpendicular; 241 minSize = folder.getTabFolder().computeSize(widthHint, SWT.DEFAULT).y; 242 } 243 244 if (getSite().getState() == IStackPresentationSite.STATE_MINIMIZED) { 245 return minSize; 246 } 247 248 return Math.max(minSize, preferredResult); 249 } 250 251 return INFINITE; 252 } 253 254 257 public void showPartList() { 258 if (partList != null) { 259 final int numberOfParts = folder.getTabFolder().getItemCount(); 260 if (numberOfParts > 0) { 261 partList.show(getControl(), folder.getTabFolder() 262 .getPartListLocation(), getSite().getSelectedPart()); 263 } 264 } 265 } 266 267 270 public void dispose() { 271 folder.getTabFolder().getControl().dispose(); 273 } 274 275 278 private void presentationDisposed() { 279 apiPreferences.dispose(); 280 themePreferences.dispose(); 281 282 Iterator iter = prefs.values().iterator(); 283 while(iter.hasNext()) { 284 PropertyMapAdapter next = (PropertyMapAdapter)iter.next(); 285 next.dispose(); 286 } 287 288 if (systemMenu != null) { 289 systemMenu.dispose(); 290 } 291 292 if (partList != null) { 293 partList.dispose(); 294 } 295 296 systemMenu = null; 297 partList = null; 298 299 } 300 301 304 public void setActive(int newState) { 305 folder.getTabFolder().setActive(newState); 306 } 307 308 311 public void setVisible(boolean isVisible) { 312 IPresentablePart current = getSite().getSelectedPart(); 313 if (current != null) { 314 current.setVisible(isVisible); 315 } 316 317 folder.setVisible(isVisible); 318 } 319 320 323 public void setState(int state) { 324 folder.getTabFolder().setState(state); 325 } 326 327 330 public Control getControl() { 331 return folder.getTabFolder().getControl(); 332 } 333 334 337 public AbstractTabFolder getTabFolder() { 338 return folder.getTabFolder(); 339 } 340 341 344 public void addPart(IPresentablePart newPart, Object cookie) { 345 ignoreSelectionChanges++; 346 try { 347 if (initializing) { 348 tabs.addInitial(newPart); 349 } else { 350 if (cookie == null) { 351 tabs.add(newPart); 352 } else { 353 int insertionPoint = dragBehavior 354 .getInsertionPosition(cookie); 355 356 tabs.insert(newPart, insertionPoint); 357 } 358 } 359 } finally { 360 ignoreSelectionChanges--; 361 } 362 } 363 364 367 public void movePart(IPresentablePart toMove, Object cookie) { 368 ignoreSelectionChanges++; 369 try { 370 int insertionPoint = dragBehavior.getInsertionPosition(cookie); 371 372 if (insertionPoint == folder.indexOf(toMove)) { 373 return; 374 } 375 376 tabs.move(toMove, insertionPoint); 377 } finally { 378 ignoreSelectionChanges--; 379 } 380 } 381 382 385 public void removePart(IPresentablePart oldPart) { 386 ignoreSelectionChanges++; 387 try { 388 tabs.remove(oldPart); 389 } finally { 390 ignoreSelectionChanges--; 391 } 392 } 393 394 397 public void selectPart(IPresentablePart toSelect) { 398 initializing = false; 399 400 tabs.select(toSelect); 401 } 402 403 406 public StackDropResult dragOver(Control currentControl, Point location) { 407 return dragBehavior.dragOver(currentControl, location, dragStart); 408 } 409 410 414 public void showSystemMenu() { 415 showSystemMenu(folder.getTabFolder().getSystemMenuLocation(), getSite().getSelectedPart()); 416 } 417 418 public void showSystemMenu(Point displayCoordinates, IPresentablePart context) { 419 if (context != getSite().getSelectedPart()) { 420 getSite().selectPart(context); 421 } 422 systemMenu.show(getControl(), displayCoordinates, context); 423 } 424 425 428 public void showPaneMenu() { 429 IPresentablePart part = getSite().getSelectedPart(); 430 431 if (part != null) { 432 showPaneMenu(part, folder.getTabFolder().getPaneMenuLocation()); 433 } 434 } 435 436 public void showPaneMenu(IPresentablePart part, Point location) { 437 Assert.isTrue(!isDisposed()); 438 439 IPartMenu menu = part.getMenu(); 440 441 if (menu != null) { 442 menu.showMenu(location); 443 } 444 } 445 446 449 public Control[] getTabList(IPresentablePart part) { 450 ArrayList list = new ArrayList (); 451 if (folder.getTabFolder().getTabPosition() == SWT.BOTTOM) { 452 if (part.getControl() != null) { 453 list.add(part.getControl()); 454 } 455 } 456 457 list.add(folder.getTabFolder().getControl()); 458 459 if (part.getToolBar() != null) { 460 list.add(part.getToolBar()); 461 } 462 463 if (folder.getTabFolder().getTabPosition() == SWT.TOP) { 464 if (part.getControl() != null) { 465 list.add(part.getControl()); 466 } 467 } 468 469 return (Control[]) list.toArray(new Control[list.size()]); 470 } 471 472 public void setPartList(ISystemMenu menu) { 473 this.partList = menu; 474 } 475 476 public IDynamicPropertyMap getTheme() { 477 return themePreferences; 478 } 479 480 public IDynamicPropertyMap getApiPreferences() { 481 return apiPreferences; 482 } 483 484 public IDynamicPropertyMap getPluginPreferences(Plugin toQuery) { 485 String id = toQuery.getBundle().getSymbolicName(); 486 IDynamicPropertyMap result = (IDynamicPropertyMap)prefs.get(id); 487 488 if (result != null) { 489 return result; 490 } 491 492 result = new PreferencesAdapter(toQuery.getPluginPreferences()); 493 prefs.put(id, result); 494 return result; 495 } 496 497 503 public void moveTab(IPresentablePart part, int index) { 504 tabs.move(part, index); 505 folder.layout(true); 506 } 507 508 513 public IPresentablePart[] getPartList() { 514 return tabs.getPartList(); 515 } 516 517 525 public void showMinMax(boolean show) { 526 folder.getTabFolder().showMinMax(show); 527 } 528 } 529 | Popular Tags |