From 5090e81b56aaf743f8d3bde133204165754964ee Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Mon, 23 Dec 2019 00:03:22 +0100 Subject: [PATCH] Enter to Send and Revising * Revised style and improved object architecture * Added Enter to Send mechanism. --- src/main/java/envoy/client/Settings.java | 10 +- .../envoy/client/event/EnterToSendEvent.java | 27 +++ .../client/event/OnCloseChangeEvent.java | 12 +- .../envoy/client/ui/PrimaryToggleSwitch.java | 8 +- src/main/java/envoy/client/ui/Startup.java | 2 +- .../envoy/client/ui/settings/General.java | 169 ++++++++++++------ 6 files changed, 154 insertions(+), 74 deletions(-) create mode 100644 src/main/java/envoy/client/event/EnterToSendEvent.java diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 42bd0f1..5e15743 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -28,7 +28,7 @@ public class Settings { private boolean enterToSend = true; private Map themes; private String currentTheme; - private int currentOnCloseMode; + private boolean currentOnCloseMode; /** * Required to save the settings. @@ -65,7 +65,7 @@ public class Settings { private void load() { setEnterToSend(prefs.getBoolean("enterToSend", true)); setCurrentTheme(prefs.get("theme", "dark")); - setCurrentOnCloseMode(prefs.getInt("onCloseMode", 1)); + setCurrentOnCloseMode(prefs.getBoolean("onCloseMode", true)); // Load themes from theme file try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) { @@ -96,7 +96,7 @@ public class Settings { public void save() throws IOException { prefs.put("theme", currentTheme); prefs.putBoolean("enterToSend", isEnterToSend()); - prefs.putInt("onCloseMode", currentOnCloseMode); + prefs.putBoolean("onCloseMode", currentOnCloseMode); // Save themes to theme file themeFile.createNewFile(); @@ -163,7 +163,7 @@ public class Settings { * @return the current on close mode. * @since Envoy v0.3-alpha */ - public int getCurrentOnCloseMode() { return currentOnCloseMode; } + public boolean getCurrentOnCloseMode() { return currentOnCloseMode; } /** * Sets the current on close mode. @@ -171,5 +171,5 @@ public class Settings { * @param currentOnCloseMode the on close mode that should be set. * @since Envoy v0.3-alpha */ - public void setCurrentOnCloseMode(int currentOnCloseMode) { this.currentOnCloseMode = currentOnCloseMode; } + public void setCurrentOnCloseMode(boolean currentOnCloseMode) { this.currentOnCloseMode = currentOnCloseMode; } } \ 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 new file mode 100644 index 0000000..f96d13e --- /dev/null +++ b/src/main/java/envoy/client/event/EnterToSendEvent.java @@ -0,0 +1,27 @@ +package envoy.client.event; + +/** + * 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; + + /** + * @param mode This is the enter to sent mode when sending messages. + *
+ * true = Enter to Send Messages
+ * false = Enter to do a line break + * @since Envoy 0.3-alpha + */ + public EnterToSendEvent(boolean mode) { this.mode = mode; } + + @Override + public Boolean get() { return mode; } + +} diff --git a/src/main/java/envoy/client/event/OnCloseChangeEvent.java b/src/main/java/envoy/client/event/OnCloseChangeEvent.java index 5cffd37..717a5da 100644 --- a/src/main/java/envoy/client/event/OnCloseChangeEvent.java +++ b/src/main/java/envoy/client/event/OnCloseChangeEvent.java @@ -8,21 +8,21 @@ package envoy.client.event; * @author Maximilian Käfer * @since Envoy v0.3-alpha */ -public class OnCloseChangeEvent implements Event { +public class OnCloseChangeEvent implements Event { - private Integer closeMode; + private boolean closeMode; /** * @param closeMode This is the on close mode for the client, that should be * set. *
- * 0 = ExitOnClose
- * 1 = HideOnClose + * true = ExitOnClose
+ * false = HideOnClose * @since Envoy 0.3-alpha */ - public OnCloseChangeEvent(int closeMode) { this.closeMode = closeMode; } + public OnCloseChangeEvent(boolean closeMode) { this.closeMode = closeMode; } @Override - public Integer get() { return closeMode; } + public Boolean get() { return closeMode; } } diff --git a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java index e2469cc..361db5d 100644 --- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java +++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java @@ -28,7 +28,7 @@ public class PrimaryToggleSwitch extends JPanel { private static final long serialVersionUID = -721155303106833184L; JButton b = new JButton(""); private boolean currentState; - private int variable; + private boolean variable; private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName()); /** @@ -69,7 +69,7 @@ public class PrimaryToggleSwitch extends JPanel { b.addActionListener((evt) -> { try { Class c = Class.forName(eventName); - Class[] types = { int.class }; + Class[] types = { boolean.class }; Constructor constructor = c.getConstructor(types); Object[] parameters = { variable }; @@ -113,7 +113,7 @@ public class PrimaryToggleSwitch extends JPanel { add(b, gbc_toggleButton); currentState = true; - variable = 1; + variable = true; } else { GridBagConstraints gbc_toggleButton = new GridBagConstraints(); gbc_toggleButton.anchor = GridBagConstraints.EAST; @@ -122,7 +122,7 @@ public class PrimaryToggleSwitch extends JPanel { add(b, gbc_toggleButton); currentState = false; - variable = 0; + variable = false; } } } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 547be9a..d2f7125 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -132,7 +132,7 @@ public class Startup { new StatusTrayIcon(chatWindow).show(); // If the tray icon is supported, hide the chat window on close - if (Settings.getInstance().getCurrentOnCloseMode() == 1) { + if (Settings.getInstance().getCurrentOnCloseMode() == true) { chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); } else { chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/main/java/envoy/client/ui/settings/General.java b/src/main/java/envoy/client/ui/settings/General.java index 017bb85..e922359 100644 --- a/src/main/java/envoy/client/ui/settings/General.java +++ b/src/main/java/envoy/client/ui/settings/General.java @@ -10,6 +10,7 @@ import javax.swing.JOptionPane; import javax.swing.JTextPane; import envoy.client.Settings; +import envoy.client.event.EnterToSendEvent; import envoy.client.event.EventBus; import envoy.client.event.OnCloseChangeEvent; import envoy.client.ui.PrimaryToggleSwitch; @@ -31,12 +32,17 @@ public class General extends SettingsPanel { private static final long serialVersionUID = -7470848775130754239L; private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName()); - - private int state; + private Theme theme; + private boolean onCloseState; + private boolean enterToSend; PrimaryToggleSwitch toggleSwitch; - JTextPane onCloseModeText = new JTextPane(); - JTextPane onCloseModeState = new JTextPane(); + JTextPane onCloseModeTextPane = new JTextPane(); + JTextPane onCloseModeStatePane = new JTextPane(); + + PrimaryToggleSwitch toggleSwitchEnterToSend; + JTextPane enterToSendTextPane = new JTextPane(); + JTextPane enterToSendStatePane = new JTextPane(); /** * This is the constructor for the General class. Here the user can set general @@ -45,62 +51,35 @@ public class General extends SettingsPanel { * @since Envoy 0.3-alpha */ public General() { - Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); - - state = Settings.getInstance().getCurrentOnCloseMode(); - if (state == 1) { - toggleSwitch = new PrimaryToggleSwitch(false, "envoy.client.event.OnCloseChangeEvent"); - } else { - toggleSwitch = new PrimaryToggleSwitch(true, "envoy.client.event.OnCloseChangeEvent"); - } + theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); setBackground(theme.getCellColor()); GridBagLayout gbl_general = new GridBagLayout(); gbl_general.columnWidths = new int[] { 1, 1 }; - gbl_general.rowHeights = new int[] { 1, 1, 1 }; + gbl_general.rowHeights = new int[] { 1, 1, 1, 1, 1 }; gbl_general.columnWeights = new double[] { 1.0, 0.1 }; - gbl_general.rowWeights = new double[] { 0.02, 0.0005, 1.0 }; + gbl_general.rowWeights = new double[] { 0.02, 0.0005, 0.02, 0.0005, 1.0 }; setLayout(gbl_general); - GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); - gbc_toggleSwitch.gridx = 1; - gbc_toggleSwitch.gridy = 0; - - add(toggleSwitch, gbc_toggleSwitch); - - if (state == 0) { - onCloseModeState.setText("OFF"); - } else { - onCloseModeState.setText("ON"); - } - - onCloseModeState.setBackground(theme.getCellColor()); - onCloseModeState.setForeground(theme.getUserNameColor()); - - GridBagConstraints gbc_onCloseModeState = new GridBagConstraints(); - gbc_onCloseModeState.anchor = GridBagConstraints.NORTH; - gbc_onCloseModeState.gridx = 1; - gbc_onCloseModeState.gridy = 1; - - add(onCloseModeState, gbc_onCloseModeState); - - onCloseModeText.setText("Client runs in the background, when window is closed"); - onCloseModeText.setBackground(theme.getBackgroundColor()); - // TODO: Change to inverted color. - onCloseModeText.setForeground(theme.getUserNameColor()); - - GridBagConstraints gbc_onCloseModeText = new GridBagConstraints(); - gbc_onCloseModeText.fill = GridBagConstraints.BOTH; - gbc_onCloseModeText.gridx = 0; - gbc_onCloseModeText.gridy = 0; - gbc_onCloseModeText.gridheight = 2; - gbc_onCloseModeText.insets = new Insets(5, 5, 5, 5); - - add(onCloseModeText, gbc_onCloseModeText); - + createSettingElement(0, + "envoy.client.event.OnCloseChangeEvent", + 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())); + + createSettingElement(2, + "envoy.client.event.EnterToSendEvent", + Settings.getInstance().isEnterToSend(), + toggleSwitchEnterToSend, + enterToSendStatePane, + enterToSendTextPane, + "Press Enter to send messages"); + EventBus.getInstance().register(EnterToSendEvent.class, (evt) -> changeEnterToSend(((EnterToSendEvent) evt).get())); } /** @@ -110,31 +89,105 @@ public class General extends SettingsPanel { * or off. * @since Envoy v0.3-alpha */ - public void changeOnClose(int state) { - System.out.println(state); - this.state = state; + public void changeOnClose(boolean state) { + this.onCloseState = state; - if (state == 0) { - onCloseModeState.setText("OFF"); + if (state == false) { + onCloseModeStatePane.setText("OFF"); } else { - onCloseModeState.setText("ON"); + onCloseModeStatePane.setText("ON"); } this.revalidate(); this.repaint(); } + /** + * This method changes the enter to send a message setting. + * + * @param state This is the integer that defines weather the toggleSwitch is on + * or off. + * @since Envoy v0.3-alpha + */ + public void changeEnterToSend(boolean state) { + this.enterToSend = state; + + if (state == false) { + enterToSendStatePane.setText("OFF"); + } else { + enterToSendStatePane.setText("ON"); + } + this.revalidate(); + this.repaint(); + } + + private void createSettingElement(int gridy, String eventPath, boolean state, PrimaryToggleSwitch toggleSwitch, JTextPane stateText, + JTextPane descriptionText, String text) { + if (state == true) { + toggleSwitch = new PrimaryToggleSwitch(false, eventPath); + } else { + toggleSwitch = new PrimaryToggleSwitch(true, eventPath); + } + + GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); + gbc_toggleSwitch.gridx = 1; + gbc_toggleSwitch.gridy = gridy; + + add(toggleSwitch, gbc_toggleSwitch); + + if (state == false) { + stateText.setText("OFF"); + } else { + stateText.setText("ON"); + } + + stateText.setBackground(theme.getCellColor()); + stateText.setForeground(theme.getUserNameColor()); + + GridBagConstraints gbc_stateText = new GridBagConstraints(); + gbc_stateText.anchor = GridBagConstraints.NORTH; + gbc_stateText.gridx = 1; + gbc_stateText.gridy = gridy + 1; + + add(stateText, gbc_stateText); + + descriptionText.setText(text); + descriptionText.setBackground(theme.getBackgroundColor()); + // TODO: Change to inverted color. + descriptionText.setForeground(theme.getUserNameColor()); + + GridBagConstraints gbc_descriptionText = new GridBagConstraints(); + gbc_descriptionText.fill = GridBagConstraints.BOTH; + gbc_descriptionText.gridx = 0; + gbc_descriptionText.gridy = gridy; + gbc_descriptionText.gridheight = 2; + gbc_descriptionText.insets = new Insets(5, 5, 5, 5); + + add(descriptionText, gbc_descriptionText); + } + @Override public ActionListener getOkButtonAction() { return (evt) -> { - if (state != Settings.getInstance().getCurrentOnCloseMode()) { + if (onCloseState != Settings.getInstance().getCurrentOnCloseMode()) { try { - Settings.getInstance().setCurrentOnCloseMode(state); + Settings.getInstance().setCurrentOnCloseMode(onCloseState); JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started."); } catch (Exception e) { logger.info("Close mode could not be changed! " + e); e.printStackTrace(); } } + + if (enterToSend != Settings.getInstance().isEnterToSend()) { + try { + Settings.getInstance().setEnterToSend(enterToSend); + ; + JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started."); + } catch (Exception e) { + logger.info("Enter to send mode could not be changed! " + e); + e.printStackTrace(); + } + } }; }