001: /*******************************************************************************
002: * Copyright (c) 2004, 2005 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.defaultpresentation;
011:
012: import org.eclipse.jface.util.Geometry;
013: import org.eclipse.swt.SWT;
014: import org.eclipse.swt.custom.CLabel;
015: import org.eclipse.swt.custom.ViewForm;
016: import org.eclipse.swt.graphics.Image;
017: import org.eclipse.swt.graphics.Point;
018: import org.eclipse.swt.graphics.Rectangle;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021: import org.eclipse.swt.widgets.Event;
022: import org.eclipse.swt.widgets.Listener;
023: import org.eclipse.swt.widgets.TabFolder;
024: import org.eclipse.swt.widgets.TabItem;
025: import org.eclipse.swt.widgets.Widget;
026: import org.eclipse.ui.IPropertyListener;
027: import org.eclipse.ui.internal.dnd.DragUtil;
028: import org.eclipse.ui.internal.layout.SizeCache;
029: import org.eclipse.ui.internal.presentations.util.AbstractTabFolder;
030: import org.eclipse.ui.internal.presentations.util.AbstractTabItem;
031: import org.eclipse.ui.internal.presentations.util.PartInfo;
032: import org.eclipse.ui.internal.presentations.util.ProxyControl;
033: import org.eclipse.ui.internal.presentations.util.StandardSystemToolbar;
034: import org.eclipse.ui.internal.presentations.util.TabFolderEvent;
035: import org.eclipse.ui.internal.util.Util;
036:
037: /**
038: * @since 3.1
039: */
040: public class NativeTabFolder extends AbstractTabFolder {
041:
042: private TabFolder control;
043: private ViewForm viewForm;
044: private StandardSystemToolbar systemToolbar;
045: private CLabel title;
046: private ProxyControl topCenter;
047: private SizeCache topCenterCache;
048:
049: private Listener selectionListener = new Listener() {
050: public void handleEvent(Event e) {
051: fireEvent(TabFolderEvent.EVENT_TAB_SELECTED, getTab(e.item));
052: }
053: };
054:
055: private IPropertyListener systemToolbarListener = new IPropertyListener() {
056:
057: public void propertyChanged(Object source, int propId) {
058: Point location;
059:
060: if (propId == TabFolderEvent.EVENT_PANE_MENU) {
061: location = getPaneMenuLocation();
062: } else {
063: location = new Point(0, 0);
064: }
065:
066: fireEvent(propId, getSelection(), location);
067: }
068:
069: };
070:
071: public NativeTabFolder(Composite parent) {
072: control = new TabFolder(parent, SWT.BOTTOM);
073: control.addListener(SWT.Selection, selectionListener);
074: attachListeners(control, false);
075:
076: viewForm = new ViewForm(control, SWT.FLAT);
077: attachListeners(viewForm, false);
078: systemToolbar = new StandardSystemToolbar(viewForm, true,
079: false, true, true, true);
080: systemToolbar.addListener(systemToolbarListener);
081: viewForm.setTopRight(systemToolbar.getControl());
082:
083: topCenter = new ProxyControl(viewForm);
084: topCenterCache = new SizeCache();
085:
086: title = new CLabel(viewForm, SWT.LEFT);
087: attachListeners(title, false);
088: viewForm.setTopLeft(title);
089: }
090:
091: /* (non-Javadoc)
092: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#computeSize(int, int)
093: */
094: public Point computeSize(int widthHint, int heightHint) {
095: return new Point(50, 50);
096: }
097:
098: /* (non-Javadoc)
099: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#add(int)
100: */
101: public AbstractTabItem add(int index, int flags) {
102: NativeTabItem item = new NativeTabItem(this , index);
103: item.getWidget().setData(item);
104:
105: return item;
106: }
107:
108: /* (non-Javadoc)
109: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#layout(boolean)
110: */
111: public void layout(boolean flushCache) {
112: super .layout(flushCache);
113:
114: Rectangle oldBounds = viewForm.getBounds();
115: Rectangle newBounds = control.getClientArea();
116:
117: viewForm.setBounds(newBounds);
118:
119: if (Util.equals(oldBounds, newBounds)) {
120: viewForm.layout(flushCache);
121: }
122: }
123:
124: /* (non-Javadoc)
125: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getPaneMenuLocation()
126: */
127: public Point getPaneMenuLocation() {
128: return systemToolbar.getPaneMenuLocation();
129: }
130:
131: /* (non-Javadoc)
132: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setState(int)
133: */
134: public void setState(int state) {
135: super .setState(state);
136:
137: systemToolbar.setState(state);
138: }
139:
140: /* (non-Javadoc)
141: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getClientArea()
142: */
143: public Rectangle getClientArea() {
144: Control content = viewForm.getContent();
145:
146: if (content == null) {
147: return new Rectangle(0, 0, 0, 0);
148: }
149:
150: return Geometry.toControl(control, DragUtil
151: .getDisplayBounds(content));
152: }
153:
154: /* (non-Javadoc)
155: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItems()
156: */
157: public AbstractTabItem[] getItems() {
158: TabItem[] items = control.getItems();
159:
160: AbstractTabItem[] result = new AbstractTabItem[items.length];
161:
162: for (int i = 0; i < result.length; i++) {
163: result[i] = getTab(items[i]);
164: }
165:
166: return result;
167: }
168:
169: /**
170: * @param item
171: * @return
172: * @since 3.1
173: */
174: private AbstractTabItem getTab(Widget item) {
175: return (AbstractTabItem) item.getData();
176: }
177:
178: /* (non-Javadoc)
179: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelection(org.eclipse.ui.internal.presentations.util.Widget)
180: */
181: public void setSelection(AbstractTabItem toSelect) {
182: if (toSelect == null) {
183: return;
184: }
185:
186: NativeTabItem tab = (NativeTabItem) toSelect;
187: control
188: .setSelection(new TabItem[] { (TabItem) tab.getWidget() });
189: }
190:
191: /* (non-Javadoc)
192: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelectedInfo(org.eclipse.ui.internal.presentations.util.PartInfo)
193: */
194: public void setSelectedInfo(PartInfo info) {
195: if (!Util.equals(title.getText(), info.title)) {
196: title.setText(info.title);
197: }
198: if (title.getImage() != info.image) {
199: title.setImage(info.image);
200: }
201: }
202:
203: /* (non-Javadoc)
204: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getToolbarParent()
205: */
206: public Composite getToolbarParent() {
207: return viewForm;
208: }
209:
210: /* (non-Javadoc)
211: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getTabArea()
212: */
213: public Rectangle getTabArea() {
214:
215: Rectangle bounds = control.getBounds();
216:
217: Rectangle clientArea = control.getClientArea();
218:
219: bounds.x = 0;
220: bounds.y = 0;
221: Geometry.expand(bounds, 0, 0,
222: -(clientArea.height + clientArea.y), 0);
223:
224: return Geometry.toDisplay(control.getParent(), bounds);
225: }
226:
227: /* (non-Javadoc)
228: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setToolbar(org.eclipse.swt.widgets.Control)
229: */
230: public void setToolbar(Control toolbarControl) {
231:
232: if (toolbarControl != null) {
233: topCenterCache.setControl(toolbarControl);
234: topCenter.setTarget(topCenterCache);
235: viewForm.setTopCenter(topCenter.getControl());
236: } else {
237: topCenterCache.setControl(null);
238: topCenter.setTarget(null);
239: viewForm.setTopCenter(null);
240: }
241:
242: super .setToolbar(toolbarControl);
243: }
244:
245: public Control getControl() {
246: return control;
247: }
248:
249: /* (non-Javadoc)
250: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#isOnBorder(org.eclipse.swt.graphics.Point)
251: */
252: public boolean isOnBorder(Point globalPos) {
253: Point localPos = getControl().toControl(globalPos);
254:
255: Rectangle clientArea = getClientArea();
256: return localPos.y > clientArea.y
257: && localPos.y < clientArea.y + clientArea.height;
258: }
259:
260: public AbstractTabItem getSelection() {
261: TabItem[] sel = control.getSelection();
262:
263: if (sel.length == 0) {
264: return null;
265: }
266:
267: return getTab(sel[0]);
268: }
269:
270: /* (non-Javadoc)
271: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getContentParent()
272: */
273: public Composite getContentParent() {
274: return viewForm;
275: }
276:
277: /* (non-Javadoc)
278: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setContent(org.eclipse.swt.widgets.Control)
279: */
280: public void setContent(Control newContent) {
281: viewForm.setContent(newContent);
282: }
283:
284: /**
285: * @return
286: * @since 3.1
287: */
288: public TabFolder getTabFolder() {
289: return control;
290: }
291:
292: /**
293: * @param item
294: * @param newTitle
295: * @since 3.1
296: */
297: /* protected */void setSelectedTitle(String newTitle) {
298: title.setText(newTitle);
299: }
300:
301: /**
302: * @param image
303: * @since 3.1
304: */
305: /* protected */void setSelectedImage(Image image) {
306: title.setImage(image);
307: }
308:
309: /* (non-Javadoc)
310: * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItem(org.eclipse.swt.graphics.Point)
311: */
312: public AbstractTabItem getItem(Point toFind) {
313: return getSelection();
314: }
315:
316: /**
317: * @param enabled
318: * @since 3.1
319: */
320: public void enablePaneMenu(boolean enabled) {
321: systemToolbar.enablePaneMenu(enabled);
322: }
323: }
|