Implemented theme serialization to a file
This commit is contained in:
parent
860d2b0dcf
commit
78be2e01a7
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,2 +1,3 @@
|
||||
/target/
|
||||
/localDB/
|
||||
/localDB/
|
||||
/themes.ser
|
||||
|
@ -1,12 +1,17 @@
|
||||
package envoy.client;
|
||||
|
||||
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.Map;
|
||||
import java.util.prefs.Preferences;
|
||||
|
||||
import envoy.client.ui.Theme;
|
||||
import envoy.schema.User;
|
||||
|
||||
/**
|
||||
* Project: <strong>envoy-client</strong><br>
|
||||
@ -15,19 +20,32 @@ import envoy.schema.User;
|
||||
*
|
||||
* @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 String username;
|
||||
private String email;
|
||||
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;
|
||||
private Preferences prefs = Preferences.userNodeForPackage(Settings.class);
|
||||
/**
|
||||
* 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();
|
||||
|
||||
/**
|
||||
* The way to instantiate the settings.
|
||||
@ -35,7 +53,7 @@ public class Settings {
|
||||
*
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
private Settings() {}
|
||||
private Settings() { load(); }
|
||||
|
||||
/**
|
||||
* 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
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public static Settings getInstance() {
|
||||
if (settings == null) {
|
||||
settings = new Settings();
|
||||
settings.load();
|
||||
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<>();
|
||||
e.printStackTrace();
|
||||
}
|
||||
return settings;
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
public void load() {
|
||||
settings.setUsername(prefs.get("username", ""));
|
||||
settings.setEmail(prefs.get("email", ""));
|
||||
settings.setEnterToSend(prefs.getBoolean("enterToSend", true));
|
||||
// currentTheme = "dark"; Activate once if NullPointerException on currentTheme
|
||||
// and change theme to dark or white in Settings
|
||||
settings.setCurrentTheme(prefs.get("theme", "dark"));
|
||||
}
|
||||
|
||||
public void save() {
|
||||
prefs.put("username", settings.getUsername());
|
||||
prefs.put("email", settings.getEmail());
|
||||
public void save() throws IOException{
|
||||
prefs.put("username", getUsername());
|
||||
prefs.put("email", getEmail());
|
||||
prefs.put("theme", currentTheme);
|
||||
System.out.println(currentTheme);
|
||||
prefs.putBoolean("enterToSend", settings.isEnterToSend());
|
||||
// TODO: override themes map
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
prefs.putBoolean("enterToSend", isEnterToSend());
|
||||
|
||||
// Save themes to theme file
|
||||
themeFile.createNewFile();
|
||||
try(ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(themeFile))) {
|
||||
out.writeObject(themes);
|
||||
}
|
||||
}
|
||||
|
||||
public void addNewThemeToMap(Theme theme) {
|
||||
@ -103,13 +111,13 @@ public class Settings {
|
||||
public void setCurrentTheme(String themeName) { currentTheme = themeName; }
|
||||
|
||||
/**
|
||||
* @return the username
|
||||
* @return the user name
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public String getUsername() { return username; }
|
||||
|
||||
/**
|
||||
* @param username the username to set
|
||||
* @param username the user name to set
|
||||
* @since Envoy v0.2-alpha
|
||||
*/
|
||||
public void setUsername(String username) { this.username = username; }
|
||||
@ -145,18 +153,4 @@ public class Settings {
|
||||
public Map<String, Theme> getThemes() { return 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; }
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -81,6 +81,7 @@ public class ChatWindow extends JFrame {
|
||||
public void windowClosing(WindowEvent e) {
|
||||
try {
|
||||
localDB.saveToLocalDB();
|
||||
Settings.getInstance().save();
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
System.err.println("Could not save localDB");
|
||||
|
@ -36,6 +36,7 @@ import envoy.client.Settings;
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @author Maximilian Käfer
|
||||
* @author Kai S. K. Engelbart
|
||||
*/
|
||||
public class SettingsScreen extends JDialog {
|
||||
|
||||
@ -62,10 +63,10 @@ public class SettingsScreen extends JDialog {
|
||||
private JButton cancelButton = new JButton("Cancel");
|
||||
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:
|
||||
// change (Picture,Username, Email, Password) and toggle(light/dark mode,
|
||||
@ -78,8 +79,6 @@ public class SettingsScreen extends JDialog {
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public static void open() {
|
||||
|
||||
// UIColors.getInstance(Settings.getInstance().isDarkMode());
|
||||
settingsScreen = new SettingsScreen();
|
||||
settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
||||
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
|
||||
*/
|
||||
@ -101,9 +100,9 @@ public class SettingsScreen extends JDialog {
|
||||
|
||||
createNewThemeButton.setEnabled(false);
|
||||
|
||||
temporaryTheme = new Theme("temporaryTheme",
|
||||
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
|
||||
// ContentPane------------------------------------------------------
|
||||
temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
|
||||
|
||||
// Content pane
|
||||
GridBagLayout gbl_contentPanel = new GridBagLayout();
|
||||
|
||||
gbl_contentPanel.columnWidths = new int[] { 1, 1 };
|
||||
@ -155,7 +154,7 @@ public class SettingsScreen extends JDialog {
|
||||
|
||||
contentPanel.add(options, gbc_optionsList);
|
||||
|
||||
// ThemeContent ---
|
||||
// Theme content
|
||||
|
||||
gbc_themeContent = new GridBagConstraints();
|
||||
gbc_themeContent.fill = GridBagConstraints.BOTH;
|
||||
@ -166,8 +165,6 @@ public class SettingsScreen extends JDialog {
|
||||
|
||||
GridBagLayout gbl_themeLayout = new GridBagLayout();
|
||||
|
||||
// themeContent.setSelectionForeground(theme.getUserNameColor());
|
||||
// themeContent.setSelectionBackground(theme.getSelectionColor());
|
||||
themeContent.setForeground(theme.getUserNameColor());
|
||||
themeContent.setBackground(theme.getCellColor());
|
||||
|
||||
@ -181,7 +178,6 @@ public class SettingsScreen extends JDialog {
|
||||
themes.setBackground(theme.getUserNameColor());
|
||||
themes.setForeground(theme.getBackgroundColor());
|
||||
themes.setSelectedItem(Settings.getInstance().getCurrentTheme());
|
||||
// themes.setBorder(null);
|
||||
|
||||
themes.addItemListener(new ItemListener() {
|
||||
|
||||
@ -191,7 +187,6 @@ public class SettingsScreen extends JDialog {
|
||||
System.out.println(selectedValue);
|
||||
selectedTheme = Settings.getInstance().getThemes().get(selectedValue);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
GridBagConstraints gbc_themes = new GridBagConstraints();
|
||||
@ -206,24 +201,9 @@ public class SettingsScreen extends JDialog {
|
||||
colorsPanel.setLayout((LayoutManager) new BoxLayout(colorsPanel, BoxLayout.Y_AXIS));
|
||||
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(),
|
||||
|
||||
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 JTextPane(),
|
||||
theme,
|
||||
@ -231,53 +211,17 @@ public class SettingsScreen extends JDialog {
|
||||
"Interactable Foreground",
|
||||
3);
|
||||
buildCustomizeElement(new JPanel(),
|
||||
|
||||
new JButton(),
|
||||
new JTextPane(),
|
||||
theme,
|
||||
theme.getInteractableBackgroundColor(),
|
||||
"Interactable Background",
|
||||
4);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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();
|
||||
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");
|
||||
System.out.println(s);
|
||||
Settings.getInstance()
|
||||
.addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(),
|
||||
temporaryTheme.getCellColor(), temporaryTheme.getInteractableForegroundColor(),
|
||||
temporaryTheme.getInteractableBackgroundColor(), temporaryTheme.getMessageColorChat(),
|
||||
temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(),
|
||||
.addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), temporaryTheme.getCellColor(),
|
||||
temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableBackgroundColor(),
|
||||
temporaryTheme.getMessageColorChat(), temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(),
|
||||
temporaryTheme.getTypingMessageColor(), temporaryTheme.getUserNameColor()));
|
||||
themeArray = Arrays.copyOf(themeArray, themeArray.length + 1);
|
||||
themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(s).getThemeName();
|
||||
|
||||
temporaryTheme = new Theme("temporaryTheme",
|
||||
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
|
||||
temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
|
||||
|
||||
createNewThemeButton.setEnabled(false);
|
||||
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);
|
||||
|
||||
temporaryTheme = new Theme("temporaryTheme",
|
||||
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(),
|
||||
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(),
|
||||
Settings.getInstance()
|
||||
.getThemes()
|
||||
.get(Settings.getInstance().getCurrentTheme())
|
||||
.getInteractableForegroundColor(),
|
||||
Settings.getInstance()
|
||||
.getThemes()
|
||||
.get(Settings.getInstance().getCurrentTheme())
|
||||
.getInteractableBackgroundColor(),
|
||||
Settings.getInstance().getThemes().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()).getDateColorChat(),
|
||||
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(),
|
||||
Settings.getInstance()
|
||||
.getThemes()
|
||||
.get(Settings.getInstance().getCurrentTheme())
|
||||
.getTypingMessageColor(),
|
||||
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getTypingMessageColor(),
|
||||
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor());
|
||||
|
||||
colorsPanel.removeAll();
|
||||
@ -526,10 +459,9 @@ public class SettingsScreen extends JDialog {
|
||||
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,
|
||||
String name, int yIndex) {
|
||||
private void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, String name, int yIndex) {
|
||||
textPane.setFont(new Font("Arial", Font.PLAIN, 14));
|
||||
textPane.setBackground(theme.getBackgroundColor());
|
||||
textPane.setForeground(theme.getUserNameColor());
|
||||
|
@ -21,6 +21,7 @@ import envoy.exception.EnvoyException;
|
||||
*
|
||||
* @author Leon Hofmeister
|
||||
* @author Maximilian Käfer
|
||||
* @author Kai S. K. Engelbart
|
||||
* @since Envoy v0.1-alpha
|
||||
*/
|
||||
public class Startup {
|
||||
@ -61,7 +62,7 @@ public class Startup {
|
||||
"Local DB error",
|
||||
JOptionPane.WARNING_MESSAGE);
|
||||
}
|
||||
Settings.getInstance().firstSave(client.getSender());
|
||||
Settings.getInstance().setUsername(userName);
|
||||
|
||||
EventQueue.invokeLater(() -> {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user