The user no longer has to click on "Apply" to see changes in the preview
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 25 Jun 2019 09:47:16 +0000 (11:47 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 25 Jun 2019 09:47:16 +0000 (11:47 +0200)
net.mograsim.plugin.core/src/net/mograsim/plugin/SimulationPreview.java
net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java [new file with mode: 0644]
net.mograsim.plugin.core/src/net/mograsim/plugin/views/LogicUIPart.java

index 381a2bc..b8488d5 100644 (file)
@@ -17,17 +17,25 @@ import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.model.wires.WireCrossPoint;
 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
+import net.mograsim.preferences.Preferences;
 
 public class SimulationPreview implements IThemePreview
 {
-
        private LogicUICanvas ui;
        private LogicExecuter exec;
+       private Preferences oldPreferences;
+       private Preferences currentThemePreferences;
 
        @Override
        @SuppressWarnings("unused")
        public void createControl(Composite parent, ITheme currentTheme)
        {
+               oldPreferences = Preferences.current();
+
+               currentThemePreferences = new ThemePreferences(currentTheme);
+               // TODO this will change the global preferences; so if another LogicUICanvas redraws, it will use the "new" colors too.
+               Preferences.setPreferences(currentThemePreferences);
+
                ViewModelModifiable model = new ViewModelModifiable();
                LogicModelParameters params = new LogicModelParameters();
                params.gateProcessTime = 50;
@@ -81,11 +89,15 @@ public class SimulationPreview implements IThemePreview
 
                ui.zoom(3.5, 10, 10);
                exec.startLiveExecution();
+
+               currentTheme.addPropertyChangeListener(e -> ui.redraw());
        }
 
        @Override
        public void dispose()
        {
                exec.stopLiveExecution();
+               if (Preferences.current() == currentThemePreferences)
+                       Preferences.setPreferences(oldPreferences);
        }
 }
diff --git a/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java b/net.mograsim.plugin.core/src/net/mograsim/plugin/ThemePreferences.java
new file mode 100644 (file)
index 0000000..b4bc0ac
--- /dev/null
@@ -0,0 +1,45 @@
+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.Preferences;
+
+public class ThemePreferences extends Preferences
+{
+       private final ITheme theme;
+
+       public ThemePreferences(ITheme theme)
+       {
+               this.theme = theme;
+       }
+
+       @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 d40f5a3..1ae8066 100644 (file)
@@ -2,17 +2,11 @@ package net.mograsim.plugin.views;
 
 import javax.inject.Inject;
 
-import org.eclipse.core.runtime.IStatus;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
-import org.eclipse.jface.resource.ColorRegistry;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.RGB;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.part.ViewPart;
-import org.eclipse.ui.statushandlers.StatusManager;
 
 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
 import net.mograsim.logic.core.timeline.Timeline;
@@ -26,7 +20,7 @@ import net.mograsim.logic.ui.model.components.mi.nandbased.am2901.GUIAm2901;
 import net.mograsim.logic.ui.model.wires.GUIWire;
 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
-import net.mograsim.preferences.ColorDefinition;
+import net.mograsim.plugin.ThemePreferences;
 import net.mograsim.preferences.Preferences;
 
 public class LogicUIPart extends ViewPart
@@ -48,31 +42,7 @@ public class LogicUIPart extends ViewPart
        public void createPartControl(Composite parent)
        {
                // set preferences
-               Preferences.setPreferences(new Preferences()
-               {
-                       @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 PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
-                       }
-               });
+               Preferences.setPreferences(new ThemePreferences(PlatformUI.getWorkbench().getThemeManager().getCurrentTheme()));
 
                // setup view model
                ViewModelModifiable viewModel = new ViewModelModifiable();