From: Daniel Kirschten <daniel.kirschten@gmx.de>
Date: Mon, 16 Sep 2019 14:28:31 +0000 (+0200)
Subject: Improved preference handling in plugin.core
X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=ab52c884bb0eea5de4e3266b9ac168791c5cf67c;p=Mograsim.git

Improved preference handling in plugin.core
---

diff --git a/plugins/net.mograsim.plugin.core/plugin.xml b/plugins/net.mograsim.plugin.core/plugin.xml
index dccf76c8..87e1edec 100644
--- a/plugins/net.mograsim.plugin.core/plugin.xml
+++ b/plugins/net.mograsim.plugin.core/plugin.xml
@@ -338,5 +338,13 @@
             label="%fontDefinition.label">
       </fontDefinition>
    </extension>
+   <extension
+         point="org.eclipse.ui.preferencePages">
+      <page
+            class="net.mograsim.plugin.MainPreferencePage"
+            id="net.mograsim.plugin.core.mainprefpage"
+            name="Mograsim preferences">
+      </page>
+   </extension>
 
 </plugin>
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/EclipsePreferences.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/EclipsePreferences.java
new file mode 100644
index 00000000..1e2c4477
--- /dev/null
+++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/EclipsePreferences.java
@@ -0,0 +1,72 @@
+package net.mograsim.plugin;
+
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.resource.ColorRegistry;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.ui.statushandlers.StatusManager;
+import org.eclipse.ui.themes.ITheme;
+
+import net.mograsim.preferences.ColorDefinition;
+import net.mograsim.preferences.DefaultPreferences;
+import net.mograsim.preferences.Preferences;
+
+public class EclipsePreferences extends Preferences
+{
+	private final ITheme theme;
+	private final IPreferenceStore prefs;
+	private final Preferences defaultPrefs;
+
+	public EclipsePreferences(ITheme theme, IPreferenceStore prefs)
+	{
+		this.theme = theme;
+		this.prefs = prefs;
+		this.defaultPrefs = new DefaultPreferences();
+	}
+
+	@Override
+	public boolean getBoolean(String name)
+	{
+		prefs.setDefault(name, defaultPrefs.getBoolean(name));
+		return prefs.getBoolean(name);
+	}
+
+	@Override
+	public int getInt(String name)
+	{
+		prefs.setDefault(name, defaultPrefs.getInt(name));
+		return prefs.getInt(name);
+	}
+
+	@Override
+	public double getDouble(String name)
+	{
+		prefs.setDefault(name, defaultPrefs.getDouble(name));
+		return prefs.getDouble(name);
+	}
+
+	@Override
+	public ColorDefinition getColorDefinition(String name)
+	{
+		RGB rgb = getColorRegistry().getRGB(name);
+		if (rgb == null)
+		{
+			StatusManager.getManager().handle(new Status(IStatus.ERROR, "net.mograsim.plugin.core", "No color for name " + name));
+			return null;
+		}
+		return new ColorDefinition(rgb.red, rgb.green, rgb.blue);
+	}
+
+	@Override
+	public Color getColor(String name)
+	{
+		return getColorRegistry().get(name);
+	}
+
+	private ColorRegistry getColorRegistry()
+	{
+		return theme.getColorRegistry();
+	}
+}
\ No newline at end of file
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MainPreferencePage.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MainPreferencePage.java
new file mode 100644
index 00000000..69430ed9
--- /dev/null
+++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MainPreferencePage.java
@@ -0,0 +1,27 @@
+package net.mograsim.plugin;
+
+import org.eclipse.jface.preference.BooleanFieldEditor;
+import org.eclipse.jface.preference.FieldEditorPreferencePage;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPreferencePage;
+
+public class MainPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage
+{
+	public MainPreferencePage()
+	{
+		super(GRID);
+	}
+
+	@Override
+	public void init(IWorkbench workbench)
+	{
+		setPreferenceStore(MograsimActivator.instance().getPreferenceStore());
+	}
+
+	@Override
+	protected void createFieldEditors()
+	{
+		addField(new BooleanFieldEditor("net.mograsim.logic.model.debug.openhlsshell", "Open the debug HLS shell", getFieldEditorParent()));
+		// TODO add other preferences
+	}
+}
\ No newline at end of file
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MograsimActivator.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MograsimActivator.java
index c17f5304..2d8e8e67 100644
--- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MograsimActivator.java
+++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MograsimActivator.java
@@ -6,9 +6,20 @@ import net.mograsim.machine.MachineRegistry;
 
 public final class MograsimActivator extends AbstractUIPlugin
 {
+	private static MograsimActivator instance;
 
 	public MograsimActivator()
 	{
+		if (instance != null)
+			throw new IllegalStateException("MograsimActivator already created!");
+		instance = this;
 		MachineRegistry.reload();
 	}
-}
+
+	public static MograsimActivator instance()
+	{
+		if (instance == null)
+			throw new IllegalStateException("MograsimActivator not yet created!");
+		return instance;
+	}
+}
\ No newline at end of file
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java
index 5fcc1688..92d45a5c 100644
--- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java
+++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java
@@ -32,7 +32,7 @@ public class SimulationPreview implements IThemePreview
 	{
 		oldPreferences = Preferences.current();
 
-		currentThemePreferences = new ThemePreferences(currentTheme);
+		currentThemePreferences = new EclipsePreferences(currentTheme, MograsimActivator.instance().getPreferenceStore());
 		// TODO this will change the global preferences; so if another LogicUICanvas redraws, it will use the "new" colors too.
 		Preferences.setPreferences(currentThemePreferences);
 
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java
deleted file mode 100644
index 42da25d1..00000000
--- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java
+++ /dev/null
@@ -1,65 +0,0 @@
-package net.mograsim.plugin;
-
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
-import org.eclipse.jface.resource.ColorRegistry;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.RGB;
-import org.eclipse.ui.statushandlers.StatusManager;
-import org.eclipse.ui.themes.ITheme;
-
-import net.mograsim.preferences.ColorDefinition;
-import net.mograsim.preferences.DefaultPreferences;
-import net.mograsim.preferences.Preferences;
-
-// TODO proper getInt/getDouble implementation, maybe via own preferences page?
-public class ThemePreferences extends Preferences
-{
-	private final ITheme theme;
-
-	public ThemePreferences(ITheme theme)
-	{
-		this.theme = theme;
-	}
-
-	@Override
-	public boolean getBoolean(String name)
-	{
-		return new DefaultPreferences().getBoolean(name);
-	}
-
-	@Override
-	public int getInt(String name)
-	{
-		return new DefaultPreferences().getInt(name);
-	}
-
-	@Override
-	public double getDouble(String name)
-	{
-		return new DefaultPreferences().getDouble(name);
-	}
-
-	@Override
-	public ColorDefinition getColorDefinition(String name)
-	{
-		RGB rgb = getColorRegistry().getRGB(name);
-		if (rgb == null)
-		{
-			StatusManager.getManager().handle(new Status(IStatus.ERROR, "net.mograsim.plugin.core", "No color for name " + name));
-			return null;
-		}
-		return new ColorDefinition(rgb.red, rgb.green, rgb.blue);
-	}
-
-	@Override
-	public Color getColor(String name)
-	{
-		return getColorRegistry().get(name);
-	}
-
-	private ColorRegistry getColorRegistry()
-	{
-		return theme.getColorRegistry();
-	}
-}
\ No newline at end of file
diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java
index 212f777c..9916bf4d 100644
--- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java
+++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java
@@ -12,8 +12,9 @@ import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInpu
 import net.mograsim.logic.model.LogicExecuter;
 import net.mograsim.logic.model.LogicUICanvas;
 import net.mograsim.machine.Machine;
+import net.mograsim.plugin.EclipsePreferences;
 import net.mograsim.plugin.MachineContext;
-import net.mograsim.plugin.ThemePreferences;
+import net.mograsim.plugin.MograsimActivator;
 import net.mograsim.preferences.Preferences;
 
 public class LogicUIPart extends ViewPart
@@ -35,7 +36,8 @@ public class LogicUIPart extends ViewPart
 	public void createPartControl(Composite parent)
 	{
 		// set preferences
-		Preferences.setPreferences(new ThemePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()));
+		Preferences.setPreferences(new EclipsePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme(),
+				MograsimActivator.instance().getPreferenceStore()));
 
 		Machine m = MachineContext.getInstance().getMachine();