001: /*******************************************************************************
002: * Copyright (c) 2004, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.presentations.r33;
011:
012: import org.eclipse.core.runtime.Assert;
013: import org.eclipse.jface.util.Geometry;
014: import org.eclipse.swt.SWT;
015: import org.eclipse.swt.custom.CTabFolder;
016: import org.eclipse.swt.custom.CTabFolderEvent;
017: import org.eclipse.swt.custom.CTabItem;
018: import org.eclipse.swt.events.MouseAdapter;
019: import org.eclipse.swt.events.MouseEvent;
020: import org.eclipse.swt.events.SelectionAdapter;
021: import org.eclipse.swt.events.SelectionEvent;
022: import org.eclipse.swt.graphics.Font;
023: import org.eclipse.swt.graphics.GC;
024: import org.eclipse.swt.graphics.Image;
025: import org.eclipse.swt.graphics.Point;
026: import org.eclipse.swt.graphics.Rectangle;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Event;
030: import org.eclipse.swt.widgets.Label;
031: import org.eclipse.swt.widgets.Listener;
032: import org.eclipse.swt.widgets.ToolBar;
033: import org.eclipse.swt.widgets.ToolItem;
034: import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
035: import org.eclipse.ui.internal.WorkbenchImages;
036: import org.eclipse.ui.internal.WorkbenchMessages;
037: import org.eclipse.ui.internal.dnd.DragUtil;
038: import org.eclipse.ui.internal.presentations.util.AbstractTabFolder;
039: import org.eclipse.ui.internal.presentations.util.AbstractTabItem;
040: import org.eclipse.ui.internal.presentations.util.PartInfo;
041: import org.eclipse.ui.internal.presentations.util.TabFolderEvent;
042: import org.eclipse.ui.internal.util.Util;
043:
044: /**
045: * @since 3.1
046: */
047: public class DefaultTabFolder extends AbstractTabFolder {
048:
049: private PaneFolder paneFolder;
050: private Control viewToolBar;
051: private Label titleLabel;
052:
053: private PaneFolderButtonListener buttonListener = new PaneFolderButtonListener() {
054: public void stateButtonPressed(int buttonId) {
055: fireEvent(TabFolderEvent.stackStateToEventId(buttonId));
056: }
057:
058: /**
059: * Called when a close button is pressed.
060: *
061: * @param item
062: * the tab whose close button was pressed
063: */
064: public void closeButtonPressed(CTabItem item) {
065: fireEvent(TabFolderEvent.EVENT_CLOSE, getTab(item));
066: }
067:
068: /**
069: *
070: * @since 3.0
071: */
072: public void showList(CTabFolderEvent event) {
073: event.doit = false;
074: fireEvent(TabFolderEvent.EVENT_SHOW_LIST);
075: }
076: };
077:
078: private Listener selectionListener = new Listener() {
079: public void handleEvent(Event e) {
080: AbstractTabItem item = getTab((CTabItem) e.item);
081:
082: if (item != null) {
083: fireEvent(TabFolderEvent.EVENT_TAB_SELECTED, item);
084: }
085: }
086: };
087:
088: private static DefaultTabFolderColors defaultColors = new DefaultTabFolderColors();
089:
090: private DefaultTabFolderColors[] activeShellColors = {
091: defaultColors, defaultColors, defaultColors };
092: private DefaultTabFolderColors[] inactiveShellColors = {
093: defaultColors, defaultColors, defaultColors };
094: private boolean shellActive = false;
095:
096: /**
097: * Create a new instance of the receiver
098: *
099: * @param parent
100: * @param flags
101: * @param allowMin
102: * @param allowMax
103: */
104: public DefaultTabFolder(Composite parent, int flags,
105: boolean allowMin, boolean allowMax) {
106: paneFolder = new PaneFolder(parent, flags | SWT.NO_BACKGROUND);
107: paneFolder.addButtonListener(buttonListener);
108: paneFolder.setMinimizeVisible(allowMin);
109: paneFolder.setMaximizeVisible(allowMax);
110: paneFolder.getControl().addListener(SWT.Selection,
111: selectionListener);
112: paneFolder.setTopRight(null);
113:
114: // Initialize view menu dropdown
115: {
116: ToolBar actualToolBar = new ToolBar(
117: paneFolder.getControl(), SWT.FLAT
118: | SWT.NO_BACKGROUND);
119: viewToolBar = actualToolBar;
120:
121: ToolItem pullDownButton = new ToolItem(actualToolBar,
122: SWT.PUSH);
123: Image hoverImage = WorkbenchImages
124: .getImage(IWorkbenchGraphicConstants.IMG_LCL_RENDERED_VIEW_MENU);
125: pullDownButton.setDisabledImage(hoverImage);
126: pullDownButton.setImage(hoverImage);
127: pullDownButton.setToolTipText(WorkbenchMessages.Menu);
128: actualToolBar.addMouseListener(new MouseAdapter() {
129: public void mouseDown(MouseEvent e) {
130: fireEvent(TabFolderEvent.EVENT_PANE_MENU,
131: getSelection(), getPaneMenuLocation());
132: }
133: });
134: pullDownButton.addSelectionListener(new SelectionAdapter() {
135: public void widgetSelected(SelectionEvent e) {
136: fireEvent(TabFolderEvent.EVENT_PANE_MENU,
137: getSelection(), getPaneMenuLocation());
138:
139: super .widgetSelected(e);
140: }
141: });
142: }
143:
144: // Initialize content description label
145: {
146: titleLabel = new Label(paneFolder.getControl(), SWT.NONE);
147: titleLabel.moveAbove(null);
148: titleLabel.setVisible(false);
149: attachListeners(titleLabel, false);
150: }
151:
152: attachListeners(paneFolder.getControl(), false);
153: attachListeners(paneFolder.getViewForm(), false);
154:
155: paneFolder.setTabHeight(computeTabHeight());
156:
157: viewToolBar.moveAbove(null);
158: }
159:
160: /**
161: * Changes the minimum number of characters to display in the pane folder
162: * tab. This control how much information will be displayed to the user.
163: *
164: * @param count
165: * The number of characters to display in the tab folder; this
166: * value should be a positive integer.
167: * @see org.eclipse.swt.custom.CTabFolder#setMinimumCharacters(int)
168: * @since 3.1
169: */
170: public void setMinimumCharacters(int count) {
171: paneFolder.setMinimumCharacters(count);
172: }
173:
174: public void setSimpleTabs(boolean simple) {
175: paneFolder.setSimpleTab(simple);
176: }
177:
178: /**
179: * @param item
180: * @return
181: * @since 3.1
182: */
183: protected DefaultTabItem getTab(CTabItem item) {
184: return (DefaultTabItem) item.getData();
185: }
186:
187: /*
188: * (non-Javadoc)
189: *
190: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#computeSize(int,
191: * int)
192: */
193: public Point computeSize(int widthHint, int heightHint) {
194: return paneFolder.computeMinimumSize();
195: }
196:
197: /* package */PaneFolder getFolder() {
198: return paneFolder;
199: }
200:
201: public AbstractTabItem getSelection() {
202: return getTab(paneFolder.getSelection());
203: }
204:
205: /*
206: * (non-Javadoc)
207: *
208: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#add(int)
209: */
210: public AbstractTabItem add(int index, int flags) {
211: DefaultTabItem result = new DefaultTabItem(
212: (CTabFolder) getFolder().getControl(), index, flags);
213:
214: result.getWidget().setData(result);
215:
216: return result;
217: }
218:
219: /*
220: * (non-Javadoc)
221: *
222: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getContentParent()
223: */
224: public Composite getContentParent() {
225: return paneFolder.getContentParent();
226: }
227:
228: /*
229: * (non-Javadoc)
230: *
231: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setContent(org.eclipse.swt.widgets.Control)
232: */
233: public void setContent(Control newContent) {
234: paneFolder.setContent(newContent);
235: }
236:
237: /*
238: * (non-Javadoc)
239: *
240: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItems()
241: */
242: public AbstractTabItem[] getItems() {
243: CTabItem[] items = paneFolder.getItems();
244:
245: AbstractTabItem[] result = new AbstractTabItem[items.length];
246:
247: for (int i = 0; i < result.length; i++) {
248: result[i] = getTab(items[i]);
249: }
250:
251: return result;
252: }
253:
254: /*
255: * (non-Javadoc)
256: *
257: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItemCount()
258: */
259: public int getItemCount() {
260: // Override retrieving all the items when we just want the count.
261: return paneFolder.getItemCount();
262: }
263:
264: /*
265: * (non-Javadoc)
266: *
267: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelection(org.eclipse.ui.internal.presentations.util.AbstractTabItem)
268: */
269: public void setSelection(AbstractTabItem toSelect) {
270: paneFolder.setSelection(indexOf(toSelect));
271: }
272:
273: /*
274: * (non-Javadoc)
275: *
276: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getToolbarParent()
277: */
278: public Composite getToolbarParent() {
279: return paneFolder.getControl();
280: }
281:
282: /*
283: * (non-Javadoc)
284: *
285: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getControl()
286: */
287: public Control getControl() {
288: return paneFolder.getControl();
289: }
290:
291: public void setUnselectedCloseVisible(boolean visible) {
292: paneFolder.setUnselectedCloseVisible(visible);
293: }
294:
295: public void setUnselectedImageVisible(boolean visible) {
296: paneFolder.setUnselectedImageVisible(visible);
297: }
298:
299: /*
300: * (non-Javadoc)
301: *
302: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getTabArea()
303: */
304: public Rectangle getTabArea() {
305: return Geometry.toDisplay(paneFolder.getControl(), paneFolder
306: .getTitleArea());
307: }
308:
309: /**
310: * @param enabled
311: * @since 3.1
312: */
313: public void enablePaneMenu(boolean enabled) {
314: if (enabled) {
315: paneFolder.setTopRight(viewToolBar);
316: viewToolBar.setVisible(true);
317: } else {
318: paneFolder.setTopRight(null);
319: viewToolBar.setVisible(false);
320: }
321: }
322:
323: /*
324: * (non-Javadoc)
325: *
326: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelectedInfo(org.eclipse.ui.internal.presentations.util.PartInfo)
327: */
328: public void setSelectedInfo(PartInfo info) {
329: String newTitle = DefaultTabItem
330: .escapeAmpersands(info.contentDescription);
331:
332: if (!Util.equals(titleLabel.getText(), newTitle)) {
333: titleLabel.setText(newTitle);
334: }
335:
336: if (!info.contentDescription.equals(Util.ZERO_LENGTH_STRING)) {
337: paneFolder.setTopLeft(titleLabel);
338: titleLabel.setVisible(true);
339: } else {
340: paneFolder.setTopLeft(null);
341: titleLabel.setVisible(false);
342: }
343: }
344:
345: /*
346: * (non-Javadoc)
347: *
348: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getPaneMenuLocation()
349: */
350: public Point getPaneMenuLocation() {
351: Point toolbarSize = viewToolBar.getSize();
352:
353: return viewToolBar.toDisplay(0, toolbarSize.y);
354: }
355:
356: /*
357: * (non-Javadoc)
358: *
359: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getPartListLocation()
360: */
361: public Point getPartListLocation() {
362: return paneFolder.getControl().toDisplay(
363: paneFolder.getChevronLocation());
364: }
365:
366: /*
367: * (non-Javadoc)
368: *
369: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getSystemMenuLocation()
370: */
371: public Point getSystemMenuLocation() {
372: Rectangle bounds = DragUtil.getDisplayBounds(paneFolder
373: .getControl());
374:
375: int idx = paneFolder.getSelectionIndex();
376: if (idx > -1) {
377: CTabItem item = paneFolder.getItem(idx);
378: Rectangle itemBounds = item.getBounds();
379:
380: bounds.x += itemBounds.x;
381: bounds.y += itemBounds.y;
382: }
383:
384: Point location = new Point(bounds.x, bounds.y
385: + paneFolder.getTabHeight());
386:
387: return location;
388: }
389:
390: /*
391: * (non-Javadoc)
392: *
393: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#isOnBorder(org.eclipse.swt.graphics.Point)
394: */
395: public boolean isOnBorder(Point toTest) {
396: Control content = paneFolder.getContent();
397: if (content != null) {
398: Rectangle displayBounds = DragUtil
399: .getDisplayBounds(content);
400:
401: if (paneFolder.getTabPosition() == SWT.TOP) {
402: return toTest.y >= displayBounds.y;
403: }
404:
405: if (toTest.y >= displayBounds.y
406: && toTest.y < displayBounds.y
407: + displayBounds.height) {
408: return true;
409: }
410: }
411:
412: return super .isOnBorder(toTest);
413: }
414:
415: /*
416: * (non-Javadoc)
417: *
418: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#layout(boolean)
419: */
420: public void layout(boolean flushCache) {
421: paneFolder.layout(flushCache);
422: super .layout(flushCache);
423: }
424:
425: /*
426: * (non-Javadoc)
427: *
428: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setState(int)
429: */
430: public void setState(int state) {
431: paneFolder.setState(state);
432: super .setState(state);
433: }
434:
435: /*
436: * (non-Javadoc)
437: *
438: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setActive(int)
439: */
440: public void setActive(int activeState) {
441: super .setActive(activeState);
442: updateColors(activeState);
443: }
444:
445: /*
446: * (non-Javadoc)
447: *
448: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setTabPosition(int)
449: */
450: public void setTabPosition(int tabPosition) {
451: paneFolder.setTabPosition(tabPosition);
452: super .setTabPosition(tabPosition);
453: layout(true);
454: }
455:
456: public void flushToolbarSize() {
457: paneFolder.flushTopCenterSize();
458: }
459:
460: /*
461: * (non-Javadoc)
462: *
463: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setToolbar(org.eclipse.swt.widgets.Control)
464: */
465: public void setToolbar(Control toolbarControl) {
466: paneFolder.setTopCenter(toolbarControl);
467: super .setToolbar(toolbarControl);
468: }
469:
470: public void setColors(DefaultTabFolderColors colors,
471: int activationState, boolean shellActivationState) {
472: Assert.isTrue(activationState < activeShellColors.length);
473:
474: if (shellActivationState) {
475: activeShellColors[activationState] = colors;
476: } else {
477: inactiveShellColors[activationState] = colors;
478: }
479:
480: if (activationState == getActive()
481: && shellActive == shellActivationState) {
482: updateColors(activationState);
483: }
484: }
485:
486: /**
487: *
488: * @since 3.1
489: */
490: private void updateColors(int activationState) {
491: DefaultTabFolderColors currentColors = shellActive ? activeShellColors[getActive()]
492: : inactiveShellColors[getActive()];
493:
494: paneFolder.setSelectionForeground(currentColors.foreground);
495: paneFolder.setSelectionBackground(currentColors.background,
496: currentColors.percentages, currentColors.vertical);
497: }
498:
499: public void setColors(DefaultTabFolderColors colors,
500: int activationState) {
501: setColors(colors, activationState, true);
502: setColors(colors, activationState, false);
503: }
504:
505: /*
506: * (non-Javadoc)
507: *
508: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#shellActive(boolean)
509: */
510: public void shellActive(boolean isActive) {
511: this .shellActive = isActive;
512: super .shellActive(isActive);
513:
514: updateColors(-1);
515: }
516:
517: /**
518: * @param font
519: * @since 3.1
520: */
521: public void setFont(Font font) {
522: if (font != paneFolder.getControl().getFont()) {
523: paneFolder.getControl().setFont(font);
524: layout(true);
525: paneFolder.setTabHeight(computeTabHeight());
526: }
527: }
528:
529: /**
530: * @return the required tab height for this folder.
531: */
532: protected int computeTabHeight() {
533: GC gc = new GC(getControl());
534:
535: // Compute the tab height
536: int tabHeight = Math.max(viewToolBar.computeSize(SWT.DEFAULT,
537: SWT.DEFAULT).y, gc.getFontMetrics().getHeight());
538:
539: gc.dispose();
540:
541: return tabHeight;
542: }
543:
544: /**
545: * @param b
546: * @since 3.1
547: */
548: public void setSingleTab(boolean b) {
549: paneFolder.setSingleTab(b);
550: AbstractTabItem[] items = getItems();
551:
552: for (int i = 0; i < items.length; i++) {
553: DefaultTabItem item = (DefaultTabItem) items[i];
554:
555: item.updateTabText();
556: }
557:
558: layout(true);
559: }
560:
561: /*
562: * (non-Javadoc)
563: *
564: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setVisible(boolean)
565: */
566: public void setVisible(boolean visible) {
567: super .setVisible(visible);
568: getFolder().setVisible(visible);
569: }
570:
571: /*
572: * (non-Javadoc)
573: *
574: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#showMinMax(boolean)
575: */
576: public void showMinMax(boolean show) {
577: paneFolder.showMinMax(show);
578: }
579: }
|