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] 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(); + } + } + }; } }