From e201ec3da1c40fe9d35d3abf6dfd16b8a91ce70c Mon Sep 17 00:00:00 2001
From: DieGurke <55625494+DieGurke@users.noreply.github.com>
Date: Sun, 22 Dec 2019 18:07:30 +0100
Subject: [PATCH 1/6] PrimaryToggleSwitch
* Adds a component, that can be used to toggle between two options.
* Is built to be able to be used for any event, that is structured like
the OnCloseChangeEvent class.
---
.../client/event/OnCloseChangeEvent.java | 25 ++++
.../envoy/client/ui/PrimaryToggleSwitch.java | 118 ++++++++++++++++++
.../envoy/client/ui/settings/General.java | 70 +++++++++++
.../client/ui/settings/SettingsScreen.java | 1 +
4 files changed, 214 insertions(+)
create mode 100644 src/main/java/envoy/client/event/OnCloseChangeEvent.java
create mode 100644 src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
create mode 100644 src/main/java/envoy/client/ui/settings/General.java
diff --git a/src/main/java/envoy/client/event/OnCloseChangeEvent.java b/src/main/java/envoy/client/event/OnCloseChangeEvent.java
new file mode 100644
index 0000000..cefc375
--- /dev/null
+++ b/src/main/java/envoy/client/event/OnCloseChangeEvent.java
@@ -0,0 +1,25 @@
+package envoy.client.event;
+
+/**
+ * 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 Integer closeMode;
+
+ /**
+ * @param closeMode This is the on close mode for the client, that should be set.
+ * 0 = ExitOnClose 1 = HideOnClose
+ * @since Envoy 0.3-alpha
+ */
+ public OnCloseChangeEvent(int closeMode) {this.closeMode = closeMode;}
+
+ @Override
+ public Integer get() { return closeMode; }
+
+}
diff --git a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
new file mode 100644
index 0000000..aa36cca
--- /dev/null
+++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
@@ -0,0 +1,118 @@
+package envoy.client.ui;
+import java.awt.*;
+import java.lang.reflect.Constructor;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.swing.*;
+
+import envoy.client.Settings;
+import envoy.client.event.Event;
+import envoy.client.event.EventBus;
+import envoy.client.event.EventHandler;
+import envoy.client.event.OnCloseChangeEvent;
+import envoy.client.ui.settings.ThemeCustomizationPanel;
+import envoy.client.util.EnvoyLog;
+
+/**
+ * This Component can be used to toggle between two options. e.g. on and off
+ *
+ * Project: envoy-client
+ * File: PrimaryToggleSwitch.java
+ * Created: 21 Dec 2019
+ *
+ * @author Maximilian Käfer
+ * @since Envoy v0.2-alpha
+ */
+public class PrimaryToggleSwitch extends JPanel{
+
+ private static final long serialVersionUID = -721155303106833184L;
+ private boolean initialState;
+ JButton b = new JButton("");
+ private boolean currentState;
+ private int variable;
+ private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
+
+ /**
+ * @param initialState The state the toggleSwitch is standardly set to. true: off false: on
+ * @param eventName the path of the event class
+ * @since Envoy v0.3-alpha
+ */
+ @SuppressWarnings({ "rawtypes", "unused" })
+ public PrimaryToggleSwitch(boolean initialState, String eventName) {
+ super();
+ setEnabled(true);
+ setVisible(true);
+ this.initialState = initialState;
+ setPreferredSize(new Dimension(50, 25));
+ b.setPreferredSize(new Dimension(25, 25));
+ b.setBackground(Settings.getInstance().getThemes()
+ .get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor());
+
+ GridBagLayout gbl_toggleSwitch = new GridBagLayout();
+ gbl_toggleSwitch.columnWidths = new int[] { 1, 1 };
+ gbl_toggleSwitch.rowHeights = new int[] { 1};
+ gbl_toggleSwitch.columnWeights = new double[] { 1.0, 1.0 };
+ gbl_toggleSwitch.rowWeights = new double[] {1.0 };
+
+ setLayout(gbl_toggleSwitch);
+
+ setState(initialState);
+
+ b.addActionListener((evt) -> {
+ try {
+ Class> c = Class.forName(eventName);
+ Class[] types = {int.class};
+ Constructor constructor = c.getConstructor(types);
+
+ Object[] parameters = {variable};
+ Object instanceOfC = constructor.newInstance(parameters);
+
+ EventBus.getInstance().dispatch((Event>) constructor.newInstance(parameters));
+ setState(!currentState);
+ this.revalidate();
+ this.repaint();
+ } catch (Exception e) {
+ logger.info("An error occured while changing the setting: " + e);
+ e.printStackTrace();
+ }
+ });
+
+ repaint();
+ }
+
+ public void paintComponent(Graphics g) {
+ g.setColor(Color.LIGHT_GRAY);
+ g.fillRect(0, 0, 50, 25);
+ g.setColor(Color.GREEN);
+ g.fillRect(0, 0, 25, 25);
+ }
+
+ /**
+ * This method sets the state of the {@link PrimaryToggleSwitch}.
+ *
+ * @param state This is the state of the {@link PrimaryToggleSwitch}, that should be set. true: off false: on
+ * @since Envoy 0.3-alpha
+ */
+ public void setState (boolean state){
+ if(state) {
+ GridBagConstraints gbc_toggleButton = new GridBagConstraints();
+ gbc_toggleButton.anchor = GridBagConstraints.WEST;
+ gbc_toggleButton.gridx = 0;
+ gbc_toggleButton.gridy = 0;
+
+ add(b, gbc_toggleButton);
+ currentState = true;
+ variable = 1;
+ }else {
+ GridBagConstraints gbc_toggleButton = new GridBagConstraints();
+ gbc_toggleButton.anchor = GridBagConstraints.EAST;
+ gbc_toggleButton.gridx = 1;
+ gbc_toggleButton.gridy = 0;
+
+ add(b, gbc_toggleButton);
+ currentState = false;
+ variable = 0;
+ }
+ }
+}
diff --git a/src/main/java/envoy/client/ui/settings/General.java b/src/main/java/envoy/client/ui/settings/General.java
new file mode 100644
index 0000000..f112f7f
--- /dev/null
+++ b/src/main/java/envoy/client/ui/settings/General.java
@@ -0,0 +1,70 @@
+package envoy.client.ui.settings;
+
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
+import java.awt.event.ActionListener;
+
+import envoy.client.event.EventBus;
+import envoy.client.event.OnCloseChangeEvent;
+import envoy.client.event.ThemeChangeEvent;
+import envoy.client.ui.PrimaryToggleSwitch;
+
+/**
+ * Displays GUI components that allow general settings regarding the client.
+ *
+ *
+ * Project: envoy-client
+ * File: General.java
+ * Created: 21 Dec 2019
+ *
+ * @author Maximilian Käfer
+ * @since Envoy v0.2-alpha
+ */
+public class General extends SettingsPanel {
+
+ private static final long serialVersionUID = -7470848775130754239L;
+
+ private String variable = "exit";
+ PrimaryToggleSwitch toggleSwitch = new PrimaryToggleSwitch(false,"envoy.client.event.OnCloseChangeEvent");
+
+ /**
+ * This is the constructor for the General class. Here the user can set general settings for the client.
+ * @since Envoy 0.3-alpha
+ */
+ public General() {
+
+ GridBagLayout gbl_general = new GridBagLayout();
+ gbl_general.columnWidths = new int[] { 1, 1 };
+ gbl_general.rowHeights = new int[] { 1, 1 };
+ gbl_general.columnWeights = new double[] { 1.0, 1.0 };
+ gbl_general.rowWeights = new double[] { 1.0, 1.0 };
+
+ setLayout(gbl_general);
+
+ GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
+ gbc_toggleSwitch.gridx = 0;
+ gbc_toggleSwitch.gridy = 0;
+
+ add(toggleSwitch, gbc_toggleSwitch);
+
+ EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get()));
+ }
+
+ /**
+ * This method changes the on close mode of the client.
+ *
+ * @param state This is the integer that defines weather the toggleSwitch is on or off.
+ * @since Envoy v0.3-alpha
+ */
+ public void changeOnClose(int state) {
+ System.out.println(state);
+ }
+
+ @Override
+ public ActionListener getOkButtonAction() {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+}
diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java
index 29bbe92..d0e4b8f 100644
--- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java
+++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java
@@ -57,6 +57,7 @@ public class SettingsScreen extends JDialog {
public SettingsScreen() {
// Initialize settings pages
Map> panels = new HashMap<>();
+ panels.put("General", General.class);
panels.put("Color Themes", ThemeCustomizationPanel.class);
setBounds(10, 10, 450, 650);
From abe36d999a891197303ea712d70ec9eb4b02abf0 Mon Sep 17 00:00:00 2001
From: DieGurke <55625494+DieGurke@users.noreply.github.com>
Date: Sun, 22 Dec 2019 21:28:13 +0100
Subject: [PATCH 2/6] OnCloseMode
* Toggle Switch in general settings effects the onCloseMode of the
window.
* Saving in prefs.
* Styled the general settings screen and added some text.
---
src/main/java/envoy/client/Settings.java | 19 +++-
.../envoy/client/ui/PrimaryToggleSwitch.java | 11 ++-
src/main/java/envoy/client/ui/Startup.java | 7 +-
.../envoy/client/ui/settings/General.java | 91 +++++++++++++++++--
4 files changed, 115 insertions(+), 13 deletions(-)
diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java
index b1c0b6b..e0c40b1 100644
--- a/src/main/java/envoy/client/Settings.java
+++ b/src/main/java/envoy/client/Settings.java
@@ -28,6 +28,7 @@ public class Settings {
private boolean enterToSend = true;
private Map themes;
private String currentTheme;
+ private int currentOnCloseMode;
/**
* Required to save the settings.
@@ -64,7 +65,8 @@ public class Settings {
private void load() {
setEnterToSend(prefs.getBoolean("enterToSend", true));
setCurrentTheme(prefs.get("theme", "dark"));
-
+ setCurrentOnCloseMode(prefs.getInt("onCloseMode", 1));
+
// Load themes from theme file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) {
Object obj = in.readObject();
@@ -94,6 +96,7 @@ public class Settings {
public void save() throws IOException {
prefs.put("theme", currentTheme);
prefs.putBoolean("enterToSend", isEnterToSend());
+ prefs.putInt("onCloseMode", currentOnCloseMode);
// Save themes to theme file
themeFile.createNewFile();
@@ -155,4 +158,18 @@ public class Settings {
* @since Envoy v0.2-alpha
*/
public void setThemes(Map themes) { this.themes = themes; }
+
+ /**
+ * @return the current on close mode.
+ * @since Envoy v0.3-alpha
+ */
+ public int getCurrentOnCloseMode () {return currentOnCloseMode;}
+
+ /**
+ * Sets the current on close mode.
+ *
+ * @param currentOnCloseMode the on close mode that should be set.
+ * @since Envoy v0.3-alpha
+ */
+ public void setCurrentOnCloseMode(int currentOnCloseMode) {this.currentOnCloseMode = currentOnCloseMode;}
}
\ 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 aa36cca..55b3ebf 100644
--- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
+++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
@@ -22,7 +22,7 @@ import envoy.client.util.EnvoyLog;
* Created: 21 Dec 2019
*
* @author Maximilian Käfer
- * @since Envoy v0.2-alpha
+ * @since Envoy v0.3-alpha
*/
public class PrimaryToggleSwitch extends JPanel{
@@ -34,6 +34,8 @@ public class PrimaryToggleSwitch extends JPanel{
private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
/**
+ * This is the constructor for the PrimaryToggleSwitch.
+ *
* @param initialState The state the toggleSwitch is standardly set to. true: off false: on
* @param eventName the path of the event class
* @since Envoy v0.3-alpha
@@ -44,8 +46,15 @@ public class PrimaryToggleSwitch extends JPanel{
setEnabled(true);
setVisible(true);
this.initialState = initialState;
+
setPreferredSize(new Dimension(50, 25));
+ setMinimumSize(new Dimension(50, 25));
+ setMaximumSize(new Dimension(50, 25));
+
b.setPreferredSize(new Dimension(25, 25));
+ b.setMinimumSize(new Dimension(25, 25));
+ b.setMaximumSize(new Dimension(25, 25));
+
b.setBackground(Settings.getInstance().getThemes()
.get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor());
diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java
index db9644a..1b33093 100644
--- a/src/main/java/envoy/client/ui/Startup.java
+++ b/src/main/java/envoy/client/ui/Startup.java
@@ -132,7 +132,12 @@ public class Startup {
new StatusTrayIcon(chatWindow).show();
// If the tray icon is supported, hide the chat window on close
- chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
+ if(Settings.getInstance().getCurrentOnCloseMode() == 1) {
+ chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
+ }else {
+ chatWindow.setDefaultCloseOperation(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/General.java
index f112f7f..9d49f06 100644
--- a/src/main/java/envoy/client/ui/settings/General.java
+++ b/src/main/java/envoy/client/ui/settings/General.java
@@ -4,11 +4,20 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
+import java.util.Arrays;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.swing.JOptionPane;
+import javax.swing.JTextPane;
+
+import envoy.client.Settings;
import envoy.client.event.EventBus;
import envoy.client.event.OnCloseChangeEvent;
import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.PrimaryToggleSwitch;
+import envoy.client.ui.Theme;
+import envoy.client.util.EnvoyLog;
/**
* Displays GUI components that allow general settings regarding the client.
@@ -19,35 +28,79 @@ import envoy.client.ui.PrimaryToggleSwitch;
* Created: 21 Dec 2019
*
* @author Maximilian Käfer
- * @since Envoy v0.2-alpha
+ * @since Envoy v0.3-alpha
*/
public class General extends SettingsPanel {
private static final long serialVersionUID = -7470848775130754239L;
+ private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName());
- private String variable = "exit";
- PrimaryToggleSwitch toggleSwitch = new PrimaryToggleSwitch(false,"envoy.client.event.OnCloseChangeEvent");
+ private int state;
+
+ PrimaryToggleSwitch toggleSwitch;
+ JTextPane onCloseModeText = new JTextPane();
+ JTextPane onCloseModeState = new JTextPane();
/**
* This is the constructor for the General class. Here the user can set general settings for the client.
* @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");
+ }
+
+ setBackground(theme.getCellColor());
GridBagLayout gbl_general = new GridBagLayout();
- gbl_general.columnWidths = new int[] { 1, 1 };
- gbl_general.rowHeights = new int[] { 1, 1 };
- gbl_general.columnWeights = new double[] { 1.0, 1.0 };
- gbl_general.rowWeights = new double[] { 1.0, 1.0 };
+ gbl_general.columnWidths = new int[] { 1, 1};
+ gbl_general.rowHeights = new int[] { 1, 1, 1 };
+ gbl_general.columnWeights = new double[] { 1.0, 0.1};
+ gbl_general.rowWeights = new double[] { 0.02, 0.0005, 1.0 };
setLayout(gbl_general);
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
- gbc_toggleSwitch.gridx = 0;
+ 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);
+
EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get()));
}
@@ -59,12 +112,30 @@ public class General extends SettingsPanel {
*/
public void changeOnClose(int state) {
System.out.println(state);
+ this.state = state;
+
+ if(state == 0) {
+ onCloseModeState.setText("OFF");
+ }else {
+ onCloseModeState.setText("ON");
+ }
+ this.revalidate();
+ this.repaint();
}
@Override
public ActionListener getOkButtonAction() {
- // TODO Auto-generated method stub
- return null;
+ return (evt) -> {
+ if (state != Settings.getInstance().getCurrentOnCloseMode()) {
+ try {
+ Settings.getInstance().setCurrentOnCloseMode(state);
+ 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();
+ }
+ }
+ };
}
}
From 141b2371cc5b65910cff09af8762e26e87f8b715 Mon Sep 17 00:00:00 2001
From: DieGurke <55625494+DieGurke@users.noreply.github.com>
Date: Sun, 22 Dec 2019 21:48:19 +0100
Subject: [PATCH 3/6] Formatting
---
src/main/java/envoy/client/Settings.java | 12 +-
.../client/event/OnCloseChangeEvent.java | 11 +-
.../envoy/client/ui/PrimaryToggleSwitch.java | 115 +++++++++---------
src/main/java/envoy/client/ui/Startup.java | 6 +-
.../envoy/client/ui/settings/General.java | 102 ++++++++--------
.../ui/settings/ThemeCustomizationPanel.java | 92 +++++++-------
6 files changed, 168 insertions(+), 170 deletions(-)
diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java
index e0c40b1..42bd0f1 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 int currentOnCloseMode;
/**
* Required to save the settings.
@@ -66,7 +66,7 @@ public class Settings {
setEnterToSend(prefs.getBoolean("enterToSend", true));
setCurrentTheme(prefs.get("theme", "dark"));
setCurrentOnCloseMode(prefs.getInt("onCloseMode", 1));
-
+
// Load themes from theme file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) {
Object obj = in.readObject();
@@ -158,18 +158,18 @@ public class Settings {
* @since Envoy v0.2-alpha
*/
public void setThemes(Map themes) { this.themes = themes; }
-
+
/**
* @return the current on close mode.
* @since Envoy v0.3-alpha
*/
- public int getCurrentOnCloseMode () {return currentOnCloseMode;}
-
+ public int getCurrentOnCloseMode() { return currentOnCloseMode; }
+
/**
* Sets the current on close mode.
*
* @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(int currentOnCloseMode) { this.currentOnCloseMode = currentOnCloseMode; }
}
\ 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
index cefc375..5cffd37 100644
--- a/src/main/java/envoy/client/event/OnCloseChangeEvent.java
+++ b/src/main/java/envoy/client/event/OnCloseChangeEvent.java
@@ -8,16 +8,19 @@ 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;
/**
- * @param closeMode This is the on close mode for the client, that should be set.
- * 0 = ExitOnClose 1 = HideOnClose
+ * @param closeMode This is the on close mode for the client, that should be
+ * set.
+ *
+ * 0 = ExitOnClose
+ * 1 = HideOnClose
* @since Envoy 0.3-alpha
*/
- public OnCloseChangeEvent(int closeMode) {this.closeMode = closeMode;}
+ public OnCloseChangeEvent(int closeMode) { this.closeMode = closeMode; }
@Override
public Integer get() { return closeMode; }
diff --git a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
index 55b3ebf..e2469cc 100644
--- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
+++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
@@ -1,7 +1,7 @@
package envoy.client.ui;
+
import java.awt.*;
import java.lang.reflect.Constructor;
-import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
@@ -9,13 +9,12 @@ import javax.swing.*;
import envoy.client.Settings;
import envoy.client.event.Event;
import envoy.client.event.EventBus;
-import envoy.client.event.EventHandler;
-import envoy.client.event.OnCloseChangeEvent;
-import envoy.client.ui.settings.ThemeCustomizationPanel;
import envoy.client.util.EnvoyLog;
/**
- * This Component can be used to toggle between two options. e.g. on and off
+ * This Component can be used to toggle between two options. e.g. on and off
+ *
+ *
*
* Project: envoy-client
* File: PrimaryToggleSwitch.java
@@ -24,20 +23,21 @@ import envoy.client.util.EnvoyLog;
* @author Maximilian Käfer
* @since Envoy v0.3-alpha
*/
-public class PrimaryToggleSwitch extends JPanel{
-
- private static final long serialVersionUID = -721155303106833184L;
- private boolean initialState;
- JButton b = new JButton("");
- private boolean currentState;
- private int variable;
- private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
-
+public class PrimaryToggleSwitch extends JPanel {
+
+ private static final long serialVersionUID = -721155303106833184L;
+ JButton b = new JButton("");
+ private boolean currentState;
+ private int variable;
+ private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
+
/**
* This is the constructor for the PrimaryToggleSwitch.
*
- * @param initialState The state the toggleSwitch is standardly set to. true: off false: on
- * @param eventName the path of the event class
+ * @param initialState The state the toggleSwitch is standardly set to.
+ * true: off
+ * false: on
+ * @param eventName the path of the event class
* @since Envoy v0.3-alpha
*/
@SuppressWarnings({ "rawtypes", "unused" })
@@ -45,38 +45,36 @@ public class PrimaryToggleSwitch extends JPanel{
super();
setEnabled(true);
setVisible(true);
- this.initialState = initialState;
-
+
setPreferredSize(new Dimension(50, 25));
setMinimumSize(new Dimension(50, 25));
setMaximumSize(new Dimension(50, 25));
-
+
b.setPreferredSize(new Dimension(25, 25));
b.setMinimumSize(new Dimension(25, 25));
b.setMaximumSize(new Dimension(25, 25));
-
- b.setBackground(Settings.getInstance().getThemes()
- .get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor());
-
+
+ b.setBackground(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor());
+
GridBagLayout gbl_toggleSwitch = new GridBagLayout();
- gbl_toggleSwitch.columnWidths = new int[] { 1, 1 };
- gbl_toggleSwitch.rowHeights = new int[] { 1};
- gbl_toggleSwitch.columnWeights = new double[] { 1.0, 1.0 };
- gbl_toggleSwitch.rowWeights = new double[] {1.0 };
+ gbl_toggleSwitch.columnWidths = new int[] { 1, 1 };
+ gbl_toggleSwitch.rowHeights = new int[] { 1 };
+ gbl_toggleSwitch.columnWeights = new double[] { 1.0, 1.0 };
+ gbl_toggleSwitch.rowWeights = new double[] { 1.0 };
setLayout(gbl_toggleSwitch);
-
+
setState(initialState);
-
+
b.addActionListener((evt) -> {
try {
- Class> c = Class.forName(eventName);
- Class[] types = {int.class};
- Constructor constructor = c.getConstructor(types);
-
- Object[] parameters = {variable};
- Object instanceOfC = constructor.newInstance(parameters);
-
+ Class> c = Class.forName(eventName);
+ Class[] types = { int.class };
+ Constructor constructor = c.getConstructor(types);
+
+ Object[] parameters = { variable };
+ Object instanceOfC = constructor.newInstance(parameters);
+
EventBus.getInstance().dispatch((Event>) constructor.newInstance(parameters));
setState(!currentState);
this.revalidate();
@@ -86,42 +84,45 @@ public class PrimaryToggleSwitch extends JPanel{
e.printStackTrace();
}
});
-
+
repaint();
}
-
+
public void paintComponent(Graphics g) {
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, 50, 25);
g.setColor(Color.GREEN);
g.fillRect(0, 0, 25, 25);
}
-
+
/**
* This method sets the state of the {@link PrimaryToggleSwitch}.
*
- * @param state This is the state of the {@link PrimaryToggleSwitch}, that should be set. true: off false: on
+ * @param state This is the state of the {@link PrimaryToggleSwitch}, that
+ * should be set.
+ * true: off
+ * false: on
* @since Envoy 0.3-alpha
*/
- public void setState (boolean state){
- if(state) {
+ public void setState(boolean state) {
+ if (state) {
GridBagConstraints gbc_toggleButton = new GridBagConstraints();
- gbc_toggleButton.anchor = GridBagConstraints.WEST;
- gbc_toggleButton.gridx = 0;
- gbc_toggleButton.gridy = 0;
-
- add(b, gbc_toggleButton);
- currentState = true;
- variable = 1;
- }else {
- GridBagConstraints gbc_toggleButton = new GridBagConstraints();
- gbc_toggleButton.anchor = GridBagConstraints.EAST;
- gbc_toggleButton.gridx = 1;
- gbc_toggleButton.gridy = 0;
-
+ gbc_toggleButton.anchor = GridBagConstraints.WEST;
+ gbc_toggleButton.gridx = 0;
+ gbc_toggleButton.gridy = 0;
+
add(b, gbc_toggleButton);
- currentState = false;
- variable = 0;
+ currentState = true;
+ variable = 1;
+ } else {
+ GridBagConstraints gbc_toggleButton = new GridBagConstraints();
+ gbc_toggleButton.anchor = GridBagConstraints.EAST;
+ gbc_toggleButton.gridx = 1;
+ gbc_toggleButton.gridy = 0;
+
+ add(b, gbc_toggleButton);
+ currentState = false;
+ variable = 0;
}
}
}
diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java
index 1b33093..547be9a 100644
--- a/src/main/java/envoy/client/ui/Startup.java
+++ b/src/main/java/envoy/client/ui/Startup.java
@@ -132,12 +132,12 @@ 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() == 1) {
chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
- }else {
+ } else {
chatWindow.setDefaultCloseOperation(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/General.java
index 9d49f06..017bb85 100644
--- a/src/main/java/envoy/client/ui/settings/General.java
+++ b/src/main/java/envoy/client/ui/settings/General.java
@@ -4,8 +4,6 @@ import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
-import java.util.Arrays;
-import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
@@ -14,7 +12,6 @@ import javax.swing.JTextPane;
import envoy.client.Settings;
import envoy.client.event.EventBus;
import envoy.client.event.OnCloseChangeEvent;
-import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.PrimaryToggleSwitch;
import envoy.client.ui.Theme;
import envoy.client.util.EnvoyLog;
@@ -31,98 +28,101 @@ import envoy.client.util.EnvoyLog;
* @since Envoy v0.3-alpha
*/
public class General extends SettingsPanel {
-
- private static final long serialVersionUID = -7470848775130754239L;
- private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName());
-
+
+ private static final long serialVersionUID = -7470848775130754239L;
+ private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName());
+
private int state;
-
- PrimaryToggleSwitch toggleSwitch;
- JTextPane onCloseModeText = new JTextPane();
- JTextPane onCloseModeState = new JTextPane();
-
+
+ PrimaryToggleSwitch toggleSwitch;
+ JTextPane onCloseModeText = new JTextPane();
+ JTextPane onCloseModeState = new JTextPane();
+
/**
- * This is the constructor for the General class. Here the user can set general settings for the client.
+ * This is the constructor for the General class. Here the user can set general
+ * settings for the client.
+ *
* @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");
+ if (state == 1) {
+ toggleSwitch = new PrimaryToggleSwitch(false, "envoy.client.event.OnCloseChangeEvent");
+ } else {
+ toggleSwitch = new PrimaryToggleSwitch(true, "envoy.client.event.OnCloseChangeEvent");
}
-
+
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.columnWeights = new double[] { 1.0, 0.1};
- gbl_general.rowWeights = new double[] { 0.02, 0.0005, 1.0 };
+ gbl_general.columnWidths = new int[] { 1, 1 };
+ gbl_general.rowHeights = new int[] { 1, 1, 1 };
+ gbl_general.columnWeights = new double[] { 1.0, 0.1 };
+ gbl_general.rowWeights = new double[] { 0.02, 0.0005, 1.0 };
setLayout(gbl_general);
-
+
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
- gbc_toggleSwitch.gridx = 1;
- gbc_toggleSwitch.gridy = 0;
+ gbc_toggleSwitch.gridx = 1;
+ gbc_toggleSwitch.gridy = 0;
add(toggleSwitch, gbc_toggleSwitch);
-
- if(state == 0) {
+
+ if (state == 0) {
onCloseModeState.setText("OFF");
- }else {
+ } 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;
+ 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.
+ // 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);
+ 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);
-
+
EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get()));
}
-
+
/**
* This method changes the on close mode of the client.
*
- * @param state This is the integer that defines weather the toggleSwitch is on or off.
+ * @param state This is the integer that defines weather the toggleSwitch is on
+ * or off.
* @since Envoy v0.3-alpha
*/
public void changeOnClose(int state) {
System.out.println(state);
this.state = state;
-
- if(state == 0) {
+
+ if (state == 0) {
onCloseModeState.setText("OFF");
- }else {
+ } else {
onCloseModeState.setText("ON");
}
this.revalidate();
this.repaint();
}
-
+
@Override
public ActionListener getOkButtonAction() {
return (evt) -> {
@@ -130,7 +130,7 @@ public class General extends SettingsPanel {
try {
Settings.getInstance().setCurrentOnCloseMode(state);
JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started.");
- } catch (Exception e) {
+ } catch (Exception e) {
logger.info("Close mode could not be changed! " + e);
e.printStackTrace();
}
diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java
index 45663af..959690e 100644
--- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java
+++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java
@@ -34,10 +34,10 @@ public class ThemeCustomizationPanel extends SettingsPanel {
private JPanel colorsPanel = new JPanel();
- private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]);
- private JComboBox themes = new JComboBox<>(themeArray);
- private Theme temporaryTheme, selectedTheme;
- private boolean themeChanged = false;
+ private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]);
+ private JComboBox themes = new JComboBox<>(themeArray);
+ private Theme temporaryTheme, selectedTheme;
+ private boolean themeChanged = false;
private final Insets insets = new Insets(5, 5, 5, 5);
@@ -51,15 +51,14 @@ public class ThemeCustomizationPanel extends SettingsPanel {
* @since Envoy v0.2-alpha
*/
public ThemeCustomizationPanel() {
- temporaryTheme = new Theme("temporaryTheme",
- Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
+ temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
GridBagLayout gbl_themeLayout = new GridBagLayout();
- gbl_themeLayout.columnWidths = new int[] { 1, 1 };
- gbl_themeLayout.rowHeights = new int[] { 1, 1 };
- gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 };
- gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0 };
+ gbl_themeLayout.columnWidths = new int[] { 1, 1 };
+ gbl_themeLayout.rowHeights = new int[] { 1, 1 };
+ gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 };
+ gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0 };
setLayout(gbl_themeLayout);
@@ -76,20 +75,20 @@ public class ThemeCustomizationPanel extends SettingsPanel {
});
GridBagConstraints gbc_themes = new GridBagConstraints();
- gbc_themes.fill = GridBagConstraints.HORIZONTAL;
- gbc_themes.gridwidth = 2;
- gbc_themes.gridx = 0;
- gbc_themes.gridy = 0;
- gbc_themes.anchor = GridBagConstraints.NORTHWEST;
- gbc_themes.insets = new Insets(10, 10, 20, 10);
+ gbc_themes.fill = GridBagConstraints.HORIZONTAL;
+ gbc_themes.gridwidth = 2;
+ gbc_themes.gridx = 0;
+ gbc_themes.gridy = 0;
+ gbc_themes.anchor = GridBagConstraints.NORTHWEST;
+ gbc_themes.insets = new Insets(10, 10, 20, 10);
add(themes, gbc_themes);
GridBagLayout gbl_colorCustomizations = new GridBagLayout();
- gbl_colorCustomizations.columnWidths = new int[] { 1, 1 };
- gbl_colorCustomizations.rowHeights = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
- gbl_colorCustomizations.columnWeights = new double[] { 1, 1 };
- gbl_colorCustomizations.rowWeights = new double[] { 1, 1, 1, 1, 1, 1, 1, 1 };
+ gbl_colorCustomizations.columnWidths = new int[] { 1, 1 };
+ gbl_colorCustomizations.rowHeights = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
+ gbl_colorCustomizations.columnWeights = new double[] { 1, 1 };
+ gbl_colorCustomizations.rowWeights = new double[] { 1, 1, 1, 1, 1, 1, 1, 1 };
colorsPanel.setLayout(gbl_colorCustomizations);
@@ -97,12 +96,12 @@ public class ThemeCustomizationPanel extends SettingsPanel {
buildCustomizeElements(theme);
GridBagConstraints gbc_colorsPanel = new GridBagConstraints();
- gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL;
- gbc_colorsPanel.gridx = 0;
- gbc_colorsPanel.gridy = 1;
- gbc_colorsPanel.gridwidth = 2;
- gbc_colorsPanel.anchor = GridBagConstraints.NORTHWEST;
- gbc_colorsPanel.insets = insets;
+ gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL;
+ gbc_colorsPanel.gridx = 0;
+ gbc_colorsPanel.gridy = 1;
+ gbc_colorsPanel.gridwidth = 2;
+ gbc_colorsPanel.anchor = GridBagConstraints.NORTHWEST;
+ gbc_colorsPanel.insets = insets;
add(colorsPanel, gbc_colorsPanel);
colorsPanel.setBackground(theme.getCellColor());
@@ -122,11 +121,10 @@ public class ThemeCustomizationPanel extends SettingsPanel {
String name = JOptionPane.showInputDialog("Enter a name for the new theme");
logger.log(Level.FINEST, name);
Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme));
- themeArray = Arrays.copyOf(themeArray, themeArray.length + 1);
- themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(name).getThemeName();
+ themeArray = Arrays.copyOf(themeArray, themeArray.length + 1);
+ themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(name).getThemeName();
- temporaryTheme = new Theme("temporaryTheme",
- Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
+ temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
themes.addItem(themeArray[themeArray.length - 1]);
themes.setSelectedIndex(themeArray.length - 1);
@@ -172,10 +170,8 @@ public class ThemeCustomizationPanel extends SettingsPanel {
private void buildCustomizeElements(Theme theme) {
buildCustomizeElement(theme, theme.getBackgroundColor(), "Background", "backgroundColor", 1);
buildCustomizeElement(theme, theme.getCellColor(), "Cells", "cellColor", 2);
- buildCustomizeElement(theme, theme.getInteractableForegroundColor(), "Interactable Foreground",
- "interactableForegroundColor", 3);
- buildCustomizeElement(theme, theme.getInteractableBackgroundColor(), "Interactable Background",
- "interactableBackgroundColor", 4);
+ buildCustomizeElement(theme, theme.getInteractableForegroundColor(), "Interactable Foreground", "interactableForegroundColor", 3);
+ buildCustomizeElement(theme, theme.getInteractableBackgroundColor(), "Interactable Background", "interactableBackgroundColor", 4);
buildCustomizeElement(theme, theme.getMessageColorChat(), "Messages Chat", "messageColorChat", 5);
buildCustomizeElement(theme, theme.getDateColorChat(), "Date Chat", "dateColorCat", 6);
buildCustomizeElement(theme, theme.getSelectionColor(), "Selection", "selectionColor", 7);
@@ -184,8 +180,8 @@ public class ThemeCustomizationPanel extends SettingsPanel {
}
private void buildCustomizeElement(Theme theme, Color color, String name, String colorName, int gridy) {
- JButton button = new JButton();
- JTextPane textPane = new JTextPane();
+ JButton button = new JButton();
+ JTextPane textPane = new JTextPane();
textPane.setFont(new Font("Arial", Font.PLAIN, 14));
textPane.setBackground(theme.getBackgroundColor());
@@ -215,25 +211,23 @@ public class ThemeCustomizationPanel extends SettingsPanel {
});
GridBagConstraints gbc_textPane = new GridBagConstraints();
- gbc_textPane.fill = GridBagConstraints.BOTH;
- gbc_textPane.gridx = 0;
- gbc_textPane.gridy = gridy;
- gbc_textPane.anchor = GridBagConstraints.CENTER;
- gbc_textPane.insets = insets;
+ gbc_textPane.fill = GridBagConstraints.BOTH;
+ gbc_textPane.gridx = 0;
+ gbc_textPane.gridy = gridy;
+ gbc_textPane.anchor = GridBagConstraints.CENTER;
+ gbc_textPane.insets = insets;
colorsPanel.add(textPane, gbc_textPane);
GridBagConstraints gbc_button = new GridBagConstraints();
- gbc_button.fill = GridBagConstraints.BOTH;
- gbc_button.gridx = 1;
- gbc_button.gridy = gridy;
- gbc_button.anchor = GridBagConstraints.CENTER;
- gbc_button.insets = insets;
+ gbc_button.fill = GridBagConstraints.BOTH;
+ gbc_button.gridx = 1;
+ gbc_button.gridy = gridy;
+ gbc_button.anchor = GridBagConstraints.CENTER;
+ gbc_button.insets = insets;
colorsPanel.add(button, gbc_button);
}
- private Color getInvertedColor(Color color) {
- return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
- }
+ private Color getInvertedColor(Color color) { return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); }
}
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 4/6] 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();
+ }
+ }
};
}
From 762d7630e34ebf21dcb5fe752c5faf83dacccd27 Mon Sep 17 00:00:00 2001
From: kske
Date: Mon, 23 Dec 2019 10:56:33 +0100
Subject: [PATCH 5/6] Fixed formatting, Javadoc and other cosmetic problems
Also fixed PrimaryToggleSwitches having editable text in their state and
description fields
---
.../envoy/client/event/EnterToSendEvent.java | 12 +-
.../client/event/OnCloseChangeEvent.java | 13 +--
.../envoy/client/ui/PrimaryToggleSwitch.java | 71 +++++-------
src/main/java/envoy/client/ui/Startup.java | 8 +-
.../envoy/client/ui/settings/General.java | 104 +++++++-----------
.../ui/settings/ThemeCustomizationPanel.java | 4 +-
6 files changed, 85 insertions(+), 127 deletions(-)
diff --git a/src/main/java/envoy/client/event/EnterToSendEvent.java b/src/main/java/envoy/client/event/EnterToSendEvent.java
index f96d13e..7b26e0a 100644
--- a/src/main/java/envoy/client/event/EnterToSendEvent.java
+++ b/src/main/java/envoy/client/event/EnterToSendEvent.java
@@ -1,6 +1,8 @@
package envoy.client.event;
/**
+ * Encapsulates a change to the {@code enterToSend} setting.
+ *
* Project: envoy-client
* File: EnterToSendEvent.java
* Created: 22 Dec 2019
@@ -13,15 +15,13 @@ 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
+ * 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
index 717a5da..2d86fb6 100644
--- a/src/main/java/envoy/client/event/OnCloseChangeEvent.java
+++ b/src/main/java/envoy/client/event/OnCloseChangeEvent.java
@@ -1,6 +1,8 @@
package envoy.client.event;
/**
+ * Encapsulates a change to the {@code currentOnCloseMode} setting.
+ *
* Project: envoy-client
* File: OnCloseChangeEvent.java
* Created: 22 Dec 2019
@@ -13,16 +15,13 @@ public class OnCloseChangeEvent implements Event {
private boolean closeMode;
/**
- * @param closeMode This is the on close mode for the client, that should be
- * set.
- *
- * true = ExitOnClose
- * false = HideOnClose
+ * 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 361db5d..109656a 100644
--- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
+++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java
@@ -4,7 +4,8 @@ import java.awt.*;
import java.lang.reflect.Constructor;
import java.util.logging.Logger;
-import javax.swing.*;
+import javax.swing.JButton;
+import javax.swing.JPanel;
import envoy.client.Settings;
import envoy.client.event.Event;
@@ -12,10 +13,9 @@ import envoy.client.event.EventBus;
import envoy.client.util.EnvoyLog;
/**
- * This Component can be used to toggle between two options. e.g. on and off
- *
- *
- *
+ * This Component can be used to toggle between two options. e.g. on and
+ * off.
+ *
* Project: envoy-client
* File: PrimaryToggleSwitch.java
* Created: 21 Dec 2019
@@ -25,11 +25,12 @@ import envoy.client.util.EnvoyLog;
*/
public class PrimaryToggleSwitch extends JPanel {
- private static final long serialVersionUID = -721155303106833184L;
- JButton b = new JButton("");
- private boolean currentState;
- private boolean variable;
+ private final JButton b = new JButton("");
+ private boolean currentState;
+ private boolean variable;
+
private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
+ private static final long serialVersionUID = -721155303106833184L;
/**
* This is the constructor for the PrimaryToggleSwitch.
@@ -37,12 +38,10 @@ public class PrimaryToggleSwitch extends JPanel {
* @param initialState The state the toggleSwitch is standardly set to.
* true: off
* false: on
- * @param eventName the path of the event class
+ * @param eventClass the class of the event dispatched by this toggleSwitch
* @since Envoy v0.3-alpha
*/
- @SuppressWarnings({ "rawtypes", "unused" })
- public PrimaryToggleSwitch(boolean initialState, String eventName) {
- super();
+ public PrimaryToggleSwitch(boolean initialState, Class extends Event> eventClass) {
setEnabled(true);
setVisible(true);
@@ -68,26 +67,22 @@ public class PrimaryToggleSwitch extends JPanel {
b.addActionListener((evt) -> {
try {
- Class> c = Class.forName(eventName);
- Class[] types = { boolean.class };
- Constructor constructor = c.getConstructor(types);
+ // Dispatch event
+ Constructor extends Event> constructor = eventClass.getConstructor(boolean.class);
+ EventBus.getInstance().dispatch((Event>) constructor.newInstance(variable));
- Object[] parameters = { variable };
- Object instanceOfC = constructor.newInstance(parameters);
-
- EventBus.getInstance().dispatch((Event>) constructor.newInstance(parameters));
setState(!currentState);
- this.revalidate();
- this.repaint();
- } catch (Exception e) {
- logger.info("An error occured while changing the setting: " + e);
- e.printStackTrace();
+ revalidate();
+ repaint();
+ } catch (ReflectiveOperationException | SecurityException e) {
+ logger.warning("An error occured while changing the setting: " + e);
}
});
repaint();
}
+ @Override
public void paintComponent(Graphics g) {
g.setColor(Color.LIGHT_GRAY);
g.fillRect(0, 0, 50, 25);
@@ -96,33 +91,25 @@ public class PrimaryToggleSwitch extends JPanel {
}
/**
- * This method sets the state of the {@link PrimaryToggleSwitch}.
+ * This method sets the state of this {@link PrimaryToggleSwitch}.
*
- * @param state This is the state of the {@link PrimaryToggleSwitch}, that
- * should be set.
- * true: off
- * false: on
+ * @param state {@code true} to enable the switch, {@code false} to disable it
* @since Envoy 0.3-alpha
*/
public void setState(boolean state) {
+ GridBagConstraints gbc_toggleButton = new GridBagConstraints();
+
if (state) {
- GridBagConstraints gbc_toggleButton = new GridBagConstraints();
gbc_toggleButton.anchor = GridBagConstraints.WEST;
gbc_toggleButton.gridx = 0;
- gbc_toggleButton.gridy = 0;
-
- add(b, gbc_toggleButton);
- currentState = true;
- variable = true;
} else {
- GridBagConstraints gbc_toggleButton = new GridBagConstraints();
gbc_toggleButton.anchor = GridBagConstraints.EAST;
gbc_toggleButton.gridx = 1;
- gbc_toggleButton.gridy = 0;
-
- add(b, gbc_toggleButton);
- currentState = false;
- variable = false;
}
+ gbc_toggleButton.gridy = 0;
+ add(b, gbc_toggleButton);
+
+ currentState = state;
+ variable = state;
}
}
diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java
index d2f7125..9cce0dd 100644
--- a/src/main/java/envoy/client/ui/Startup.java
+++ b/src/main/java/envoy/client/ui/Startup.java
@@ -131,13 +131,9 @@ public class Startup {
try {
new StatusTrayIcon(chatWindow).show();
- // If the tray icon is supported, hide the chat window on close
- if (Settings.getInstance().getCurrentOnCloseMode() == true) {
+ // 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);
- } else {
- chatWindow.setDefaultCloseOperation(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/General.java
index e922359..faa46bc 100644
--- a/src/main/java/envoy/client/ui/settings/General.java
+++ b/src/main/java/envoy/client/ui/settings/General.java
@@ -4,15 +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.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.event.*;
import envoy.client.ui.PrimaryToggleSwitch;
import envoy.client.ui.Theme;
import envoy.client.util.EnvoyLog;
@@ -20,7 +19,6 @@ import envoy.client.util.EnvoyLog;
/**
* Displays GUI components that allow general settings regarding the client.
*
- *
* Project: envoy-client
* File: General.java
* Created: 21 Dec 2019
@@ -30,19 +28,20 @@ import envoy.client.util.EnvoyLog;
*/
public class General extends SettingsPanel {
- private static final long serialVersionUID = -7470848775130754239L;
+ private Theme theme;
+ private boolean onCloseState;
+ private boolean enterToSend;
+
+ 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 Theme theme;
- private boolean onCloseState;
- private boolean enterToSend;
-
- PrimaryToggleSwitch toggleSwitch;
- JTextPane onCloseModeTextPane = new JTextPane();
- JTextPane onCloseModeStatePane = new JTextPane();
-
- PrimaryToggleSwitch toggleSwitchEnterToSend;
- JTextPane enterToSendTextPane = new JTextPane();
- JTextPane enterToSendStatePane = new JTextPane();
+ private static final long serialVersionUID = -7470848775130754239L;
/**
* This is the constructor for the General class. Here the user can set general
@@ -64,7 +63,7 @@ public class General extends SettingsPanel {
setLayout(gbl_general);
createSettingElement(0,
- "envoy.client.event.OnCloseChangeEvent",
+ OnCloseChangeEvent.class,
Settings.getInstance().getCurrentOnCloseMode(),
toggleSwitch,
onCloseModeStatePane,
@@ -73,7 +72,7 @@ public class General extends SettingsPanel {
EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get()));
createSettingElement(2,
- "envoy.client.event.EnterToSendEvent",
+ EnterToSendEvent.class,
Settings.getInstance().isEnterToSend(),
toggleSwitchEnterToSend,
enterToSendStatePane,
@@ -92,13 +91,9 @@ public class General extends SettingsPanel {
public void changeOnClose(boolean state) {
this.onCloseState = state;
- if (state == false) {
- onCloseModeStatePane.setText("OFF");
- } else {
- onCloseModeStatePane.setText("ON");
- }
- this.revalidate();
- this.repaint();
+ onCloseModeStatePane.setText(state ? "ON" : "OFF");
+ revalidate();
+ repaint();
}
/**
@@ -111,22 +106,14 @@ public class General extends SettingsPanel {
public void changeEnterToSend(boolean state) {
this.enterToSend = state;
- if (state == false) {
- enterToSendStatePane.setText("OFF");
- } else {
- enterToSendStatePane.setText("ON");
- }
- this.revalidate();
- this.repaint();
+ enterToSendStatePane.setText(state ? "ON" : "OFF");
+ revalidate();
+ 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);
- }
+ private void createSettingElement(int gridy, Class extends Event> eventClass, boolean state, PrimaryToggleSwitch toggleSwitch,
+ JTextPane stateText, JTextPane descriptionText, String text) {
+ toggleSwitch = new PrimaryToggleSwitch(state, eventClass);
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
gbc_toggleSwitch.gridx = 1;
@@ -134,14 +121,10 @@ public class General extends SettingsPanel {
add(toggleSwitch, gbc_toggleSwitch);
- if (state == false) {
- stateText.setText("OFF");
- } else {
- stateText.setText("ON");
- }
-
+ stateText.setText(state ? "ON" : "OFF");
stateText.setBackground(theme.getCellColor());
stateText.setForeground(theme.getUserNameColor());
+ stateText.setEditable(false);
GridBagConstraints gbc_stateText = new GridBagConstraints();
gbc_stateText.anchor = GridBagConstraints.NORTH;
@@ -154,6 +137,7 @@ public class General extends SettingsPanel {
descriptionText.setBackground(theme.getBackgroundColor());
// TODO: Change to inverted color.
descriptionText.setForeground(theme.getUserNameColor());
+ descriptionText.setEditable(false);
GridBagConstraints gbc_descriptionText = new GridBagConstraints();
gbc_descriptionText.fill = GridBagConstraints.BOTH;
@@ -168,27 +152,19 @@ public class General extends SettingsPanel {
@Override
public ActionListener getOkButtonAction() {
return (evt) -> {
- if (onCloseState != Settings.getInstance().getCurrentOnCloseMode()) {
- try {
- 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 (onCloseState != Settings.getInstance().getCurrentOnCloseMode()) try {
+ Settings.getInstance().setCurrentOnCloseMode(onCloseState);
+ JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started.");
+ } catch (Exception e) {
+ logger.log(Level.WARNING, "Close mode could not be changed! ", e);
}
- 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();
- }
+ 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.log(Level.WARNING, "Enter to send mode could not be changed! ", e);
}
};
}
-
-}
+}
\ No newline at end of file
diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java
index 959690e..5310a5e 100644
--- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java
+++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java
@@ -30,7 +30,6 @@ import envoy.client.util.EnvoyLog;
*/
public class ThemeCustomizationPanel extends SettingsPanel {
- private static final long serialVersionUID = -8697897390666456624L;
private JPanel colorsPanel = new JPanel();
@@ -42,6 +41,7 @@ public class ThemeCustomizationPanel extends SettingsPanel {
private final Insets insets = new Insets(5, 5, 5, 5);
private static final Logger logger = EnvoyLog.getLogger(ThemeCustomizationPanel.class.getSimpleName());
+ private static final long serialVersionUID = -8697897390666456624L;
/**
* Initializes a {@link ThemeCustomizationPanel} that enables the user to change
@@ -230,4 +230,4 @@ public class ThemeCustomizationPanel extends SettingsPanel {
}
private Color getInvertedColor(Color color) { return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); }
-}
+}
\ No newline at end of file
From eebc5ab7ad70c1b70b1bbd42a81dc566afc4a8cb Mon Sep 17 00:00:00 2001
From: kske
Date: Mon, 23 Dec 2019 11:28:00 +0100
Subject: [PATCH 6/6] Added custom Color class to envoy.ui with invert() and
toHex() methods.
---
src/main/java/envoy/client/Settings.java | 4 +-
src/main/java/envoy/client/ui/Color.java | 106 ++++++++++++++++++
.../envoy/client/ui/MessageListRenderer.java | 13 +--
.../envoy/client/ui/PrimaryToggleSwitch.java | 11 +-
src/main/java/envoy/client/ui/Theme.java | 1 -
.../envoy/client/ui/UserListRenderer.java | 11 +-
.../envoy/client/ui/settings/General.java | 3 +-
.../client/ui/settings/SettingsScreen.java | 12 +-
.../ui/settings/ThemeCustomizationPanel.java | 10 +-
9 files changed, 127 insertions(+), 44 deletions(-)
create mode 100644 src/main/java/envoy/client/ui/Color.java
diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java
index 5e15743..fc60877 100644
--- a/src/main/java/envoy/client/Settings.java
+++ b/src/main/java/envoy/client/Settings.java
@@ -1,11 +1,11 @@
package envoy.client;
-import java.awt.Color;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.prefs.Preferences;
+import envoy.client.ui.Color;
import envoy.client.ui.Theme;
/**
@@ -28,7 +28,7 @@ public class Settings {
private boolean enterToSend = true;
private Map themes;
private String currentTheme;
- private boolean currentOnCloseMode;
+ private boolean currentOnCloseMode;
/**
* Required to save the settings.
diff --git a/src/main/java/envoy/client/ui/Color.java b/src/main/java/envoy/client/ui/Color.java
new file mode 100644
index 0000000..33be64b
--- /dev/null
+++ b/src/main/java/envoy/client/ui/Color.java
@@ -0,0 +1,106 @@
+package envoy.client.ui;
+
+import java.awt.color.ColorSpace;
+
+/**
+ * Project: envoy-clientChess
+ * File: Color.javaEvent.java
+ * Created: 23.12.2019
+ *
+ * @author Kai S. K. Engelbart
+ */
+@SuppressWarnings("javadoc")
+public class Color extends java.awt.Color {
+
+ /**
+ * The color white. In the default sRGB space.
+ */
+ public static final Color white = new Color(255, 255, 255);
+
+ /**
+ * The color light gray. In the default sRGB space.
+ */
+ public static final Color lightGray = new Color(192, 192, 192);
+
+ /**
+ * The color gray. In the default sRGB space.
+ */
+ public static final Color gray = new Color(128, 128, 128);
+
+ /**
+ * The color dark gray. In the default sRGB space.
+ */
+ public static final Color darkGray = new Color(64, 64, 64);
+
+ /**
+ * The color black. In the default sRGB space.
+ */
+ public static final Color black = new Color(0, 0, 0);
+
+ /**
+ * The color red. In the default sRGB space.
+ */
+ public static final Color red = new Color(255, 0, 0);
+
+ /**
+ * The color pink. In the default sRGB space.
+ */
+ public static final Color pink = new Color(255, 175, 175);
+
+ /**
+ * The color orange. In the default sRGB space.
+ */
+ public static final Color orange = new Color(255, 200, 0);
+
+ /**
+ * The color yellow. In the default sRGB space.
+ */
+ public static final Color yellow = new Color(255, 255, 0);
+
+ /**
+ * The color green. In the default sRGB space.
+ */
+ public static final Color green = new Color(0, 255, 0);
+
+ /**
+ * The color magenta. In the default sRGB space.
+ */
+ public static final Color magenta = new Color(255, 0, 255);
+
+ /**
+ * The color cyan. In the default sRGB space.
+ */
+ public static final Color cyan = new Color(0, 255, 255);
+
+ /**
+ * The color blue. In the default sRGB space.
+ */
+ public static final Color blue = new Color(0, 0, 255);
+
+ private static final long serialVersionUID = -9166233199998257344L;
+
+ public Color(int rgb) { super(rgb); }
+
+ public Color(int rgba, boolean hasalpha) { super(rgba, hasalpha); }
+
+ public Color(int r, int g, int b) { super(r, g, b); }
+
+ public Color(float r, float g, float b) { super(r, g, b); }
+
+ public Color(ColorSpace cspace, float[] components, float alpha) { super(cspace, components, alpha); }
+
+ public Color(int r, int g, int b, int a) { super(r, g, b, a); }
+
+ public Color(float r, float g, float b, float a) { super(r, g, b, a); }
+
+ /**
+ * @return the inversion of this {@link Color} by replacing the red, green and
+ * blue values by subtracting them form 255
+ */
+ public Color invert() { return new Color(255 - getRed(), 255 - getGreen(), 255 - getBlue()); }
+
+ /**
+ * @return the hex value of this {@link Color}
+ */
+ public String toHex() { return String.format("#%02x%02x%02x", getRed(), getGreen(), getBlue()); }
+}
\ No newline at end of file
diff --git a/src/main/java/envoy/client/ui/MessageListRenderer.java b/src/main/java/envoy/client/ui/MessageListRenderer.java
index 5623fb6..ddd2230 100644
--- a/src/main/java/envoy/client/ui/MessageListRenderer.java
+++ b/src/main/java/envoy/client/ui/MessageListRenderer.java
@@ -1,6 +1,5 @@
package envoy.client.ui;
-import java.awt.Color;
import java.awt.Component;
import java.text.SimpleDateFormat;
@@ -46,11 +45,11 @@ public class MessageListRenderer extends JLabel implements ListCellRenderer%s
%s :%s