KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.progress;
12
13 import java.util.Collections JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.Set JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Color;
23 import org.eclipse.swt.widgets.Control;
24 import org.eclipse.ui.progress.WorkbenchJob;
25
26 /**
27  * The AnimationManager is the class that keeps track of the animation items to
28  * update.
29  */

30 public class AnimationManager {
31     private static AnimationManager singleton;
32
33     boolean animated = false;
34
35     private IJobProgressManagerListener listener;
36
37     IAnimationProcessor animationProcessor;
38
39     WorkbenchJob animationUpdateJob;
40
41     public static AnimationManager getInstance() {
42         if (singleton == null) {
43             singleton = new AnimationManager();
44         }
45         return singleton;
46     }
47
48     /**
49      * Get the background color to be used.
50      *
51      * @param control
52      * The source of the display.
53      * @return Color
54      */

55     static Color getItemBackgroundColor(Control control) {
56         return control.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
57     }
58
59     AnimationManager() {
60
61         animationProcessor = new ProgressAnimationProcessor(this);
62
63         animationUpdateJob = new WorkbenchJob(ProgressMessages.AnimationManager_AnimationStart) {
64
65             /*
66              * (non-Javadoc)
67              *
68              * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
69              */

70             public IStatus runInUIThread(IProgressMonitor monitor) {
71
72                 if (animated) {
73                     animationProcessor.animationStarted();
74                 } else {
75                     animationProcessor.animationFinished();
76                 }
77                 return Status.OK_STATUS;
78             }
79         };
80         animationUpdateJob.setSystem(true);
81         
82         listener = getProgressListener();
83         ProgressManager.getInstance().addListener(listener);
84
85
86     }
87
88     /**
89      * Add an item to the list
90      *
91      * @param item
92      */

93     void addItem(final AnimationItem item) {
94         animationProcessor.addItem(item);
95     }
96
97     /**
98      * Remove an item from the list
99      *
100      * @param item
101      */

102     void removeItem(final AnimationItem item) {
103         animationProcessor.removeItem(item);
104     }
105
106     /**
107      * Return whether or not the current state is animated.
108      *
109      * @return boolean
110      */

111     boolean isAnimated() {
112         return animated;
113     }
114
115     /**
116      * Set whether or not the receiver is animated.
117      *
118      * @param boolean
119      */

120     void setAnimated(final boolean bool) {
121         animated = bool;
122         animationUpdateJob.schedule(100);
123     }
124
125     /**
126      * Dispose the images in the receiver.
127      */

128     void dispose() {
129         setAnimated(false);
130         ProgressManager.getInstance().removeListener(listener);
131     }
132
133     private IJobProgressManagerListener getProgressListener() {
134         return new IJobProgressManagerListener() {
135             Set JavaDoc jobs = Collections.synchronizedSet(new HashSet JavaDoc());
136
137             /*
138              * (non-Javadoc)
139              *
140              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#addJob(org.eclipse.ui.internal.progress.JobInfo)
141              */

142             public void addJob(JobInfo info) {
143                 incrementJobCount(info);
144             }
145
146             /*
147              * (non-Javadoc)
148              *
149              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#refreshJobInfo(org.eclipse.ui.internal.progress.JobInfo)
150              */

151             public void refreshJobInfo(JobInfo info) {
152                 int state = info.getJob().getState();
153                 if (state == Job.RUNNING) {
154                     addJob(info);
155                 } else {
156                     removeJob(info);
157                 }
158             }
159
160             /*
161              * (non-Javadoc)
162              *
163              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#refreshAll()
164              */

165             public void refreshAll() {
166                 ProgressManager manager = ProgressManager.getInstance();
167                 jobs.clear();
168                 setAnimated(false);
169                 JobInfo[] currentInfos = manager.getJobInfos(showsDebug());
170                 for (int i = 0; i < currentInfos.length; i++) {
171                     addJob(currentInfos[i]);
172                 }
173             }
174
175             /*
176              * (non-Javadoc)
177              *
178              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#remove(org.eclipse.ui.internal.progress.JobInfo)
179              */

180             public void removeJob(JobInfo info) {
181                 decrementJobCount(info.getJob());
182             }
183
184             /*
185              * (non-Javadoc)
186              *
187              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#showsDebug()
188              */

189             public boolean showsDebug() {
190                 return false;
191             }
192
193             private void incrementJobCount(JobInfo info) {
194                 //Don't count the animate job itself
195
if (isNotTracked(info)) {
196                     return;
197                 }
198                 if (jobs.isEmpty()) {
199                     setAnimated(true);
200                 }
201                 jobs.add(info.getJob());
202             }
203
204             /*
205              * Decrement the job count for the job
206              */

207             private void decrementJobCount(Job job) {
208                 jobs.remove(job);
209                 if (jobs.isEmpty()) {
210                     setAnimated(false);
211                 }
212             }
213
214             /**
215              * If this is one of our jobs or not running then don't bother.
216              */

217             private boolean isNotTracked(JobInfo info) {
218                 //We always track errors
219
Job job = info.getJob();
220                 return job.getState() != Job.RUNNING
221                         || animationProcessor.isProcessorJob(job);
222             }
223
224             /*
225              * (non-Javadoc)
226              *
227              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#addGroup(org.eclipse.ui.internal.progress.GroupInfo)
228              */

229             public void addGroup(GroupInfo info) {
230                 //Don't care about groups
231
}
232
233             /*
234              * (non-Javadoc)
235              *
236              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#removeGroup(org.eclipse.ui.internal.progress.GroupInfo)
237              */

238             public void removeGroup(GroupInfo group) {
239                 //Don't care about groups
240
}
241
242             /*
243              * (non-Javadoc)
244              *
245              * @see org.eclipse.ui.internal.progress.IJobProgressManagerListener#refreshGroup(org.eclipse.ui.internal.progress.GroupInfo)
246              */

247             public void refreshGroup(GroupInfo info) {
248                 //Don't care about groups
249
}
250         };
251     }
252
253     /**
254      * Get the preferred width for widgets displaying the animation.
255      *
256      * @return int. Return 0 if there is no image data.
257      */

258     int getPreferredWidth() {
259         return animationProcessor.getPreferredWidth();
260     }
261
262 }
263
Popular Tags