KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.progress;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.jobs.Job;
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.DisposeEvent;
19 import org.eclipse.swt.events.DisposeListener;
20 import org.eclipse.swt.events.MouseAdapter;
21 import org.eclipse.swt.events.MouseEvent;
22 import org.eclipse.swt.events.MouseListener;
23 import org.eclipse.swt.events.SelectionAdapter;
24 import org.eclipse.swt.events.SelectionEvent;
25 import org.eclipse.swt.graphics.Image;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.ProgressBar;
33 import org.eclipse.swt.widgets.ToolBar;
34 import org.eclipse.swt.widgets.ToolItem;
35 import org.eclipse.ui.PlatformUI;
36 import org.eclipse.ui.internal.WorkbenchImages;
37 import org.eclipse.ui.progress.IProgressConstants;
38 import org.eclipse.ui.statushandlers.StatusAdapter;
39 import org.eclipse.ui.statushandlers.StatusManager;
40
41 /**
42  * The ProgressAnimationItem is the animation items that uses
43  * the progress bar.
44  */

45 public class ProgressAnimationItem extends AnimationItem implements
46         FinishedJobs.KeptJobsListener {
47
48     ProgressBar bar;
49
50     MouseListener mouseListener;
51
52     Composite top;
53
54     ToolBar toolbar;
55
56     ToolItem toolButton;
57
58     ProgressRegion progressRegion;
59
60     Image noneImage, okImage, errorImage;
61
62     boolean animationRunning;
63
64     JobInfo lastJobInfo;
65
66     // ProgressBar flags
67
private int flags;
68
69     /**
70      * Create an instance of the receiver in the supplied region.
71      *
72      * @param region The ProgressRegion that contains the receiver.
73      * @param flags flags to use for creation of the progress bar
74      */

75     ProgressAnimationItem(ProgressRegion region, int flags) {
76         super(region.workbenchWindow);
77         this.flags = flags;
78         FinishedJobs.getInstance().addListener(this);
79
80         progressRegion = region;
81         mouseListener = new MouseAdapter() {
82             public void mouseDoubleClick(MouseEvent e) {
83                 doAction();
84             }
85         };
86     }
87
88     void doAction() {
89
90         JobTreeElement[] jobTreeElements = FinishedJobs.getInstance()
91                 .getJobInfos();
92         // search from end (youngest)
93
for (int i = jobTreeElements.length - 1; i >= 0; i--) {
94             if (jobTreeElements[i] instanceof JobInfo) {
95                 JobInfo ji = (JobInfo) jobTreeElements[i];
96                 Job job = ji.getJob();
97                 if (job != null) {
98
99                     IStatus status = job.getResult();
100                     if (status != null && status.getSeverity() == IStatus.ERROR) {
101                         StatusAdapter statusAdapter = StatusAdapterHelper
102                                 .getInstance().getStatusAdapter(ji);
103
104                         StatusManager.getManager().handle(statusAdapter,
105                                 StatusManager.SHOW);
106
107                         JobTreeElement topElement = (JobTreeElement) ji
108                                 .getParent();
109                         if (topElement == null) {
110                             topElement = ji;
111                         }
112                         FinishedJobs.getInstance().remove(topElement);
113                     }
114
115                     IAction action = null;
116                     Object JavaDoc property = job
117                             .getProperty(IProgressConstants.ACTION_PROPERTY);
118                     if (property instanceof IAction) {
119                         action = (IAction) property;
120                     }
121                     if (action != null && action.isEnabled()) {
122                         action.run();
123                         JobTreeElement topElement = (JobTreeElement) ji
124                                 .getParent();
125                         if (topElement == null) {
126                             topElement = ji;
127                         }
128                         FinishedJobs.getInstance().remove(topElement);
129                         return;
130                     }
131                 }
132             }
133         }
134         
135         progressRegion.processDoubleClick();
136         refresh();
137     }
138
139     private IAction getAction(Job job) {
140         Object JavaDoc property = job.getProperty(IProgressConstants.ACTION_PROPERTY);
141         if (property instanceof IAction) {
142             return (IAction) property;
143         }
144         return null;
145     }
146
147     private void refresh() {
148
149         // Abort the refresh if we are in the process of shutting down
150
if (!PlatformUI.isWorkbenchRunning()) {
151             return;
152         }
153
154         if (toolbar == null || toolbar.isDisposed()) {
155             return;
156         }
157
158         lastJobInfo = null;
159
160         JobTreeElement[] jobTreeElements = FinishedJobs.getInstance()
161                 .getJobInfos();
162         // search from end (youngest)
163
for (int i = jobTreeElements.length - 1; i >= 0; i--) {
164             if (jobTreeElements[i] instanceof JobInfo) {
165                 JobInfo ji = (JobInfo) jobTreeElements[i];
166                 lastJobInfo = ji;
167                 Job job = ji.getJob();
168                 if (job != null) {
169                     IStatus status = job.getResult();
170                     if (status != null && status.getSeverity() == IStatus.ERROR) {
171                         // green arrow with error overlay
172
initButton(
173                                 errorImage,
174                                 NLS.bind(ProgressMessages.ProgressAnimationItem_error, job.getName()));
175                         return;
176                     }
177                     IAction action = getAction(job);
178                     if (action != null && action.isEnabled()) {
179                         // green arrow with exclamation mark
180
String JavaDoc tt = action.getToolTipText();
181                         if (tt == null || tt.trim().length() == 0) {
182                             tt = NLS.bind(ProgressMessages.ProgressAnimationItem_ok, job.getName());
183                         }
184                         initButton(okImage, tt);
185                         return;
186                     }
187                     // just the green arrow
188
initButton(noneImage, ProgressMessages.ProgressAnimationItem_tasks);
189                     return;
190                 }
191             }
192         }
193         
194         if (animationRunning) {
195             initButton(noneImage, ProgressMessages.ProgressAnimationItem_tasks);
196             return;
197         }
198
199         // if nothing found hide tool item
200
toolbar.setVisible(false);
201     }
202
203     private void initButton(Image im, String JavaDoc tt) {
204         toolButton.setImage(im);
205         toolButton.setToolTipText(tt);
206         toolbar.setVisible(true);
207         toolbar.getParent().layout(); // must layout
208
}
209
210     /*
211      * (non-Javadoc)
212      *
213      * @see org.eclipse.ui.internal.progress.AnimationItem#createAnimationItem(org.eclipse.swt.widgets.Composite)
214      */

215     protected Control createAnimationItem(Composite parent) {
216
217         if (okImage == null) {
218             Display display = parent.getDisplay();
219             noneImage = WorkbenchImages.getWorkbenchImageDescriptor("progress/progress_none.gif").createImage(display); //$NON-NLS-1$
220
okImage = WorkbenchImages.getWorkbenchImageDescriptor("progress/progress_ok.gif").createImage(display); //$NON-NLS-1$
221
errorImage = WorkbenchImages.getWorkbenchImageDescriptor("progress/progress_error.gif").createImage(display); //$NON-NLS-1$
222
}
223
224         top = new Composite(parent, SWT.NULL);
225         top.addDisposeListener(new DisposeListener() {
226             public void widgetDisposed(DisposeEvent e) {
227                 FinishedJobs.getInstance().removeListener(
228                         ProgressAnimationItem.this);
229                 noneImage.dispose();
230                 okImage.dispose();
231                 errorImage.dispose();
232             }
233         });
234
235         boolean isCarbon = "carbon".equals(SWT.getPlatform()); //$NON-NLS-1$
236

237         GridLayout gl = new GridLayout();
238         if (isHorizontal())
239             gl.numColumns = isCarbon ? 3 : 2;
240         gl.marginHeight = 0;
241         gl.marginWidth = 0;
242         if (isHorizontal()) {
243             gl.horizontalSpacing = 2;
244         } else {
245             gl.verticalSpacing = 2;
246         }
247         top.setLayout(gl);
248
249         bar = new ProgressBar(top, flags | SWT.INDETERMINATE);
250         bar.setVisible(false);
251         bar.addMouseListener(mouseListener);
252         
253         GridData gd;
254         int hh = 12;
255         if (isHorizontal()) {
256             gd = new GridData(SWT.BEGINNING, SWT.CENTER, true, false);
257             gd.heightHint = hh;
258         } else {
259             gd = new GridData(SWT.CENTER, SWT.BEGINNING, false, true);
260             gd.widthHint = hh;
261         }
262
263         bar.setLayoutData(gd);
264
265         toolbar = new ToolBar(top, SWT.FLAT);
266         toolbar.setVisible(false);
267
268         toolButton = new ToolItem(toolbar, SWT.NONE);
269         toolButton.addSelectionListener(new SelectionAdapter() {
270             public void widgetSelected(SelectionEvent e) {
271                 doAction();
272             }
273         });
274
275         if (isCarbon) {
276             new Label(top, SWT.NONE).setLayoutData(new GridData(4, 4));
277         }
278
279         refresh();
280
281         return top;
282     }
283
284     /**
285      * @return <code>true</code> if the control is horizontally oriented
286      */

287     private boolean isHorizontal() {
288         return (flags & SWT.HORIZONTAL) != 0;
289     }
290
291     /*
292      * (non-Javadoc)
293      *
294      * @see org.eclipse.ui.internal.progress.AnimationItem#getControl()
295      */

296     public Control getControl() {
297         return top;
298     }
299
300     /*
301      * (non-Javadoc)
302      *
303      * @see org.eclipse.ui.internal.progress.AnimationItem#animationDone()
304      */

305     void animationDone() {
306         super.animationDone();
307         animationRunning = false;
308         if (bar.isDisposed()) {
309             return;
310         }
311         bar.setVisible(false);
312         refresh();
313     }
314
315     /**
316      * @return <code>true</code> when the animation is running
317      */

318     public boolean animationRunning() {
319         return animationRunning;
320     }
321     /*
322      * (non-Javadoc)
323      *
324      * @see org.eclipse.ui.internal.progress.AnimationItem#animationStart()
325      */

326     void animationStart() {
327         super.animationStart();
328         animationRunning = true;
329         if (bar.isDisposed()) {
330             return;
331         }
332         bar.setVisible(true);
333         refresh();
334     }
335
336     public void removed(JobTreeElement info) {
337         final Display display = Display.getDefault();
338         display.asyncExec(new Runnable JavaDoc() {
339             public void run() {
340                 refresh();
341             }
342         });
343     }
344
345     public void finished(final JobTreeElement jte) {
346         final Display display = Display.getDefault();
347         display.asyncExec(new Runnable JavaDoc() {
348             public void run() {
349                 refresh();
350             }
351         });
352     }
353     
354 }
Popular Tags