]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/base/VisualizationScheduler.java
e7011eb7ede77d8096e520eb7291dae9176e8c57
[simantics/3d.git] / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / base / VisualizationScheduler.java
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
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.base;\r
12 \r
13 import java.util.ArrayList;\r
14 \r
15 import org.eclipse.swt.widgets.Display;\r
16 import org.eclipse.ui.PlatformUI;\r
17 import org.simantics.utils.ErrorLogger;\r
18 \r
19 \r
20 /**\r
21  * TODO : This is a copy-paste from old proconf.utils plug-ing:\r
22  *              : PreferencePage is not ported,\r
23  *              : Using Webmon ?\r
24  * \r
25  * \r
26  * VisualizationScheduler is singleton class that schedules\r
27  * visualization redraws. \r
28  * \r
29  * Scheduler has three behaviour patters: \r
30  * \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
33  *    \r
34  * ALWAYS : Redraws one visualization and sleeps before updating next\r
35  *          visualization.\r
36  *    \r
37  * OFF : uses asyncExec busy-loop (doesn't block eclipse, but other applications may suffer) \r
38  * \r
39  * \r
40  * @author Marko Luukkainen\r
41  *\r
42  */\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
51     \r
52     public enum SleepType{ONCE,ALWAYS,OFF};\r
53     \r
54     private static SleepType sleepType = SleepType.ONCE;\r
55     \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
61         run();\r
62     }\r
63 \r
64     /**\r
65      * Scheduler's run loop<br>\r
66      * <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
71      */\r
72     public void run() {\r
73         if (!isDisposed && !display.isDisposed() && !PlatformUI.getWorkbench().isClosing()) {\r
74             switch(sleepType) {\r
75             case ALWAYS:\r
76                 stateFlag = !stateFlag;\r
77                                 if (stateFlag) {\r
78                                         if (visualizations.size() > 0) {\r
79                                                 if (index >= visualizations.size())\r
80                                                         index = 0;\r
81                                                 Runnable scene = visualizations.get(index);\r
82                                                 try {\r
83                                                         display.asyncExec(scene);\r
84                                                 } catch (Exception e) {\r
85                                                         ErrorLogger.defaultLogWarning("Scheduler exception", e);\r
86                                                 }\r
87                                                 index++;\r
88                                         }\r
89                                         display.asyncExec(this);\r
90                                 } else {\r
91                                         display.timerExec(sleepTime, this);\r
92                                 }\r
93                 break;\r
94             case ONCE: \r
95                 stateFlag = !stateFlag;\r
96                 if (stateFlag) {\r
97                         for (Runnable scene : visualizations) {\r
98                         try {\r
99                                 display.asyncExec(scene);\r
100                         } catch (Exception e) {\r
101                                 ErrorLogger.defaultLogWarning("Scheduler exception",e);\r
102                         }\r
103                         }\r
104                         display.asyncExec(this);\r
105                 } else {\r
106                         display.timerExec(sleepTime,this);\r
107                 }\r
108                 break;\r
109             case OFF: \r
110                 for (Runnable scene : visualizations) {\r
111                         try {\r
112                         display.asyncExec(scene);\r
113                         } catch (Exception e) {\r
114                                 ErrorLogger.defaultLogWarning("Scheduler exception",e);\r
115                         }\r
116                 }\r
117                 display.asyncExec(this);\r
118                 break;\r
119             }\r
120         }      \r
121     }\r
122     \r
123     /**\r
124      * Adds visualization into scheduler\r
125      * @param scene\r
126      */\r
127     public void addVisualization(Runnable scene) {\r
128         visualizations.add(scene);\r
129     }\r
130     \r
131     /**\r
132      * Removes visualization from scheduler\r
133      * @param scene\r
134      */\r
135     public void removeVisualization(Runnable scene) {\r
136         visualizations.remove(scene);\r
137     }\r
138     \r
139     /**\r
140      * disposes scheduler.\r
141      *\r
142      */\r
143     public void dispose() {\r
144         isDisposed = true;\r
145     } \r
146     \r
147     /**\r
148      * @return scheduler's instance\r
149      */\r
150     public static VisualizationScheduler getInstance() {\r
151         if (instance == null)\r
152             instance = new VisualizationScheduler();\r
153         \r
154         return instance;\r
155     }\r
156 \r
157     /**\r
158      * @return the sleep time of the scheduler\r
159      */\r
160     public int getSleepTime() {\r
161         return sleepTime;\r
162     }\r
163 \r
164     /**\r
165      * <p>\r
166      * Sets scheduler's sleep time. Larger sleep time gives more\r
167      * cpu time to other applications, but makes visualizations less\r
168      * responsive.\r
169      * </p>\r
170      *  \r
171      * @param sleepTime\r
172      */\r
173     public static void setSleepTime(int time) {\r
174         sleepTime = time;\r
175     }\r
176 \r
177 //      public boolean isSleepAlways() {\r
178 //              return sleepAlways;\r
179 //      }\r
180 \r
181         /**\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
186          */\r
187 //      public static void setSleepAlways(boolean sleep) {\r
188 //              sleepAlways = sleep;\r
189 //      }  \r
190     \r
191     public static SleepType getSleepType() {\r
192         return sleepType;\r
193     }\r
194     \r
195     public static void setSleepType(SleepType s) {\r
196         sleepType = s;\r
197     }\r
198     \r
199 }\r