KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > r21 > widgets > R21PaneFolder


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.r21.widgets;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.MouseAdapter;
19 import org.eclipse.swt.events.MouseEvent;
20 import org.eclipse.swt.events.MouseListener;
21 import org.eclipse.swt.graphics.Color;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.ui.internal.layout.SizeCache;
27 import org.eclipse.ui.internal.presentations.util.ProxyControl;
28 import org.eclipse.ui.presentations.IStackPresentationSite;
29
30 /**
31  * This class implements the tab folders that contains can contain two toolbars and
32  * status text. Wherever possible, the toolbars are aligned with the tabs.
33  * If there is not enough room beside the tabs, the toolbars are aligned with the status text. This
34  * is the same tab folder that is used to arrange views and editors in Eclipse.
35  * <p>
36  * This is closely related to DefaultPartPresentation, but they have different responsibilities. This
37  * is essentially a CTabFolder that can manage a toolbar. It should not depend on
38  * data structures from the workbench, and its public interface should only use SWT objects or
39  * listeners. DefaultPartPresentation uses a PaneFolder to arrange views or editors. Knowledge
40  * of higher-level data structures should go there.
41  * </p>
42  * <p>
43  * Although it is not actually a control, the public interface is much like
44  * an SWT control. Implementation-wise, this is actually a combination of a CTabFolder and
45  * a ViewForm. It encapsulates the details of moving the toolbar between the CTabFolder and
46  * the ViewForm, and provides a simpler interface to the ViewForm/CTabFolder.
47  * </p>
48  *
49  * @since 3.0
50  */

51 public final class R21PaneFolder {
52     // Tab folder and associated proxy controls
53
private CTabFolder tabFolder;
54
55     // private Control titleAreaProxy;
56

57     // View form and associated proxy controls
58
private ViewForm viewForm;
59
60     private ProxyControl contentProxy;
61
62     private ProxyControl viewFormTopLeftProxy;
63
64     private ProxyControl viewFormTopRightProxy;
65
66     private ProxyControl viewFormTopCenterProxy;
67
68     // Cached sizes of the top-right and top-center controls
69
private SizeCache topRightCache = new SizeCache();
70
71     private SizeCache topCenterCache = new SizeCache();
72
73     private SizeCache topLeftCache = new SizeCache();
74
75     private int tabPos;
76
77     private boolean putTrimOnTop = false;
78
79     /**
80      * List of PaneFolderButtonListener
81      */

82     private List JavaDoc buttonListeners = new ArrayList JavaDoc(1);
83
84     private int state = IStackPresentationSite.STATE_RESTORED;
85
86     /**
87      * State of the folder at the last mousedown event. This is used to prevent
88      * a mouseup over the minimize or maximize buttons from undoing a state change
89      * that was caused by the mousedown.
90      */

91     private int mousedownState = -1;
92
93     // // CTabFolder listener
94
// private CTabFolder2Adapter expandListener = new CTabFolder2Adapter() {
95
// public void minimize(CTabFolderEvent event) {
96
// event.doit = false;
97
// notifyButtonListeners(IStackPresentationSite.STATE_MINIMIZED);
98
// }
99
//
100
// public void restore(CTabFolderEvent event) {
101
// event.doit = false;
102
// notifyButtonListeners(IStackPresentationSite.STATE_RESTORED);
103
// }
104
//
105
// public void maximize(CTabFolderEvent event) {
106
// event.doit = false;
107
// notifyButtonListeners(IStackPresentationSite.STATE_MAXIMIZED);
108
// }
109
//
110
// /* (non-Javadoc)
111
// * @see org.eclipse.swt.custom.CTabFolder2Adapter#close(org.eclipse.swt.custom.CTabFolderEvent)
112
// */
113
// public void close(CTabFolderEvent event) {
114
// event.doit = false;
115
// notifyCloseListeners((CTabItem)event.item);
116
// }
117
//
118
// public void showList(CTabFolderEvent event) {
119
// notifyShowListeners(event);
120
// }
121
//
122
// };
123
//
124
private MouseListener mouseListener = new MouseAdapter() {
125         public void mouseDown(MouseEvent e) {
126             mousedownState = getState();
127         }
128
129         public void mouseDoubleClick(MouseEvent e) {
130         }
131     };
132
133     /**
134      * Creates a pane folder. This will create exactly one child control in the
135      * given parent.
136      *
137      * @param parent
138      * @param flags
139      */

140     public R21PaneFolder(Composite parent, int flags) {
141         // Initialize tab folder
142
{
143             tabFolder = new CTabFolder(parent, flags);
144
145             // // Create a proxy control to measure the title area of the tab folder
146
// titleAreaProxy = new Composite(tabFolder, SWT.NONE);
147
// titleAreaProxy.setVisible(false);
148
// tabFolder.setTopRight(titleAreaProxy, SWT.FILL);
149

150             // tabFolder.addCTabFolder2Listener(expandListener);
151
//
152
tabFolder.addMouseListener(mouseListener);
153         }
154
155         // Initialize view form
156
{
157             viewForm = new ViewForm(tabFolder, SWT.NONE);
158
159             // Only attach these to the viewForm when there's actuall a control to display
160
viewFormTopLeftProxy = new ProxyControl(viewForm);
161             viewFormTopCenterProxy = new ProxyControl(viewForm);
162             viewFormTopRightProxy = new ProxyControl(viewForm);
163
164             contentProxy = new ProxyControl(viewForm);
165             viewForm.setContent(contentProxy.getControl());
166         }
167     }
168
169     /**
170      * Return the main control for this pane folder
171      *
172      * @return Composite the control
173      */

174     public Composite getControl() {
175         return tabFolder;
176     }
177
178     /**
179      * Sets the top-center control (usually a toolbar), or null if none.
180      * Note that the control can have any parent.
181      *
182      * @param topCenter the top-center control or null if none
183      */

184     public void setTopCenter(Control topCenter) {
185         topCenterCache.setControl(topCenter);
186         if (topCenter != null) {
187             if (!putTrimOnTop) {
188                 viewFormTopCenterProxy.setTarget(topCenterCache);
189                 viewForm.setTopCenter(viewFormTopCenterProxy.getControl());
190             }
191         } else {
192             if (!putTrimOnTop) {
193                 viewForm.setTopCenter(null);
194             }
195         }
196     }
197
198     /**
199      * Sets the top-right control (usually a dropdown), or null if none
200      *
201      * @param topRight
202      */

203     public void setTopRight(Control topRight) {
204         topRightCache.setControl(topRight);
205         if (topRight != null) {
206             if (!putTrimOnTop) {
207                 viewFormTopRightProxy.setTarget(topRightCache);
208                 viewForm.setTopRight(viewFormTopRightProxy.getControl());
209             }
210         } else {
211             if (!putTrimOnTop) {
212                 viewForm.setTopRight(null);
213             }
214         }
215     }
216
217     /**
218      * Sets the top-left control (usually a title label), or null if none
219      *
220      * @param topLeft
221      */

222     public void setTopLeft(Control topLeft) {
223         if (topLeftCache.getControl() != topLeft) {
224             topLeftCache.setControl(topLeft);
225             // The top-left control always goes directly in the ViewForm
226
if (topLeft != null) {
227                 viewFormTopLeftProxy.setTarget(topLeftCache);
228                 viewForm.setTopLeft(viewFormTopLeftProxy.getControl());
229             } else {
230                 viewFormTopLeftProxy.setTargetControl(null);
231                 viewForm.setTopLeft(null);
232             }
233         }
234     }
235
236     /**
237      * Flush all of this folder's size caches to ensure they will be re-computed
238      * on the next layout.
239      */

240     public void flush() {
241         topLeftCache.flush();
242         topRightCache.flush();
243         topCenterCache.flush();
244     }
245
246     /**
247      * Layout the receiver, flusing the cache if needed.
248      *
249      * @param flushCache
250      */

251     public void layout(boolean flushCache) {
252         // Flush the cached sizes if necessary
253
if (flushCache) {
254             flush();
255         }
256
257         Rectangle tabFolderClientArea = tabFolder.getClientArea();
258
259         // Hide tabs if there is only one
260
if (tabFolder.getItemCount() < 2) {
261             //Rectangle tabFolderBounds = tabFolder.getBounds();
262

263             int delta = getTabHeight() + 1;
264             tabFolderClientArea.height += delta;
265
266             if (getTabPosition() == SWT.TOP) {
267                 tabFolderClientArea.y -= delta;
268             }
269         }
270
271         viewForm.setBounds(tabFolderClientArea);
272         viewFormTopRightProxy.layout();
273         viewFormTopLeftProxy.layout();
274         viewFormTopCenterProxy.layout();
275     }
276
277     /**
278      * Returns the client area for this PaneFolder, relative to the pane folder's control.
279      *
280      * @return Rectangle the client area
281      */

282     public Rectangle getClientArea() {
283         Rectangle bounds = contentProxy.getControl().getBounds();
284
285         Rectangle formArea = viewForm.getBounds();
286
287         bounds.x += formArea.x;
288         bounds.y += formArea.y;
289
290         return bounds;
291     }
292
293     /**
294      * Returns the current state of the folder (as shown on the button icons)
295      *
296      * @return one of the IStackPresentationSite.STATE_* constants
297      */

298     public int getState() {
299         return state;
300     }
301
302     /**
303      * @param buttonId one of the IStackPresentationSite.STATE_* constants
304      */

305     protected void notifyButtonListeners(int buttonId) {
306         if (mousedownState == getState()) {
307             Iterator JavaDoc iter = buttonListeners.iterator();
308
309             while (iter.hasNext()) {
310                 R21PaneFolderButtonListener listener = (R21PaneFolderButtonListener) iter
311                         .next();
312
313                 listener.stateButtonPressed(buttonId);
314             }
315         }
316     }
317
318     /**
319      * Notifies all listeners that the user clicked on the chevron
320      *
321      * @param event
322      */

323     protected void notifyShowListeners(CTabFolderEvent event) {
324         Iterator JavaDoc iter = buttonListeners.iterator();
325
326         while (iter.hasNext()) {
327             R21PaneFolderButtonListener listener = (R21PaneFolderButtonListener) iter
328                     .next();
329
330             listener.showList(event);
331         }
332     }
333
334     /**
335      * Notifies all listeners that the close button was pressed
336      *
337      * @param tabItem
338      */

339     protected void notifyCloseListeners(CTabItem tabItem) {
340         Iterator JavaDoc iter = buttonListeners.iterator();
341
342         while (iter.hasNext()) {
343             R21PaneFolderButtonListener listener = (R21PaneFolderButtonListener) iter
344                     .next();
345
346             listener.closeButtonPressed(tabItem);
347         }
348     }
349
350     /**
351      * @param listener
352      */

353     public void addButtonListener(R21PaneFolderButtonListener listener) {
354         buttonListeners.add(listener);
355     }
356
357     /**
358      * @param listener
359      */

360     public void removeButtonListener(R21PaneFolderButtonListener listener) {
361         buttonListeners.remove(listener);
362     }
363
364     /**
365      * @param newTabPosition
366      */

367     public void setTabPosition(int newTabPosition) {
368         tabPos = newTabPosition;
369         tabFolder.setTabPosition(tabPos);
370     }
371
372     /**
373      * @return int the postion of the tab
374      */

375     public int getTabPosition() {
376         return tabPos;
377     }
378
379     /**
380      * @return boolean <code>true</code> if the receiver has been disposed
381      */

382     public boolean isDisposed() {
383         return tabFolder == null || tabFolder.isDisposed();
384     }
385
386     /**
387      * @param style
388      * @param index
389      * @return CTabItem the created item
390      */

391     public CTabItem createItem(int style, int index) {
392         return new CTabItem(tabFolder, style, index);
393     }
394
395     // The remainder of the methods in this class redirect directly to CTabFolder methods
396

397     /**
398      * @param selection
399      */

400     public void setSelection(int selection) {
401         tabFolder.setSelection(selection);
402     }
403
404     /**
405      * @param i
406      * @param j
407      * @param k
408      * @param l
409      * @return Rectangle the trim rectangle
410      */

411     public Rectangle computeTrim(int i, int j, int k, int l) {
412         return tabFolder.computeTrim(i, j, k, l);
413     }
414
415     /**
416      * @param fgColor
417      */

418     public void setSelectionForeground(Color fgColor) {
419         tabFolder.setSelectionForeground(fgColor);
420     }
421
422     /**
423      * @param idx
424      * @return CTabItem the indexed item
425      */

426     public CTabItem getItem(int idx) {
427         return tabFolder.getItem(idx);
428     }
429
430     /**
431      * @return int the selected items index
432      */

433     public int getSelectionIndex() {
434         return tabFolder.getSelectionIndex();
435     }
436
437     /**
438      * @return int the height of the tabs
439      */

440     public int getTabHeight() {
441         return tabFolder.getTabHeight();
442     }
443
444     /**
445      * @param toFind
446      * @return int the index of the item to find
447      */

448     public int indexOf(CTabItem toFind) {
449         return tabFolder.indexOf(toFind);
450     }
451
452     /**
453      * @param height
454      */

455     public void setTabHeight(int height) {
456         tabFolder.setTabHeight(height);
457     }
458
459     /**
460      * @return int the item count
461      */

462     public int getItemCount() {
463         return tabFolder.getItemCount();
464     }
465
466     /**
467      * @return CTabItem the items
468      */

469     public CTabItem[] getItems() {
470         return tabFolder.getItems();
471     }
472
473     /**
474      * @param toGet
475      * @return CTabItem the indexed item
476      */

477     public CTabItem getItem(Point toGet) {
478         return tabFolder.getItem(toGet);
479     }
480
481     /**
482      * @return CTabItem the selected item
483      */

484     public CTabItem getSelection() {
485         return tabFolder.getSelection();
486     }
487 }
488
Popular Tags