KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > util > TabbedStackPresentation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
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 /**
43  * @since 3.0
44  */

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 JavaDoc prefs = new HashMap JavaDoc();
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         // Add a dispose listener. This will call the presentationDisposed()
173
// method when the widget is destroyed.
174
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     /**
186      * Restores a presentation from a previously stored state
187      *
188      * @param serializer (not null)
189      * @param savedState (not null)
190      */

191     public void restoreState(IPresentationSerializer serializer,
192             IMemento savedState) {
193         tabs.restoreState(serializer, savedState);
194     }
195
196     /* (non-Javadoc)
197      * @see org.eclipse.ui.presentations.StackPresentation#saveState(org.eclipse.ui.presentations.IPresentationSerializer, org.eclipse.ui.IMemento)
198      */

199     public void saveState(IPresentationSerializer context, IMemento memento) {
200         super.saveState(context, memento);
201
202         tabs.saveState(context, memento);
203     }
204     
205     /**
206      * Returns true iff the presentation has been disposed
207      *
208      * @return true iff the presentation has been disposed
209      */

210     private boolean isDisposed() {
211         return folder == null || folder.isDisposed();
212     }
213
214     /* (non-Javadoc)
215      * @see org.eclipse.ui.presentations.StackPresentation#setBounds(org.eclipse.swt.graphics.Rectangle)
216      */

217     public void setBounds(Rectangle bounds) {
218         folder.setBounds(bounds);
219     }
220
221     /* (non-Javadoc)
222      * @see org.eclipse.ui.presentations.StackPresentation#computeMinimumSize()
223      */

224     public Point computeMinimumSize() {
225         return folder.getTabFolder().computeSize(SWT.DEFAULT, SWT.DEFAULT);
226     }
227
228     /* (non-Javadoc)
229      * @see org.eclipse.ui.ISizeProvider#computePreferredSize(boolean, int, int, int)
230      */

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     /* (non-Javadoc)
255      * @see org.eclipse.ui.presentations.StackPresentation#showPartList()
256      */

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     /* (non-Javadoc)
268      * @see org.eclipse.ui.presentations.StackPresentation#dispose()
269      */

270     public void dispose() {
271         // Dispose the tab folder's widgetry
272
folder.getTabFolder().getControl().dispose();
273     }
274
275     /**
276      * Called when the tab folder is disposed.
277      */

278     private void presentationDisposed() {
279         apiPreferences.dispose();
280         themePreferences.dispose();
281         
282         Iterator JavaDoc 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     /* (non-Javadoc)
302      * @see org.eclipse.ui.presentations.StackPresentation#setActive(int)
303      */

304     public void setActive(int newState) {
305         folder.getTabFolder().setActive(newState);
306     }
307
308     /* (non-Javadoc)
309      * @see org.eclipse.ui.presentations.StackPresentation#setVisible(boolean)
310      */

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     /* (non-Javadoc)
321      * @see org.eclipse.ui.presentations.StackPresentation#setState(int)
322      */

323     public void setState(int state) {
324         folder.getTabFolder().setState(state);
325     }
326
327     /* (non-Javadoc)
328      * @see org.eclipse.ui.presentations.StackPresentation#getControl()
329      */

330     public Control getControl() {
331         return folder.getTabFolder().getControl();
332     }
333
334     /**
335      * @return AbstractTabFolder the presentation's tab folder
336      */

337     public AbstractTabFolder getTabFolder() {
338         return folder.getTabFolder();
339     }
340
341     /* (non-Javadoc)
342      * @see org.eclipse.ui.presentations.StackPresentation#addPart(org.eclipse.ui.presentations.IPresentablePart, java.lang.Object)
343      */

344     public void addPart(IPresentablePart newPart, Object JavaDoc 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     /* (non-Javadoc)
365      * @see org.eclipse.ui.presentations.StackPresentation#movePart(org.eclipse.ui.presentations.IPresentablePart, java.lang.Object)
366      */

367     public void movePart(IPresentablePart toMove, Object JavaDoc 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     /* (non-Javadoc)
383      * @see org.eclipse.ui.presentations.StackPresentation#removePart(org.eclipse.ui.presentations.IPresentablePart)
384      */

385     public void removePart(IPresentablePart oldPart) {
386         ignoreSelectionChanges++;
387         try {
388             tabs.remove(oldPart);
389         } finally {
390             ignoreSelectionChanges--;
391         }
392     }
393
394     /* (non-Javadoc)
395      * @see org.eclipse.ui.presentations.StackPresentation#selectPart(org.eclipse.ui.presentations.IPresentablePart)
396      */

397     public void selectPart(IPresentablePart toSelect) {
398         initializing = false;
399
400         tabs.select(toSelect);
401     }
402
403     /* (non-Javadoc)
404      * @see org.eclipse.ui.presentations.StackPresentation#dragOver(org.eclipse.swt.widgets.Control, org.eclipse.swt.graphics.Point)
405      */

406     public StackDropResult dragOver(Control currentControl, Point location) {
407         return dragBehavior.dragOver(currentControl, location, dragStart);
408     }
409
410     /**
411      * @param part
412      * @param point
413      */

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     /* (non-Javadoc)
426      * @see org.eclipse.ui.presentations.StackPresentation#showPaneMenu()
427      */

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     /* (non-Javadoc)
447      * @see org.eclipse.ui.presentations.StackPresentation#getTabList(org.eclipse.ui.presentations.IPresentablePart)
448      */

449     public Control[] getTabList(IPresentablePart part) {
450         ArrayList JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc 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     /**
498      * Move the tabs around. This is for testing <b>ONLY</b>.
499      * @param part the part to move
500      * @param index the new index
501      * @since 3.2
502      */

503     public void moveTab(IPresentablePart part, int index) {
504         tabs.move(part, index);
505         folder.layout(true);
506     }
507     
508     /**
509      * Get the tab list. This is for testing <b>ONLY</b>.
510      * @return the presentable parts in order.
511      * @since 3.2
512      */

513     public IPresentablePart[] getPartList() {
514         return tabs.getPartList();
515     }
516
517     /**
518      * Cause the folder to hide or show its
519      * Minimize and Maximize affordances.
520      *
521      * @param show
522      * <code>true</code> - the min/max buttons are visible.
523      * @since 3.3
524      */

525     public void showMinMax(boolean show) {
526         folder.getTabFolder().showMinMax(show);
527     }
528 }
529
Popular Tags