]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation.ui/src/org/simantics/simulation/ui/preferences/SimulationPreferenceUtil.java
Added Set End Time command and handler as an alternate stepping mode
[simantics/platform.git] / bundles / org.simantics.simulation.ui / src / org / simantics / simulation / ui / preferences / SimulationPreferenceUtil.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.simulation.ui.preferences;
13
14 import org.eclipse.core.runtime.preferences.DefaultScope;
15 import org.eclipse.core.runtime.preferences.IEclipsePreferences;
16 import org.eclipse.core.runtime.preferences.IScopeContext;
17 import org.eclipse.core.runtime.preferences.InstanceScope;
18 import org.osgi.service.prefs.BackingStoreException;
19 import org.osgi.service.prefs.Preferences;
20 import org.simantics.simulation.ui.Activator;
21 import org.simantics.simulation.ui.preferences.SimulationPreferences.StepMode;
22
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public final class SimulationPreferenceUtil {
28
29     public static SimulationPreferences getDefaultPrefs() {
30         return getPrefs(DefaultScope.INSTANCE);
31     }
32
33     /**
34      * @return
35      */
36     public static SimulationPreferences getPrefs() {
37         return getPrefs(InstanceScope.INSTANCE);
38     }
39
40     /**
41      * @return
42      */
43     public static SimulationPreferences getPrefs(IScopeContext context) {
44         IEclipsePreferences node = context.getNode(Activator.PLUGIN_ID);
45         double stepDuration = node.getDouble(SimulationPreferences.P_SIMULATION_STEP_DURATION, SimulationPreferences.DEFAULT_SIMULATION_STEP_DURATION);
46         double stepEndTime = node.getDouble(SimulationPreferences.P_SIMULATION_STEP_END_TIME, SimulationPreferences.DEFAULT_SIMULATION_STEP_END_TIME);
47         int stepMode = node.getInt(SimulationPreferences.P_SIMULATION_STEP_MODE, SimulationPreferences.DEFAULT_SIMULATION_STEP_MODE.ordinal());
48
49         return new SimulationPreferences(node, stepDuration, stepEndTime, StepMode.fromInt(stepMode));
50     }
51
52     /**
53      * @return
54      * @throws BackingStoreException 
55      */
56     public static void setPrefs(SimulationPreferences prefs) {
57         setPrefs(InstanceScope.INSTANCE, prefs);
58     }
59
60     /**
61      * @return
62      * @throws BackingStoreException 
63      */
64     public static void flushPrefs(SimulationPreferences prefs) throws BackingStoreException {
65         Preferences p = _setPrefs(InstanceScope.INSTANCE, prefs);
66         p.flush();
67     }
68
69     /**
70      * @return
71      * @throws BackingStoreException 
72      */
73     public static void setPrefs(IScopeContext context, SimulationPreferences prefs) {
74         _setPrefs(context, prefs);
75     }
76
77     /**
78      * @return
79      * @throws BackingStoreException 
80      */
81     private static IEclipsePreferences _setPrefs(IScopeContext context, SimulationPreferences prefs) {
82         IEclipsePreferences node = context.getNode(Activator.PLUGIN_ID);
83         node.putDouble(SimulationPreferences.P_SIMULATION_STEP_DURATION, prefs.stepDuration);
84         node.putDouble(SimulationPreferences.P_SIMULATION_STEP_END_TIME, prefs.stepEndTime);
85         node.putInt(SimulationPreferences.P_SIMULATION_STEP_MODE, prefs.stepMode.ordinal());
86         return node;
87     }
88
89 }