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

* Sets scheduler's sleep time. Larger sleep time gives more * cpu time to other applications, but makes visualizations less * responsive. *

* * @param sleepTime */ public static void setSleepTime(int time) { sleepTime = time; } // public boolean isSleepAlways() { // return sleepAlways; // } /** * if this flag is set scheludler gives time to other aplications * between each visualization update. Otherwise all visualizations are updated in row and * then priority is given to other applications. * @param sleepAlways */ // public static void setSleepAlways(boolean sleep) { // sleepAlways = sleep; // } public static SleepType getSleepType() { return sleepType; } public static void setSleepType(SleepType s) { sleepType = s; } }