Improved preference handling in plugin.core
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 16 Sep 2019 14:28:31 +0000 (16:28 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 16 Sep 2019 14:32:27 +0000 (16:32 +0200)
plugins/net.mograsim.plugin.core/plugin.xml
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/EclipsePreferences.java [new file with mode: 0644]
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MainPreferencePage.java [new file with mode: 0644]
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MograsimActivator.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java [deleted file]
plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java

index dccf76c..87e1ede 100644 (file)
             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 (file)
index 0000000..1e2c447
--- /dev/null
@@ -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 (file)
index 0000000..69430ed
--- /dev/null
@@ -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
index c17f530..2d8e8e6 100644 (file)
@@ -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
index 5fcc168..92d45a5 100644 (file)
@@ -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 (file)
index 42da25d..0000000
+++ /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
index 212f777..9916bf4 100644 (file)
@@ -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();