package envoy.client;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;
import envoy.client.ui.Color;
import envoy.client.ui.Theme;
import envoy.client.util.SerializationUtils;
import envoy.exception.EnvoyException;
/**
* Manages all application settings, which are different objects that can be
* changed during runtime and serialized them by using either the file system or
* the {@link Preferences} API.
*
* Project: envoy-client
* File: Settings.java
* Created: 11 Nov 2019
*
* @author Leon Hofmeister
* @author Maximilian Käfer
* @author Kai S. K. Engelbart
* @since Envoy v0.2-alpha
*/
public class Settings {
// Actual settings accessible by the rest of the application
private Map> items;
private Map themes;
/**
* Settings are stored in this file.
*/
private static final File settingsFile = new File(Config.getInstance().getHomeDirectory(), "settings.ser");
/**
* User-defined themes are stored inside this file.
*/
private static final File themeFile = new File(Config.getInstance().getHomeDirectory(), "themes.ser");
/**
* Singleton instance of this class.
*/
private static Settings settings = new Settings();
/**
* The way to instantiate the settings.
* Is set to private to deny other instances of that object.
*
* @since Envoy v0.2-alpha
*/
private Settings() {
// Load settings from settings file
try {
items = SerializationUtils.read(settingsFile, HashMap.class);
} catch (EnvoyException e) {
items = new HashMap<>();
}
supplementDefaults();
// Load themes from theme file
try {
themes = SerializationUtils.read(themeFile, HashMap.class);
} catch (EnvoyException e1) {
themes = new HashMap<>();
setCurrentTheme("dark");
}
// Load standard themes not defined in the themes file
themes.put("dark",
new Theme("dark", Color.black, Color.darkGray, Color.white, new Color(165, 60, 232), Color.white, Color.orange, Color.blue,
Color.white, Color.white));
themes.put("light",
new Theme("light", new Color(235, 235, 235), Color.white, Color.white, Color.darkGray, Color.black, Color.orange, Color.darkGray,
Color.black, Color.black));
}
/**
* This method is used to ensure that there is only one instance of Settings.
*
* @return the instance of Settings
* @since Envoy v0.2-alpha
*/
public static Settings getInstance() { return settings; }
/**
* Updates the preferences when the save button is clicked.
*
* @throws IOException if an error occurs while saving the themes to the theme
* file
* @since Envoy v0.2-alpha
*/
public void save() throws IOException {
// Save settings to settings file
SerializationUtils.write(settingsFile, items);
// Save themes to theme file
SerializationUtils.write(themeFile, themes);
}
private void supplementDefaults() {
items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key."));
items.putIfAbsent("onCloseMode", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed."));
items.putIfAbsent("currentTheme", new SettingsItem<>("dark", null));
}
/**
* Adds new theme to the theme map.
*
* @param theme the {@link Theme} to add
* @since Envoy v0.2-alpha
*/
public void addNewThemeToMap(Theme theme) { settings.getThemes().put(theme.getThemeName(), theme); }
/**
* @return the name of the currently active {@link Theme}
* @since Envoy v0.2-alpha
*/
public String getCurrentTheme() { return (String) items.get("currentTheme").get(); }
/**
* Sets the name of the current {@link Theme}.
*
* @param themeName the name to set
* @since Envoy v0.2-alpha
*/
@SuppressWarnings("unchecked")
public void setCurrentTheme(String themeName) { ((SettingsItem) items.get("currentTheme")).set(themeName); }
/**
* @return {@code true}, if pressing the {@code Enter} key suffices to send a
* message. Otherwise it has to be pressed in conjunction with the
* {@code Control} key.
* @since Envoy v0.2-alpha
*/
public boolean isEnterToSend() { return (boolean) items.get("enterToSend").get(); }
/**
* Changes the keystrokes performed by the user to send a message.
*
* @param enterToSend If set to {@code true} a message can be sent by pressing
* the {@code Enter} key. Otherwise it has to be pressed in
* conjunction with the {@code Control} key.
* @since Envoy v0.2-alpha
*/
@SuppressWarnings("unchecked")
public void setEnterToSend(boolean enterToSend) { ((SettingsItem) items.get("enterToSend")).set(enterToSend); }
/**
* @return the current on close mode.
* @since Envoy v0.3-alpha
*/
public boolean getCurrentOnCloseMode() { return (boolean) items.get("onCloseMode").get(); }
/**
* Sets the current on close mode.
*
* @param currentOnCloseMode the on close mode that should be set.
* @since Envoy v0.3-alpha
*/
@SuppressWarnings("unchecked")
public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem) items.get("onCloseMode")).set(currentOnCloseMode); }
/**
* @return the items
*/
public Map> getItems() { return items; }
/**
* @param items the items to set
*/
public void setItems(Map> items) { this.items = items; }
/**
* @return a {@code Map} of all themes with their names as keys
* @since Envoy v0.2-alpha
*/
public Map getThemes() { return themes; }
/**
* Sets the {@code Map} of all themes with their names as keys
*
* @param themes the theme map to set
* @since Envoy v0.2-alpha
*/
public void setThemes(Map themes) { this.themes = themes; }
/**
* @param themeName the name of the {@link Theme} to get
* @return the {@link Theme} with the specified name
* @since Envoy v0.3-alpha
*/
public Theme getTheme(String themeName) { return themes.get(themeName); }
}