2019-11-22 06:37:59 +01:00
|
|
|
package envoy.client;
|
|
|
|
|
2019-12-07 10:58:03 +01:00
|
|
|
import java.awt.Color;
|
2019-12-07 11:48:41 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.ObjectInputStream;
|
|
|
|
import java.io.ObjectOutputStream;
|
2019-12-07 10:58:03 +01:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
2019-11-22 06:37:59 +01:00
|
|
|
import java.util.prefs.Preferences;
|
|
|
|
|
2019-12-07 10:58:03 +01:00
|
|
|
import envoy.client.ui.Theme;
|
2019-11-23 13:25:12 +01:00
|
|
|
|
2019-11-22 06:37:59 +01:00
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-client</strong><br>
|
|
|
|
* File: <strong>Settings.java</strong><br>
|
|
|
|
* Created: <strong>11 Nov 2019</strong><br>
|
|
|
|
*
|
|
|
|
* @author Leon Hofmeister
|
2019-12-07 10:58:03 +01:00
|
|
|
* @author Maximilian Käfer
|
2019-12-07 11:48:41 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
2019-11-22 06:37:59 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
|
|
|
public class Settings {
|
|
|
|
|
2019-12-07 11:48:41 +01:00
|
|
|
// Actual settings accessible by the rest of the application
|
2019-12-07 10:58:03 +01:00
|
|
|
private String username;
|
|
|
|
private String email;
|
|
|
|
private boolean enterToSend = true;
|
2019-12-07 11:48:41 +01:00
|
|
|
private Map<String, Theme> themes;
|
|
|
|
private String currentTheme;
|
2019-12-07 10:58:03 +01:00
|
|
|
|
2019-12-07 11:48:41 +01:00
|
|
|
/**
|
|
|
|
* 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("themes.ser");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Singleton instance of this class.
|
|
|
|
*/
|
|
|
|
private static Settings settings = new Settings();
|
2019-11-22 06:37:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The way to instantiate the settings.
|
|
|
|
* Is set to private to deny other instances of that object.
|
|
|
|
*
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 11:48:41 +01:00
|
|
|
private Settings() { load(); }
|
2019-11-22 06:37:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to ensure that there is only one instance of Settings.
|
|
|
|
*
|
|
|
|
* @return the instance of Settings
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 11:48:41 +01:00
|
|
|
public static Settings getInstance() { return settings; }
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
private void load() {
|
|
|
|
setUsername(prefs.get("username", ""));
|
|
|
|
setEmail(prefs.get("email", ""));
|
|
|
|
setEnterToSend(prefs.getBoolean("enterToSend", true));
|
|
|
|
setCurrentTheme(prefs.get("theme", "dark"));
|
|
|
|
|
|
|
|
// 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<>();
|
2019-12-07 13:31:08 +01:00
|
|
|
currentTheme = "dark";
|
2019-12-07 11:48:41 +01:00
|
|
|
e.printStackTrace();
|
2019-11-22 06:37:59 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:48:41 +01:00
|
|
|
// Load standard themes not defined in the themes file
|
|
|
|
themes.put("dark",
|
|
|
|
new Theme("dark", Color.black, Color.darkGray, Color.white, Color.blue, 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));
|
2019-11-22 06:37:59 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
|
|
|
* updates prefs when save button is clicked
|
|
|
|
*
|
|
|
|
* @throws IOException
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 11:48:41 +01:00
|
|
|
public void save() throws IOException{
|
|
|
|
prefs.put("username", getUsername());
|
|
|
|
prefs.put("email", getEmail());
|
2019-12-07 10:58:03 +01:00
|
|
|
prefs.put("theme", currentTheme);
|
2019-12-07 11:48:41 +01:00
|
|
|
prefs.putBoolean("enterToSend", isEnterToSend());
|
|
|
|
|
|
|
|
// Save themes to theme file
|
|
|
|
themeFile.createNewFile();
|
|
|
|
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) {
|
|
|
|
out.writeObject(themes);
|
|
|
|
}
|
2019-12-07 10:58:03 +01:00
|
|
|
}
|
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
|
|
|
* adds new theme to the theme map and sets current theme to the new theme.
|
|
|
|
*
|
|
|
|
* @param theme
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 10:58:03 +01:00
|
|
|
public void addNewThemeToMap(Theme theme) {
|
|
|
|
settings.getThemes().put(theme.getThemeName(), theme);
|
|
|
|
currentTheme = theme.getThemeName();
|
2019-11-23 13:25:12 +01:00
|
|
|
}
|
2019-11-22 06:37:59 +01:00
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
|
|
|
* @return {@link currentTheme}
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 10:58:03 +01:00
|
|
|
public String getCurrentTheme() { return currentTheme; }
|
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
|
|
|
* Sets the currentTheme
|
|
|
|
*
|
|
|
|
* @param themeName
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 10:58:03 +01:00
|
|
|
public void setCurrentTheme(String themeName) { currentTheme = themeName; }
|
|
|
|
|
2019-11-22 06:37:59 +01:00
|
|
|
/**
|
2019-12-07 11:48:41 +01:00
|
|
|
* @return the user name
|
2019-11-22 06:37:59 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
|
|
|
public String getUsername() { return username; }
|
|
|
|
|
|
|
|
/**
|
2019-12-07 11:48:41 +01:00
|
|
|
* @param username the user name to set
|
2019-11-22 06:37:59 +01:00
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
|
|
|
public void setUsername(String username) { this.username = username; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the email associated with that user.
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
|
|
|
public String getEmail() { return email; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param email the email to set
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
|
|
|
public void setEmail(String email) { this.email = email; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return true, if "enter" suffices to send a message, else it has to be "ctrl"
|
|
|
|
* + "enter"
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
|
|
|
public boolean isEnterToSend() { return enterToSend; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change mode of posting a message via Keystroke.
|
|
|
|
*
|
|
|
|
* @param enterToSend if true, "enter" suffices to send a message, <br>
|
|
|
|
* else it has to be "ctrl" + "enter"
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
|
|
|
public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; }
|
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
|
|
|
* @return {@link themes} map
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 10:58:03 +01:00
|
|
|
public Map<String, Theme> getThemes() { return themes; }
|
2019-11-22 06:37:59 +01:00
|
|
|
|
2019-12-07 17:58:59 +01:00
|
|
|
/**
|
|
|
|
* Sets {@link themes}
|
|
|
|
*
|
|
|
|
* @param themes
|
|
|
|
* @since Envoy v0.2-alpha
|
|
|
|
*/
|
2019-12-07 10:58:03 +01:00
|
|
|
public void setThemes(Map<String, Theme> themes) { this.themes = themes; }
|
2019-12-07 11:48:41 +01:00
|
|
|
}
|