KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > presentations > util > AbstractTabFolder


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.util;
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.Point;
22 import org.eclipse.swt.graphics.Rectangle;
23 import org.eclipse.swt.widgets.Composite;
24 import org.eclipse.swt.widgets.Control;
25 import org.eclipse.swt.widgets.Event;
26 import org.eclipse.swt.widgets.Listener;
27 import org.eclipse.ui.presentations.IStackPresentationSite;
28 import org.eclipse.ui.presentations.PresentationUtil;
29
30 /**
31  * @since 3.1
32  */

33 public abstract class AbstractTabFolder {
34     
35     private List JavaDoc listeners = new ArrayList JavaDoc(1);
36
37     private Control toolbar;
38     private int state;
39
40     public abstract Point computeSize(int widthHint, int heightHint);
41
42     public abstract AbstractTabItem add(int index, int flags);
43
44     public abstract Composite getContentParent();
45     public abstract void setContent(Control newContent);
46     
47     public abstract AbstractTabItem[] getItems();
48
49     public abstract AbstractTabItem getSelection();
50     public abstract void setSelection(AbstractTabItem toSelect);
51     public abstract void setSelectedInfo(PartInfo info);
52     public abstract void enablePaneMenu(boolean enabled);
53     private int activeState = IStackPresentationSite.STATE_RESTORED;
54     
55     private Listener menuListener = new Listener() {
56         /* (non-Javadoc)
57          * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
58          */

59         public void handleEvent(Event event) {
60             Point globalPos = new Point(event.x, event.y);
61             handleContextMenu(globalPos, event);
62         }
63     };
64
65     private Listener dragListener = new Listener() {
66         public void handleEvent(Event e) {
67             Point globalPos = ((Control)e.widget).toDisplay(e.x, e.y);
68             handleDragStarted(globalPos, e);
69         }
70     };
71     
72     private MouseListener mouseListener = new MouseAdapter() {
73         
74         // If we single-click on an empty space on the toolbar, move focus to the
75
// active control
76
public void mouseDown(MouseEvent e) {
77             Point p = ((Control)e.widget).toDisplay(e.x, e.y);
78
79             handleMouseDown(p, e);
80         }
81         
82         
83         // If we double-click on the toolbar, maximize the presentation
84
public void mouseDoubleClick(MouseEvent e) {
85             Point p = ((Control)e.widget).toDisplay(e.x, e.y);
86
87             handleDoubleClick(p, e);
88         }
89     };
90     
91     public void setActive(int activeState) {
92         this.activeState = activeState;
93     }
94     
95     public int getActive() {
96         return activeState;
97     }
98
99     /**
100      * Returns the location where the pane menu should be opened when activated
101      * by a keyboard shortcut (display coordinates)
102      *
103      * @return the location for the pane menu (display coordinates)
104      * @since 3.1
105      */

106     public Point getPaneMenuLocation() {
107         return getControl().toDisplay(new Point(0,0));
108     }
109
110     /**
111      * Returns the location where the part list should be opened when activated
112      * by a keyboard shortcut (display coordinates)
113      *
114      * @return the location for the part list (display coordinates)
115      * @since 3.1
116      */

117     public Point getPartListLocation() {
118         return getSystemMenuLocation();
119     }
120
121     /**
122      * Returns the location where the pane menu should be opened when activated
123      * by a keyboard shortcut (display coordinates)
124      *
125      * @return the location for the pane menu (display coordinates)
126      * @since 3.1
127      */

128     public Point getSystemMenuLocation() {
129         return getControl().toDisplay(new Point(0,0));
130     }
131     
132     /**
133      * Returns the parent composite that should be used for creating the toolbar.
134      * Any control passed into setToolbar must have this composite as its parent.
135      *
136      * @return the parent composite that should be used for creating the toolbar
137      *
138      * @since 3.1
139      */

140     public abstract Composite getToolbarParent();
141
142     /**
143      * Returns the main control for this folder.
144      *
145      * @return the main control for the folder
146      * @since 3.1
147      */

148     public abstract Control getControl();
149     
150     public AbstractTabItem getItem(int idx) {
151         return getItems()[idx];
152     }
153
154     public AbstractTabItem getItem(Point toFind) {
155         AbstractTabItem[] items = getItems();
156         
157         for (int i = 0; i < items.length; i++) {
158             AbstractTabItem item = items[i];
159             
160             if (item.getBounds().contains(toFind)) {
161                 return item;
162             }
163         }
164         
165         return null;
166     }
167     
168     public AbstractTabItem findItem(Object JavaDoc dataToFind) {
169         AbstractTabItem[] items = getItems();
170         
171         for (int i = 0; i < items.length; i++) {
172             AbstractTabItem item = items[i];
173             
174             if (item.getData() == dataToFind) {
175                 return item;
176             }
177         }
178         
179         return null;
180     }
181     
182     /**
183      * Returns the index of the given item, or -1 if the given item is
184      * not found in this tab folder. Subclasses should override this if
185      * the underlying SWT widget has an equivalent method
186      *
187      * @param item item to find
188      * @return the index of the given item or -1
189      */

190     public int indexOf(AbstractTabItem item) {
191         AbstractTabItem[] items = getItems();
192
193         for (int idx = 0; idx < items.length; idx++) {
194             AbstractTabItem next = items[idx];
195
196             if (next == item) {
197                 return idx;
198             }
199         }
200
201         return -1;
202     }
203
204     public int getItemCount() {
205         return getItems().length;
206     }
207
208     public void setToolbar(Control toolbarControl) {
209         this.toolbar = toolbarControl;
210     }
211
212     public final Control getToolbar() {
213         return toolbar;
214     }
215
216     /**
217      * Sets the current state for the folder
218      *
219      * @param state one of the IStackPresentationSite.STATE_* constants
220      */

221     public void setState(int state) {
222         this.state = state;
223     }
224
225     /**
226      * Returns the title area for this control (in the control's coordinate system)
227      *
228      * @return
229      */

230     public abstract Rectangle getTabArea();
231
232     /**
233      * Called when the tab folder's shell becomes active or inactive. Subclasses
234      * can override this to change the appearance of the tabs based on activation.
235      *
236      * @param isActive
237      */

238     public void shellActive(boolean isActive) {
239     }
240
241     /**
242      * Adds the given listener to this AbstractTabFolder
243      *
244      * @param newListener the listener to add
245      */

246     public final void addListener(TabFolderListener newListener) {
247         listeners.add(newListener);
248     }
249
250     /**
251      * Removes the given listener from this AbstractTabFolder
252      *
253      * @param toRemove the listener to remove
254      */

255     public final void removeListener(TabFolderListener toRemove) {
256         listeners.remove(toRemove);
257     }
258
259     public void flushToolbarSize() {
260         
261     }
262     
263     protected final void fireEvent(TabFolderEvent e) {
264         for (Iterator JavaDoc iter = listeners.iterator(); iter.hasNext();) {
265             TabFolderListener next = (TabFolderListener)iter.next();
266
267             next.handleEvent(e);
268         }
269     }
270     
271     protected final void fireEvent(int id) {
272         fireEvent(new TabFolderEvent(id));
273     }
274     
275     protected final void fireEvent(int id, AbstractTabItem w) {
276         fireEvent(new TabFolderEvent(id, w, 0, 0));
277     }
278     
279     protected final void fireEvent(int id, AbstractTabItem w, Point pos) {
280         fireEvent(new TabFolderEvent(id, w, pos));
281     }
282     
283     public void layout(boolean flushCache) {
284     }
285     
286     public void setTabPosition(int tabPosition) {
287     }
288     
289     public int getTabPosition() {
290         return SWT.TOP;
291     }
292     
293     public int getState() {
294         return state;
295     }
296     
297     protected void attachListeners(Control theControl, boolean recursive) {
298         theControl.addListener(SWT.MenuDetect, menuListener);
299         theControl.addMouseListener(mouseListener);
300         PresentationUtil.addDragListener(theControl, dragListener);
301         
302         if (recursive && theControl instanceof Composite) {
303             Composite composite = (Composite) theControl;
304             Control[] children = composite.getChildren();
305             
306             for (int i = 0; i < children.length; i++) {
307                 Control control = children[i];
308                 
309                 attachListeners(control, recursive);
310             }
311         }
312     }
313     
314     protected void detachListeners(Control theControl, boolean recursive) {
315         theControl.removeListener(SWT.MenuDetect, menuListener);
316         theControl.removeMouseListener(mouseListener);
317         PresentationUtil.removeDragListener(theControl, dragListener);
318         
319         if (recursive && theControl instanceof Composite) {
320             Composite composite = (Composite) theControl;
321             Control[] children = composite.getChildren();
322             
323             for (int i = 0; i < children.length; i++) {
324                 Control control = children[i];
325                 
326                 detachListeners(control, recursive);
327             }
328         }
329     }
330     
331     protected void handleContextMenu(Point displayPos, Event e) {
332         if (isOnBorder(displayPos)) {
333             return;
334         }
335
336         AbstractTabItem tab = getItem(displayPos);
337         
338         fireEvent(TabFolderEvent.EVENT_SYSTEM_MENU, tab, displayPos);
339     }
340     
341     protected void handleMouseDown(Point displayPos, MouseEvent e) {
342         fireEvent(TabFolderEvent.EVENT_GIVE_FOCUS_TO_PART);
343     }
344
345     protected void handleDoubleClick(Point displayPos, MouseEvent e) {
346         if (isOnBorder(displayPos)) {
347             return;
348         }
349         
350         if (getState() == IStackPresentationSite.STATE_MAXIMIZED) {
351             fireEvent(TabFolderEvent.EVENT_RESTORE);
352         } else {
353             fireEvent(TabFolderEvent.EVENT_MAXIMIZE);
354         }
355     }
356     
357     protected void handleDragStarted(Point displayPos, Event e) {
358
359         if (isOnBorder(displayPos)) {
360             return;
361         }
362         
363         AbstractTabItem tab = getItem(displayPos);
364         fireEvent(TabFolderEvent.EVENT_DRAG_START, tab, displayPos);
365     }
366
367     /**
368      * Returns true iff the given point is on the border of the folder.
369      * By default, double-clicking, context menus, and drag/drop are disabled
370      * on the folder's border.
371      *
372      * @param toTest a point (display coordinates)
373      * @return true iff the point is on the presentation border
374      * @since 3.1
375      */

376     public boolean isOnBorder(Point toTest) {
377         return false;
378     }
379     
380     /**
381      * Set the folder to visible. This can be extended to propogate the
382      * visibility request to other components in the subclass.
383      *
384      * @param visible
385      * <code>true</code> - the folder is visible.
386      * @since 3.2
387      */

388     public void setVisible(boolean visible) {
389         getControl().setVisible(visible);
390     }
391
392     /**
393      * Cause the folder to hide or show its
394      * Minimize and Maximize affordances.
395      *
396      * @param show
397      * <code>true</code> - the min/max buttons are visible.
398      * @since 3.3
399      */

400     public void showMinMax(boolean show) {
401     }
402 }
403
Popular Tags