KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.r33;
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.util.AbstractTabFolder;
40 import org.eclipse.ui.internal.presentations.util.AbstractTabItem;
41 import org.eclipse.ui.internal.presentations.util.PartInfo;
42 import org.eclipse.ui.internal.presentations.util.TabFolderEvent;
43 import org.eclipse.ui.internal.util.Util;
44
45 /**
46  * @since 3.1
47  */

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

65         public void closeButtonPressed(CTabItem item) {
66             fireEvent(TabFolderEvent.EVENT_CLOSE, getTab(item));
67         }
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,
92             defaultColors, defaultColors };
93     private DefaultTabFolderColors[] inactiveShellColors = { defaultColors,
94             defaultColors, defaultColors };
95     private boolean shellActive = false;
96
97     /**
98      * Create a new instance of the receiver
99      *
100      * @param parent
101      * @param flags
102      * @param allowMin
103      * @param allowMax
104      */

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

168     public void setMinimumCharacters(int count) {
169         paneFolder.setMinimumCharacters(count);
170     }
171
172     public void setSimpleTabs(boolean simple) {
173         paneFolder.setSimpleTab(simple);
174     }
175
176     /**
177      * @param item
178      * @return
179      * @since 3.1
180      */

181     protected DefaultTabItem getTab(CTabItem item) {
182         return (DefaultTabItem) item.getData();
183     }
184
185     /*
186      * (non-Javadoc)
187      *
188      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#computeSize(int,
189      * int)
190      */

191     public Point computeSize(int widthHint, int heightHint) {
192         return paneFolder.computeMinimumSize();
193     }
194
195     /* package */PaneFolder getFolder() {
196         return paneFolder;
197     }
198
199     public AbstractTabItem getSelection() {
200         return getTab(paneFolder.getSelection());
201     }
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#add(int)
207      */

208     public AbstractTabItem add(int index, int flags) {
209         DefaultTabItem result = new DefaultTabItem((CTabFolder) getFolder()
210                 .getControl(), index, flags);
211
212         result.getWidget().setData(result);
213
214         return result;
215     }
216
217     /*
218      * (non-Javadoc)
219      *
220      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getContentParent()
221      */

222     public Composite getContentParent() {
223         return paneFolder.getContentParent();
224     }
225
226     /*
227      * (non-Javadoc)
228      *
229      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setContent(org.eclipse.swt.widgets.Control)
230      */

231     public void setContent(Control newContent) {
232         paneFolder.setContent(newContent);
233     }
234
235     /*
236      * (non-Javadoc)
237      *
238      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItems()
239      */

240     public AbstractTabItem[] getItems() {
241         CTabItem[] items = paneFolder.getItems();
242
243         AbstractTabItem[] result = new AbstractTabItem[items.length];
244
245         for (int i = 0; i < result.length; i++) {
246             result[i] = getTab(items[i]);
247         }
248
249         return result;
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItemCount()
256      */

257     public int getItemCount() {
258         // Override retrieving all the items when we just want the count.
259
return paneFolder.getItemCount();
260     }
261
262     /*
263      * (non-Javadoc)
264      *
265      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelection(org.eclipse.ui.internal.presentations.util.AbstractTabItem)
266      */

267     public void setSelection(AbstractTabItem toSelect) {
268         paneFolder.setSelection(indexOf(toSelect));
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getToolbarParent()
275      */

276     public Composite getToolbarParent() {
277         return paneFolder.getControl();
278     }
279
280     /*
281      * (non-Javadoc)
282      *
283      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getControl()
284      */

285     public Control getControl() {
286         return paneFolder.getControl();
287     }
288
289     public void setUnselectedCloseVisible(boolean visible) {
290         paneFolder.setUnselectedCloseVisible(visible);
291     }
292
293     public void setUnselectedImageVisible(boolean visible) {
294         paneFolder.setUnselectedImageVisible(visible);
295     }
296
297     /*
298      * (non-Javadoc)
299      *
300      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getTabArea()
301      */

302     public Rectangle getTabArea() {
303         return Geometry.toDisplay(paneFolder.getControl(), paneFolder
304                 .getTitleArea());
305     }
306
307     /**
308      * @param enabled
309      * @since 3.1
310      */

311     public void enablePaneMenu(boolean enabled) {
312         if (enabled) {
313             paneFolder.setTopRight(viewToolBar);
314             viewToolBar.setVisible(true);
315         } else {
316             paneFolder.setTopRight(null);
317             viewToolBar.setVisible(false);
318         }
319     }
320
321     /*
322      * (non-Javadoc)
323      *
324      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelectedInfo(org.eclipse.ui.internal.presentations.util.PartInfo)
325      */

326     public void setSelectedInfo(PartInfo info) {
327         String JavaDoc newTitle = DefaultTabItem
328                 .escapeAmpersands(info.contentDescription);
329
330         if (!Util.equals(titleLabel.getText(), newTitle)) {
331             titleLabel.setText(newTitle);
332         }
333
334         if (!info.contentDescription.equals(Util.ZERO_LENGTH_STRING)) {
335             paneFolder.setTopLeft(titleLabel);
336             titleLabel.setVisible(true);
337         } else {
338             paneFolder.setTopLeft(null);
339             titleLabel.setVisible(false);
340         }
341     }
342
343     /*
344      * (non-Javadoc)
345      *
346      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getPaneMenuLocation()
347      */

348     public Point getPaneMenuLocation() {
349         Point toolbarSize = viewToolBar.getSize();
350
351         return viewToolBar.toDisplay(0, toolbarSize.y);
352     }
353
354     /*
355      * (non-Javadoc)
356      *
357      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getPartListLocation()
358      */

359     public Point getPartListLocation() {
360         return paneFolder.getControl().toDisplay(
361                 paneFolder.getChevronLocation());
362     }
363
364     /*
365      * (non-Javadoc)
366      *
367      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getSystemMenuLocation()
368      */

369     public Point getSystemMenuLocation() {
370         Rectangle bounds = DragUtil.getDisplayBounds(paneFolder.getControl());
371
372         int idx = paneFolder.getSelectionIndex();
373         if (idx > -1) {
374             CTabItem item = paneFolder.getItem(idx);
375             Rectangle itemBounds = item.getBounds();
376
377             bounds.x += itemBounds.x;
378             bounds.y += itemBounds.y;
379         }
380
381         Point location = new Point(bounds.x, bounds.y
382                 + paneFolder.getTabHeight());
383
384         return location;
385     }
386
387     /*
388      * (non-Javadoc)
389      *
390      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#isOnBorder(org.eclipse.swt.graphics.Point)
391      */

392     public boolean isOnBorder(Point toTest) {
393         Control content = paneFolder.getContent();
394         if (content != null) {
395             Rectangle displayBounds = DragUtil.getDisplayBounds(content);
396
397             if (paneFolder.getTabPosition() == SWT.TOP) {
398                 return toTest.y >= displayBounds.y;
399             }
400
401             if (toTest.y >= displayBounds.y
402                     && toTest.y < displayBounds.y + displayBounds.height) {
403                 return true;
404             }
405         }
406
407         return super.isOnBorder(toTest);
408     }
409
410     /*
411      * (non-Javadoc)
412      *
413      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#layout(boolean)
414      */

415     public void layout(boolean flushCache) {
416         paneFolder.layout(flushCache);
417         super.layout(flushCache);
418     }
419
420     /*
421      * (non-Javadoc)
422      *
423      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setState(int)
424      */

425     public void setState(int state) {
426         paneFolder.setState(state);
427         super.setState(state);
428     }
429
430     /*
431      * (non-Javadoc)
432      *
433      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setActive(int)
434      */

435     public void setActive(int activeState) {
436         super.setActive(activeState);
437         updateColors(activeState);
438     }
439
440     /*
441      * (non-Javadoc)
442      *
443      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setTabPosition(int)
444      */

445     public void setTabPosition(int tabPosition) {
446         paneFolder.setTabPosition(tabPosition);
447         super.setTabPosition(tabPosition);
448         layout(true);
449     }
450
451     public void flushToolbarSize() {
452         paneFolder.flushTopCenterSize();
453     }
454
455     /*
456      * (non-Javadoc)
457      *
458      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setToolbar(org.eclipse.swt.widgets.Control)
459      */

460     public void setToolbar(Control toolbarControl) {
461         paneFolder.setTopCenter(toolbarControl);
462         super.setToolbar(toolbarControl);
463     }
464
465     public void setColors(DefaultTabFolderColors colors, int activationState,
466             boolean shellActivationState) {
467         Assert.isTrue(activationState < activeShellColors.length);
468
469         if (shellActivationState) {
470             activeShellColors[activationState] = colors;
471         } else {
472             inactiveShellColors[activationState] = colors;
473         }
474
475         if (activationState == getActive()
476                 && shellActive == shellActivationState) {
477             updateColors(activationState);
478         }
479     }
480
481     /**
482      *
483      * @since 3.1
484      */

485     private void updateColors(int activationState) {
486         DefaultTabFolderColors currentColors = shellActive ? activeShellColors[getActive()]
487                 : inactiveShellColors[getActive()];
488
489         paneFolder.setSelectionForeground(currentColors.foreground);
490         paneFolder.setSelectionBackground(currentColors.background,
491                 currentColors.percentages, currentColors.vertical);
492     }
493
494     public void setColors(DefaultTabFolderColors colors, int activationState) {
495         setColors(colors, activationState, true);
496         setColors(colors, activationState, false);
497     }
498
499     /*
500      * (non-Javadoc)
501      *
502      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#shellActive(boolean)
503      */

504     public void shellActive(boolean isActive) {
505         this.shellActive = isActive;
506         super.shellActive(isActive);
507
508         updateColors(-1);
509     }
510
511     /**
512      * @param font
513      * @since 3.1
514      */

515     public void setFont(Font font) {
516         if (font != paneFolder.getControl().getFont()) {
517             paneFolder.getControl().setFont(font);
518             layout(true);
519             paneFolder.setTabHeight(computeTabHeight());
520         }
521     }
522
523     /**
524      * @return the required tab height for this folder.
525      */

526     protected int computeTabHeight() {
527         GC gc = new GC(getControl());
528
529         // Compute the tab height
530
int tabHeight = Math.max(viewToolBar.computeSize(SWT.DEFAULT,
531                 SWT.DEFAULT).y, gc.getFontMetrics().getHeight());
532
533         gc.dispose();
534
535         return tabHeight;
536     }
537
538     /**
539      * @param b
540      * @since 3.1
541      */

542     public void setSingleTab(boolean b) {
543         paneFolder.setSingleTab(b);
544         AbstractTabItem[] items = getItems();
545
546         for (int i = 0; i < items.length; i++) {
547             DefaultTabItem item = (DefaultTabItem) items[i];
548
549             item.updateTabText();
550         }
551
552         layout(true);
553     }
554
555     /*
556      * (non-Javadoc)
557      *
558      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setVisible(boolean)
559      */

560     public void setVisible(boolean visible) {
561         super.setVisible(visible);
562         getFolder().setVisible(visible);
563     }
564
565     /*
566      * (non-Javadoc)
567      *
568      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#showMinMax(boolean)
569      */

570     public void showMinMax(boolean show) {
571         paneFolder.showMinMax(show);
572     }
573 }
574
Popular Tags