KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > progress > ProgressRegion


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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.progress;
12
13 import org.eclipse.jface.viewers.IContentProvider;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.MouseAdapter;
16 import org.eclipse.swt.events.MouseEvent;
17 import org.eclipse.swt.graphics.GC;
18 import org.eclipse.swt.graphics.Point;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.layout.GridLayout;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.ui.internal.TrimUtil;
24 import org.eclipse.ui.internal.WorkbenchMessages;
25 import org.eclipse.ui.internal.WorkbenchWindow;
26 import org.eclipse.ui.internal.layout.IWindowTrim;
27
28 /**
29  * The ProgressRegion is class for the region of the workbench where the
30  * progress line and the animation item are shown.
31  */

32 public class ProgressRegion implements IWindowTrim {
33     ProgressCanvasViewer viewer;
34
35     ProgressAnimationItem animationItem;
36
37     Composite region;
38
39     WorkbenchWindow workbenchWindow;
40     
41     private int fWidthHint = SWT.DEFAULT;
42     
43     private int fHeightHint = SWT.DEFAULT;
44
45     /**
46      * the side the receiver is placed on
47      */

48     private int side = SWT.BOTTOM;
49     
50     private boolean forceHorizontal;
51
52     /**
53      * Create a new instance of the receiver.
54      */

55     public ProgressRegion() {
56         //No default behavior.
57
}
58
59     /**
60      * Create the contents of the receiver in the parent. Use the window for the
61      * animation item.
62      *
63      * @param parent
64      * The parent widget of the composite.
65      * @param window
66      * The WorkbenchWindow this is in.
67      * @return Control
68      */

69     public Control createContents(Composite parent, WorkbenchWindow window) {
70         workbenchWindow = window;
71
72         // Test whether or not 'advanced' graphics are available
73
// If not then we'll 'force' the ProgressBar to always be
74
// HORIZONTAL...
75
//TODO: This should likely be at some 'global' level state
76
GC gc = new GC(parent);
77         gc.setAdvanced(true);
78         forceHorizontal = !gc.getAdvanced();
79         gc.dispose();
80         
81         region = new Composite(parent, SWT.NONE) {
82             /*
83              * (non-Javadoc)
84              *
85              * @see org.eclipse.swt.widgets.Composite#computeSize(int, int,
86              * boolean)
87              */

88             public Point computeSize(int wHint, int hHint, boolean changed) {
89                 Point size = super.computeSize(wHint, hHint, changed);
90                 if (isHorizontal(side))
91                     size.y = TrimUtil.TRIM_DEFAULT_HEIGHT;
92                 else {
93                     size.x = TrimUtil.TRIM_DEFAULT_HEIGHT;
94                 }
95                 return size;
96             }
97         };
98         
99         GridLayout gl = new GridLayout();
100         gl.marginHeight = 0;
101         gl.marginWidth = 0;
102         if (isHorizontal(side))
103             gl.numColumns = 3;
104         region.setLayout(gl);
105
106         viewer = new ProgressCanvasViewer(region, SWT.NO_FOCUS, 1, 36, isHorizontal(side) ? SWT.HORIZONTAL : SWT.VERTICAL);
107         viewer.setUseHashlookup(true);
108         Control viewerControl = viewer.getControl();
109         GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
110         Point viewerSizeHints = viewer.getSizeHints();
111         if (isHorizontal(side)) {
112             gd.widthHint = viewerSizeHints.x;
113             gd.heightHint = viewerSizeHints.y;
114         } else {
115             gd.widthHint = viewerSizeHints.y;
116             gd.heightHint = viewerSizeHints.x;
117         }
118         viewerControl.setLayoutData(gd);
119
120         int widthPreference = AnimationManager.getInstance()
121                 .getPreferredWidth() + 25;
122         animationItem = new ProgressAnimationItem(this, isHorizontal(side) ? SWT.HORIZONTAL : SWT.VERTICAL);
123         animationItem.createControl(region);
124
125         animationItem.setAnimationContainer(new AnimationItem.IAnimationContainer() {
126             /* (non-Javadoc)
127              * @see org.eclipse.ui.internal.progress.AnimationItem.IAnimationContainer#animationDone()
128              */

129             public void animationDone() {
130                 //Add an extra refresh to the viewer in case
131
//of stale input if the controls are not disposed
132
if (viewer.getControl().isDisposed()) {
133                     return;
134                 }
135                 viewer.refresh();
136             }
137
138             /* (non-Javadoc)
139              * @see org.eclipse.ui.internal.progress.AnimationItem.IAnimationContainer#animationStart()
140              */

141             public void animationStart() {
142                 // Nothing by default here.
143

144             }
145         });
146         if (isHorizontal(side)) {
147             gd = new GridData(GridData.FILL_VERTICAL);
148             gd.widthHint = widthPreference;
149         } else {
150             gd = new GridData(GridData.FILL_HORIZONTAL);
151             gd.heightHint = widthPreference;
152         }
153
154         animationItem.getControl().setLayoutData(gd);
155
156         viewerControl.addMouseListener(new MouseAdapter() {
157             /*
158              * (non-Javadoc)
159              *
160              * @see org.eclipse.swt.events.MouseAdapter#mouseDoubleClick(org.eclipse.swt.events.MouseEvent)
161              */

162             public void mouseDoubleClick(MouseEvent e) {
163                 processDoubleClick();
164             }
165         });
166
167         //Never show debug info
168
IContentProvider provider = new ProgressViewerContentProvider(viewer,
169                 false,false);
170         viewer.setContentProvider(provider);
171         viewer.setInput(provider);
172         viewer.setLabelProvider(new ProgressViewerLabelProvider(viewerControl));
173         viewer.setComparator(ProgressManagerUtil.getProgressViewerComparator());
174         return region;
175     }
176
177     /**
178      * Return the animationItem for the receiver.
179      *
180      * @return AnimationItem
181      */

182     public AnimationItem getAnimationItem() {
183         return animationItem;
184     }
185
186     /**
187      * Return the control for the receiver.
188      *
189      * @return Control
190      */

191     public Control getControl() {
192         return region;
193     }
194
195     /**
196      * Process the double click event.
197      */

198     public void processDoubleClick() {
199         ProgressManagerUtil.openProgressView(workbenchWindow);
200     }
201
202     /* (non-Javadoc)
203      * @see org.eclipse.ui.internal.IWindowTrim#dock(int)
204      */

205     public void dock(int dropSide) {
206         int oldSide = side;
207         side = dropSide;
208         if (oldSide == dropSide || (isVertical(oldSide) && isVertical(dropSide)) || (isHorizontal(oldSide) && isHorizontal(dropSide)))
209             return;
210         recreate();
211         
212     }
213
214     /**
215      * Answer true if the side is a horizonal one
216      *
217      * @param dropSide
218      * @return <code>true</code> if the side is horizontal
219      */

220     private boolean isHorizontal(int dropSide) {
221         if (forceHorizontal)
222             return true;
223         return dropSide == SWT.TOP || dropSide == SWT.BOTTOM;
224     }
225
226
227     /**
228      * Answer true if the side is a horizonal one
229      *
230      * @param dropSide
231      * @return <code>true</code> if the side is horizontal
232      */

233     private boolean isVertical(int dropSide) {
234         if (forceHorizontal)
235             return false;
236         return dropSide == SWT.LEFT || dropSide == SWT.RIGHT;
237     }
238
239     /**
240      * Recreate the receiver given the new side
241      */

242     private void recreate() {
243         if (region != null && !region.isDisposed()) {
244             Composite parent = region.getParent();
245             boolean animating = animationItem.animationRunning();
246             AnimationManager.getInstance().removeItem(animationItem);
247             region.dispose();
248             createContents(parent, workbenchWindow);
249             if (animating)
250                 animationItem.animationStart();
251         }
252     }
253
254     /* (non-Javadoc)
255      * @see org.eclipse.ui.internal.IWindowTrim#getId()
256      */

257     public String JavaDoc getId() {
258         return "org.eclipse.ui.internal.progress.ProgressRegion"; //$NON-NLS-1$
259
}
260
261     public String JavaDoc getDisplayName() {
262         return WorkbenchMessages.TrimCommon_Progress_TrimName;
263     }
264
265     /* (non-Javadoc)
266      * @see org.eclipse.ui.internal.IWindowTrim#getValidSides()
267      */

268     public int getValidSides() {
269         return SWT.BOTTOM | SWT.TOP | SWT.LEFT | SWT.RIGHT ;
270     }
271
272     /* (non-Javadoc)
273      * @see org.eclipse.ui.internal.IWindowTrim#isCloseable()
274      */

275     public boolean isCloseable() {
276         return false;
277     }
278
279     /* (non-Javadoc)
280      * @see org.eclipse.ui.internal.IWindowTrim#handleClose()
281      */

282     public void handleClose() {
283         // nothing to do...
284
}
285
286     /*
287      * (non-Javadoc)
288      *
289      * @see org.eclipse.ui.IWindowTrim#getWidthHint()
290      */

291     public int getWidthHint() {
292         return fWidthHint;
293     }
294     
295     /**
296      * Update the width hint for this control.
297      * @param w pixels, or SWT.DEFAULT
298      */

299     public void setWidthHint(int w) {
300         fWidthHint = w;
301     }
302
303     /*
304      * (non-Javadoc)
305      *
306      * @see org.eclipse.ui.IWindowTrim#getHeightHint()
307      */

308     public int getHeightHint() {
309         return fHeightHint;
310     }
311     
312     /**
313      * Update the height hint for this control.
314      * @param h pixels, or SWT.DEFAULT
315      */

316     public void setHeightHint(int h) {
317         fHeightHint = h;
318     }
319
320     /* (non-Javadoc)
321      * @see org.eclipse.ui.IWindowTrim#isResizeable()
322      */

323     public boolean isResizeable() {
324         return false;
325     }
326 }
327
Popular Tags