--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.proconf.g3d.base;\r
+\r
+import java.util.ArrayList;\r
+\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.ui.PlatformUI;\r
+import org.simantics.utils.ErrorLogger;\r
+\r
+\r
+/**\r
+ * TODO : This is a copy-paste from old proconf.utils plug-ing:\r
+ * : PreferencePage is not ported,\r
+ * : Using Webmon ?\r
+ * \r
+ * \r
+ * VisualizationScheduler is singleton class that schedules\r
+ * visualization redraws. \r
+ * \r
+ * Scheduler has three behaviour patters: \r
+ * \r
+ * ONCE : Redraws all visualizations one by one and then sleeps for\r
+ * a while so that visualizations won't use all available cpu-time.\r
+ * \r
+ * ALWAYS : Redraws one visualization and sleeps before updating next\r
+ * visualization.\r
+ * \r
+ * OFF : uses asyncExec busy-loop (doesn't block eclipse, but other applications may suffer) \r
+ * \r
+ * \r
+ * @author Marko Luukkainen\r
+ *\r
+ */\r
+public class VisualizationScheduler implements Runnable{\r
+ private static VisualizationScheduler instance;\r
+ private Display display;\r
+ private ArrayList<Runnable> visualizations;\r
+ private boolean isDisposed = false;\r
+ private boolean stateFlag = false;\r
+ private static int sleepTime = 40;\r
+ private int index = 0;\r
+ \r
+ public enum SleepType{ONCE,ALWAYS,OFF};\r
+ \r
+ private static SleepType sleepType = SleepType.ONCE;\r
+ \r
+ private VisualizationScheduler() {\r
+ visualizations = new ArrayList<Runnable>();\r
+ display = PlatformUI.getWorkbench().getDisplay();\r
+ //sleepTime = UtilsPlugin.getDefault().getPreferenceStore().getInt(PreferenceConstants.SLEEP_TIME);\r
+ //sleepType = SleepType.valueOf(UtilsPlugin.getDefault().getPreferenceStore().getString(PreferenceConstants.SLEEP_TYPE));\r
+ run();\r
+ }\r
+\r
+ /**\r
+ * Scheduler's run loop<br>\r
+ * <br>\r
+ * Loop contains two stages:<br>\r
+ * In the first stage scheduler runs asyncExec for each visualization\r
+ * and after that it runs itself with asyncExec.<br>\r
+ * In the second stage scheduler runs itself with timerExec which allows OS to run it's own code.\r
+ */\r
+ public void run() {\r
+ if (!isDisposed && !display.isDisposed() && !PlatformUI.getWorkbench().isClosing()) {\r
+ switch(sleepType) {\r
+ case ALWAYS:\r
+ stateFlag = !stateFlag;\r
+ if (stateFlag) {\r
+ if (visualizations.size() > 0) {\r
+ if (index >= visualizations.size())\r
+ index = 0;\r
+ Runnable scene = visualizations.get(index);\r
+ try {\r
+ display.asyncExec(scene);\r
+ } catch (Exception e) {\r
+ ErrorLogger.defaultLogWarning("Scheduler exception", e);\r
+ }\r
+ index++;\r
+ }\r
+ display.asyncExec(this);\r
+ } else {\r
+ display.timerExec(sleepTime, this);\r
+ }\r
+ break;\r
+ case ONCE: \r
+ stateFlag = !stateFlag;\r
+ if (stateFlag) {\r
+ for (Runnable scene : visualizations) {\r
+ try {\r
+ display.asyncExec(scene);\r
+ } catch (Exception e) {\r
+ ErrorLogger.defaultLogWarning("Scheduler exception",e);\r
+ }\r
+ }\r
+ display.asyncExec(this);\r
+ } else {\r
+ display.timerExec(sleepTime,this);\r
+ }\r
+ break;\r
+ case OFF: \r
+ for (Runnable scene : visualizations) {\r
+ try {\r
+ display.asyncExec(scene);\r
+ } catch (Exception e) {\r
+ ErrorLogger.defaultLogWarning("Scheduler exception",e);\r
+ }\r
+ }\r
+ display.asyncExec(this);\r
+ break;\r
+ }\r
+ } \r
+ }\r
+ \r
+ /**\r
+ * Adds visualization into scheduler\r
+ * @param scene\r
+ */\r
+ public void addVisualization(Runnable scene) {\r
+ visualizations.add(scene);\r
+ }\r
+ \r
+ /**\r
+ * Removes visualization from scheduler\r
+ * @param scene\r
+ */\r
+ public void removeVisualization(Runnable scene) {\r
+ visualizations.remove(scene);\r
+ }\r
+ \r
+ /**\r
+ * disposes scheduler.\r
+ *\r
+ */\r
+ public void dispose() {\r
+ isDisposed = true;\r
+ } \r
+ \r
+ /**\r
+ * @return scheduler's instance\r
+ */\r
+ public static VisualizationScheduler getInstance() {\r
+ if (instance == null)\r
+ instance = new VisualizationScheduler();\r
+ \r
+ return instance;\r
+ }\r
+\r
+ /**\r
+ * @return the sleep time of the scheduler\r
+ */\r
+ public int getSleepTime() {\r
+ return sleepTime;\r
+ }\r
+\r
+ /**\r
+ * <p>\r
+ * Sets scheduler's sleep time. Larger sleep time gives more\r
+ * cpu time to other applications, but makes visualizations less\r
+ * responsive.\r
+ * </p>\r
+ * \r
+ * @param sleepTime\r
+ */\r
+ public static void setSleepTime(int time) {\r
+ sleepTime = time;\r
+ }\r
+\r
+// public boolean isSleepAlways() {\r
+// return sleepAlways;\r
+// }\r
+\r
+ /**\r
+ * if this flag is set scheludler gives time to other aplications\r
+ * between each visualization update. Otherwise all visualizations are updated in row and\r
+ * then priority is given to other applications.\r
+ * @param sleepAlways\r
+ */\r
+// public static void setSleepAlways(boolean sleep) {\r
+// sleepAlways = sleep;\r
+// } \r
+ \r
+ public static SleepType getSleepType() {\r
+ return sleepType;\r
+ }\r
+ \r
+ public static void setSleepType(SleepType s) {\r
+ sleepType = s;\r
+ }\r
+ \r
+}\r