Changes in the preference system
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 25 Jun 2019 08:45:39 +0000 (10:45 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Tue, 25 Jun 2019 08:45:39 +0000 (10:45 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/ColorHelper.java
net.mograsim.preferences/META-INF/MANIFEST.MF
net.mograsim.preferences/src/net/mograsim/preferences/ColorDefinition.java
net.mograsim.preferences/src/net/mograsim/preferences/ColorManager.java
net.mograsim.preferences/src/net/mograsim/preferences/DefaultPreferences.java
net.mograsim.preferences/src/net/mograsim/preferences/Preferences.java
net.mograsim.preferences/src/net/mograsim/preferences/SimpleColorManager.java

index b612b59..67a2730 100644 (file)
@@ -33,15 +33,15 @@ public class BitVectorFormatter
                switch (bitVector.getBit(0))
                {
                case ONE:
-                       return Preferences.current().getColor("net.mograsim.logic.ui.wire.bit.one");
+                       return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.one");
                case U:
-                       return Preferences.current().getColor("net.mograsim.logic.ui.wire.bit.u");
+                       return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.u");
                case X:
-                       return Preferences.current().getColor("net.mograsim.logic.ui.wire.bit.x");
+                       return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.x");
                case Z:
-                       return Preferences.current().getColor("net.mograsim.logic.ui.wire.bit.z");
+                       return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.z");
                case ZERO:
-                       return Preferences.current().getColor("net.mograsim.logic.ui.wire.bit.zero");
+                       return Preferences.current().getColorDefinition("net.mograsim.logic.ui.color.bit.zero");
                default:
                        throw new IllegalArgumentException("Unknown enum constant: " + bitVector.getBit(0));
                }
index b44a975..0ec42f5 100644 (file)
@@ -4,7 +4,6 @@ import java.util.function.Consumer;
 import java.util.function.Supplier;
 
 import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Device;
 
 import net.haspamelodica.swt.helper.gcs.GeneralGC;
 import net.mograsim.preferences.ColorDefinition;
@@ -14,24 +13,22 @@ public class ColorHelper
 {
        public static void executeWithDifferentForeground(GeneralGC gc, ColorDefinition col, Runnable exec)
        {
-               executeWithDifferentColor(gc.getDevice(), col, gc::getForeground, gc::setForeground, exec);
+               executeWithDifferentColor(col, gc::getForeground, gc::setForeground, exec);
        }
 
        public static void executeWithDifferentBackground(GeneralGC gc, ColorDefinition col, Runnable exec)
        {
-               executeWithDifferentColor(gc.getDevice(), col, gc::getBackground, gc::setBackground, exec);
+               executeWithDifferentColor(col, gc::getBackground, gc::setBackground, exec);
        }
 
-       private static void executeWithDifferentColor(Device device, ColorDefinition col, Supplier<Color> getColor, Consumer<Color> setColor,
-                       Runnable exec)
+       private static void executeWithDifferentColor(ColorDefinition col, Supplier<Color> getColor, Consumer<Color> setColor, Runnable exec)
        {
                ColorManager cm = ColorManager.current();
                Color oldColor = getColor.get();
-               Color newColor = cm.toColor(device, col);
+               Color newColor = cm.toColor(col);
                setColor.accept(newColor);
                exec.run();
                setColor.accept(oldColor);
-               cm.dispose(newColor);
        }
 
        private ColorHelper()
index e407be3..7fb5fc0 100644 (file)
@@ -7,4 +7,5 @@ Export-Package: net.mograsim.preferences
 Bundle-Vendor: Mograsim Team
 Automatic-Module-Name: net.mograsim.preferences
 Import-Package: org.eclipse.swt,
- org.eclipse.swt.graphics
+ org.eclipse.swt.graphics,
+ org.eclipse.swt.widgets
index 8d3c0cd..621de5f 100644 (file)
@@ -47,6 +47,39 @@ public class ColorDefinition
                this.b = b;
        }
 
+       @Override
+       public int hashCode()
+       {
+               final int prime = 31;
+               int result = 1;
+               result = prime * result + b;
+               result = prime * result + ((builtInColor == null) ? 0 : builtInColor.hashCode());
+               result = prime * result + g;
+               result = prime * result + r;
+               return result;
+       }
+
+       @Override
+       public boolean equals(Object obj)
+       {
+               if (this == obj)
+                       return true;
+               if (obj == null)
+                       return false;
+               if (getClass() != obj.getClass())
+                       return false;
+               ColorDefinition other = (ColorDefinition) obj;
+               if (b != other.b)
+                       return false;
+               if (builtInColor != other.builtInColor)
+                       return false;
+               if (g != other.g)
+                       return false;
+               if (r != other.r)
+                       return false;
+               return true;
+       }
+
        public static enum BuiltInColor
        {
                COLOR_WHITE, COLOR_BLACK, COLOR_RED, COLOR_DARK_RED, COLOR_GREEN, COLOR_DARK_GREEN, COLOR_YELLOW, COLOR_DARK_YELLOW, COLOR_BLUE,
index 79f7cee..f6f5d8d 100644 (file)
@@ -1,7 +1,6 @@
 package net.mograsim.preferences;
 
 import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Device;
 
 public abstract class ColorManager
 {
@@ -21,7 +20,10 @@ public abstract class ColorManager
                return currentManager;
        }
 
-       public abstract Color toColor(Device device, ColorDefinition col);
+       public abstract Color toColor(ColorDefinition col);
 
-       public abstract void dispose(Color col);
+       public void clearCache()
+       {
+               // this method is intended to be overridden
+       }
 }
\ No newline at end of file
index 49c7c0d..b843cf3 100644 (file)
@@ -5,19 +5,19 @@ import net.mograsim.preferences.ColorDefinition.BuiltInColor;
 public class DefaultPreferences extends Preferences
 {
        @Override
-       public ColorDefinition getColor(String name)
+       public ColorDefinition getColorDefinition(String name)
        {
                switch (name)
                {
-               case "net.mograsim.logic.ui.wire.bit.one":
+               case "net.mograsim.logic.ui.color.bit.one":
                        return new ColorDefinition(BuiltInColor.COLOR_GREEN);
-               case "net.mograsim.logic.ui.wire.bit.u":
+               case "net.mograsim.logic.ui.color.bit.u":
                        return new ColorDefinition(BuiltInColor.COLOR_CYAN);
-               case "net.mograsim.logic.ui.wire.bit.x":
+               case "net.mograsim.logic.ui.color.bit.x":
                        return new ColorDefinition(BuiltInColor.COLOR_RED);
-               case "net.mograsim.logic.ui.wire.bit.z":
+               case "net.mograsim.logic.ui.color.bit.z":
                        return new ColorDefinition(BuiltInColor.COLOR_YELLOW);
-               case "net.mograsim.logic.ui.wire.bit.zero":
+               case "net.mograsim.logic.ui.color.bit.zero":
                        return new ColorDefinition(BuiltInColor.COLOR_GRAY);
                default:
                        // TODO proper logging here...
index 70505ca..5977490 100644 (file)
@@ -1,5 +1,7 @@
 package net.mograsim.preferences;
 
+import org.eclipse.swt.graphics.Color;
+
 public abstract class Preferences
 {
        private static Preferences currentPreferences;
@@ -18,5 +20,10 @@ public abstract class Preferences
                return currentPreferences;
        }
 
-       public abstract ColorDefinition getColor(String name);
+       public abstract ColorDefinition getColorDefinition(String name);
+
+       public Color getColor(String name)
+       {
+               return ColorManager.current().toColor(getColorDefinition(name));
+       }
 }
index 74f74dc..60b5311 100644 (file)
@@ -2,39 +2,54 @@ package net.mograsim.preferences;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.Device;
+import org.eclipse.swt.widgets.Display;
 
 import net.mograsim.preferences.ColorDefinition.BuiltInColor;
 
 public class SimpleColorManager extends ColorManager
 {
-       private static final Map<BuiltInColor, Color> systemColors = new HashMap<>();
-       private static final Map<Color, BuiltInColor> systemColorConstants = new HashMap<>();
+       private static final Map<ColorDefinition, Color> systemColors = new HashMap<>();
+
+       private final Device device;
+       private final Map<ColorDefinition, Color> cachedColors;
+
+       public SimpleColorManager()
+       {
+               this.device = Display.getCurrent();
+               this.cachedColors = new HashMap<>(systemColors);
+       }
 
        @Override
-       public Color toColor(Device device, ColorDefinition col)
+       public Color toColor(ColorDefinition col)
        {
-               Color systemColor = systemColors.get(col.builtInColor);
-               if (systemColor != null)
-                       return systemColor;
+               if (col == null)
+                       return null;
+               Color cachedColor = cachedColors.get(col);
+               if (cachedColor != null)
+                       return cachedColor;
                if (col.builtInColor != null)
                {
-                       systemColor = device.getSystemColor(toSWTColorConstant(col.builtInColor));
-                       systemColors.put(col.builtInColor, systemColor);
-                       systemColorConstants.put(systemColor, col.builtInColor);
+                       Color systemColor = device.getSystemColor(toSWTColorConstant(col.builtInColor));
+                       systemColors.put(col, systemColor);
+                       cachedColors.put(col, systemColor);
                        return systemColor;
                }
-               return new Color(device, col.r, col.g, col.b);
+               Color nonSystemColor = new Color(device, col.r, col.g, col.b);
+               cachedColors.put(col, nonSystemColor);
+               return nonSystemColor;
        }
 
        @Override
-       public void dispose(Color col)
+       public void clearCache()
        {
-               if (!systemColorConstants.containsKey(col))
-                       col.dispose();
+               cachedColors.entrySet().stream().filter(e -> !systemColors.containsKey(e.getKey())).map(Entry::getValue).forEach(Color::dispose);
+               cachedColors.clear();
+               cachedColors.putAll(systemColors);
        }
 
        public static int toSWTColorConstant(BuiltInColor col)