From 91880424d362f7e49836d73f7ce2bd94c1defb07 Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 23 Dec 2019 14:51:52 +0100 Subject: [PATCH] Adjusting PrimaryToggleSwitch implementation to SettingsItem class --- src/main/java/envoy/client/Settings.java | 10 ++++ .../envoy/client/ui/PrimaryToggleSwitch.java | 35 ++++---------- .../envoy/client/ui/settings/General.java | 48 +++++++------------ 3 files changed, 36 insertions(+), 57 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index e79596c..9732248 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -163,6 +163,16 @@ public class Settings { @SuppressWarnings("unchecked") public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem) items.get("onCloseMode")).set(currentOnCloseMode); } + /** + * @return the items + */ + public Map> getItems() { return items; } + + /** + * @param items the items to set + */ + public void setItems(Map> items) { this.items = items; } + /** * @return a {@code Map} of all themes with their names as keys * @since Envoy v0.2-alpha diff --git a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java index 15e8a83..58bc817 100644 --- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java +++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java @@ -1,16 +1,12 @@ package envoy.client.ui; import java.awt.*; -import java.lang.reflect.Constructor; -import java.util.logging.Logger; import javax.swing.JButton; import javax.swing.JPanel; import envoy.client.Settings; -import envoy.client.event.Event; -import envoy.client.event.EventBus; -import envoy.client.util.EnvoyLog; +import envoy.client.SettingsItem; /** * This Component can be used to toggle between two options. e.g. on and @@ -25,23 +21,19 @@ import envoy.client.util.EnvoyLog; */ public class PrimaryToggleSwitch extends JPanel { - private final JButton b = new JButton(""); + private final JButton b = new JButton(); private boolean currentState; - private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName()); private static final long serialVersionUID = -721155303106833184L; /** * This is the constructor for the PrimaryToggleSwitch. * - * @param initialState The state the toggleSwitch is standardly set to.
- * true: off
- * false: on - * @param eventClass the class of the event dispatched by this toggleSwitch + * @param settingsItem * @since Envoy v0.3-alpha */ - public PrimaryToggleSwitch(boolean initialState, Class> eventClass) { + public PrimaryToggleSwitch(SettingsItem settingsItem) { setEnabled(true); setVisible(true); @@ -63,20 +55,13 @@ public class PrimaryToggleSwitch extends JPanel { setLayout(gbl_toggleSwitch); - setState(initialState); + setState(settingsItem.get()); b.addActionListener((evt) -> { - try { - // Dispatch event - Constructor> constructor = eventClass.getConstructor(boolean.class); - EventBus.getInstance().dispatch(constructor.newInstance(currentState)); - - setState(!currentState); - revalidate(); - repaint(); - } catch (ReflectiveOperationException | SecurityException e) { - logger.warning("An error occured while changing the setting: " + e); - } + settingsItem.set(!currentState); + setState(!currentState); + revalidate(); + repaint(); }); repaint(); @@ -96,7 +81,7 @@ public class PrimaryToggleSwitch extends JPanel { * @param state {@code true} to enable the switch, {@code false} to disable it * @since Envoy 0.3-alpha */ - public void setState(boolean state) { + private void setState(boolean state) { GridBagConstraints gbc_toggleButton = new GridBagConstraints(); if (state) { diff --git a/src/main/java/envoy/client/ui/settings/General.java b/src/main/java/envoy/client/ui/settings/General.java index 235cee9..3d09993 100644 --- a/src/main/java/envoy/client/ui/settings/General.java +++ b/src/main/java/envoy/client/ui/settings/General.java @@ -11,7 +11,7 @@ import javax.swing.JOptionPane; import javax.swing.JTextPane; import envoy.client.Settings; -import envoy.client.event.*; +import envoy.client.SettingsItem; import envoy.client.ui.PrimaryToggleSwitch; import envoy.client.ui.Theme; import envoy.client.util.EnvoyLog; @@ -31,15 +31,7 @@ public class General extends SettingsPanel { private Theme theme; private boolean onCloseState = Settings.getInstance().getCurrentOnCloseMode(); private boolean enterToSend = Settings.getInstance().isEnterToSend(); - - private PrimaryToggleSwitch toggleSwitch; - private JTextPane onCloseModeTextPane = new JTextPane(); - private JTextPane onCloseModeStatePane = new JTextPane(); - - private PrimaryToggleSwitch toggleSwitchEnterToSend; - private JTextPane enterToSendTextPane = new JTextPane(); - private JTextPane enterToSendStatePane = new JTextPane(); - + private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName()); private static final long serialVersionUID = -7470848775130754239L; @@ -61,26 +53,16 @@ public class General extends SettingsPanel { gbl_general.rowWeights = new double[] { 0.02, 0.0005, 0.02, 0.0005, 1.0 }; setLayout(gbl_general); - + createSettingElement(0, - OnCloseChangeEvent.class, - Settings.getInstance().getCurrentOnCloseMode(), - toggleSwitch, - onCloseModeStatePane, - onCloseModeTextPane, - "Client runs in the background, when window is closed"); - EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get())); + (SettingsItem) Settings.getInstance().getItems().get("onCloseMode")); createSettingElement(2, - EnterToSendEvent.class, - Settings.getInstance().isEnterToSend(), - toggleSwitchEnterToSend, - enterToSendStatePane, - enterToSendTextPane, - "Press Enter to send messages"); - EventBus.getInstance().register(EnterToSendEvent.class, (evt) -> changeEnterToSend(((EnterToSendEvent) evt).get())); + (SettingsItem) Settings.getInstance().getItems().get("enterToSend")); } + // TODO: Move ON / OFF text to PrimaryToggleswitch + /** * This method changes the on close mode of the client. * @@ -91,7 +73,7 @@ public class General extends SettingsPanel { public void changeOnClose(boolean state) { this.onCloseState = state; - onCloseModeStatePane.setText(state ? "ON" : "OFF"); + //onCloseModeStatePane.setText(state ? "ON" : "OFF"); revalidate(); repaint(); } @@ -106,14 +88,16 @@ public class General extends SettingsPanel { public void changeEnterToSend(boolean state) { this.enterToSend = state; - enterToSendStatePane.setText(state ? "ON" : "OFF"); + //enterToSendStatePane.setText(state ? "ON" : "OFF"); revalidate(); repaint(); } - private void createSettingElement(int gridy, Class> eventClass, boolean state, PrimaryToggleSwitch toggleSwitch, - JTextPane stateText, JTextPane descriptionText, String text) { - toggleSwitch = new PrimaryToggleSwitch(state, eventClass); + private void createSettingElement(int gridy, SettingsItem settingsItem) { + JTextPane stateText = new JTextPane(); + JTextPane descriptionText = new JTextPane(); + + PrimaryToggleSwitch toggleSwitch = new PrimaryToggleSwitch(settingsItem); GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); gbc_toggleSwitch.gridx = 1; @@ -121,7 +105,7 @@ public class General extends SettingsPanel { add(toggleSwitch, gbc_toggleSwitch); - stateText.setText(state ? "ON" : "OFF"); + stateText.setText(settingsItem.get() ? "ON" : "OFF"); stateText.setBackground(theme.getCellColor()); stateText.setForeground(theme.getUserNameColor()); stateText.setEditable(false); @@ -133,7 +117,7 @@ public class General extends SettingsPanel { add(stateText, gbc_stateText); - descriptionText.setText(text); + descriptionText.setText(settingsItem.getDescription()); descriptionText.setBackground(theme.getBackgroundColor()); descriptionText.setForeground(theme.getBackgroundColor().invert()); descriptionText.setEditable(false);