1 11 package org.eclipse.ui.internal.presentations; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.jface.action.IMenuManager; 16 import org.eclipse.jface.action.MenuManager; 17 import org.eclipse.jface.preference.IPreferenceStore; 18 import org.eclipse.jface.util.Geometry; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.DisposeEvent; 21 import org.eclipse.swt.events.DisposeListener; 22 import org.eclipse.swt.events.MouseAdapter; 23 import org.eclipse.swt.events.MouseEvent; 24 import org.eclipse.swt.events.MouseListener; 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.Listener; 32 import org.eclipse.swt.widgets.Menu; 33 import org.eclipse.swt.widgets.TabFolder; 34 import org.eclipse.swt.widgets.TabItem; 35 import org.eclipse.ui.IPropertyListener; 36 import org.eclipse.ui.internal.IPreferenceConstants; 37 import org.eclipse.ui.internal.WorkbenchPlugin; 38 import org.eclipse.ui.presentations.IPresentablePart; 39 import org.eclipse.ui.presentations.IStackPresentationSite; 40 import org.eclipse.ui.presentations.PresentationUtil; 41 import org.eclipse.ui.presentations.StackDropResult; 42 import org.eclipse.ui.presentations.StackPresentation; 43 44 52 public class NativeStackPresentation extends StackPresentation { 53 54 private TabFolder tabFolder; 55 56 private Listener dragListener; 57 58 private IPresentablePart current; 59 60 private MenuManager systemMenuManager = new MenuManager(); 61 62 private static IPreferenceStore preferenceStore = WorkbenchPlugin.getDefault() 63 .getPreferenceStore(); 64 65 private static int tabPos = preferenceStore.getInt(IPreferenceConstants.VIEW_TAB_POSITION); 69 70 private final static String TAB_DATA = NativeStackPresentation.class 71 .getName() 72 + ".partId"; 74 private MouseListener mouseListener = new MouseAdapter() { 75 public void mouseDown(MouseEvent e) { 76 if (current != null) { 85 current.setFocus(); 86 } 87 } 88 }; 89 90 private Listener menuListener = new Listener() { 91 94 public void handleEvent(Event event) { 95 Point pos = new Point(event.x, event.y); 96 TabItem item = null; 98 IPresentablePart part = null; 99 if (item != null) { 100 part = getPartForTab(item); 101 } 102 showPaneMenu(part, pos); 103 } 104 }; 105 106 private Listener selectionListener = new Listener() { 107 public void handleEvent(Event e) { 108 IPresentablePart item = getPartForTab((TabItem) e.item); 109 if (item != null) { 110 getSite().selectPart(item); 111 } 113 } 114 }; 115 116 private Listener resizeListener = new Listener() { 117 public void handleEvent(Event e) { 118 setControlSize(); 119 } 120 }; 121 122 private IPropertyListener childPropertyChangeListener = new IPropertyListener() { 123 public void propertyChanged(Object source, int property) { 124 125 if (isDisposed()) { 126 return; 127 } 128 129 if (source instanceof IPresentablePart) { 130 IPresentablePart part = (IPresentablePart) source; 131 childPropertyChanged(part, property); 132 } 133 } 134 }; 135 136 private DisposeListener tabDisposeListener = new DisposeListener() { 137 public void widgetDisposed(DisposeEvent e) { 138 if (e.widget instanceof TabItem) { 139 TabItem item = (TabItem) e.widget; 140 IPresentablePart part = getPartForTab(item); 141 part.removePropertyListener(childPropertyChangeListener); 142 } 143 } 144 }; 145 146 public NativeStackPresentation(Composite parent, 147 IStackPresentationSite stackSite) { 148 super(stackSite); 149 150 tabFolder = new TabFolder(parent, tabPos); 151 152 tabFolder.addListener(SWT.Selection, selectionListener); 154 155 tabFolder.addListener(SWT.Resize, resizeListener); 157 158 tabFolder.addMouseListener(mouseListener); 160 161 tabFolder.addListener(SWT.MenuDetect, menuListener); 162 163 dragListener = new Listener() { 164 public void handleEvent(Event event) { 165 Point localPos = new Point(event.x, event.y); 166 TabItem tabUnderPointer = null; 168 169 if (tabUnderPointer == null) { 170 return; 171 } 172 173 IPresentablePart part = getPartForTab(tabUnderPointer); 174 175 if (getSite().isPartMoveable(part)) { 176 getSite().dragStart(part, tabFolder.toDisplay(localPos), 177 false); 178 } 179 } 180 }; 181 182 PresentationUtil.addDragListener(tabFolder, dragListener); 183 184 } 185 186 194 private final int indexOf(IPresentablePart part) { 195 if (part == null) { 196 return tabFolder.getItemCount(); 197 } 198 199 TabItem[] items = tabFolder.getItems(); 200 201 for (int idx = 0; idx < items.length; idx++) { 202 IPresentablePart tabPart = getPartForTab(items[idx]); 203 204 if (part == tabPart) { 205 return idx; 206 } 207 } 208 209 return items.length; 210 } 211 212 218 protected final TabItem getTab(IPresentablePart part) { 219 TabItem[] items = tabFolder.getItems(); 220 221 int idx = indexOf(part); 222 223 if (idx < items.length) { 224 return items[idx]; 225 } 226 227 return null; 228 } 229 230 234 protected void childPropertyChanged(IPresentablePart part, int property) { 235 TabItem tab = getTab(part); 236 initTab(tab, part); 237 } 238 239 protected final IPresentablePart getPartForTab(TabItem item) { 240 IPresentablePart part = (IPresentablePart) item.getData(TAB_DATA); 241 return part; 242 } 243 244 protected TabFolder getTabFolder() { 245 return tabFolder; 246 } 247 248 public boolean isDisposed() { 249 return tabFolder == null || tabFolder.isDisposed(); 250 } 251 252 255 private void setControlSize() { 256 if (current == null || tabFolder == null) { 257 return; 258 } 259 current.setBounds(calculatePageBounds(tabFolder)); 267 } 269 270 public static Rectangle calculatePageBounds(TabFolder folder) { 271 if (folder == null) { 272 return new Rectangle(0, 0, 0, 0); 273 } 274 Rectangle bounds = folder.getBounds(); 275 Rectangle offset = folder.getClientArea(); 276 bounds.x += offset.x; 277 bounds.y += offset.y; 278 bounds.width = offset.width; 279 bounds.height = offset.height; 280 return bounds; 281 } 282 283 286 public void dispose() { 287 if (isDisposed()) { 288 return; 289 } 290 PresentationUtil.removeDragListener(tabFolder, dragListener); 291 292 294 tabFolder.dispose(); 295 tabFolder = null; 296 } 297 298 private TabItem createPartTab(IPresentablePart part, int tabIndex) { 299 TabItem tabItem = new TabItem(tabFolder, SWT.NONE, tabIndex); 300 tabItem.setData(TAB_DATA, part); 301 part.addPropertyListener(childPropertyChangeListener); 302 tabItem.addDisposeListener(tabDisposeListener); 303 initTab(tabItem, part); 304 return tabItem; 305 } 306 307 316 protected void initTab(TabItem tabItem, IPresentablePart part) { 317 tabItem.setText(part.getName()); 318 tabItem.setToolTipText(part.getTitleToolTip()); 319 320 Image tabImage = part.getTitleImage(); 321 if (tabImage != tabItem.getImage()) { 322 tabItem.setImage(tabImage); 323 } 324 } 325 326 329 public void addPart(IPresentablePart newPart, Object cookie) { 330 createPartTab(newPart, tabFolder.getItemCount()); 331 } 332 333 336 public void removePart(IPresentablePart oldPart) { 337 TabItem item = getTab(oldPart); 338 if (item == null) { 339 return; 340 } 341 oldPart.setVisible(false); 342 343 item.dispose(); 344 } 345 346 349 public void selectPart(IPresentablePart toSelect) { 350 if (toSelect == current) { 351 return; 352 } 353 354 if (current != null) { 355 current.setVisible(false); 356 } 357 358 current = toSelect; 359 360 if (current != null) { 361 tabFolder.setSelection(indexOf(current)); 362 current.setVisible(true); 363 setControlSize(); 364 } 365 } 366 367 370 public void setBounds(Rectangle bounds) { 371 tabFolder.setBounds(bounds); 372 setControlSize(); 373 } 374 375 378 public Point computeMinimumSize() { 379 return Geometry.getSize(tabFolder.computeTrim(0, 0, 0, 0)); 380 } 381 382 385 public void setVisible(boolean isVisible) { 386 if (current != null) { 387 current.setVisible(isVisible); 388 } 389 tabFolder.setVisible(isVisible); 390 } 391 392 395 public void setState(int state) { 396 } 399 400 403 public IMenuManager getSystemMenuManager() { 404 return systemMenuManager; 405 } 406 407 411 protected void showPaneMenu(IPresentablePart part, Point point) { 412 systemMenuManager.update(false); 413 Menu aMenu = systemMenuManager.createContextMenu(tabFolder.getParent()); 414 aMenu.setLocation(point.x, point.y); 415 aMenu.setVisible(true); 416 } 417 418 421 public Control getControl() { 422 return tabFolder; 423 } 424 425 428 public StackDropResult dragOver(Control currentControl, Point location) { 429 430 final TabItem tabUnderPointer = null; 434 435 if (tabUnderPointer == null) { 438 return null; 439 } 440 441 return null; 444 } 445 446 449 public void showSystemMenu() { 450 452 } 453 454 457 public void showPaneMenu() { 458 460 } 461 462 465 public Control[] getTabList(IPresentablePart part) { 466 ArrayList list = new ArrayList (); 467 if (getControl() != null) { 468 list.add(getControl()); 469 } 470 if (part.getToolBar() != null) { 471 list.add(part.getToolBar()); 472 } 473 if (part.getControl() != null) { 474 list.add(part.getControl()); 475 } 476 return (Control[]) list.toArray(new Control[list.size()]); 477 } 478 479 482 public IPresentablePart getCurrentPart() { 483 return current; 484 } 485 486 489 public void setActive(int newState) { 490 491 } 492 493 } 494 | Popular Tags |