KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > R21BasicStackPresentation


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * Gunnar Wagenknecht - some contributions (bug fixes and enhancements)
11  *******************************************************************************/

12 package org.eclipse.ui.internal.presentations;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.jface.action.GroupMarker;
20 import org.eclipse.jface.action.IMenuManager;
21 import org.eclipse.jface.action.MenuManager;
22 import org.eclipse.jface.action.Separator;
23 import org.eclipse.jface.util.Geometry;
24 import org.eclipse.jface.window.Window;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.custom.CLabel;
27 import org.eclipse.swt.events.DisposeEvent;
28 import org.eclipse.swt.events.DisposeListener;
29 import org.eclipse.swt.events.MouseAdapter;
30 import org.eclipse.swt.events.MouseEvent;
31 import org.eclipse.swt.events.MouseListener;
32 import org.eclipse.swt.events.SelectionAdapter;
33 import org.eclipse.swt.events.SelectionEvent;
34 import org.eclipse.swt.events.ShellAdapter;
35 import org.eclipse.swt.events.ShellEvent;
36 import org.eclipse.swt.graphics.Color;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.graphics.Point;
39 import org.eclipse.swt.graphics.Rectangle;
40 import org.eclipse.swt.widgets.Control;
41 import org.eclipse.swt.widgets.Event;
42 import org.eclipse.swt.widgets.Listener;
43 import org.eclipse.swt.widgets.Menu;
44 import org.eclipse.swt.widgets.Shell;
45 import org.eclipse.swt.widgets.ToolBar;
46 import org.eclipse.swt.widgets.ToolItem;
47 import org.eclipse.ui.IMemento;
48 import org.eclipse.ui.IPropertyListener;
49 import org.eclipse.ui.internal.IWorkbenchConstants;
50 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
51 import org.eclipse.ui.internal.WorkbenchImages;
52 import org.eclipse.ui.internal.dnd.DragUtil;
53 import org.eclipse.ui.internal.presentations.r21.R21Colors;
54 import org.eclipse.ui.internal.presentations.r21.R21PresentationMessages;
55 import org.eclipse.ui.internal.presentations.r21.widgets.CTabItem;
56 import org.eclipse.ui.internal.presentations.r21.widgets.R21PaneFolder;
57 import org.eclipse.ui.internal.util.Util;
58 import org.eclipse.ui.presentations.IPartMenu;
59 import org.eclipse.ui.presentations.IPresentablePart;
60 import org.eclipse.ui.presentations.IPresentationSerializer;
61 import org.eclipse.ui.presentations.IStackPresentationSite;
62 import org.eclipse.ui.presentations.PresentationUtil;
63 import org.eclipse.ui.presentations.StackDropResult;
64 import org.eclipse.ui.presentations.StackPresentation;
65
66 /**
67  * Base class for StackPresentations that display IPresentableParts in a
68  * CTabFolder.
69  *
70  * @since 3.0
71  */

72 public class R21BasicStackPresentation extends StackPresentation {
73
74     private R21PaneFolder paneFolder;
75
76     private IPresentablePart current;
77
78     private boolean activeState = false;
79
80     private MenuManager systemMenuManager = new MenuManager();
81
82     private CLabel titleLabel;
83
84     private boolean shellActive = true;
85
86     private final static String JavaDoc TAB_DATA = R21BasicStackPresentation.class
87             .getName()
88             + ".partId"; //$NON-NLS-1$
89

90     // private PaneFolderButtonListener buttonListener = new
91
// PaneFolderButtonListener() {
92
// public void stateButtonPressed(int buttonId) {
93
// getSite().setState(buttonId);
94
// }
95
//
96
// public void closeButtonPressed(CTabItem item) {
97
// IPresentablePart part = getPartForTab(item);
98
//
99
// getSite().close(part);
100
// }
101
// };
102
//
103
private MouseListener mouseListener = new MouseAdapter() {
104         public void mouseDown(MouseEvent e) {
105             if (e.widget instanceof Control) {
106                 Control ctrl = (Control) e.widget;
107                 Point globalPos = ctrl.toDisplay(new Point(e.x, e.y));
108
109                 // PR#1GDEZ25 - If selection will change in mouse up ignore
110
// mouse down.
111
// Else, set focus.
112
CTabItem newItem = paneFolder.getItem(paneFolder.getControl()
113                         .toControl(globalPos));
114                 if (newItem != null) {
115                     CTabItem oldItem = paneFolder.getSelection();
116                     if (newItem != oldItem) {
117                         return;
118                     }
119                 }
120                 if (current != null) {
121                     current.setFocus();
122                 }
123             }
124         }
125
126         public void mouseDoubleClick(MouseEvent e) {
127             if (getSite().getState() == IStackPresentationSite.STATE_MAXIMIZED) {
128                 getSite().setState(IStackPresentationSite.STATE_RESTORED);
129             } else {
130                 getSite().setState(IStackPresentationSite.STATE_MAXIMIZED);
131             }
132         }
133     };
134
135     private MouseListener titleMouseListener = new MouseAdapter() {
136         public void mouseDown(MouseEvent e) {
137             if (e.widget instanceof Control) {
138                 Control ctrl = (Control) e.widget;
139                 Point globalPos = ctrl.toDisplay(new Point(0, titleLabel
140                         .getBounds().height));
141
142                 if ((e.button == 1) && overImage(e.x)) {
143                     showSystemMenu(globalPos);
144                 }
145             }
146         }
147     };
148
149     private Listener menuListener = new Listener() {
150         /*
151          * (non-Javadoc)
152          *
153          * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
154          */

155         public void handleEvent(Event event) {
156             Point pos = new Point(event.x, event.y);
157
158             showSystemMenu(pos);
159         }
160     };
161
162     private Listener dragListener = new Listener() {
163         public void handleEvent(Event event) {
164
165             Point localPos = new Point(event.x, event.y);
166             CTabItem tabUnderPointer = paneFolder.getItem(localPos);
167
168             // Drags on the title area drag the selected part only
169
if (tabUnderPointer == null) {
170                 if (paneFolder.getTabPosition() == SWT.BOTTOM
171                         && localPos.y < paneFolder.getControl().getBounds().height
172                                 - paneFolder.getTabHeight()) {
173                     tabUnderPointer = paneFolder.getSelection();
174                 } else if (paneFolder.getTabPosition() == SWT.TOP
175                         && localPos.y > paneFolder.getTabHeight()) {
176                     tabUnderPointer = paneFolder.getSelection();
177                 }
178             }
179
180             // Not in a tab, not in a title area, must be dragging the whole
181
// stack
182
if (tabUnderPointer == null) {
183                 getSite().dragStart(
184                         paneFolder.getControl().toDisplay(localPos), false);
185                 return;
186             }
187
188             IPresentablePart part = getPartForTab(tabUnderPointer);
189
190             if (getSite().isPartMoveable(part)) {
191                 getSite().dragStart(part,
192                         paneFolder.getControl().toDisplay(localPos), false);
193             }
194         }
195     };
196
197     private Listener selectionListener = new Listener() {
198         public void handleEvent(Event e) {
199             IPresentablePart item = getPartForTab((CTabItem) e.item);
200
201             if (item != null) {
202                 getSite().selectPart(item);
203             }
204         }
205     };
206
207     private Listener resizeListener = new Listener() {
208         public void handleEvent(Event e) {
209             setControlSize();
210         }
211     };
212
213     private IPropertyListener childPropertyChangeListener = new IPropertyListener() {
214         public void propertyChanged(Object JavaDoc source, int property) {
215             if (source instanceof IPresentablePart) {
216                 IPresentablePart part = (IPresentablePart) source;
217                 childPropertyChanged(part, property);
218             }
219         }
220     };
221
222     private DisposeListener tabDisposeListener = new DisposeListener() {
223         public void widgetDisposed(DisposeEvent e) {
224             if (e.widget instanceof CTabItem) {
225                 CTabItem item = (CTabItem) e.widget;
226
227                 IPresentablePart part = getPartForTab(item);
228
229                 part.removePropertyListener(childPropertyChangeListener);
230             }
231         }
232     };
233
234     /** the shell listener for upgrading the gradient */
235     private ShellAdapter shellListener = new ShellAdapter() {
236
237         public void shellActivated(ShellEvent event) {
238             shellActive = true;
239             updateGradient();
240         }
241
242         public void shellDeactivated(ShellEvent event) {
243             shellActive = false;
244             updateGradient();
245         }
246     };
247
248     private ToolBar viewToolBar;
249
250     private ToolItem pullDownButton;
251
252     private ToolItem closeButton;
253
254     public R21BasicStackPresentation(R21PaneFolder control,
255             IStackPresentationSite stackSite) {
256         super(stackSite);
257         paneFolder = control;
258
259         shellActive = paneFolder.getControl().getShell().equals(
260                 control.getControl().getDisplay().getActiveShell());
261
262         // tabFolder.setMinimizeVisible(stackSite.supportsState(IStackPresentationSite.STATE_MINIMIZED));
263
// tabFolder.setMaximizeVisible(stackSite.supportsState(IStackPresentationSite.STATE_MAXIMIZED));
264
//
265
titleLabel = new CLabel(paneFolder.getControl(), SWT.SHADOW_NONE);
266         titleLabel.setVisible(false);
267         titleLabel.moveAbove(null);
268         titleLabel.addMouseListener(titleMouseListener);
269         titleLabel.addMouseListener(mouseListener);
270         titleLabel.addListener(SWT.MenuDetect, menuListener);
271         PresentationUtil.addDragListener(titleLabel, dragListener);
272
273         // ColorSchemeService.setViewTitleFont(this, titleLabel);
274

275         viewToolBar = new ToolBar(control.getControl(), SWT.HORIZONTAL
276                 | SWT.FLAT);
277         viewToolBar.moveAbove(null);
278
279         pullDownButton = new ToolItem(viewToolBar, SWT.PUSH);
280         // Image img =
281
// WorkbenchImages.getImage(IWorkbenchGraphicConstants.IMG_LCL_VIEW_MENU);
282
Image hoverImage = WorkbenchImages
283                 .getImage(IWorkbenchGraphicConstants.IMG_LCL_VIEW_MENU);
284         pullDownButton.setDisabledImage(null); // TODO: comment this out?
285
// PR#1GE56QT - Avoid creation of unnecessary image.
286
pullDownButton.setImage(hoverImage);
287         pullDownButton.setToolTipText(R21PresentationMessages
288                 .getString("BasicStackPresentation.menu.tooltip")); //$NON-NLS-1$
289
pullDownButton.addSelectionListener(new SelectionAdapter() {
290             public void widgetSelected(SelectionEvent e) {
291                 showPaneMenu();
292             }
293         });
294
295         // listener to switch between visible tabItems
296
paneFolder.getControl().addListener(SWT.Selection, selectionListener);
297
298         // listener to resize visible components
299
paneFolder.getControl().addListener(SWT.Resize, resizeListener);
300
301         // listen for mouse down on tab to set focus.
302
paneFolder.getControl().addMouseListener(mouseListener);
303
304         paneFolder.getControl().addListener(SWT.MenuDetect, menuListener);
305
306         // tabFolder.addButtonListener(buttonListener);
307

308         PresentationUtil.addDragListener(paneFolder.getControl(), dragListener);
309
310         // add the shell listener to track shell activations
311
// TODO: check if workaround can be removed (see bug 55458)
312
paneFolder.getControl().getShell().addShellListener(shellListener);
313
314         // Uncomment to allow dragging from the title label
315
// PresentationUtil.addDragListener(titleLabel, new Listener() {
316
// public void handleEvent(Event event) {
317
// if (layout.isTrimOnTop()) {
318
// Point localPos = new Point(event.x, event.y);
319
// getSite().dragStart(titleLabel.toDisplay(localPos), false);
320
// }
321
// }
322
// });
323

324         // // Compute the tab height
325
// int tabHeight = viewToolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT).y;
326
//
327
// // Enforce a minimum tab height
328
// if (tabHeight < 20) {
329
// tabHeight = 20;
330
// }
331
// paneFolder.setTabHeight(tabHeight);
332
//
333
populateSystemMenu(systemMenuManager);
334     }
335
336     /*
337      * Return true if <code>x</code> is over the label image.
338      */

339     private boolean overImage(int x) {
340         return x < titleLabel.getImage().getBounds().width;
341     }
342
343     /**
344      * @param systemMenuManager
345      */

346     private void populateSystemMenu(IMenuManager systemMenuManager) {
347
348         systemMenuManager.add(new GroupMarker("misc")); //$NON-NLS-1$
349
systemMenuManager.add(new GroupMarker("restore")); //$NON-NLS-1$
350
systemMenuManager.add(new UpdatingActionContributionItem(
351                 new SystemMenuRestore(getSite())));
352
353         systemMenuManager.add(new SystemMenuMove(getSite(), getPaneName()));
354         systemMenuManager.add(new GroupMarker("size")); //$NON-NLS-1$
355
systemMenuManager.add(new GroupMarker("state")); //$NON-NLS-1$
356
systemMenuManager.add(new UpdatingActionContributionItem(
357                 new SystemMenuMinimize(getSite())));
358
359         systemMenuManager.add(new UpdatingActionContributionItem(
360                 new SystemMenuMaximize(getSite())));
361         systemMenuManager.add(new Separator("close")); //$NON-NLS-1$
362
systemMenuManager.add(new UpdatingActionContributionItem(
363                 new SystemMenuClose(getSite())));
364
365         getSite().addSystemActions(systemMenuManager);
366     }
367
368     protected String JavaDoc getPaneName() {
369         return R21PresentationMessages.getString("BasicStackPresentation.pane"); //$NON-NLS-1$
370
}
371
372     /**
373      * Displays the view menu as a popup
374      */

375     public void showPaneMenu() {
376         IPartMenu menu = getPartMenu();
377
378         if (menu != null) {
379             Rectangle bounds = DragUtil.getDisplayBounds(viewToolBar);
380             menu.showMenu(new Point(bounds.x, bounds.y + bounds.height));
381         }
382     }
383
384     /**
385      * Returns the currently selected part, or <code>null</code>.
386      *
387      * @return the currently selected part, or <code>null</code>
388      */

389     protected IPresentablePart getCurrent() {
390         return current;
391     }
392
393     /**
394      * Returns the index of the tab for the given part, or returns
395      * tabFolder.getItemCount() if there is no such tab.
396      *
397      * @param part
398      * part being searched for
399      * @return the index of the tab for the given part, or the number of tabs if
400      * there is no such tab
401      */

402     private final int indexOf(IPresentablePart part) {
403         if (part == null) {
404             return paneFolder.getItemCount();
405         }
406
407         CTabItem[] items = paneFolder.getItems();
408
409         for (int idx = 0; idx < items.length; idx++) {
410             IPresentablePart tabPart = getPartForTab(items[idx]);
411
412             if (part == tabPart) {
413                 return idx;
414             }
415         }
416
417         return items.length;
418     }
419
420     /**
421      * Returns the tab for the given part, or null if there is no such tab
422      *
423      * @param part
424      * the part being searched for
425      * @return the tab for the given part, or null if there is no such tab
426      */

427     protected final CTabItem getTab(IPresentablePart part) {
428         CTabItem[] items = paneFolder.getItems();
429
430         int idx = indexOf(part);
431
432         if (idx < items.length) {
433             return items[idx];
434         }
435
436         return null;
437     }
438
439     /**
440      * @param part
441      * @param property
442      */

443     protected void childPropertyChanged(IPresentablePart part, int property) {
444
445         CTabItem tab = getTab(part);
446         initTab(tab, part);
447
448         switch (property) {
449         case IPresentablePart.PROP_BUSY:
450             break;
451         case IPresentablePart.PROP_HIGHLIGHT_IF_BACK:
452             // FontRegistry registry =
453
// PlatformUI.getWorkbench().
454
// getThemeManager().getCurrentTheme().
455
// getFontRegistry();
456
//
457
// if(!getCurrent().equals(part))//Set bold if it does currently
458
// have focus
459
// tab.setFont(registry.getBold(IWorkbenchThemeConstants.TAB_TEXT_FONT));
460
// break;
461
case IPresentablePart.PROP_TOOLBAR:
462         case IPresentablePart.PROP_PANE_MENU:
463         case IPresentablePart.PROP_TITLE:
464             setControlSize();
465             break;
466         }
467     }
468
469     protected final IPresentablePart getPartForTab(CTabItem item) {
470         IPresentablePart part = (IPresentablePart) item.getData(TAB_DATA);
471
472         return part;
473     }
474
475     /**
476      * Returns the underlying tab folder for this presentation.
477      *
478      * @return
479      */

480     protected R21PaneFolder getPaneFolder() {
481         return paneFolder;
482     }
483
484     /**
485      * Returns true iff the underlying tab folder has been disposed.
486      *
487      * @return
488      */

489     public boolean isDisposed() {
490         return paneFolder == null || paneFolder.isDisposed();
491     }
492
493     /**
494      * Update the tab folder's colours to match the current theme settings and
495      * active state
496      */

497     protected void updateGradient() {
498
499         if (isDisposed()) {
500             return;
501         }
502
503         Color fgColor;
504         Color[] bgColors;
505         int[] bgPercents;
506         boolean vertical = false;
507         if (isActive()) {
508             if (getShellActivated()) {
509                 fgColor = R21Colors.getSystemColor(SWT.COLOR_TITLE_FOREGROUND);
510                 bgColors = R21Colors.getActiveViewGradient();
511                 bgPercents = R21Colors.getActiveViewGradientPercents();
512             } else {
513                 fgColor = R21Colors
514                         .getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND);
515                 bgColors = R21Colors.getDeactivatedViewGradient();
516                 bgPercents = R21Colors.getDeactivatedViewGradientPercents();
517             }
518
519         } else {
520             fgColor = R21Colors.getSystemColor(SWT.COLOR_LIST_FOREGROUND);
521             bgColors = null;
522             bgPercents = null;
523         }
524
525         drawGradient(fgColor, bgColors, bgPercents, vertical);
526
527         // Color fgColor;
528
// ITheme currentTheme =
529
// PlatformUI.getWorkbench().getThemeManager().getCurrentTheme();
530
// FontRegistry fontRegistry = currentTheme.getFontRegistry();
531
// ColorRegistry colorRegistry = currentTheme.getColorRegistry();
532
// Color [] bgColors = new Color[2];
533
// int [] percent = new int[1];
534
// boolean vertical;
535

536         // if (isActive()){
537
//
538
// CTabItem item = getPaneFolder().getSelection();
539
// if(item != null && !getPartForTab(item).isBusy()){
540
// Font tabFont =
541
// fontRegistry.get(IWorkbenchThemeConstants.TAB_TEXT_FONT);
542
// // item.setFont(tabFont);
543
// }
544
//
545
// fgColor =
546
// colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_TAB_TEXT_COLOR);
547
// bgColors[0] =
548
// colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_START);
549
// bgColors[1] =
550
// colorRegistry.get(IWorkbenchThemeConstants.ACTIVE_TAB_BG_END);
551
// percent[0] =
552
// currentTheme.getInt(IWorkbenchThemeConstants.ACTIVE_TAB_PERCENT);
553
// vertical =
554
// currentTheme.getBoolean(IWorkbenchThemeConstants.ACTIVE_TAB_VERTICAL);
555
// } else {
556
// fgColor =
557
// colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_TEXT_COLOR);
558
// bgColors[0] =
559
// colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_START);
560
// bgColors[1] =
561
// colorRegistry.get(IWorkbenchThemeConstants.INACTIVE_TAB_BG_END);
562
// percent[0] =
563
// currentTheme.getInt(IWorkbenchThemeConstants.INACTIVE_TAB_PERCENT);
564
// vertical =
565
// currentTheme.getBoolean(IWorkbenchThemeConstants.INACTIVE_TAB_VERTICAL);
566
// }
567
//
568
//
569
// drawGradient(fgColor, bgColors, bgPercents, false);
570
}
571
572     /**
573      * Draws the applicable gradient on the title area
574      *
575      * @param fgColor
576      * @param bgColors
577      * @param percentages
578      * @param vertical
579      */

580     public void drawGradient(Color fgColor, Color[] bgColors,
581             int[] percentages, boolean vertical) {
582         // paneFolder.setSelectionForeground(fgColor);
583
// paneFolder.setSelectionBackground(bgColors, percentages, vertical);
584

585         if (titleLabel == null || viewToolBar == null) {
586             return;
587         }
588
589         titleLabel.setBackground(bgColors, percentages, vertical);
590         titleLabel.setForeground(fgColor);
591
592         titleLabel.update();
593     }
594
595     public boolean isActive() {
596         return activeState;
597     }
598
599     /**
600      * Set the size of a page in the folder.
601      *
602      * TODO: Kim here...I had to make this public so that the when the font was
603      * updated via the color scheme service it could relayout the
604      * presentation... calling control.getLayout() doesn't do the trick.
605      */

606     public void setControlSize() {
607         // Set up the top-right controls
608
// List topRight = new ArrayList(3);
609

610         if (current != null) {
611             paneFolder.setTopLeft(titleLabel);
612             titleLabel.setText(current.getTitle());
613             titleLabel.setImage(current.getTitleImage());
614             titleLabel.setVisible(true);
615
616             // set tooltip (https://bugs.eclipse.org/bugs/show_bug.cgi?id=67513)
617
String JavaDoc toolTipText = current.getTitleToolTip();
618             titleLabel.setToolTipText(toolTipText
619                     .equals(Util.ZERO_LENGTH_STRING) ? null : toolTipText);
620             
621         }
622
623         Control currentToolbar = getCurrentToolbar();
624         paneFolder.setTopCenter(currentToolbar);
625
626         IPartMenu partMenu = getPartMenu();
627
628         if (partMenu != null) {
629             pullDownButton.setEnabled(true);
630         } else {
631             pullDownButton.setEnabled(false);
632         }
633         paneFolder.setTopRight(viewToolBar);
634         viewToolBar.setVisible(true);
635
636         paneFolder.layout(true);
637
638         if (current != null) {
639             Rectangle clientArea = paneFolder.getClientArea();
640             Rectangle bounds = paneFolder.getControl().getBounds();
641             clientArea.x += bounds.x;
642             clientArea.y += bounds.y;
643
644             current.setBounds(clientArea);
645         }
646
647     }
648
649     /**
650      * Returns the IPartMenu for the currently selected part, or null if the
651      * current part does not have a menu.
652      *
653      * @return the IPartMenu for the currently selected part or null if none
654      */

655     protected IPartMenu getPartMenu() {
656         IPresentablePart part = getCurrentPart();
657         if (part == null) {
658             return null;
659         }
660
661         return part.getMenu();
662     }
663
664     /*
665      * (non-Javadoc)
666      *
667      * @see org.eclipse.ui.internal.skins.Presentation#dispose()
668      */

669     public void dispose() {
670         if (isDisposed()) {
671             return;
672         }
673
674         // remove shell listener
675
paneFolder.getControl().getShell().removeShellListener(shellListener);
676
677         PresentationUtil.removeDragListener(paneFolder.getControl(),
678                 dragListener);
679         PresentationUtil.removeDragListener(titleLabel, dragListener);
680
681         systemMenuManager.dispose();
682         systemMenuManager.removeAll();
683         paneFolder.getControl().dispose();
684         paneFolder = null;
685
686         titleLabel.dispose();
687         titleLabel = null;
688
689         viewToolBar.dispose();
690     }
691
692     /*
693      * (non-Javadoc)
694      *
695      * @see org.eclipse.ui.internal.skins.Presentation#setActive(boolean)
696      */

697     public void setActive(boolean isActive) {
698         activeState = isActive;
699         updateGradient();
700     }
701
702     /**
703      * Return whether the window's shell is activated
704      */

705     /* package */boolean getShellActivated() {
706         return shellActive;
707     }
708
709     /**
710      * Returns the top level window.
711      */

712     public Window getWindow() {
713         Control ctrl = getControl();
714         if (ctrl != null) {
715             Object JavaDoc data = ctrl.getShell().getData();
716             if (data instanceof Window) {
717                 return (Window) data;
718             }
719         }
720         return null;
721     }
722
723     private CTabItem createPartTab(IPresentablePart part, int tabIndex) {
724         CTabItem tabItem;
725
726         int style = SWT.NONE;
727
728         if (getSite().isCloseable(part)) {
729             style |= SWT.CLOSE;
730         }
731
732         tabItem = paneFolder.createItem(style, tabIndex);
733
734         tabItem.setData(TAB_DATA, part);
735
736         part.addPropertyListener(childPropertyChangeListener);
737         tabItem.addDisposeListener(tabDisposeListener);
738
739         initTab(tabItem, part);
740
741         return tabItem;
742     }
743
744     // Create a close button in the title bar for the argument part (if needed).
745
private void updateCloseButton() {
746         // remove the close button if needed
747
if (current == null || !getSite().isCloseable(current)) {
748             if (closeButton != null) {
749                 closeButton.dispose();
750                 closeButton = null;
751
752                 paneFolder.flush();
753             }
754             return;
755         }
756
757         // a close button is needed, so if its already there, we're done
758
if (closeButton != null) {
759             return;
760         }
761
762         // otherwise create it
763
closeButton = new ToolItem(viewToolBar, SWT.PUSH);
764         closeButton.setDisabledImage(null);
765         closeButton.setImage(WorkbenchImages
766                 .getImage(IWorkbenchGraphicConstants.IMG_LCL_CLOSE_VIEW));
767         closeButton.setToolTipText(R21PresentationMessages
768                 .getString("BasicStackPresentation.close.tooltip")); //$NON-NLS-1$
769
closeButton.addSelectionListener(new SelectionAdapter() {
770             public void widgetSelected(SelectionEvent e) {
771                 close(getCurrent());
772             }
773         });
774
775         paneFolder.flush();
776     }
777
778     /**
779      * Initializes a tab for the given part. Sets the text, icon, tool tip, etc.
780      * This will also be called whenever a relevant property changes in the part
781      * to reflect those changes in the tab. Subclasses may override to change
782      * the appearance of tabs for a particular part.
783      *
784      * @param tabItem
785      * tab for the part
786      * @param part
787      * the part being displayed
788      */

789     protected void initTab(CTabItem tabItem, IPresentablePart part) {
790         tabItem.setText(part.getName());
791
792         // tabItem.setImage(part.getTitleImage());
793

794         // String toolTipText = part.getTitleToolTip();
795
// if (!toolTipText.equals(Util.ZERO_LENGTH_STRING)) {
796
// tabItem.setToolTipText(toolTipText);
797
// }
798

799         // FontRegistry registry =
800
// PlatformUI.getWorkbench().
801
// getThemeManager().getCurrentTheme().
802
// getFontRegistry();
803
//
804
// if(part.isBusy())
805
// tabItem.setFont(registry.getItalic(IWorkbenchThemeConstants.TAB_TEXT_FONT));
806
// else{
807
// tabItem.setFont(registry.get(IWorkbenchThemeConstants.TAB_TEXT_FONT));
808
// }
809

810     }
811
812     /*
813      * (non-Javadoc)
814      *
815      * @see org.eclipse.ui.internal.skins.StackPresentation#addPart(org.eclipse.ui.internal.skins.IPresentablePart,
816      * org.eclipse.ui.internal.skins.IPresentablePart)
817      */

818     public void addPart(IPresentablePart newPart, Object JavaDoc cookie) {
819
820         int idx;
821
822         if (cookie instanceof Integer JavaDoc) {
823             idx = ((Integer JavaDoc) cookie).intValue();
824         } else {
825             // Select a location for newly inserted parts
826
idx = paneFolder.getItemCount();
827         }
828
829         addPart(newPart, idx);
830     }
831
832     /**
833      * Adds the given presentable part to this presentation at the given index.
834      * Does nothing if a tab already exists for the given part.
835      *
836      * @param newPart
837      * @param index
838      */

839     public void addPart(IPresentablePart newPart, int index) {
840         // If we already have a tab for this part, do nothing
841
if (getTab(newPart) != null) {
842             return;
843         }
844         createPartTab(newPart, index);
845
846         setControlSize();
847     }
848
849     /*
850      * (non-Javadoc)
851      *
852      * @see org.eclipse.ui.internal.skins.StackPresentation#removePart(org.eclipse.ui.internal.skins.IPresentablePart)
853      */

854     public void removePart(IPresentablePart oldPart) {
855         if (current == oldPart) {
856             titleLabel.setImage(null);
857             current = null;
858         }
859
860         CTabItem item = getTab(oldPart);
861         if (item == null) {
862             return;
863         }
864         oldPart.setVisible(false);
865
866         item.dispose();
867
868         // Layout the folder again in case there is only one item
869
setControlSize();
870     }
871
872     /*
873      * (non-Javadoc)
874      *
875      * @see org.eclipse.ui.internal.skins.StackPresentation#selectPart(org.eclipse.ui.internal.skins.IPresentablePart)
876      */

877     public void selectPart(IPresentablePart toSelect) {
878         if (toSelect == current) {
879             return;
880         }
881
882         IPresentablePart oldPart = current;
883
884         current = toSelect;
885
886         if (current != null) {
887             paneFolder.setSelection(indexOf(current));
888             current.setVisible(true);
889             updateCloseButton();
890             setControlSize();
891         }
892
893         if (oldPart != null) {
894             oldPart.setVisible(false);
895         }
896     }
897
898     public IPresentablePart getCurrentPart() {
899         return current;
900     }
901
902     /*
903      * (non-Javadoc)
904      *
905      * @see org.eclipse.ui.internal.skins.Presentation#setBounds(org.eclipse.swt.graphics.Rectangle)
906      */

907     public void setBounds(Rectangle bounds) {
908         if (getSite().getState() == IStackPresentationSite.STATE_MINIMIZED) {
909             bounds = Geometry.copy(bounds);
910             bounds.height = computePreferredSize(false, Integer.MAX_VALUE,
911                     bounds.width, Integer.MAX_VALUE);
912         }
913
914         paneFolder.getControl().setBounds(bounds);
915         setControlSize();
916     }
917
918     /*
919      * (non-Javadoc)
920      *
921      * @see org.eclipse.ui.internal.skins.Presentation#computeMinimumSize()
922      */

923     public Point computeMinimumSize() {
924         Point result = Geometry.getSize(paneFolder.computeTrim(0, 0, 0, 0));
925
926         result.x += 100;
927
928         return result;
929     }
930
931     /*
932      * (non-Javadoc)
933      *
934      * @see org.eclipse.ui.internal.skins.Presentation#setVisible(boolean)
935      */

936     public void setVisible(boolean isVisible) {
937         if (current != null) {
938             current.setVisible(isVisible);
939         }
940         paneFolder.getControl().setVisible(isVisible);
941     }
942
943     /*
944      * (non-Javadoc)
945      *
946      * @see org.eclipse.ui.internal.skins.Presentation#setState(int)
947      */

948     public void setState(int state) {
949         // tabFolder.setState(state);
950
}
951
952     /*
953      * (non-Javadoc)
954      *
955      * @see org.eclipse.ui.internal.skins.Presentation#getSystemMenuManager()
956      */

957     public IMenuManager getSystemMenuManager() {
958         return systemMenuManager;
959     }
960
961     /**
962      * @param point
963      */

964     protected void showSystemMenu(Point point) {
965         Menu aMenu = systemMenuManager.createContextMenu(paneFolder
966                 .getControl().getParent());
967         systemMenuManager.update(true);
968         aMenu.setLocation(point.x, point.y);
969         aMenu.setVisible(true);
970     }
971
972     /*
973      * (non-Javadoc)
974      *
975      * @see org.eclipse.ui.internal.skins.Presentation#getControl()
976      */

977     public Control getControl() {
978         return paneFolder.getControl();
979     }
980
981     /*
982      * (non-Javadoc)
983      *
984      * @see org.eclipse.ui.internal.skins.StackPresentation#dragOver(org.eclipse.swt.widgets.Control,
985      * org.eclipse.swt.graphics.Point)
986      */

987     public StackDropResult dragOver(Control currentControl, Point location) {
988
989         // Determine which tab we're currently dragging over
990
Point localPos = paneFolder.getControl().toControl(location);
991         final CTabItem tabUnderPointer = paneFolder.getItem(localPos);
992
993         // This drop target only deals with tabs... if we're not dragging over
994
// a tab, exit.
995
if (tabUnderPointer == null) {
996             return null;
997         }
998
999         // workaround when left tab is dragged over next
1000
int dragOverIndex = paneFolder.indexOf(tabUnderPointer);
1001
1002        return new StackDropResult(Geometry.toDisplay(paneFolder.getControl(),
1003                tabUnderPointer.getBounds()), new Integer JavaDoc(dragOverIndex));
1004    }
1005
1006    /**
1007     * Returns the toolbar control for the currently selected part, or null if
1008     * none (not all parts have a toolbar).
1009     *
1010     * @return the current toolbar or null if none
1011     */

1012    protected Control getCurrentToolbar() {
1013        IPresentablePart part = getCurrentPart();
1014        if (part == null) {
1015            return null;
1016        }
1017
1018        return part.getToolBar();
1019    }
1020
1021    /*
1022     * (non-Javadoc)
1023     *
1024     * @see org.eclipse.ui.presentations.StackPresentation#showSystemMenu()
1025     */

1026    public void showSystemMenu() {
1027        IPresentablePart part = getCurrentPart();
1028        if (part != null) {
1029            Rectangle bounds = DragUtil.getDisplayBounds(paneFolder
1030                    .getControl());
1031
1032            int idx = paneFolder.getSelectionIndex();
1033            if (idx > -1) {
1034                CTabItem item = paneFolder.getItem(idx);
1035                Rectangle itemBounds = item.getBounds();
1036
1037                bounds.x += itemBounds.x;
1038                bounds.y += itemBounds.y;
1039            }
1040
1041            Point location = new Point(bounds.x, bounds.y
1042                    + paneFolder.getTabHeight());
1043            showSystemMenu(location);
1044        }
1045    }
1046
1047    /*
1048     * (non-Javadoc)
1049     *
1050     * @see org.eclipse.ui.presentations.StackPresentation#getTabList(IPresentablePart)
1051     */

1052    public Control[] getTabList(IPresentablePart part) {
1053        ArrayList JavaDoc list = new ArrayList JavaDoc();
1054        if (paneFolder.getTabPosition() == SWT.BOTTOM) {
1055            if (part.getToolBar() != null) {
1056                list.add(part.getToolBar());
1057            }
1058            if (part.getControl() != null) {
1059                list.add(part.getControl());
1060            }
1061            if (getPaneFolder() != null) {
1062                list.add(getPaneFolder().getControl());
1063            }
1064        } else {
1065            if (getPaneFolder() != null) {
1066                list.add(getPaneFolder().getControl());
1067            }
1068            if (part.getToolBar() != null) {
1069                list.add(part.getToolBar());
1070            }
1071            if (part.getControl() != null) {
1072                list.add(part.getControl());
1073            }
1074        }
1075        return (Control[]) list.toArray(new Control[list.size()]);
1076    }
1077
1078    protected void showList(Shell parentShell, int x, int y) {
1079        // final R21PaneFolder tabFolder = getTabFolder();
1080
//
1081
// int shellStyle = SWT.RESIZE | SWT.ON_TOP | SWT.NO_TRIM;
1082
// int tableStyle = SWT.V_SCROLL | SWT.H_SCROLL;
1083
// final BasicStackList editorList = new
1084
// BasicStackList(tabFolder.getControl().getShell(),
1085
// shellStyle, tableStyle);
1086
// editorList.setInput(this);
1087
// Point size = editorList.computeSizeHint();
1088
//
1089
// Rectangle bounds = Display.getCurrent().getBounds();
1090
// if (x + size.x > bounds.width) x = bounds.width - size.x;
1091
// if (y + size.y > bounds.height) y = bounds.height - size.y;
1092
// editorList.setLocation(new Point(x, y));
1093
// editorList.setVisible(true);
1094
// editorList.setFocus();
1095
// editorList.getTableViewer().getTable().getShell().addListener(
1096
// SWT.Deactivate, new Listener() {
1097
//
1098
// public void handleEvent(Event event) {
1099
// editorList.setVisible(false);
1100
// }
1101
// });
1102
}
1103
1104    /*
1105     * Shows the list of tabs at the top left corner of the editor
1106     */

1107    protected void showListDefaultLocation() {
1108        R21PaneFolder tabFolder = getPaneFolder();
1109        Shell shell = tabFolder.getControl().getShell();
1110        Rectangle clientArea = tabFolder.getClientArea();
1111        Point location = tabFolder.getControl().getDisplay().map(
1112                tabFolder.getControl(), null, clientArea.x, clientArea.y);
1113        showList(shell, location.x, location.y);
1114    }
1115
1116    void setSelection(CTabItem tabItem) {
1117        getSite().selectPart(getPartForTab(tabItem));
1118    }
1119
1120    void close(IPresentablePart presentablePart) {
1121        getSite().close(new IPresentablePart[] { presentablePart });
1122    }
1123
1124    Image getLabelImage(IPresentablePart presentablePart) {
1125        return presentablePart.getTitleImage();
1126    }
1127
1128    String JavaDoc getLabelText(IPresentablePart presentablePart, boolean includePath) {
1129        String JavaDoc title = presentablePart.getTitle().trim();
1130        return title;
1131    }
1132
1133    /*
1134     * (non-Javadoc)
1135     *
1136     * @see org.eclipse.ui.presentations.StackPresentation#setActive(int)
1137     */

1138    public void setActive(int newState) {
1139        setActive(newState == AS_ACTIVE_FOCUS);
1140    }
1141
1142    /*
1143     * (non-Javadoc)
1144     *
1145     * @see org.eclipse.ui.presentations.StackPresentation#restoreState(org.eclipse.ui.presentations.IPresentationSerializer,
1146     * org.eclipse.ui.IMemento)
1147     */

1148    public void restoreState(IPresentationSerializer serializer,
1149            IMemento savedState) {
1150        IMemento[] parts = savedState.getChildren(IWorkbenchConstants.TAG_PART);
1151
1152        for (int idx = 0; idx < parts.length; idx++) {
1153            String JavaDoc id = parts[idx].getString(IWorkbenchConstants.TAG_ID);
1154
1155            if (id != null) {
1156                IPresentablePart part = serializer.getPart(id);
1157
1158                if (part != null) {
1159                    addPart(part, getPaneFolder().getItemCount());
1160                }
1161            }
1162        }
1163    }
1164
1165    /*
1166     * (non-Javadoc)
1167     *
1168     * @see org.eclipse.ui.presentations.StackPresentation#saveState(org.eclipse.ui.presentations.IPresentationSerializer,
1169     * org.eclipse.ui.IMemento)
1170     */

1171    public void saveState(IPresentationSerializer context, IMemento memento) {
1172        super.saveState(context, memento);
1173
1174        List JavaDoc parts = getPresentableParts();
1175
1176        Iterator JavaDoc iter = parts.iterator();
1177        while (iter.hasNext()) {
1178            IPresentablePart next = (IPresentablePart) iter.next();
1179
1180            IMemento childMem = memento
1181                    .createChild(IWorkbenchConstants.TAG_PART);
1182            childMem.putString(IWorkbenchConstants.TAG_ID, context.getId(next));
1183        }
1184    }
1185
1186    /**
1187     * Returns the List of IPresentablePart currently in this presentation
1188     */

1189    private List JavaDoc getPresentableParts() {
1190        Assert.isTrue(!isDisposed());
1191
1192        CTabItem[] items = getPaneFolder().getItems();
1193        List JavaDoc result = new ArrayList JavaDoc(items.length);
1194
1195        for (int idx = 0; idx < getPaneFolder().getItemCount(); idx++) {
1196            result.add(getPartForTab(items[idx]));
1197        }
1198
1199        return result;
1200    }
1201}
1202
Popular Tags