1 /*******************************************************************************
\r
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * which accompanies this distribution, and is available at
\r
6 * http://www.eclipse.org/legal/epl-v10.html
\r
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.proconf.g3d.base;
\r
13 import java.util.ArrayList;
\r
15 import org.eclipse.swt.widgets.Display;
\r
16 import org.eclipse.ui.PlatformUI;
\r
17 import org.simantics.utils.ui.ErrorLogger;
\r
21 * TODO : This is a copy-paste from old proconf.utils plug-ing:
\r
22 * : PreferencePage is not ported,
\r
26 * VisualizationScheduler is singleton class that schedules
\r
27 * visualization redraws.
\r
29 * Scheduler has three behaviour patters:
\r
31 * ONCE : Redraws all visualizations one by one and then sleeps for
\r
32 * a while so that visualizations won't use all available cpu-time.
\r
34 * ALWAYS : Redraws one visualization and sleeps before updating next
\r
37 * OFF : uses asyncExec busy-loop (doesn't block eclipse, but other applications may suffer)
\r
40 * @author Marko Luukkainen
\r
43 public class VisualizationScheduler implements Runnable{
\r
44 private static VisualizationScheduler instance;
\r
45 private Display display;
\r
46 private ArrayList<Runnable> visualizations;
\r
47 private boolean isDisposed = false;
\r
48 private boolean stateFlag = false;
\r
49 private static int sleepTime = 40;
\r
50 private int index = 0;
\r
52 public enum SleepType{ONCE,ALWAYS,OFF};
\r
54 private static SleepType sleepType = SleepType.ONCE;
\r
56 private VisualizationScheduler() {
\r
57 visualizations = new ArrayList<Runnable>();
\r
58 display = PlatformUI.getWorkbench().getDisplay();
\r
59 //sleepTime = UtilsPlugin.getDefault().getPreferenceStore().getInt(PreferenceConstants.SLEEP_TIME);
\r
60 //sleepType = SleepType.valueOf(UtilsPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.SLEEP_TYPE));
\r
65 * Scheduler's run loop<br>
\r
67 * Loop contains two stages:<br>
\r
68 * In the first stage scheduler runs asyncExec for each visualization
\r
69 * and after that it runs itself with asyncExec.<br>
\r
70 * In the second stage scheduler runs itself with timerExec which allows OS to run it's own code.
\r
73 if (!isDisposed && !display.isDisposed() && !PlatformUI.getWorkbench().isClosing()) {
\r
76 stateFlag = !stateFlag;
\r
78 if (visualizations.size() > 0) {
\r
79 if (index >= visualizations.size())
\r
81 Runnable scene = visualizations.get(index);
\r
83 display.asyncExec(scene);
\r
84 } catch (Exception e) {
\r
85 ErrorLogger.defaultLogWarning("Scheduler exception", e);
\r
89 display.asyncExec(this);
\r
91 display.timerExec(sleepTime, this);
\r
95 stateFlag = !stateFlag;
\r
97 for (Runnable scene : visualizations) {
\r
99 display.asyncExec(scene);
\r
100 } catch (Exception e) {
\r
101 ErrorLogger.defaultLogWarning("Scheduler exception",e);
\r
104 display.asyncExec(this);
\r
106 display.timerExec(sleepTime,this);
\r
110 for (Runnable scene : visualizations) {
\r
112 display.asyncExec(scene);
\r
113 } catch (Exception e) {
\r
114 ErrorLogger.defaultLogWarning("Scheduler exception",e);
\r
117 display.asyncExec(this);
\r
124 * Adds visualization into scheduler
\r
127 public void addVisualization(Runnable scene) {
\r
128 visualizations.add(scene);
\r
132 * Removes visualization from scheduler
\r
135 public void removeVisualization(Runnable scene) {
\r
136 visualizations.remove(scene);
\r
140 * disposes scheduler.
\r
143 public void dispose() {
\r
148 * @return scheduler's instance
\r
150 public static VisualizationScheduler getInstance() {
\r
151 if (instance == null)
\r
152 instance = new VisualizationScheduler();
\r
158 * @return the sleep time of the scheduler
\r
160 public int getSleepTime() {
\r
166 * Sets scheduler's sleep time. Larger sleep time gives more
\r
167 * cpu time to other applications, but makes visualizations less
\r
173 public static void setSleepTime(int time) {
\r
177 // public boolean isSleepAlways() {
\r
178 // return sleepAlways;
\r
182 * if this flag is set scheludler gives time to other aplications
\r
183 * between each visualization update. Otherwise all visualizations are updated in row and
\r
184 * then priority is given to other applications.
\r
185 * @param sleepAlways
\r
187 // public static void setSleepAlways(boolean sleep) {
\r
188 // sleepAlways = sleep;
\r
191 public static SleepType getSleepType() {
\r
195 public static void setSleepType(SleepType s) {
\r