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] 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);