KickJava   Java API By Example, From Geeks To Geeks.

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


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  *******************************************************************************/

11 package org.eclipse.ui.internal.presentations;
12
13 import java.util.ArrayList JavaDoc;
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 /**
45  * A stack presentation using native widgets.
46  * <p>
47  * EXPERIMENTAL
48  * </p>
49  *
50  * @since 3.0
51  */

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     // don't reset this dynamically, so just keep the information static.
66
// see bug:
67
// 75422 [Presentations] Switching presentation to R21 switches immediately, but only partially
68
private static int tabPos = preferenceStore.getInt(IPreferenceConstants.VIEW_TAB_POSITION);
69   
70     private final static String JavaDoc TAB_DATA = NativeStackPresentation.class
71             .getName()
72             + ".partId"; //$NON-NLS-1$
73

74     private MouseListener mouseListener = new MouseAdapter() {
75         public void mouseDown(MouseEvent e) {
76             // // PR#1GDEZ25 - If selection will change in mouse up ignore mouse down.
77
// // Else, set focus.
78
// TabItem newItem = tabFolder.getItem(new Point(e.x, e.y));
79
// if (newItem != null) {
80
// TabItem oldItem = tabFolder.getSelection();
81
// if (newItem != oldItem)
82
// return;
83
// }
84
if (current != null) {
85                 current.setFocus();
86             }
87         }
88     };
89
90     private Listener menuListener = new Listener() {
91         /* (non-Javadoc)
92          * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
93          */

94         public void handleEvent(Event event) {
95             Point pos = new Point(event.x, event.y);
96             // TabItem item = tabFolder.getItem(pos);
97
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                 // item.setFocus();
112
}
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 JavaDoc 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         // listener to switch between visible tabItems
153
tabFolder.addListener(SWT.Selection, selectionListener);
154
155         // listener to resize visible components
156
tabFolder.addListener(SWT.Resize, resizeListener);
157
158         // listen for mouse down on tab to set focus.
159
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 = tabFolder.getItem(localPos);
167
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     /**
187      * Returns the index of the tab for the given part, or returns tabFolder.getItemCount()
188      * if there is no such tab.
189      *
190      * @param part part being searched for
191      * @return the index of the tab for the given part, or the number of tabs
192      * if there is no such tab
193      */

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     /**
213      * Returns the tab for the given part, or null if there is no such tab
214      *
215      * @param part the part being searched for
216      * @return the tab for the given part, or null if there is no such tab
217      */

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     /**
231      * @param part
232      * @param property
233      */

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     /**
253      * Set the size of a page in the folder.
254      */

255     private void setControlSize() {
256         if (current == null || tabFolder == null) {
257             return;
258         }
259         // Rectangle bounds;
260
// @issue as above, the mere presence of a theme should not change the behaviour
261
// if ((mapTabToPart.size() > 1)
262
// || ((tabThemeDescriptor != null) && (mapTabToPart.size() >= 1)))
263
// bounds = calculatePageBounds(tabFolder);
264
// else
265
// bounds = tabFolder.getBounds();
266
current.setBounds(calculatePageBounds(tabFolder));
267         //current.moveAbove(tabFolder);
268
}
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     /* (non-Javadoc)
284      * @see org.eclipse.ui.internal.skins.Presentation#dispose()
285      */

286     public void dispose() {
287         if (isDisposed()) {
288             return;
289         }
290         PresentationUtil.removeDragListener(tabFolder, dragListener);
291
292         //systemMenuManager.dispose();
293

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     /**
308      * Initializes a tab for the given part. Sets the text, icon, tool tip,
309      * etc. This will also be called whenever a relevant property changes
310      * in the part to reflect those changes in the tab. Subclasses may override
311      * to change the appearance of tabs for a particular part.
312      *
313      * @param tabItem tab for the part
314      * @param part the part being displayed
315      */

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     /* (non-Javadoc)
327      * @see org.eclipse.ui.internal.skins.StackPresentation#addPart(org.eclipse.ui.internal.skins.IPresentablePart, org.eclipse.ui.internal.skins.IPresentablePart)
328      */

329     public void addPart(IPresentablePart newPart, Object JavaDoc cookie) {
330         createPartTab(newPart, tabFolder.getItemCount());
331     }
332
333     /* (non-Javadoc)
334      * @see org.eclipse.ui.internal.skins.StackPresentation#removePart(org.eclipse.ui.internal.skins.IPresentablePart)
335      */

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     /* (non-Javadoc)
347      * @see org.eclipse.ui.internal.skins.StackPresentation#selectPart(org.eclipse.ui.internal.skins.IPresentablePart)
348      */

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     /* (non-Javadoc)
368      * @see org.eclipse.ui.internal.skins.Presentation#setBounds(org.eclipse.swt.graphics.Rectangle)
369      */

370     public void setBounds(Rectangle bounds) {
371         tabFolder.setBounds(bounds);
372         setControlSize();
373     }
374
375     /* (non-Javadoc)
376      * @see org.eclipse.ui.internal.skins.Presentation#computeMinimumSize()
377      */

378     public Point computeMinimumSize() {
379         return Geometry.getSize(tabFolder.computeTrim(0, 0, 0, 0));
380     }
381
382     /* (non-Javadoc)
383      * @see org.eclipse.ui.internal.skins.Presentation#setVisible(boolean)
384      */

385     public void setVisible(boolean isVisible) {
386         if (current != null) {
387             current.setVisible(isVisible);
388         }
389         tabFolder.setVisible(isVisible);
390     }
391
392     /* (non-Javadoc)
393      * @see org.eclipse.ui.internal.skins.Presentation#setState(int)
394      */

395     public void setState(int state) {
396         // tabFolder.setMinimized(state == IPresentationSite.STATE_MINIMIZED);
397
// tabFolder.setMaximized(state == IPresentationSite.STATE_MAXIMIZED);
398
}
399
400     /* (non-Javadoc)
401      * @see org.eclipse.ui.internal.skins.Presentation#getSystemMenuManager()
402      */

403     public IMenuManager getSystemMenuManager() {
404         return systemMenuManager;
405     }
406
407     /**
408      * @param part
409      * @param point
410      */

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     /* (non-Javadoc)
419      * @see org.eclipse.ui.internal.skins.Presentation#getControl()
420      */

421     public Control getControl() {
422         return tabFolder;
423     }
424
425     /* (non-Javadoc)
426      * @see org.eclipse.ui.internal.skins.StackPresentation#dragOver(org.eclipse.swt.widgets.Control, org.eclipse.swt.graphics.Point)
427      */

428     public StackDropResult dragOver(Control currentControl, Point location) {
429
430         // Determine which tab we're currently dragging over
431
// Point localPos = tabFolder.toControl(location);
432
// final TabItem tabUnderPointer = tabFolder.getItem(localPos);
433
final TabItem tabUnderPointer = null;
434
435         // This drop target only deals with tabs... if we're not dragging over
436
// a tab, exit.
437
if (tabUnderPointer == null) {
438             return null;
439         }
440
441         // return new StackDropResult(Geometry.toDisplay(tabFolder, tabUnderPointer.getBounds()),
442
// tabFolder.indexOf(tabUnderPointer));
443
return null;
444     }
445
446     /* (non-Javadoc)
447      * @see org.eclipse.ui.presentations.StackPresentation#showSystemMenu()
448      */

449     public void showSystemMenu() {
450         // TODO Auto-generated method stub
451

452     }
453
454     /* (non-Javadoc)
455      * @see org.eclipse.ui.presentations.StackPresentation#showPaneMenu()
456      */

457     public void showPaneMenu() {
458         // TODO Auto-generated method stub
459

460     }
461
462     /* (non-Javadoc)
463      * @see org.eclipse.ui.presentations.StackPresentation#getTabList(IPresentablePart)
464      */

465     public Control[] getTabList(IPresentablePart part) {
466         ArrayList JavaDoc list = new ArrayList JavaDoc();
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     /* (non-Javadoc)
480      * @see org.eclipse.ui.presentations.StackPresentation#getCurrentPart()
481      */

482     public IPresentablePart getCurrentPart() {
483         return current;
484     }
485
486     /* (non-Javadoc)
487      * @see org.eclipse.ui.presentations.StackPresentation#setActive(int)
488      */

489     public void setActive(int newState) {
490
491     }
492
493 }
494
Popular Tags