KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > defaultpresentation > DefaultTabFolder


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.defaultpresentation;
12
13 import org.eclipse.core.runtime.Assert;
14 import org.eclipse.jface.util.Geometry;
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.custom.CTabFolder;
17 import org.eclipse.swt.custom.CTabFolderEvent;
18 import org.eclipse.swt.custom.CTabItem;
19 import org.eclipse.swt.events.MouseAdapter;
20 import org.eclipse.swt.events.MouseEvent;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.GC;
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.Label;
32 import org.eclipse.swt.widgets.Listener;
33 import org.eclipse.swt.widgets.ToolBar;
34 import org.eclipse.swt.widgets.ToolItem;
35 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
36 import org.eclipse.ui.internal.WorkbenchImages;
37 import org.eclipse.ui.internal.WorkbenchMessages;
38 import org.eclipse.ui.internal.dnd.DragUtil;
39 import org.eclipse.ui.internal.presentations.PaneFolder;
40 import org.eclipse.ui.internal.presentations.PaneFolderButtonListener;
41 import org.eclipse.ui.internal.presentations.util.AbstractTabFolder;
42 import org.eclipse.ui.internal.presentations.util.AbstractTabItem;
43 import org.eclipse.ui.internal.presentations.util.PartInfo;
44 import org.eclipse.ui.internal.presentations.util.TabFolderEvent;
45 import org.eclipse.ui.internal.util.Util;
46
47 /**
48  * @since 3.1
49  */

50 public class DefaultTabFolder extends AbstractTabFolder {
51
52     private PaneFolder paneFolder;
53     private Control viewToolBar;
54     private Label titleLabel;
55     
56     private PaneFolderButtonListener buttonListener = new PaneFolderButtonListener() {
57         public void stateButtonPressed(int buttonId) {
58             fireEvent(TabFolderEvent.stackStateToEventId(buttonId));
59         }
60
61         /**
62          * Called when a close button is pressed.
63          *
64          * @param item the tab whose close button was pressed
65          */

66         public void closeButtonPressed(CTabItem item) {
67             fireEvent(TabFolderEvent.EVENT_CLOSE, getTab(item));
68         }
69         /**
70          *
71          * @since 3.0
72          */

73         public void showList(CTabFolderEvent event) {
74             event.doit = false;
75             fireEvent(TabFolderEvent.EVENT_SHOW_LIST);
76         }
77     };
78     
79     private Listener selectionListener = new Listener() {
80         public void handleEvent(Event e) {
81             AbstractTabItem item = getTab((CTabItem) e.item);
82
83             if (item != null) {
84                 fireEvent(TabFolderEvent.EVENT_TAB_SELECTED, item);
85             }
86         }
87     };
88     
89     private static DefaultTabFolderColors defaultColors = new DefaultTabFolderColors();
90     
91     private DefaultTabFolderColors[] activeShellColors = {defaultColors, defaultColors, defaultColors};
92     private DefaultTabFolderColors[] inactiveShellColors = {defaultColors, defaultColors, defaultColors};
93     private boolean shellActive = false;
94     
95     /**
96      * Create a new instance of the receiver
97      *
98      * @param parent
99      * @param flags
100      * @param allowMin
101      * @param allowMax
102      */

103     public DefaultTabFolder(Composite parent, int flags, boolean allowMin, boolean allowMax) {
104         paneFolder = new PaneFolder(parent, flags | SWT.NO_BACKGROUND);
105         paneFolder.addButtonListener(buttonListener);
106         paneFolder.setMinimizeVisible(allowMin);
107         paneFolder.setMaximizeVisible(allowMax);
108         paneFolder.getControl().addListener(SWT.Selection, selectionListener);
109         paneFolder.setTopRight(null);
110         
111         // Initialize view menu dropdown
112
{
113             ToolBar actualToolBar = new ToolBar(paneFolder.getControl(), SWT.FLAT | SWT.NO_BACKGROUND);
114             viewToolBar = actualToolBar;
115             
116             ToolItem pullDownButton = new ToolItem(actualToolBar, SWT.PUSH);
117             Image hoverImage = WorkbenchImages
118                     .getImage(IWorkbenchGraphicConstants.IMG_LCL_RENDERED_VIEW_MENU);
119             pullDownButton.setDisabledImage(hoverImage);
120             pullDownButton.setImage(hoverImage);
121             pullDownButton.setToolTipText(WorkbenchMessages.Menu);
122             actualToolBar.addMouseListener(new MouseAdapter() {
123                 public void mouseDown(MouseEvent e) {
124                     fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), getPaneMenuLocation());
125                 }
126             });
127             pullDownButton.addSelectionListener(new SelectionAdapter() {
128                 public void widgetSelected(SelectionEvent e) {
129                     fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), getPaneMenuLocation());
130
131                     super.widgetSelected(e);
132                 }
133             });
134         }
135         
136         // Initialize content description label
137
{
138             titleLabel = new Label(paneFolder.getControl(), SWT.NONE);
139             titleLabel.moveAbove(null);
140             titleLabel.setVisible(false);
141             attachListeners(titleLabel, false);
142         }
143         
144         attachListeners(paneFolder.getControl(), false);
145         attachListeners(paneFolder.getViewForm(), false);
146         
147         paneFolder.setTabHeight(computeTabHeight());
148         
149         viewToolBar.moveAbove(null);
150     }
151
152     /**
153      * Changes the minimum number of characters to display in the pane folder
154      * tab. This control how much information will be displayed to the user.
155      *
156      * @param count
157      * The number of characters to display in the tab folder; this
158      * value should be a positive integer.
159      * @see org.eclipse.swt.custom.CTabFolder#setMinimumCharacters(int)
160      * @since 3.1
161      */

162     public void setMinimumCharacters(int count) {
163         paneFolder.setMinimumCharacters(count);
164     }
165     
166     public void setSimpleTabs(boolean simple) {
167         paneFolder.setSimpleTab(simple);
168     }
169     
170     /**
171      * @param item
172      * @return
173      * @since 3.1
174      */

175     protected DefaultTabItem getTab(CTabItem item) {
176         return (DefaultTabItem)item.getData();
177     }
178
179     /* (non-Javadoc)
180      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#computeSize(int, int)
181      */

182     public Point computeSize(int widthHint, int heightHint) {
183         return paneFolder.computeMinimumSize();
184     }
185
186     /* package */ PaneFolder getFolder() {
187         return paneFolder;
188     }
189     
190     public AbstractTabItem getSelection() {
191         return getTab(paneFolder.getSelection());
192     }
193     
194     /* (non-Javadoc)
195      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#add(int)
196      */

197     public AbstractTabItem add(int index, int flags) {
198         DefaultTabItem result = new DefaultTabItem((CTabFolder)getFolder().getControl(), index, flags);
199         
200         result.getWidget().setData(result);
201         
202         return result;
203     }
204
205     /* (non-Javadoc)
206      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getContentParent()
207      */

208     public Composite getContentParent() {
209         return paneFolder.getContentParent();
210     }
211
212     /* (non-Javadoc)
213      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setContent(org.eclipse.swt.widgets.Control)
214      */

215     public void setContent(Control newContent) {
216         paneFolder.setContent(newContent);
217     }
218
219     /* (non-Javadoc)
220      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItems()
221      */

222     public AbstractTabItem[] getItems() {
223         CTabItem[] items = paneFolder.getItems();
224         
225         AbstractTabItem[] result = new AbstractTabItem[items.length];
226         
227         for (int i = 0; i < result.length; i++) {
228             result[i] = getTab(items[i]);
229         }
230         
231         return result;
232     }
233
234     /* (non-Javadoc)
235      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItemCount()
236      */

237     public int getItemCount() {
238         // Override retrieving all the items when we just want the count.
239
return paneFolder.getItemCount();
240     }
241     
242     /* (non-Javadoc)
243      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelection(org.eclipse.ui.internal.presentations.util.AbstractTabItem)
244      */

245     public void setSelection(AbstractTabItem toSelect) {
246         paneFolder.setSelection(indexOf(toSelect));
247     }
248
249     /* (non-Javadoc)
250      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getToolbarParent()
251      */

252     public Composite getToolbarParent() {
253         return paneFolder.getControl();
254     }
255
256     /* (non-Javadoc)
257      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getControl()
258      */

259     public Control getControl() {
260         return paneFolder.getControl();
261     }
262     
263     public void setUnselectedCloseVisible(boolean visible) {
264         paneFolder.setUnselectedCloseVisible(visible);
265     }
266
267     public void setUnselectedImageVisible(boolean visible) {
268         paneFolder.setUnselectedImageVisible(visible);
269     }
270     
271     /* (non-Javadoc)
272      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getTabArea()
273      */

274     public Rectangle getTabArea() {
275         return Geometry.toDisplay(paneFolder.getControl(), paneFolder.getTitleArea());
276     }
277
278     /**
279      * @param enabled
280      * @since 3.1
281      */

282     public void enablePaneMenu(boolean enabled) {
283         if (enabled) {
284             paneFolder.setTopRight(viewToolBar);
285             viewToolBar.setVisible(true);
286         } else {
287             paneFolder.setTopRight(null);
288             viewToolBar.setVisible(false);
289         }
290     }
291
292     /* (non-Javadoc)
293      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelectedInfo(org.eclipse.ui.internal.presentations.util.PartInfo)
294      */

295     public void setSelectedInfo(PartInfo info) {
296         String JavaDoc newTitle = DefaultTabItem.escapeAmpersands(info.contentDescription);
297         
298         if (!Util.equals(titleLabel.getText(), newTitle)) {
299             titleLabel.setText(newTitle);
300         }
301         
302         if (!info.contentDescription.equals(Util.ZERO_LENGTH_STRING)) {
303             paneFolder.setTopLeft(titleLabel);
304             titleLabel.setVisible(true);
305         } else {
306             paneFolder.setTopLeft(null);
307             titleLabel.setVisible(false);
308         }
309     }
310
311     /* (non-Javadoc)
312      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getPaneMenuLocation()
313      */

314     public Point getPaneMenuLocation() {
315         Point toolbarSize = viewToolBar.getSize();
316         
317         return viewToolBar.toDisplay(0,toolbarSize.y);
318     }
319     
320     /* (non-Javadoc)
321      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getPartListLocation()
322      */

323     public Point getPartListLocation() {
324         return paneFolder.getControl().toDisplay(paneFolder.getChevronLocation());
325     }
326     
327     /* (non-Javadoc)
328      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getSystemMenuLocation()
329      */

330     public Point getSystemMenuLocation() {
331         Rectangle bounds = DragUtil.getDisplayBounds(paneFolder.getControl());
332         
333         int idx = paneFolder.getSelectionIndex();
334         if (idx > -1) {
335             CTabItem item = paneFolder.getItem(idx);
336             Rectangle itemBounds = item.getBounds();
337         
338             bounds.x += itemBounds.x;
339             bounds.y += itemBounds.y;
340         }
341         
342         Point location = new Point(bounds.x, bounds.y
343                 + paneFolder.getTabHeight());
344         
345         return location;
346     }
347     
348     /* (non-Javadoc)
349      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#isOnBorder(org.eclipse.swt.graphics.Point)
350      */

351     public boolean isOnBorder(Point toTest) {
352         Control content = paneFolder.getContent();
353         if (content != null) {
354             Rectangle displayBounds = DragUtil.getDisplayBounds(content);
355             
356             if (paneFolder.getTabPosition() == SWT.TOP) {
357                 return toTest.y >= displayBounds.y;
358             }
359             
360             if (toTest.y >= displayBounds.y && toTest.y < displayBounds.y + displayBounds.height) {
361                 return true;
362             }
363         }
364         
365         return super.isOnBorder(toTest);
366     }
367     
368     /* (non-Javadoc)
369      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#layout(boolean)
370      */

371     public void layout(boolean flushCache) {
372         paneFolder.layout(flushCache);
373         super.layout(flushCache);
374     }
375     
376     /* (non-Javadoc)
377      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setState(int)
378      */

379     public void setState(int state) {
380         paneFolder.setState(state);
381         super.setState(state);
382     }
383     
384     /* (non-Javadoc)
385      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setActive(int)
386      */

387     public void setActive(int activeState) {
388         super.setActive(activeState);
389         updateColors();
390     }
391     
392     /* (non-Javadoc)
393      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setTabPosition(int)
394      */

395     public void setTabPosition(int tabPosition) {
396         paneFolder.setTabPosition(tabPosition);
397         super.setTabPosition(tabPosition);
398         layout(true);
399     }
400     
401     public void flushToolbarSize() {
402         paneFolder.flushTopCenterSize();
403     }
404     
405     /* (non-Javadoc)
406      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setToolbar(org.eclipse.swt.widgets.Control)
407      */

408     public void setToolbar(Control toolbarControl) {
409         paneFolder.setTopCenter(toolbarControl);
410         super.setToolbar(toolbarControl);
411     }
412     
413     public void setColors(DefaultTabFolderColors colors, int activationState, boolean shellActivationState) {
414         Assert.isTrue(activationState < activeShellColors.length);
415                 
416         if (shellActivationState) {
417             activeShellColors[activationState] = colors;
418         } else {
419             inactiveShellColors[activationState] = colors;
420         }
421         
422         if (activationState == getActive() && shellActive == shellActivationState) {
423             updateColors();
424         }
425     }
426     
427     /**
428      *
429      * @since 3.1
430      */

431     private void updateColors() {
432         DefaultTabFolderColors currentColors = shellActive ?
433                 activeShellColors[getActive()]
434                 : inactiveShellColors[getActive()];
435                 
436         paneFolder.setSelectionForeground(currentColors.foreground);
437         paneFolder.setSelectionBackground(currentColors.background, currentColors.percentages, currentColors.vertical);
438     }
439
440     public void setColors(DefaultTabFolderColors colors, int activationState) {
441         setColors(colors, activationState, true);
442         setColors(colors, activationState, false);
443     }
444     
445     /* (non-Javadoc)
446      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#shellActive(boolean)
447      */

448     public void shellActive(boolean isActive) {
449         this.shellActive = isActive;
450         super.shellActive(isActive);
451         
452         updateColors();
453     }
454
455     /**
456      * @param font
457      * @since 3.1
458      */

459     public void setFont(Font font) {
460         if (font != paneFolder.getControl().getFont()) {
461             paneFolder.getControl().setFont(font);
462             layout(true);
463             paneFolder.setTabHeight(computeTabHeight());
464         }
465     }
466     
467     /**
468      * @return the required tab height for this folder.
469      */

470     protected int computeTabHeight() {
471         GC gc = new GC(getControl());
472
473         // Compute the tab height
474
int tabHeight = Math.max(viewToolBar.computeSize(SWT.DEFAULT,
475                 SWT.DEFAULT).y, gc.getFontMetrics().getHeight());
476
477         gc.dispose();
478
479         return tabHeight;
480     }
481
482     /**
483      * @param b
484      * @since 3.1
485      */

486     public void setSingleTab(boolean b) {
487         paneFolder.setSingleTab(b);
488         AbstractTabItem[] items = getItems();
489         
490         for (int i = 0; i < items.length; i++) {
491             DefaultTabItem item = (DefaultTabItem)items[i];
492         
493             item.updateTabText();
494         }
495         
496         layout(true);
497     }
498
499     /* (non-Javadoc)
500      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setVisible(boolean)
501      */

502     public void setVisible(boolean visible) {
503         super.setVisible(visible);
504         getFolder().setVisible(visible);
505     }
506
507     /* (non-Javadoc)
508      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#showMinMax(boolean)
509      */

510     public void showMinMax(boolean show) {
511         paneFolder.showMinMax(show);
512     }
513 }
514
Popular Tags