KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.jface.util.Geometry;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.custom.CTabFolder;
16 import org.eclipse.swt.custom.CTabFolder2Adapter;
17 import org.eclipse.swt.custom.CTabFolderEvent;
18 import org.eclipse.swt.custom.CTabItem;
19 import org.eclipse.swt.custom.ViewForm;
20 import org.eclipse.swt.events.DisposeEvent;
21 import org.eclipse.swt.events.DisposeListener;
22 import org.eclipse.swt.events.MouseAdapter;
23 import org.eclipse.swt.events.MouseEvent;
24 import org.eclipse.swt.graphics.Cursor;
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.Listener;
32 import org.eclipse.swt.widgets.ToolBar;
33 import org.eclipse.swt.widgets.ToolItem;
34 import org.eclipse.swt.widgets.Widget;
35 import org.eclipse.ui.IPropertyListener;
36 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
37 import org.eclipse.ui.internal.WorkbenchImages;
38 import org.eclipse.ui.internal.WorkbenchMessages;
39 import org.eclipse.ui.internal.dnd.DragUtil;
40 import org.eclipse.ui.internal.layout.CellLayout;
41 import org.eclipse.ui.internal.layout.LayoutUtil;
42 import org.eclipse.ui.internal.layout.Row;
43 import org.eclipse.ui.internal.layout.SizeCache;
44 import org.eclipse.ui.internal.presentations.util.AbstractTabFolder;
45 import org.eclipse.ui.internal.presentations.util.AbstractTabItem;
46 import org.eclipse.ui.internal.presentations.util.PartInfo;
47 import org.eclipse.ui.internal.presentations.util.ProxyControl;
48 import org.eclipse.ui.internal.presentations.util.TabFolderEvent;
49 import org.eclipse.ui.internal.util.Util;
50
51 /**
52  *
53  *
54  *
55  * @since 3.1
56  */

57 public class DetachedViewTabFolder extends AbstractTabFolder {
58
59     private Composite control;
60     private CTabFolder folder;
61     private Control viewToolBar;
62     private ViewForm viewForm;
63     private ProxyControl toolbarProxy;
64     private Composite topLeftControl;
65     private SizeCache toolbarCache;
66     private Cursor dragCursor;
67     
68     private static final String JavaDoc FULL_TITLE = "part_title"; //$NON-NLS-1$
69

70     // CTabFolder listener
71
private CTabFolder2Adapter expandListener = new CTabFolder2Adapter() {
72         /*
73          * (non-Javadoc)
74          *
75          * @see org.eclipse.swt.custom.CTabFolder2Adapter#close(org.eclipse.swt.custom.CTabFolderEvent)
76          */

77         public void close(CTabFolderEvent event) {
78             event.doit = false;
79             fireEvent(TabFolderEvent.EVENT_CLOSE, getTab(event.item));
80         }
81
82         public void showList(CTabFolderEvent event) {
83             event.doit = false;
84             fireEvent(TabFolderEvent.EVENT_SHOW_LIST);
85         }
86
87     };
88     
89     
90     private Listener selectionListener = new Listener() {
91         public void handleEvent(Event e) {
92             fireEvent(TabFolderEvent.EVENT_TAB_SELECTED, getTab(e.item));
93         }
94     };
95     
96     private IPropertyListener systemToolbarListener = new IPropertyListener() {
97
98         public void propertyChanged(Object JavaDoc source, int propId) {
99             Point location;
100             
101             if (propId == TabFolderEvent.EVENT_PANE_MENU) {
102                 location = getPaneMenuLocation();
103             } else {
104                 location = new Point(0,0);
105             }
106             
107             fireEvent(propId, getSelection(), location);
108         }
109         
110     };
111
112     private DisposeListener tabDisposeListener = new DisposeListener() {
113         public void widgetDisposed(DisposeEvent e)
114         {
115             if (e.widget == control) {
116                 disposed();
117             }
118         }
119     };
120     
121     public DetachedViewTabFolder(Composite parent) {
122         control = new Composite(parent, SWT.NONE);
123         control.addDisposeListener(tabDisposeListener);
124         CellLayout layout = new CellLayout(1)
125             .setDefaultRow(Row.fixed())
126             .setDefaultColumn(Row.growing())
127             .setRow(0, Row.growing())
128             .setSpacing(0, 0)
129             .setMargins(0, 0);
130         control.setLayout(layout);
131         dragCursor = parent.getDisplay().getSystemCursor(SWT.CURSOR_SIZEALL);
132         
133         viewForm = new ViewForm(control, SWT.FLAT);
134         attachListeners(viewForm, false);
135         
136         topLeftControl = new Composite(viewForm, SWT.NONE);
137         topLeftControl.setLayout(new CellLayout(0).setDefaultColumn(Row.fixed()).setSpacing(0,0).setMargins(0,0));
138         topLeftControl.setCursor(dragCursor);
139         attachListeners(topLeftControl, false);
140         toolbarProxy = new ProxyControl(topLeftControl);
141         viewForm.setTopLeft(topLeftControl);
142        
143         toolbarCache = new SizeCache();
144         
145         folder = new CTabFolder(control, SWT.BOTTOM);
146         folder.setMinimizeVisible(false);
147         folder.setMaximizeVisible(false);
148         folder.setUnselectedCloseVisible(true);
149         folder.setUnselectedImageVisible(true);
150         folder.addListener(SWT.Selection, selectionListener);
151         folder.addCTabFolder2Listener(expandListener);
152         attachListeners(folder, false);
153         
154         // Initialize view menu dropdown
155
{
156             ToolBar actualToolBar = new ToolBar(viewForm, SWT.FLAT | SWT.NO_BACKGROUND);
157             viewToolBar = actualToolBar;
158             
159             ToolItem pullDownButton = new ToolItem(actualToolBar, SWT.PUSH);
160             Image hoverImage = WorkbenchImages
161                     .getImage(IWorkbenchGraphicConstants.IMG_LCL_RENDERED_VIEW_MENU);
162             pullDownButton.setDisabledImage(hoverImage);
163             pullDownButton.setImage(hoverImage);
164             pullDownButton.setToolTipText(WorkbenchMessages.Menu);
165             actualToolBar.addMouseListener(new MouseAdapter() {
166                 public void mouseDown(MouseEvent e) {
167                     fireEvent(TabFolderEvent.EVENT_PANE_MENU, getSelection(), getPaneMenuLocation());
168                 }
169             });
170         }
171         
172         viewForm.setTopRight(viewToolBar);
173     }
174
175     protected void disposed() {
176
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 new Point(50, 50);
184     }
185
186     /* (non-Javadoc)
187      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#add(int)
188      */

189     public AbstractTabItem add(int index, int flags) {
190         DetachedViewTabItem item = new DetachedViewTabItem(this, index, flags);
191         item.getWidget().setData(item);
192         item.getWidget().addDisposeListener(tabDisposeListener);
193         
194         if (folder.getItemCount() == 2) {
195             layout(true);
196         }
197         
198         return item;
199     }
200     
201     /* (non-Javadoc)
202      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#layout(boolean)
203      */

204     public void layout(boolean flushCache) {
205         
206         Rectangle oldBounds = viewForm.getBounds();
207         
208         CellLayout layout = (CellLayout)control.getLayout();
209         if (folder.getItemCount() < 2) {
210             layout.setRow(1, Row.fixed(0));
211         } else {
212             layout.setRow(1, Row.fixed(folder.getTabHeight() + 2));
213         }
214         
215         super.layout(flushCache);
216         
217         control.layout(flushCache);
218         
219         Rectangle newBounds = viewForm.getBounds();
220         
221         if (Util.equals(oldBounds, newBounds)) {
222             viewForm.layout(flushCache);
223         }
224     }
225     
226     /* (non-Javadoc)
227      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getClientArea()
228      */

229     public Rectangle getClientArea() {
230         Control content = viewForm.getContent();
231         
232         if (content == null) {
233             return new Rectangle(0,0,0,0);
234         }
235         
236         return Geometry.toControl(control, DragUtil.getDisplayBounds(content));
237     }
238
239     /* (non-Javadoc)
240      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getItems()
241      */

242     public AbstractTabItem[] getItems() {
243         CTabItem[] items = folder.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      * @param item
256      * @return
257      * @since 3.1
258      */

259     private AbstractTabItem getTab(Widget item) {
260         return (AbstractTabItem)item.getData();
261     }
262
263     /* (non-Javadoc)
264      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelection(org.eclipse.ui.internal.presentations.util.Widget)
265      */

266     public void setSelection(AbstractTabItem toSelect) {
267         if (toSelect == null) {
268             return;
269         }
270         
271         DetachedViewTabItem tab = (DetachedViewTabItem) toSelect;
272         folder.setSelection((CTabItem)tab.getWidget());
273     }
274
275     /* (non-Javadoc)
276      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setSelectedInfo(org.eclipse.ui.internal.presentations.util.PartInfo)
277      */

278     public void setSelectedInfo(PartInfo info) {
279     }
280     /* (non-Javadoc)
281      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getToolbarParent()
282      */

283     public Composite getToolbarParent() {
284         return viewForm;
285     }
286
287     /* (non-Javadoc)
288      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getTabArea()
289      */

290     public Rectangle getTabArea() {
291         return Geometry.toDisplay(folder.getParent(), folder.getBounds());
292     }
293     
294     /* (non-Javadoc)
295      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setToolbar(org.eclipse.swt.widgets.Control)
296      */

297     public void setToolbar(Control toolbarControl) {
298         
299         if (toolbarControl == getToolbar()) {
300             return;
301         }
302         
303         if (toolbarControl != null) {
304             toolbarCache.setControl(toolbarControl);
305             toolbarProxy.setTarget(toolbarCache);
306             toolbarProxy.getControl().getParent().changed(new Control[]{toolbarProxy.getControl()});
307             LayoutUtil.resize(toolbarProxy.getControl());
308
309             toolbarProxy.layout();
310
311         } else {
312             toolbarCache.setControl(null);
313             toolbarProxy.setTarget(null);
314         }
315         
316         super.setToolbar(toolbarControl);
317     }
318     
319     public Control getControl() {
320         return control;
321     }
322     
323     /* (non-Javadoc)
324      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#isOnBorder(org.eclipse.swt.graphics.Point)
325      */

326     public boolean isOnBorder(Point globalPos) {
327         Point localPos = getControl().toControl(globalPos);
328         
329         Rectangle clientArea = getClientArea();
330         return localPos.y > clientArea.y && localPos.y < clientArea.y + clientArea.height;
331     }
332     
333     public AbstractTabItem getSelection() {
334         CTabItem sel = folder.getSelection();
335         
336         if (sel == null) {
337             return null;
338         }
339         
340         return getTab(sel);
341     }
342     
343     /* (non-Javadoc)
344      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#getContentParent()
345      */

346     public Composite getContentParent() {
347         return viewForm;
348     }
349     
350     /* (non-Javadoc)
351      * @see org.eclipse.ui.internal.presentations.util.AbstractTabFolder#setContent(org.eclipse.swt.widgets.Control)
352      */

353     public void setContent(Control newContent) {
354         viewForm.setContent(newContent);
355     }
356     
357     /**
358      * @return
359      * @since 3.1
360      */

361     public CTabFolder getTabFolder() {
362         return folder;
363     }
364     
365     protected void handleDragStarted(Point displayPos, Event e) {
366         if (isOnBorder(displayPos)) {
367             return;
368         }
369         
370         AbstractTabItem tab = null;
371         
372         if (DragUtil.getDisplayBounds(viewForm).contains(displayPos)) {
373             tab = getSelection();
374         } else {
375             tab = getItem(displayPos);
376         }
377         
378         fireEvent(TabFolderEvent.EVENT_DRAG_START, tab, displayPos);
379     }
380
381     public Point getPartListLocation() {
382
383         // get the last visible item
384
int numItems = folder.getItemCount();
385         CTabItem item = null, tempItem = null;
386         for (int i = 0; i < numItems; i++) {
387             tempItem = folder.getItem(i);
388             if (tempItem.isShowing())
389                 item = tempItem;
390         }
391
392         // if we have no visible tabs, abort.
393
if (item == null)
394             return new Point(0, 0);
395
396         Rectangle itemBounds = item.getBounds();
397         int x = itemBounds.x + itemBounds.width;
398         int y = itemBounds.y + itemBounds.height;
399         return folder.toDisplay(new Point(x, y));
400     }
401     
402     public Point getPaneMenuLocation() {
403         Point toolbarSize = viewToolBar.getSize();
404         
405         return viewToolBar.toDisplay(0,toolbarSize.y);
406     }
407     
408     public void enablePaneMenu(boolean enabled) {
409         if (enabled) {
410             viewToolBar.setVisible(true);
411         } else {
412             viewToolBar.setVisible(false);
413         }
414     }
415
416     public void itemRemoved() {
417         if (folder.getItemCount() == 1 && !control.isDisposed() && !viewForm.isDisposed()) {
418             layout(true);
419         }
420     }
421
422 }
423
Popular Tags