From ab52c884bb0eea5de4e3266b9ac168791c5cf67c Mon Sep 17 00:00:00 2001 From: Daniel Kirschten Date: Mon, 16 Sep 2019 16:28:31 +0200 Subject: [PATCH] Improved preference handling in plugin.core --- plugins/net.mograsim.plugin.core/plugin.xml | 8 ++++++ ...eferences.java => EclipsePreferences.java} | 19 ++++++++----- .../mograsim/plugin/MainPreferencePage.java | 27 +++++++++++++++++++ .../mograsim/plugin/MograsimActivator.java | 13 ++++++++- .../mograsim/plugin/SimulationPreview.java | 2 +- .../mograsim/plugin/views/LogicUIPart.java | 6 +++-- 6 files changed, 65 insertions(+), 10 deletions(-) rename plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/{ThemePreferences.java => EclipsePreferences.java} (67%) create mode 100644 plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/MainPreferencePage.java 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"> + + + + diff --git a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/EclipsePreferences.java similarity index 67% rename from plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java rename to plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/EclipsePreferences.java index 42da25d1..1e2c4477 100644 --- a/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java +++ b/plugins/net.mograsim.plugin.core/src/net/mograsim/plugin/EclipsePreferences.java @@ -2,6 +2,7 @@ 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; @@ -12,32 +13,38 @@ 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 +public class EclipsePreferences extends Preferences { private final ITheme theme; + private final IPreferenceStore prefs; + private final Preferences defaultPrefs; - public ThemePreferences(ITheme theme) + public EclipsePreferences(ITheme theme, IPreferenceStore prefs) { this.theme = theme; + this.prefs = prefs; + this.defaultPrefs = new DefaultPreferences(); } @Override public boolean getBoolean(String name) { - return new DefaultPreferences().getBoolean(name); + prefs.setDefault(name, defaultPrefs.getBoolean(name)); + return prefs.getBoolean(name); } @Override public int getInt(String name) { - return new DefaultPreferences().getInt(name); + prefs.setDefault(name, defaultPrefs.getInt(name)); + return prefs.getInt(name); } @Override public double getDouble(String name) { - return new DefaultPreferences().getDouble(name); + prefs.setDefault(name, defaultPrefs.getDouble(name)); + return prefs.getDouble(name); } @Override 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/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(); -- 2.17.1