From a6c4d42c98789eff361763dc41591e5d5521d70c Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Mon, 23 Dec 2019 16:33:13 +0100 Subject: [PATCH] Polished settings mechanism * Set initial value of onCloseMode setting to true * Added setting change handlers * Applying settings changes immediately * Made PrimaryToggleSwitch round --- src/main/java/envoy/client/Settings.java | 2 +- src/main/java/envoy/client/SettingsItem.java | 18 ++++++++++--- .../envoy/client/event/EnterToSendEvent.java | 27 ------------------- .../client/event/OnCloseChangeEvent.java | 27 ------------------- .../envoy/client/ui/PrimaryToggleSwitch.java | 11 ++++---- src/main/java/envoy/client/ui/Startup.java | 3 +-- ...General.java => GeneralSettingsPanel.java} | 27 +++++++++++-------- .../client/ui/settings/SettingsScreen.java | 2 +- 8 files changed, 39 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/envoy/client/event/EnterToSendEvent.java delete mode 100644 src/main/java/envoy/client/event/OnCloseChangeEvent.java rename src/main/java/envoy/client/ui/settings/{General.java => GeneralSettingsPanel.java} (67%) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 9732248..199653d 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -102,7 +102,7 @@ public class Settings { private void supplementDefaults() { items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key.")); - items.putIfAbsent("onCloseMode", new SettingsItem<>(false, "Hide on close", "Hides the chat window when it is closed.")); + items.putIfAbsent("onCloseMode", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed.")); items.putIfAbsent("currentTheme", new SettingsItem<>("dark", null)); } diff --git a/src/main/java/envoy/client/SettingsItem.java b/src/main/java/envoy/client/SettingsItem.java index 23da76a..4c26201 100644 --- a/src/main/java/envoy/client/SettingsItem.java +++ b/src/main/java/envoy/client/SettingsItem.java @@ -3,6 +3,7 @@ package envoy.client; import java.io.Serializable; import java.util.HashMap; import java.util.Map; +import java.util.function.Consumer; import javax.swing.JComponent; @@ -21,12 +22,14 @@ public class SettingsItem implements Serializable { private Class componentClass; private String userFriendlyName, description; + transient private Consumer changeHandler; + private static final Map, Class> componentClasses = new HashMap<>(); - + private static final long serialVersionUID = 2146837835556852218L; static { - componentClasses.put(boolean.class, PrimaryToggleSwitch.class); + componentClasses.put(Boolean.class, PrimaryToggleSwitch.class); } public SettingsItem(T value, String userFriendlyName, String description) { @@ -53,7 +56,11 @@ public class SettingsItem implements Serializable { /** * @param value the value to set */ - public void set(T value) { this.value = value; } + public void set(T value) { + if(changeHandler != null && value != this.value) + changeHandler.accept(value); + this.value = value; + } /** * @return the componentClass @@ -84,4 +91,9 @@ public class SettingsItem implements Serializable { * @param description the description to set */ public void setDescription(String description) { this.description = description; } + + /** + * @param changeHandler the changeHandler to set + */ + public void setChangeHandler(Consumer changeHandler) { this.changeHandler = changeHandler; changeHandler.accept(value); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/event/EnterToSendEvent.java b/src/main/java/envoy/client/event/EnterToSendEvent.java deleted file mode 100644 index 7b26e0a..0000000 --- a/src/main/java/envoy/client/event/EnterToSendEvent.java +++ /dev/null @@ -1,27 +0,0 @@ -package envoy.client.event; - -/** - * Encapsulates a change to the {@code enterToSend} setting.
- *
- * Project: envoy-client
- * File: EnterToSendEvent.java
- * Created: 22 Dec 2019
- * - * @author Maximilian Käfer - * @since Envoy v0.3-alpha - */ -public class EnterToSendEvent implements Event { - - private boolean mode; - - /** - * Initializes an {@link EnterToSendEvent}. - * - * @param mode the state of the {@code enterToSend} setting - * @since Envoy 0.3-alpha - */ - public EnterToSendEvent(boolean mode) { this.mode = mode; } - - @Override - public Boolean get() { return mode; } -} \ No newline at end of file diff --git a/src/main/java/envoy/client/event/OnCloseChangeEvent.java b/src/main/java/envoy/client/event/OnCloseChangeEvent.java deleted file mode 100644 index 2d86fb6..0000000 --- a/src/main/java/envoy/client/event/OnCloseChangeEvent.java +++ /dev/null @@ -1,27 +0,0 @@ -package envoy.client.event; - -/** - * Encapsulates a change to the {@code currentOnCloseMode} setting.
- *
- * Project: envoy-client
- * File: OnCloseChangeEvent.java
- * Created: 22 Dec 2019
- * - * @author Maximilian Käfer - * @since Envoy v0.3-alpha - */ -public class OnCloseChangeEvent implements Event { - - private boolean closeMode; - - /** - * Initializes an {@link OnCloseChangeEvent}. - * - * @param closeMode the state of the {@code currentOnCloseMode} setting - * @since Envoy 0.3-alpha - */ - public OnCloseChangeEvent(boolean closeMode) { this.closeMode = closeMode; } - - @Override - public Boolean get() { return closeMode; } -} \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java index 6ea198d..8e91ab5 100644 --- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java +++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java @@ -47,14 +47,13 @@ public class PrimaryToggleSwitch extends JButton { @Override public void paintComponent(Graphics g) { g.setColor(state ? Color.GREEN : Color.LIGHT_GRAY); - g.fillRect(0, 0, getWidth(), getHeight()); + g.fillRoundRect(0, 0, getWidth(), getHeight(), 25, 25); g.setColor(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor()); - if (state) { - g.fillRect(25, 0, 25, 25); - } else { - g.fillRect(0, 0, 25, 25); - } + if (state) + g.fillRoundRect(25, 0, 25, 25, 25, 25); + else + g.fillRoundRect(0, 0, 25, 25, 25, 25); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 5e5bc92..8e57a08 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -143,8 +143,7 @@ public class Startup { new StatusTrayIcon(chatWindow).show(); // If the tray icon is supported and corresponding settings is set, hide the chat window on close - if (Settings.getInstance().getCurrentOnCloseMode()) - chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + Settings.getInstance().getItems().get("onCloseMode").setChangeHandler((onCloseMode) -> chatWindow.setDefaultCloseOperation((boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE)); } catch (EnvoyException e) { logger.warning("The StatusTrayIcon is not supported on this platform!"); } diff --git a/src/main/java/envoy/client/ui/settings/General.java b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java similarity index 67% rename from src/main/java/envoy/client/ui/settings/General.java rename to src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java index 59a6345..60e5458 100644 --- a/src/main/java/envoy/client/ui/settings/General.java +++ b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java @@ -4,13 +4,14 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionListener; +import java.util.logging.Level; import java.util.logging.Logger; +import javax.swing.JComponent; import javax.swing.JTextPane; import envoy.client.Settings; import envoy.client.SettingsItem; -import envoy.client.ui.PrimaryToggleSwitch; import envoy.client.ui.Theme; import envoy.client.util.EnvoyLog; @@ -24,12 +25,13 @@ import envoy.client.util.EnvoyLog; * @author Maximilian Käfer * @since Envoy v0.3-alpha */ -public class General extends SettingsPanel { +public class GeneralSettingsPanel extends SettingsPanel { private Theme theme; - private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName()); - private static final long serialVersionUID = -7470848775130754239L; + private static final String[] items = { "onCloseMode", "enterToSend" }; + private static final Logger logger = EnvoyLog.getLogger(GeneralSettingsPanel.class.getSimpleName()); + private static final long serialVersionUID = -7470848775130754239L; /** * This is the constructor for the General class. Here the user can set general @@ -37,7 +39,7 @@ public class General extends SettingsPanel { * * @since Envoy 0.3-alpha */ - public General() { + public GeneralSettingsPanel() { theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); setBackground(theme.getCellColor()); @@ -50,21 +52,24 @@ public class General extends SettingsPanel { setLayout(gbl_general); - createSettingElement(0, (SettingsItem) Settings.getInstance().getItems().get("onCloseMode")); - - createSettingElement(1, (SettingsItem) Settings.getInstance().getItems().get("enterToSend")); + for (int i = 0; i < items.length; i++) + try { + createSettingElement(i, Settings.getInstance().getItems().get(items[i])); + } catch (SecurityException | ReflectiveOperationException e) { + logger.log(Level.WARNING, "Could not create settings item", e); + } } - private void createSettingElement(int gridy, SettingsItem settingsItem) { + private void createSettingElement(int gridy, SettingsItem settingsItem) throws SecurityException, ReflectiveOperationException { JTextPane descriptionText = new JTextPane(); - PrimaryToggleSwitch toggleSwitch = new PrimaryToggleSwitch(settingsItem); + JComponent settingComponent = settingsItem.getComponent(); GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); gbc_toggleSwitch.gridx = 1; gbc_toggleSwitch.gridy = gridy; - add(toggleSwitch, gbc_toggleSwitch); + add(settingComponent, gbc_toggleSwitch); descriptionText.setText(settingsItem.getDescription()); descriptionText.setBackground(theme.getBackgroundColor()); diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index b971625..b37bf9a 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -57,7 +57,7 @@ public class SettingsScreen extends JDialog { public SettingsScreen() { // Initialize settings pages Map> panels = new HashMap<>(); - panels.put("General", General.class); + panels.put("General", GeneralSettingsPanel.class); panels.put("Color Themes", ThemeCustomizationPanel.class); setBounds(10, 10, 450, 650);