KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > AnimationEngine


1 /*******************************************************************************
2  * Copyright (c) 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;
12
13 import org.eclipse.core.runtime.IProgressMonitor;
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Status;
16 import org.eclipse.core.runtime.jobs.Job;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.swt.widgets.Display;
19 import org.eclipse.ui.IWorkbenchPreferenceConstants;
20 import org.eclipse.ui.internal.util.PrefUtil;
21
22 /**
23  * This job creates an Animation Engine that uses an Animation Feedback to render
24  * the animation. To begin the animation, instantiate this
25  * object then call schedule().
26  * @since 3.3
27  *
28  */

29 public class AnimationEngine extends Job {
30     public static final int TICK_TIMER = 1;
31     public static final int unlimitedDuration = -1;
32     private boolean enableAnimations;
33     private Display display;
34     private AnimationFeedbackBase feedbackRenderer;
35     private long startTime;
36     private long curTime;
37     private long prevTime;
38     private int timingStyle = TICK_TIMER;
39     private long frameCount;
40     private int duration;
41     private long stepCount;
42     public static final int FRAME_COUNT = 2;
43     private boolean stopAnimating = false;
44     private long sleepAmount;
45
46     public AnimationEngine(AnimationFeedbackBase animationFeedback,
47             int durationIn) {
48         this(animationFeedback, durationIn, 0);
49     }
50
51     /**
52      * Creates an Animation that will run for the given number of milliseconds.
53      *
54      * @param animationFeedback provides renderStep(), initialize() and jobInit() methods
55      * @param durationIn number of milliseconds over which the animation will run
56      * @param sleepAmountIn number of milliseconds to slow/delay the animation
57      */

58     public AnimationEngine(AnimationFeedbackBase animationFeedback,
59             int durationIn, long sleepAmountIn) {
60         super(WorkbenchMessages.RectangleAnimation_Animating_Rectangle);
61         sleepAmount = sleepAmountIn;
62         feedbackRenderer = animationFeedback;
63         duration = durationIn;
64         
65         // if animations aren't on this is a NO-OP
66
IPreferenceStore preferenceStore = PrefUtil.getAPIPreferenceStore();
67         enableAnimations = preferenceStore
68                 .getBoolean(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS);
69         if (!enableAnimations) {
70             return;
71         }
72
73         stopAnimating = false;
74
75         // Capture parameters
76
display = feedbackRenderer.getAnimationShell().getDisplay();
77         // Don't show the job in monitors
78
setSystem(true);
79
80         // Set it up
81
feedbackRenderer.initialize(this);
82
83         // Set the animation's initial state
84
stepCount = 0;
85         curTime = startTime = System.currentTimeMillis();
86
87     }
88
89     /**
90      * @return The current renderer
91      */

92     public AnimationFeedbackBase getFeedback() {
93         return feedbackRenderer;
94     }
95     
96     private Runnable JavaDoc animationStep = new Runnable JavaDoc() {
97
98         public void run() {
99             // Capture time
100
prevTime = curTime;
101             curTime = System.currentTimeMillis();
102
103             if (isUpdateStep()) {
104                 updateDisplay();
105                 frameCount++;
106             }
107             stepCount++;
108         }
109
110     };
111
112     protected void updateDisplay() {
113         feedbackRenderer.renderStep(this);
114     }
115
116     protected boolean isUpdateStep() {
117         if (duration == unlimitedDuration) {
118             return true;
119         }
120         
121         switch (timingStyle) {
122             case TICK_TIMER:
123                 return prevTime != curTime;
124                 //for testing purposes
125
case FRAME_COUNT:
126                 return true;
127         }
128
129         return false;
130     }
131
132     private boolean done() {
133         return amount() >= 1.0;
134     }
135
136     public double amount() {
137         if (duration == unlimitedDuration) {
138             return 0;
139         }
140         double amount = 0.0;
141         switch (timingStyle) {
142             case TICK_TIMER:
143                 amount = (double) (curTime - startTime) / (double) duration;
144                 break;
145     
146             // For testing purposes
147
case FRAME_COUNT:
148                 amount = (double) frameCount / (double) duration;
149         }
150
151         if (amount > 1.0)
152             amount = 1.0;
153
154         return amount;
155     }
156
157     protected IStatus run(IProgressMonitor monitor) {
158         // TODO Auto-generated method stub
159

160         // We use preferece value to indicate that the animation should be skipped on this platform.
161
if (!enableAnimations) {
162             return Status.OK_STATUS;
163         }
164
165         // We're starting, initialize
166
display.syncExec(new Runnable JavaDoc() {
167             public void run() {
168                 // 'jobInit' returns 'false' if it doesn't want to run...
169
stopAnimating = !feedbackRenderer.jobInit(AnimationEngine.this);
170             }
171         });
172
173         // Only start the animation timer -after- we've initialized
174
curTime = startTime = System.currentTimeMillis();
175
176         while (!done() && !stopAnimating) {
177             display.syncExec(animationStep);
178             // Don't pin the CPU
179
try {
180                 Thread.sleep(sleepAmount);
181             } catch (InterruptedException JavaDoc e) {
182                 // TODO Auto-generated catch block
183
//e.printStackTrace();
184
}
185         }
186
187         // We're done, clean up
188
display.syncExec(new Runnable JavaDoc() {
189             public void run() {
190                 feedbackRenderer.dispose();
191             }
192         });
193
194         return Status.OK_STATUS;
195     }
196
197     public void cancelAnimation() {
198         stopAnimating = true;
199     }
200
201     public long getFrameCount() {
202         return frameCount;
203     }
204 }
205
Popular Tags