Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI colors #765

Open
wants to merge 13 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
260 changes: 182 additions & 78 deletions src/main/java/edu/rpi/legup/app/LegupPreferences.java
Original file line number Diff line number Diff line change
@@ -1,75 +1,135 @@
package edu.rpi.legup.app;

import java.util.HashMap;
import edu.rpi.legup.ui.color.ColorPreferences;

import java.io.File;
import java.util.EnumMap;
import java.util.Map;
import java.util.function.Function;
import java.util.prefs.Preferences;

public class LegupPreferences {

private static LegupPreferences instance;

private static String SAVED_PATH = "";
private static String savedPath = "";

private static final Preferences preferences =
Preferences.userNodeForPackage(LegupPreferences.class);

private static final Map<String, String> preferencesMap = new HashMap<>();
private static final Map<String, String> defaultPreferencesMap = new HashMap<>();
private static final Map<LegupPreference, Object> preferencesMap = new EnumMap<>(LegupPreference.class);

public enum LegupPreference {
WORK_DIRECTORY("work-directory", System.getProperty("user.dir"), o -> o),
START_FULL_SCREEN("start-full-screen", false, Boolean::parseBoolean),
AUTO_UPDATE("auto-update", true, Boolean::parseBoolean),
DARK_MODE("night-mode", false, Boolean::parseBoolean),
USE_CUSTOM_COLOR_THEME("use-custom-color-theme", false, Boolean::parseBoolean),
SHOW_MISTAKES("show-mistakes", true, Boolean::parseBoolean),
SHOW_ANNOTATIONS("show-annotations", true, Boolean::parseBoolean),
ALLOW_DEFAULT_RULES("allow-default-rules", false, Boolean::parseBoolean),
AUTO_GENERATE_CASES("auto-generate-cases", true, Boolean::parseBoolean),
IMMEDIATE_FEEDBACK("immediate-feedback", true, Boolean::parseBoolean),
COLOR_THEME_FILE("color-theme-file", System.getProperty("user.dir") + File.separator + ColorPreferences.LIGHT_COLOR_THEME_FILE_NAME, o -> o),
COLOR_BLIND("color-blind", false, Boolean::parseBoolean);

private final String id;
private final Object defaultValue;
/**
* Converts the object to a string so that it can be saved in preferences
*/
private final Function<Object, String> stringMapper;
/**
* Converts a string value to an object so it's more convenient to use in code
*/
private final Function<String, Object> stringToValueMapper;

@SuppressWarnings("unchecked")
<T> LegupPreference(
String id,
T defaultValue,
Function<T, String> stringMapper,
Function<String, T> stringToValueMapper
) {
this.id = id;
this.defaultValue = defaultValue;
this.stringMapper = (Function<Object, String>) stringMapper;
this.stringToValueMapper = (Function<String, Object>) stringToValueMapper;
}

public static final String WORK_DIRECTORY = "work-directory";
public static final String START_FULL_SCREEN = "start-full-screen";
public static final String AUTO_UPDATE = "auto-update";
public static final String DARK_MODE = "night-mode";
public static final String SHOW_MISTAKES = "show-mistakes";
public static final String SHOW_ANNOTATIONS = "show-annotations";
public static final String ALLOW_DEFAULT_RULES = "allow-default-rules";
public static final String AUTO_GENERATE_CASES = "auto-generate-cases";
public static final String IMMEDIATE_FEEDBACK = "immediate-feedback";
public static final String COLOR_BLIND = "color-blind";
<T> LegupPreference(String id, T defaultValue, Function<String, Object> stringToValueMapper) {
this(id, defaultValue, String::valueOf, stringToValueMapper);
}

static {
defaultPreferencesMap.put(WORK_DIRECTORY, System.getProperty("user.home"));
defaultPreferencesMap.put(START_FULL_SCREEN, Boolean.toString(false));
defaultPreferencesMap.put(AUTO_UPDATE, Boolean.toString(true));
defaultPreferencesMap.put(DARK_MODE, Boolean.toString(false));
defaultPreferencesMap.put(SHOW_MISTAKES, Boolean.toString(true));
defaultPreferencesMap.put(SHOW_ANNOTATIONS, Boolean.toString(false));
defaultPreferencesMap.put(ALLOW_DEFAULT_RULES, Boolean.toString(false));
defaultPreferencesMap.put(AUTO_GENERATE_CASES, Boolean.toString(true));
defaultPreferencesMap.put(IMMEDIATE_FEEDBACK, Boolean.toString(true));
defaultPreferencesMap.put(COLOR_BLIND, Boolean.toString(false));
public String id() {
return this.id;
}

public String defaultStringValue() {
return stringMapper.apply(defaultValue);
}

@SuppressWarnings("unused")
public Object defaultValue() {
return defaultValue;
}

public String stringValue() {
return stringMapper.apply(preferencesMap.get(this));
}

/**
* Convenience method to return the value of this preference cast to {@code clazz}
* @param clazz
* @return
* @param <T>
*/
public <T> T as(Class<T> clazz) {
return clazz.cast(stringToValueMapper.apply(stringValue()));
}

public boolean asBoolean() {
return as(Boolean.class);
}

public boolean asBoolean(String errorMessage) {
return as(Boolean.class, errorMessage);
}

public <T> T as(Class<T> clazz, String errorMessage) {
try {
return clazz.cast(defaultValue);
} catch (Exception e) {
throw new RuntimeException(errorMessage);
}
}

@SuppressWarnings("unchecked")
public <T> T convertToValue(String value) {
return (T) stringToValueMapper.apply(value);
}

public String convertToString(Object value) {
return stringMapper.apply(value);
}
}

private static Object getPreferenceOrDefault(LegupPreference preference) {
final String current = preferences.get(preference.id(), null);
if (current == null) {
return preference.defaultValue;
}
return current;
}

private static void addPreferenceFromDefault(LegupPreference preference) {
preferencesMap.put(preference, getPreferenceOrDefault(preference));
}

static {
preferencesMap.put(
WORK_DIRECTORY,
preferences.get(WORK_DIRECTORY, defaultPreferencesMap.get(WORK_DIRECTORY)));
preferencesMap.put(
START_FULL_SCREEN,
preferences.get(START_FULL_SCREEN, defaultPreferencesMap.get(START_FULL_SCREEN)));
preferencesMap.put(
AUTO_UPDATE, preferences.get(AUTO_UPDATE, defaultPreferencesMap.get(AUTO_UPDATE)));
preferencesMap.put(
DARK_MODE, preferences.get(DARK_MODE, defaultPreferencesMap.get(DARK_MODE)));
preferencesMap.put(
SHOW_MISTAKES,
preferences.get(SHOW_MISTAKES, defaultPreferencesMap.get(SHOW_MISTAKES)));
preferencesMap.put(
SHOW_ANNOTATIONS,
preferences.get(SHOW_ANNOTATIONS, defaultPreferencesMap.get(SHOW_ANNOTATIONS)));
preferencesMap.put(
ALLOW_DEFAULT_RULES,
preferences.get(
ALLOW_DEFAULT_RULES, defaultPreferencesMap.get(ALLOW_DEFAULT_RULES)));
preferencesMap.put(
AUTO_GENERATE_CASES,
preferences.get(
AUTO_GENERATE_CASES, defaultPreferencesMap.get(AUTO_GENERATE_CASES)));
preferencesMap.put(
IMMEDIATE_FEEDBACK,
preferences.get(IMMEDIATE_FEEDBACK, defaultPreferencesMap.get(IMMEDIATE_FEEDBACK)));
preferencesMap.put(
COLOR_BLIND, preferences.get(COLOR_BLIND, defaultPreferencesMap.get(COLOR_BLIND)));
for (final LegupPreference preference : LegupPreference.values()) {
addPreferenceFromDefault(preference);
}
}

/**
Expand All @@ -84,47 +144,91 @@ public static LegupPreferences getInstance() {
return instance;
}

/** Private LegupPreferences Singleton Constructor */
private LegupPreferences() {}
/**
* Private LegupPreferences Singleton Constructor
*/
private LegupPreferences() {
}

/**
* Gets the user preference by the string key
*
* @param key key name of the preference
* @param preference the preference to get
* @return value of the preference
*/
public String getUserPref(String key) {
return preferencesMap.get(key);
private Object getUserPref(LegupPreference preference) {
return preferencesMap.get(preference);
}

/**
* Gets the user preference by the string key, value pair
*
* @param key key name of the preference
* @param value value of the preference
* @param preference the preference to change
* @param value value of the preference
*/
public void setUserPref(String key, String value) {
preferences.put(key, value);
preferencesMap.put(key, value);
}

public boolean getUserPrefAsBool(String key) {
if (preferencesMap.get(key).equalsIgnoreCase(Boolean.toString(true))) {
return true;
} else {
if (preferencesMap.get(key).equalsIgnoreCase(Boolean.toString(false))) {
return false;
} else {
throw new RuntimeException("Cannot get user preference - " + key);
}
}
public void setUserPref(LegupPreference preference, Object value) {
preferences.put(preference.id(), preference.convertToString(value));
preferencesMap.put(preference, value);
}

public boolean getUserPrefAsBool(LegupPreference preference) {
return preference.asBoolean("Cannot get user preference - " + preference.id());
}

public String getSavedPath() {
return SAVED_PATH;
return savedPath;
}

public void setSavedPath(String path) {
SAVED_PATH = path;
savedPath = path;
}

public static boolean colorBlind() {
return LegupPreference.COLOR_BLIND.asBoolean();
}

public static boolean darkMode() {
return LegupPreference.DARK_MODE.asBoolean();
}

public static boolean showAnnotations() {
return LegupPreference.SHOW_ANNOTATIONS.asBoolean();
}

public static boolean allowDefaultRules() {
return LegupPreference.ALLOW_DEFAULT_RULES.asBoolean();
}

public static String workDirectory() {
return LegupPreference.WORK_DIRECTORY.stringValue();
}

public static boolean startFullScreen() {
return LegupPreference.START_FULL_SCREEN.asBoolean();
}

public static boolean autoUpdate() {
return LegupPreference.AUTO_UPDATE.asBoolean();
}

public static boolean showMistakes() {
return LegupPreference.SHOW_MISTAKES.asBoolean();
}

public static boolean autoGenerateCases() {
return LegupPreference.AUTO_GENERATE_CASES.asBoolean();
}

public static boolean immediateFeedback() {
return LegupPreference.IMMEDIATE_FEEDBACK.asBoolean();
}

public static boolean useCustomColorTheme() {
return LegupPreference.USE_CUSTOM_COLOR_THEME.asBoolean();
}

public static String colorThemeFile() {
return LegupPreference.COLOR_THEME_FILE.stringValue();
}

}
4 changes: 2 additions & 2 deletions src/main/java/edu/rpi/legup/controller/ElementController.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public void mouseEntered(MouseEvent e) {
String error = null;
if (elementView != null) {
selection.newHover(elementView);
if (LegupPreferences.getInstance().getUserPrefAsBool(LegupPreferences.SHOW_MISTAKES)) {
if (LegupPreferences.LegupPreference.SHOW_MISTAKES.asBoolean()) {
PuzzleElement element = elementView.getPuzzleElement();
if (treeElement != null
&& treeElement.getType() == TreeElementType.TRANSITION
Expand Down Expand Up @@ -249,7 +249,7 @@ public void mouseMoved(MouseEvent e) {
String error = null;
if (elementView != null && elementView != selection.getHover()) {
selection.newHover(elementView);
if (LegupPreferences.getInstance().getUserPrefAsBool(LegupPreferences.SHOW_MISTAKES)) {
if (LegupPreferences.LegupPreference.SHOW_MISTAKES.asBoolean()) {
PuzzleElement element = elementView.getPuzzleElement();
if (treeElement != null
&& treeElement.getType() == TreeElementType.TRANSITION
Expand Down
Loading
Loading