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));
}
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;
{
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()
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
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,
package net.mograsim.preferences;
import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Device;
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
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...
package net.mograsim.preferences;
+import org.eclipse.swt.graphics.Color;
+
public abstract class Preferences
{
private static Preferences currentPreferences;
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));
+ }
}
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)