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/Settings.java

175 lines
5.2 KiB
Java
Raw Normal View History

package envoy.client;
import java.awt.Color;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;
import envoy.client.ui.Theme;
/**
* 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 v0.2-alpha
*/
public class Settings {
// Actual settings accessible by the rest of the application
private boolean enterToSend = true;
private Map<String, Theme> themes;
private String currentTheme;
private int currentOnCloseMode;
/**
* Required to save the settings.
*/
private Preferences prefs = Preferences.userNodeForPackage(Settings.class);
/**
* User-defined themes are stored inside this file.
*/
private 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(); }
/**
* 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; }
@SuppressWarnings("unchecked")
private void load() {
setEnterToSend(prefs.getBoolean("enterToSend", true));
setCurrentTheme(prefs.get("theme", "dark"));
setCurrentOnCloseMode(prefs.getInt("onCloseMode", 1));
// Load themes from theme file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) {
Object obj = in.readObject();
if (obj instanceof HashMap) themes = (Map<String, Theme>) obj;
} catch (IOException | ClassNotFoundException e) {
themes = new HashMap<>();
currentTheme = "dark";
e.printStackTrace();
}
// 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));
}
/**
* 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 {
prefs.put("theme", currentTheme);
prefs.putBoolean("enterToSend", isEnterToSend());
prefs.putInt("onCloseMode", currentOnCloseMode);
// Save themes to theme file
themeFile.createNewFile();
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) {
out.writeObject(themes);
}
}
/**
* 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 currentTheme; }
/**
* Sets the name of the current {@link Theme}.
*
* @param themeName the name to set
* @since Envoy v0.2-alpha
*/
public void setCurrentTheme(String themeName) { currentTheme = 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 enterToSend; }
/**
* 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
*/
public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; }
/**
* @return a {@code Map<String, Theme>} of all themes with their names as keys
* @since Envoy v0.2-alpha
*/
public Map<String, Theme> getThemes() { return themes; }
/**
* Sets the {@code Map<String, Theme>} of all themes with their names as keys
*
* @param themes the theme map to set
* @since Envoy v0.2-alpha
*/
public void setThemes(Map<String, Theme> themes) { this.themes = themes; }
/**
* @return the current on close mode.
* @since Envoy v0.3-alpha
*/
public int getCurrentOnCloseMode () {return currentOnCloseMode;}
/**
* Sets the current on close mode.
*
* @param currentOnCloseMode the on close mode that should be set.
* @since Envoy v0.3-alpha
*/
public void setCurrentOnCloseMode(int currentOnCloseMode) {this.currentOnCloseMode = currentOnCloseMode;}
}