Implemented theme serialization to a file

This commit is contained in:
Kai S. K. Engelbart 2019-12-07 11:48:41 +01:00
parent e245a129af
commit 057c21061b
5 changed files with 93 additions and 164 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target/ /target/
/localDB/ /localDB/
/themes.ser

View File

@ -1,12 +1,17 @@
package envoy.client; package envoy.client;
import java.awt.Color; import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.prefs.Preferences; import java.util.prefs.Preferences;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
import envoy.schema.User;
/** /**
* Project: <strong>envoy-client</strong><br> * Project: <strong>envoy-client</strong><br>
@ -15,27 +20,40 @@ import envoy.schema.User;
* *
* @author Leon Hofmeister * @author Leon Hofmeister
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public class Settings { public class Settings {
// Actual settings accessible by the rest of the application
private String username; private String username;
private String email; private String email;
private boolean enterToSend = true; private boolean enterToSend = true;
private Map<String, Theme> themes = new HashMap<>(); private Map<String, Theme> themes;
private String currentTheme; private String currentTheme;
// private Image profilePic;
private static Settings settings; /**
* Required to save the settings.
*/
private Preferences prefs = Preferences.userNodeForPackage(Settings.class); 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();
/** /**
* The way to instantiate the settings. * The way to instantiate the settings.
* Is set to private to deny other instances of that object. * Is set to private to deny other instances of that object.
* *
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
private Settings() {} private Settings() { load(); }
/** /**
* This method is used to ensure that there is only one instance of Settings. * This method is used to ensure that there is only one instance of Settings.
@ -43,54 +61,44 @@ public class Settings {
* @return the instance of Settings * @return the instance of Settings
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha
*/ */
public static Settings getInstance() { public static Settings getInstance() { return settings; }
if (settings == null) {
settings = new Settings(); @SuppressWarnings("unchecked")
settings.load(); private void load() {
} setUsername(prefs.get("username", ""));
return settings; 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<>();
e.printStackTrace();
} }
public void load() { // Load standard themes not defined in the themes file
settings.setUsername(prefs.get("username", "")); themes.put("dark",
settings.setEmail(prefs.get("email", "")); new Theme("dark", Color.black, Color.darkGray, Color.white, Color.blue, Color.white, Color.orange, Color.blue, Color.white,
settings.setEnterToSend(prefs.getBoolean("enterToSend", true)); Color.white));
// currentTheme = "dark"; Activate once if NullPointerException on currentTheme themes.put("light",
// and change theme to dark or white in Settings new Theme("light", new Color(235, 235, 235), Color.white, Color.white, Color.darkGray, Color.black, Color.orange, Color.darkGray,
settings.setCurrentTheme(prefs.get("theme", "dark")); Color.black, Color.black));
} }
public void save() { public void save() throws IOException{
prefs.put("username", settings.getUsername()); prefs.put("username", getUsername());
prefs.put("email", settings.getEmail()); prefs.put("email", getEmail());
prefs.put("theme", currentTheme); prefs.put("theme", currentTheme);
System.out.println(currentTheme); prefs.putBoolean("enterToSend", isEnterToSend());
prefs.putBoolean("enterToSend", settings.isEnterToSend());
// TODO: override themes map
// Save themes to theme file
themeFile.createNewFile();
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) {
out.writeObject(themes);
} }
public void firstSave(User user) {
// TODO: load themes
settings.getThemes()
.put("dark",
new Theme("dark", Color.black, Color.darkGray, Color.white, Color.blue, Color.white, Color.orange,
Color.blue, Color.white, Color.white));
settings.getThemes()
.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));
prefs.put("username", user.getName());
// prefs.put("email", user.getEmail());
// prefs.putBoolean("darkMode", true);
// prefs.putBoolean("enterToSend", true);
} }
public void addNewThemeToMap(Theme theme) { public void addNewThemeToMap(Theme theme) {
@ -145,18 +153,4 @@ public class Settings {
public Map<String, Theme> getThemes() { return themes; } public Map<String, Theme> getThemes() { return themes; }
public void setThemes(Map<String, Theme> themes) { this.themes = themes; } public void setThemes(Map<String, Theme> themes) { this.themes = themes; }
// /**
// * @return the profilePic
// * @since Envoy v0.2-alpha
// */
// public Image getProfilePic() { return profilePic; }
//
// /**
// * @param profilePic the profilePic to set
// * @since Envoy v0.1-alpha
// */
// public void setProfilePic(Image profilePic) { this.profilePic = profilePic; }
} }

View File

@ -81,6 +81,7 @@ public class ChatWindow extends JFrame {
public void windowClosing(WindowEvent e) { public void windowClosing(WindowEvent e) {
try { try {
localDB.saveToLocalDB(); localDB.saveToLocalDB();
Settings.getInstance().save();
} catch (IOException e1) { } catch (IOException e1) {
e1.printStackTrace(); e1.printStackTrace();
System.err.println("Could not save localDB"); System.err.println("Could not save localDB");

View File

@ -36,6 +36,7 @@ import envoy.client.Settings;
* *
* @author Leon Hofmeister * @author Leon Hofmeister
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
*/ */
public class SettingsScreen extends JDialog { public class SettingsScreen extends JDialog {
@ -63,8 +64,8 @@ public class SettingsScreen extends JDialog {
private static int space = 5; private static int space = 5;
private boolean colorChanged = false; private boolean colorChanged = false;
private Theme temporaryTheme; private Theme temporaryTheme;
private static SettingsScreen settingsScreen; private static SettingsScreen settingsScreen;
// TODO: Add a JPanel with all the Information necessary: // TODO: Add a JPanel with all the Information necessary:
@ -78,8 +79,6 @@ public class SettingsScreen extends JDialog {
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public static void open() { public static void open() {
// UIColors.getInstance(Settings.getInstance().isDarkMode());
settingsScreen = new SettingsScreen(); settingsScreen = new SettingsScreen();
settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
settingsScreen.setModal(true); settingsScreen.setModal(true);
@ -87,7 +86,7 @@ public class SettingsScreen extends JDialog {
} }
/** /**
* Builds the settings screen.<br> * Builds the settings screen.
* *
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
@ -101,9 +100,9 @@ public class SettingsScreen extends JDialog {
createNewThemeButton.setEnabled(false); createNewThemeButton.setEnabled(false);
temporaryTheme = new Theme("temporaryTheme", temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
// ContentPane------------------------------------------------------ // Content pane
GridBagLayout gbl_contentPanel = new GridBagLayout(); GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[] { 1, 1 }; gbl_contentPanel.columnWidths = new int[] { 1, 1 };
@ -155,7 +154,7 @@ public class SettingsScreen extends JDialog {
contentPanel.add(options, gbc_optionsList); contentPanel.add(options, gbc_optionsList);
// ThemeContent --- // Theme content
gbc_themeContent = new GridBagConstraints(); gbc_themeContent = new GridBagConstraints();
gbc_themeContent.fill = GridBagConstraints.BOTH; gbc_themeContent.fill = GridBagConstraints.BOTH;
@ -166,8 +165,6 @@ public class SettingsScreen extends JDialog {
GridBagLayout gbl_themeLayout = new GridBagLayout(); GridBagLayout gbl_themeLayout = new GridBagLayout();
// themeContent.setSelectionForeground(theme.getUserNameColor());
// themeContent.setSelectionBackground(theme.getSelectionColor());
themeContent.setForeground(theme.getUserNameColor()); themeContent.setForeground(theme.getUserNameColor());
themeContent.setBackground(theme.getCellColor()); themeContent.setBackground(theme.getCellColor());
@ -181,7 +178,6 @@ public class SettingsScreen extends JDialog {
themes.setBackground(theme.getUserNameColor()); themes.setBackground(theme.getUserNameColor());
themes.setForeground(theme.getBackgroundColor()); themes.setForeground(theme.getBackgroundColor());
themes.setSelectedItem(Settings.getInstance().getCurrentTheme()); themes.setSelectedItem(Settings.getInstance().getCurrentTheme());
// themes.setBorder(null);
themes.addItemListener(new ItemListener() { themes.addItemListener(new ItemListener() {
@ -191,7 +187,6 @@ public class SettingsScreen extends JDialog {
System.out.println(selectedValue); System.out.println(selectedValue);
selectedTheme = Settings.getInstance().getThemes().get(selectedValue); selectedTheme = Settings.getInstance().getThemes().get(selectedValue);
} }
}); });
GridBagConstraints gbc_themes = new GridBagConstraints(); GridBagConstraints gbc_themes = new GridBagConstraints();
@ -206,24 +201,9 @@ public class SettingsScreen extends JDialog {
colorsPanel.setLayout((LayoutManager) new BoxLayout(colorsPanel, BoxLayout.Y_AXIS)); colorsPanel.setLayout((LayoutManager) new BoxLayout(colorsPanel, BoxLayout.Y_AXIS));
colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT); colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getBackgroundColor(), "Background", 1);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getCellColor(), "Cells", 2);
buildCustomizeElement(new JPanel(), buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getBackgroundColor(),
"Background",
1);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getCellColor(),
"Cells",
2);
buildCustomizeElement(new JPanel(),
new JButton(), new JButton(),
new JTextPane(), new JTextPane(),
theme, theme,
@ -231,53 +211,17 @@ public class SettingsScreen extends JDialog {
"Interactable Foreground", "Interactable Foreground",
3); 3);
buildCustomizeElement(new JPanel(), buildCustomizeElement(new JPanel(),
new JButton(), new JButton(),
new JTextPane(), new JTextPane(),
theme, theme,
theme.getInteractableBackgroundColor(), theme.getInteractableBackgroundColor(),
"Interactable Background", "Interactable Background",
4); 4);
buildCustomizeElement(new JPanel(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getMessageColorChat(), "Messages Chat", 5);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getDateColorChat(), "Date Chat", 6);
new JButton(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getSelectionColor(), "Selection", 7);
new JTextPane(), buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getTypingMessageColor(), "Typing Message", 8);
theme, buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getUserNameColor(), "User Names", 9);
theme.getMessageColorChat(),
"Messages Chat",
5);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getDateColorChat(),
"Date Chat",
6);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getSelectionColor(),
"Selection",
7);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getTypingMessageColor(),
"Typing Message",
8);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getUserNameColor(),
"User Names",
9);
GridBagConstraints gbc_colorsPanel = new GridBagConstraints(); GridBagConstraints gbc_colorsPanel = new GridBagConstraints();
gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL; gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL;
@ -298,16 +242,14 @@ public class SettingsScreen extends JDialog {
String s = JOptionPane.showInputDialog("Enter a name for the new theme"); String s = JOptionPane.showInputDialog("Enter a name for the new theme");
System.out.println(s); System.out.println(s);
Settings.getInstance() Settings.getInstance()
.addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), .addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), temporaryTheme.getCellColor(),
temporaryTheme.getCellColor(), temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableBackgroundColor(),
temporaryTheme.getInteractableBackgroundColor(), temporaryTheme.getMessageColorChat(), temporaryTheme.getMessageColorChat(), temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(),
temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(),
temporaryTheme.getTypingMessageColor(), temporaryTheme.getUserNameColor())); temporaryTheme.getTypingMessageColor(), temporaryTheme.getUserNameColor()));
themeArray = Arrays.copyOf(themeArray, themeArray.length + 1); themeArray = Arrays.copyOf(themeArray, themeArray.length + 1);
themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(s).getThemeName(); themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(s).getThemeName();
temporaryTheme = new Theme("temporaryTheme", temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
createNewThemeButton.setEnabled(false); createNewThemeButton.setEnabled(false);
themes.addItem(themeArray[themeArray.length - 1]); themes.addItem(themeArray[themeArray.length - 1]);
@ -421,27 +363,18 @@ public class SettingsScreen extends JDialog {
} }
public void updateColorVariables(String key) { private void updateColorVariables(String key) {
Theme theme = Settings.getInstance().getThemes().get(key); Theme theme = Settings.getInstance().getThemes().get(key);
temporaryTheme = new Theme("temporaryTheme", temporaryTheme = new Theme("temporaryTheme",
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(),
Settings.getInstance() Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableForegroundColor(),
.getThemes() Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor(),
.get(Settings.getInstance().getCurrentTheme())
.getInteractableForegroundColor(),
Settings.getInstance()
.getThemes()
.get(Settings.getInstance().getCurrentTheme())
.getInteractableBackgroundColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(), Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(),
Settings.getInstance() Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getTypingMessageColor(),
.getThemes()
.get(Settings.getInstance().getCurrentTheme())
.getTypingMessageColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor()); Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor());
colorsPanel.removeAll(); colorsPanel.removeAll();
@ -526,10 +459,9 @@ public class SettingsScreen extends JDialog {
colorsPanel.add(createNewThemeButton, gbc_createNewTheme); colorsPanel.add(createNewThemeButton, gbc_createNewTheme);
} }
public void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); } private void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); }
public void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, private void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, String name, int yIndex) {
String name, int yIndex) {
textPane.setFont(new Font("Arial", Font.PLAIN, 14)); textPane.setFont(new Font("Arial", Font.PLAIN, 14));
textPane.setBackground(theme.getBackgroundColor()); textPane.setBackground(theme.getBackgroundColor());
textPane.setForeground(theme.getUserNameColor()); textPane.setForeground(theme.getUserNameColor());

View File

@ -21,6 +21,7 @@ import envoy.exception.EnvoyException;
* *
* @author Leon Hofmeister * @author Leon Hofmeister
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
* @since Envoy v0.1-alpha * @since Envoy v0.1-alpha
*/ */
public class Startup { public class Startup {
@ -61,7 +62,7 @@ public class Startup {
"Local DB error", "Local DB error",
JOptionPane.WARNING_MESSAGE); JOptionPane.WARNING_MESSAGE);
} }
Settings.getInstance().firstSave(client.getSender()); Settings.getInstance().setUsername(userName);
EventQueue.invokeLater(() -> { EventQueue.invokeLater(() -> {
try { try {