1 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 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 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 IPreferenceStore preferenceStore = PrefUtil.getAPIPreferenceStore(); 67 enableAnimations = preferenceStore 68 .getBoolean(IWorkbenchPreferenceConstants.ENABLE_ANIMATIONS); 69 if (!enableAnimations) { 70 return; 71 } 72 73 stopAnimating = false; 74 75 display = feedbackRenderer.getAnimationShell().getDisplay(); 77 setSystem(true); 79 80 feedbackRenderer.initialize(this); 82 83 stepCount = 0; 85 curTime = startTime = System.currentTimeMillis(); 86 87 } 88 89 92 public AnimationFeedbackBase getFeedback() { 93 return feedbackRenderer; 94 } 95 96 private Runnable animationStep = new Runnable () { 97 98 public void run() { 99 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 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 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 160 if (!enableAnimations) { 162 return Status.OK_STATUS; 163 } 164 165 display.syncExec(new Runnable () { 167 public void run() { 168 stopAnimating = !feedbackRenderer.jobInit(AnimationEngine.this); 170 } 171 }); 172 173 curTime = startTime = System.currentTimeMillis(); 175 176 while (!done() && !stopAnimating) { 177 display.syncExec(animationStep); 178 try { 180 Thread.sleep(sleepAmount); 181 } catch (InterruptedException e) { 182 } 185 } 186 187 display.syncExec(new Runnable () { 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 |