From: Daniel Kirschten Date: Tue, 25 Jun 2019 08:45:39 +0000 (+0200) Subject: Changes in the preference system X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=9b0530074d07ba11b12c23df4370dd035da774b3;p=Mograsim.git Changes in the preference system --- diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java index b612b59d..67a2730d 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVectorFormatter.java @@ -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)); } diff --git a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/ColorHelper.java b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/ColorHelper.java index b44a975e..0ec42f57 100644 --- a/net.mograsim.logic.ui/src/net/mograsim/logic/ui/ColorHelper.java +++ b/net.mograsim.logic.ui/src/net/mograsim/logic/ui/ColorHelper.java @@ -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 getColor, Consumer setColor, - Runnable exec) + private static void executeWithDifferentColor(ColorDefinition col, Supplier getColor, Consumer 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() diff --git a/net.mograsim.preferences/META-INF/MANIFEST.MF b/net.mograsim.preferences/META-INF/MANIFEST.MF index e407be3a..7fb5fc03 100644 --- a/net.mograsim.preferences/META-INF/MANIFEST.MF +++ b/net.mograsim.preferences/META-INF/MANIFEST.MF @@ -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 diff --git a/net.mograsim.preferences/src/net/mograsim/preferences/ColorDefinition.java b/net.mograsim.preferences/src/net/mograsim/preferences/ColorDefinition.java index 8d3c0cd9..621de5f6 100644 --- a/net.mograsim.preferences/src/net/mograsim/preferences/ColorDefinition.java +++ b/net.mograsim.preferences/src/net/mograsim/preferences/ColorDefinition.java @@ -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, diff --git a/net.mograsim.preferences/src/net/mograsim/preferences/ColorManager.java b/net.mograsim.preferences/src/net/mograsim/preferences/ColorManager.java index 79f7ceea..f6f5d8dc 100644 --- a/net.mograsim.preferences/src/net/mograsim/preferences/ColorManager.java +++ b/net.mograsim.preferences/src/net/mograsim/preferences/ColorManager.java @@ -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 diff --git a/net.mograsim.preferences/src/net/mograsim/preferences/DefaultPreferences.java b/net.mograsim.preferences/src/net/mograsim/preferences/DefaultPreferences.java index 49c7c0d0..b843cf37 100644 --- a/net.mograsim.preferences/src/net/mograsim/preferences/DefaultPreferences.java +++ b/net.mograsim.preferences/src/net/mograsim/preferences/DefaultPreferences.java @@ -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... diff --git a/net.mograsim.preferences/src/net/mograsim/preferences/Preferences.java b/net.mograsim.preferences/src/net/mograsim/preferences/Preferences.java index 70505ca4..59774906 100644 --- a/net.mograsim.preferences/src/net/mograsim/preferences/Preferences.java +++ b/net.mograsim.preferences/src/net/mograsim/preferences/Preferences.java @@ -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)); + } } diff --git a/net.mograsim.preferences/src/net/mograsim/preferences/SimpleColorManager.java b/net.mograsim.preferences/src/net/mograsim/preferences/SimpleColorManager.java index 74f74dc0..60b53110 100644 --- a/net.mograsim.preferences/src/net/mograsim/preferences/SimpleColorManager.java +++ b/net.mograsim.preferences/src/net/mograsim/preferences/SimpleColorManager.java @@ -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 systemColors = new HashMap<>(); - private static final Map systemColorConstants = new HashMap<>(); + private static final Map systemColors = new HashMap<>(); + + private final Device device; + private final Map 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)