This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/data/Settings.java

238 lines
6.6 KiB
Java
Raw Normal View History

package envoy.client.data;
import java.io.File;
import java.io.IOException;
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.util.SerializationUtils;
/**
* 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.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>Settings.java</strong><br>
* Created: <strong>11 Nov 2019</strong><br>
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
* @since Envoy Client v0.2-alpha
*/
public class Settings {
// Actual settings accessible by the rest of the application
private Map<String, SettingsItem<?>> items;
private Map<String, Theme> themes;
/**
* Settings are stored in this file.
*/
private static final File settingsFile = new File(ClientConfig.getInstance().getHomeDirectory(), "settings.ser");
/**
* User-defined themes are stored inside this file.
*/
private static final File themeFile = new File(ClientConfig.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 Client v0.2-alpha
*/
private Settings() {
// Load settings from settings file
try {
items = SerializationUtils.read(settingsFile, HashMap.class);
} catch (ClassNotFoundException | IOException e) {
items = new HashMap<>();
}
supplementDefaults();
2019-12-22 21:48:19 +01:00
// Load themes from theme file
try {
themes = SerializationUtils.read(themeFile, HashMap.class);
} catch (ClassNotFoundException | IOException 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 Client v0.2-alpha
*/
public static Settings getInstance() {
return settings;
}
/**
* Updates the preferences when the save button is clicked.
*
2020-04-18 21:37:44 +02:00
* @throws IOException if an error occurs while saving the themes
* @since Envoy Client v0.2-alpha
*/
public void save() throws IOException {
2020-04-18 21:37:44 +02:00
// 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", "Current Theme Name", "The name of the currently selected theme."));
}
/**
* Adds new theme to the theme map.
*
* @param theme the {@link Theme} to add
* @since Envoy Client v0.2-alpha
*/
public void addNewThemeToMap(Theme theme) {
getThemes().put(theme.getThemeName(), theme);
}
/**
* @return the name of the currently active {@link Theme}
* @since Envoy Client v0.2-alpha
*/
public String getCurrentThemeName() {
return (String) items.get("currentTheme").get();
}
/**
* @return the currently active {@link Theme}
* @since Envoy Client v0.1-beta
*/
public Theme getCurrentTheme() {
return getTheme(getCurrentThemeName());
}
/**
* Sets the name of the current {@link Theme}.
*
* @param themeName the name to set
* @since Envoy Client v0.2-alpha
*/
public void setCurrentTheme(String themeName) {
((SettingsItem<String>) items.get("currentTheme")).set(themeName);
}
/**
* @return whether the current {@link Theme} is one of the default themes.
* Currently checks for dark and light theme.
* @since Envoy Client v0.1-beta
*/
public boolean isUsingDefaultTheme() {
return getCurrentThemeName().equals("dark") || getCurrentThemeName().equals("light");
}
/**
* @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 Client 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 Client v0.2-alpha
*/
public void setEnterToSend(boolean enterToSend) {
((SettingsItem<Boolean>) items.get("enterToSend")).set(enterToSend);
}
/**
* @return the current on close mode.
* @since Envoy Client 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 Client v0.3-alpha
*/
public void setCurrentOnCloseMode(boolean currentOnCloseMode) {
((SettingsItem<Boolean>) items.get("onCloseMode")).set(currentOnCloseMode);
}
2019-12-22 21:48:19 +01:00
/**
* @return the items
*/
public Map<String, SettingsItem<?>> getItems() {
return items;
}
/**
* @param items the items to set
*/
public void setItems(Map<String, SettingsItem<?>> items) {
this.items = items;
}
/**
* @return a {@code Map<String, Theme>} of all themes with their names as keys
* @since Envoy Client v0.2-alpha
*/
public Map<String, Theme> getThemes() {
return themes;
}
2019-12-22 21:48:19 +01:00
/**
* Sets the {@code Map<String, Theme>} of all themes with their names as keys
*
* @param themes the theme map to set
* @since Envoy Client v0.2-alpha
*/
public void setThemes(Map<String, Theme> themes) {
this.themes = themes;
}
/**
* @param themeName the name of the {@link Theme} to get
* @return the {@link Theme} with the specified name
* @since Envoy Client v0.3-alpha
*/
public Theme getTheme(String themeName) {
return themes.get(themeName);
}
}