From ddc6e27abb25f98a3876610ab31c1f247cac3046 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 23 Nov 2019 13:25:12 +0100 Subject: [PATCH] Added settings object and light theme Improvements: * settings are implemented via Preferences API * fixed "bug" that made partner name pane editable * light theme is added as new display method --- src/main/java/envoy/client/Settings.java | 11 +++++- src/main/java/envoy/client/ui/ChatWindow.java | 16 ++++---- .../java/envoy/client/ui/SettingsScreen.java | 39 ++++++++++--------- src/main/java/envoy/client/ui/Startup.java | 6 +-- src/main/java/envoy/client/ui/UIColors.java | 3 +- 5 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 7377876..76d8ce3 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -2,6 +2,8 @@ package envoy.client; import java.util.prefs.Preferences; +import envoy.schema.User; + /** * Project: envoy-client
* File: Settings.java
@@ -37,8 +39,8 @@ public class Settings { public static Settings getInstance() { if (settings == null) { settings = new Settings(); + settings.load(); } - settings.load(); return settings; } @@ -55,6 +57,13 @@ public class Settings { prefs.putBoolean("darkMode", settings.isDarkMode()); prefs.putBoolean("enterToSend", settings.isEnterToSend()); } + + public void firstSave(User user) { + prefs.put("username", user.getName()); +// prefs.put("email", user.getEmail()); +// prefs.putBoolean("darkMode", true); +// prefs.putBoolean("enterToSend", true); + } /** * @return the username diff --git a/src/main/java/envoy/client/ui/ChatWindow.java b/src/main/java/envoy/client/ui/ChatWindow.java index 7d5166d..38634ea 100644 --- a/src/main/java/envoy/client/ui/ChatWindow.java +++ b/src/main/java/envoy/client/ui/ChatWindow.java @@ -51,7 +51,6 @@ public class ChatWindow extends JFrame { // user specific objects private Client client; private LocalDB localDB; - private Settings settings; // used colors in Envoy private UIColors uiColors = UIColors.getInstance(true); // GUI components @@ -63,15 +62,14 @@ public class ChatWindow extends JFrame { private JScrollPane scrollPane = new JScrollPane(); private JTextPane textPane = new JTextPane(); // private JCheckBox jCbChangeMode; - private JButton postButton = new JButton(); - private JButton settingsButton = new JButton(); + private JButton postButton = new JButton("Post"); + private JButton settingsButton = new JButton("Settings"); private static int space = 4; - public ChatWindow(Client client, LocalDB localDB, Settings setting) { + public ChatWindow(Client client, LocalDB localDB) { this.client = client; this.localDB = localDB; - this.settings = setting; setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 600, 800); @@ -119,7 +117,6 @@ public class ChatWindow extends JFrame { gbc_scrollPane.gridy = 1; gbc_scrollPane.insets = new Insets(space, space, space, space); - contentPane.add(scrollPane, gbc_scrollPane); // Message enter field @@ -128,7 +125,7 @@ public class ChatWindow extends JFrame { @Override public void keyReleased(KeyEvent e) { - if (e.getKeyCode() == KeyEvent.VK_ENTER && ((settings.isEnterToSend() && e.getModifiersEx() == 0) + if (e.getKeyCode() == KeyEvent.VK_ENTER && ((Settings.getInstance().isEnterToSend() && e.getModifiersEx() == 0) || (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK))) { postMessage(messageList); @@ -179,6 +176,7 @@ public class ChatWindow extends JFrame { settingsButton.addActionListener((evt) -> { try { SettingsScreen.open(); + changeChatWindowColors(); } catch (Exception e) { System.err.println("An error occured while opening the settings screen: " + e); e.printStackTrace(); @@ -188,6 +186,7 @@ public class ChatWindow extends JFrame { // Partner name display textPane.setFont(new Font("Arial", Font.PLAIN, 20)); + textPane.setEditable(false); GridBagConstraints gbc_partnerName = new GridBagConstraints(); gbc_partnerName.fill = GridBagConstraints.HORIZONTAL; @@ -216,7 +215,6 @@ public class ChatWindow extends JFrame { readCurrentChat(); client.setRecipient(user); - textPane.setText(currentChat.getRecipient().getName()); messageList.setModel(currentChat.getModel()); @@ -251,7 +249,7 @@ public class ChatWindow extends JFrame { * @since Envoy v0.1-alpha */ public void changeChatWindowColors() { - uiColors.setDisplayMode(settings.isDarkMode()); + uiColors.setDisplayMode(Settings.getInstance().isDarkMode()); // contentPane contentPane.setBackground(uiColors.getBackgroundColor()); diff --git a/src/main/java/envoy/client/ui/SettingsScreen.java b/src/main/java/envoy/client/ui/SettingsScreen.java index 4198648..56730aa 100644 --- a/src/main/java/envoy/client/ui/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/SettingsScreen.java @@ -8,7 +8,6 @@ import java.awt.Insets; import javax.swing.JButton; import javax.swing.JDialog; -import javax.swing.JOptionPane;//TODO: temporary import javax.swing.JPanel; import javax.swing.border.EmptyBorder; @@ -30,8 +29,6 @@ public class SettingsScreen extends JDialog { private JPanel buttonPane = new JPanel(); private JButton okButton = new JButton("Save"); private JButton cancelButton = new JButton("Cancel"); - private static Settings settings; - private static UIColors uiColors; private static int space = 5; private static SettingsScreen settingsScreen; @@ -47,8 +44,7 @@ public class SettingsScreen extends JDialog { */ public static void open() { - settings = Settings.getInstance(); - uiColors.setDisplayMode(settings.isDarkMode()); + UIColors.getInstance(Settings.getInstance().isDarkMode()); settingsScreen = new SettingsScreen(); settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); settingsScreen.setModal(true); @@ -98,16 +94,23 @@ public class SettingsScreen extends JDialog { getRootPane().setDefaultButton(okButton); okButton.addActionListener((evt) -> { try { - settings.setUsername(settings.getUsername());// still temporary value - settings.setEmail(settings.getEmail());// still temporary value - settings.setDarkMode(false);// TODO temporary values while no UI is implemented - settings.setEnterToSend(settings.isEnterToSend());// still temporary value - settings.save(); + Settings.getInstance().setUsername(Settings.getInstance().getUsername());// still temporary + // value + Settings.getInstance().setEmail(Settings.getInstance().getEmail());// still temporary value + Settings.getInstance().setDarkMode(!Settings.getInstance().isDarkMode());// TODO temporary + // values while no + // UI is implemented + Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary + // value + Settings.getInstance().save(); + UIColors.getUIColors().setDisplayMode(Settings.getInstance().isDarkMode()); + changeSettingsScreenColors(); + revalidate(); + repaint(); } catch (Exception e) { System.err.println("Something went wrong when changing the setting"); settingsScreen.dispose(); } - JOptionPane.showConfirmDialog(settingsScreen, "Successfully changed settings"); }); } } @@ -116,17 +119,17 @@ public class SettingsScreen extends JDialog { private void changeSettingsScreenColors() { // whole JDialog - setBackground(uiColors.getBackgroundColor()); + setBackground(UIColors.getUIColors().getBackgroundColor()); // contentPanel - contentPanel.setBackground(uiColors.getBackgroundColor()); + contentPanel.setBackground(UIColors.getUIColors().getBackgroundColor()); // buttonPane - buttonPane.setBackground(uiColors.getBackgroundColor()); + buttonPane.setBackground(UIColors.getUIColors().getBackgroundColor()); // cancelButton - cancelButton.setBackground(uiColors.getSpecialUseColor()); - cancelButton.setForeground(uiColors.getTextColor()); + cancelButton.setBackground(UIColors.getUIColors().getSpecialUseColor()); + cancelButton.setForeground(UIColors.getUIColors().getTextColor()); // okButton - okButton.setBackground(uiColors.getSpecialUseColor()); - okButton.setForeground(uiColors.getTextColor()); + okButton.setBackground(UIColors.getUIColors().getSpecialUseColor()); + okButton.setForeground(UIColors.getUIColors().getTextColor()); } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index a456d6e..2781a8a 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -61,11 +61,11 @@ public class Startup { "Local DB error", JOptionPane.WARNING_MESSAGE); } - Settings settings = Settings.getInstance();//TODO delete line - + Settings.getInstance().firstSave(client.getSender()); + EventQueue.invokeLater(() -> { try { - ChatWindow frame = new ChatWindow(client, localDB, settings); + ChatWindow frame = new ChatWindow(client, localDB); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); diff --git a/src/main/java/envoy/client/ui/UIColors.java b/src/main/java/envoy/client/ui/UIColors.java index e810250..fdad862 100644 --- a/src/main/java/envoy/client/ui/UIColors.java +++ b/src/main/java/envoy/client/ui/UIColors.java @@ -115,10 +115,9 @@ public class UIColors { */ public void setTextColor(Color textColor) { this.textColor = textColor; } - @Deprecated /** * @return the uiColors object * @since Envoy v0.2-alpha */ - public static UIColors getEnvoyColors() { return uIColors; } + public static UIColors getUIColors() { return uIColors; } } \ No newline at end of file