From e245a129af5df208865b8990f41d4f426cdb66e1 Mon Sep 17 00:00:00 2001
From: DieGurke <55625494+DieGurke@users.noreply.github.com>
Date: Sat, 7 Dec 2019 10:58:03 +0100
Subject: [PATCH] Themes & Themes Configuration
Added themes to choose from and provides customization setting for
these.
---
src/main/java/envoy/client/Settings.java | 76 ++-
src/main/java/envoy/client/ui/ChatWindow.java | 91 ++--
.../envoy/client/ui/MessageListRenderer.java | 28 +-
.../java/envoy/client/ui/SettingsScreen.java | 500 ++++++++++++++++--
src/main/java/envoy/client/ui/Theme.java | 121 +++++
src/main/java/envoy/client/ui/UIColors.java | 123 -----
.../envoy/client/ui/UserListRenderer.java | 29 +-
7 files changed, 736 insertions(+), 232 deletions(-)
create mode 100644 src/main/java/envoy/client/ui/Theme.java
delete mode 100644 src/main/java/envoy/client/ui/UIColors.java
diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java
index 76d8ce3..fa6088f 100644
--- a/src/main/java/envoy/client/Settings.java
+++ b/src/main/java/envoy/client/Settings.java
@@ -1,7 +1,11 @@
package envoy.client;
+import java.awt.Color;
+import java.util.HashMap;
+import java.util.Map;
import java.util.prefs.Preferences;
+import envoy.client.ui.Theme;
import envoy.schema.User;
/**
@@ -10,14 +14,17 @@ import envoy.schema.User;
* Created: 11 Nov 2019
*
* @author Leon Hofmeister
+ * @author Maximilian Käfer
* @since Envoy v0.2-alpha
*/
public class Settings {
- private String username;
- private String email;
- private boolean enterToSend = true;
- private boolean darkMode = true;
+ private String username;
+ private String email;
+ private boolean enterToSend = true;
+ private Map themes = new HashMap<>();
+
+ private String currentTheme;
// private Image profilePic;
private static Settings settings;
private Preferences prefs = Preferences.userNodeForPackage(Settings.class);
@@ -47,24 +54,54 @@ public class Settings {
public void load() {
settings.setUsername(prefs.get("username", ""));
settings.setEmail(prefs.get("email", ""));
- settings.setDarkMode(prefs.getBoolean("darkMode", true));
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());
- prefs.putBoolean("darkMode", settings.isDarkMode());
+ 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.put("email", user.getEmail());
+ // prefs.putBoolean("darkMode", true);
+ // prefs.putBoolean("enterToSend", true);
}
+ public void addNewThemeToMap(Theme theme) {
+ settings.getThemes().put(theme.getThemeName(), theme);
+ currentTheme = theme.getThemeName();
+ }
+
+ public String getCurrentTheme() { return currentTheme; }
+
+ public void setCurrentTheme(String themeName) { currentTheme = themeName; }
+
/**
* @return the username
* @since Envoy v0.2-alpha
@@ -105,23 +142,9 @@ public class Settings {
*/
public void setEnterToSend(boolean enterToSend) { this.enterToSend = enterToSend; }
- /**
- * Describes whether the Envoy GUI should be displayed in dark mode or not.
- *
- * @return true, if dark mode display is currently set, else the light theme
- * will be displayed
- * @since Envoy v0.2-alpha
- */
- public boolean isDarkMode() { return darkMode; }
+ public Map getThemes() { return themes; }
- /**
- * Change display mode of Envoy GUI.
- *
- * @param darkMode true, if dark mode display is currently set,
- * else the light theme will be displayed
- * @since Envoy v0.2-alpha
- */
- public void setDarkMode(boolean darkMode) { this.darkMode = darkMode; }
+ public void setThemes(Map themes) { this.themes = themes; }
// /**
// * @return the profilePic
@@ -135,4 +158,5 @@ public class Settings {
// */
// public void setProfilePic(Image profilePic) { this.profilePic = profilePic; }
+
}
diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java
index 38634ea..e0ea09b 100644
--- a/src/main/java/envoy/client/ui/ChatWindow.java
+++ b/src/main/java/envoy/client/ui/ChatWindow.java
@@ -49,10 +49,8 @@ public class ChatWindow extends JFrame {
private static final long serialVersionUID = 6865098428255463649L;
// user specific objects
- private Client client;
- private LocalDB localDB;
- // used colors in Envoy
- private UIColors uiColors = UIColors.getInstance(true);
+ private Client client;
+ private LocalDB localDB;
// GUI components
private JPanel contentPane = new JPanel();
private JTextArea messageEnterTextArea = new JTextArea();
@@ -62,8 +60,8 @@ public class ChatWindow extends JFrame {
private JScrollPane scrollPane = new JScrollPane();
private JTextPane textPane = new JTextPane();
// private JCheckBox jCbChangeMode;
- private JButton postButton = new JButton("Post");
- private JButton settingsButton = new JButton("Settings");
+ private JButton postButton = new JButton("Post");
+ private JButton settingsButton = new JButton("Settings");
private static int space = 4;
@@ -80,12 +78,14 @@ public class ChatWindow extends JFrame {
addWindowListener(new WindowAdapter() {
@Override
- public void windowClosing(WindowEvent e) { try {
- localDB.saveToLocalDB();
- } catch (IOException e1) {
- e1.printStackTrace();
- System.err.println("Could nnot save localDB");
- } }
+ public void windowClosing(WindowEvent e) {
+ try {
+ localDB.saveToLocalDB();
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ System.err.println("Could not save localDB");
+ }
+ }
});
contentPane.setBorder(new EmptyBorder(space, space, space, space));
@@ -125,8 +125,9 @@ public class ChatWindow extends JFrame {
@Override
public void keyReleased(KeyEvent e) {
- if (e.getKeyCode() == KeyEvent.VK_ENTER && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0)
- || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
+ if (e.getKeyCode() == KeyEvent.VK_ENTER
+ && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0)
+ || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) {
postMessage(messageList);
}
@@ -176,7 +177,9 @@ public class ChatWindow extends JFrame {
settingsButton.addActionListener((evt) -> {
try {
SettingsScreen.open();
- changeChatWindowColors();
+
+ changeChatWindowColors(Settings.getInstance().getCurrentTheme());
+
} catch (Exception e) {
System.err.println("An error occured while opening the settings screen: " + e);
e.printStackTrace();
@@ -232,8 +235,8 @@ public class ChatWindow extends JFrame {
gbc_userList.anchor = GridBagConstraints.PAGE_START;
gbc_userList.insets = new Insets(space, space, space, space);
- changeChatWindowColors();
-
+ changeChatWindowColors(Settings.getInstance().getCurrentTheme());
+
contentPane.add(userList, gbc_userList);
contentPane.revalidate();
@@ -243,43 +246,44 @@ public class ChatWindow extends JFrame {
contentPane.revalidate();
}
+
/**
* Used to immediately reload the ChatWindow when settings were changed.
*
* @since Envoy v0.1-alpha
*/
- public void changeChatWindowColors() {
- uiColors.setDisplayMode(Settings.getInstance().isDarkMode());
+ public void changeChatWindowColors(String key) {
+ Theme theme = Settings.getInstance().getThemes().get(key);
// contentPane
- contentPane.setBackground(uiColors.getBackgroundColor());
- contentPane.setForeground(uiColors.getTextColor());
+ contentPane.setBackground(theme.getBackgroundColor());
+ contentPane.setForeground(theme.getUserNameColor());
// messageList
- messageList.setSelectionForeground(uiColors.getTextColor());
- messageList.setSelectionBackground(uiColors.getSpecialUseColor());
- messageList.setForeground(uiColors.getTextColor());
- messageList.setBackground(uiColors.getUserInteractionColor());
+ messageList.setSelectionForeground(theme.getUserNameColor());
+ messageList.setSelectionBackground(theme.getSelectionColor());
+ messageList.setForeground(theme.getMessageColorChat());
+ messageList.setBackground(theme.getCellColor());
// scrollPane
- scrollPane.setForeground(uiColors.getBackgroundColor());
- scrollPane.setBackground(uiColors.getUserInteractionColor());
+ scrollPane.setForeground(theme.getBackgroundColor());
+ scrollPane.setBackground(theme.getCellColor());
// messageEnterTextArea
- messageEnterTextArea.setCaretColor(uiColors.getTextColor());
- messageEnterTextArea.setForeground(uiColors.getTextColor());
- messageEnterTextArea.setBackground(uiColors.getUserInteractionColor());
+ messageEnterTextArea.setCaretColor(theme.getTypingMessageColor());
+ messageEnterTextArea.setForeground(theme.getTypingMessageColor());
+ messageEnterTextArea.setBackground(theme.getCellColor());
// postButton
- postButton.setForeground(uiColors.getTextColor());
- postButton.setBackground(uiColors.getSpecialUseColor());
+ postButton.setForeground(theme.getInteractableForegroundColor());
+ postButton.setBackground(theme.getInteractableBackgroundColor());
// settingsButton
- settingsButton.setForeground(uiColors.getTextColor());
- settingsButton.setBackground(uiColors.getSpecialUseColor());
+ settingsButton.setForeground(theme.getInteractableForegroundColor());
+ settingsButton.setBackground(theme.getInteractableBackgroundColor());
// textPane
- textPane.setBackground(uiColors.getBackgroundColor());
- textPane.setForeground(uiColors.getTextColor());
+ textPane.setBackground(theme.getBackgroundColor());
+ textPane.setForeground(theme.getUserNameColor());
// userList
- userList.setSelectionForeground(uiColors.getTextColor());
- userList.setSelectionBackground(uiColors.getSpecialUseColor());
- userList.setForeground(uiColors.getTextColor());
- userList.setBackground(uiColors.getUserInteractionColor());
+ userList.setSelectionForeground(theme.getUserNameColor());
+ userList.setSelectionBackground(theme.getSelectionColor());
+ userList.setForeground(theme.getUserNameColor());
+ userList.setBackground(theme.getCellColor());
}
@@ -293,9 +297,10 @@ public class ChatWindow extends JFrame {
if (!messageEnterTextArea.getText().isEmpty()) try {
- // Create and send message object
- final Message message = localDB.createMessage(messageEnterTextArea.getText(), currentChat.getRecipient().getID());
- localDB.addWaitingMessageToLocalDB(message, currentChat);
+ // Create and send message object
+ final Message message = localDB.createMessage(messageEnterTextArea.getText(),
+ currentChat.getRecipient().getID());
+ localDB.addWaitingMessageToLocalDB(message, currentChat);
messageList.setModel(currentChat.getModel());
// Clear text field
diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java
index 9053c6c..ec2aaab 100644
--- a/src/main/java/envoy/client/ui/MessageListRenderer.java
+++ b/src/main/java/envoy/client/ui/MessageListRenderer.java
@@ -1,5 +1,6 @@
package envoy.client.ui;
+import java.awt.Color;
import java.awt.Component;
import java.text.SimpleDateFormat;
@@ -7,6 +8,7 @@ import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
+import envoy.client.Settings;
import envoy.schema.Message;
/**
@@ -39,16 +41,38 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer%s
%s :%s