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.
This commit is contained in:
DieGurke 2019-12-22 18:07:30 +01:00
parent 27dc78cfe8
commit e201ec3da1
4 changed files with 214 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package envoy.client.event;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>OnCloseChangeEvent.java</strong><br>
* Created: <strong>22 Dec 2019</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy v0.3-alpha
*/
public class OnCloseChangeEvent implements Event<Integer>{
private Integer closeMode;
/**
* @param closeMode This is the on close mode for the client, that should be set.
* </br> 0 = ExitOnClose </br> 1 = HideOnClose
* @since Envoy 0.3-alpha
*/
public OnCloseChangeEvent(int closeMode) {this.closeMode = closeMode;}
@Override
public Integer get() { return closeMode; }
}

View File

@ -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 </br></br>
*
* Project: <strong>envoy-client</strong><br>
* File: <strong>PrimaryToggleSwitch.java</strong><br>
* Created: <strong>21 Dec 2019</strong><br>
*
* @author Maximilian K&auml;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. </br> true: off </br> 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. </br> true: off </br> 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;
}
}
}

View File

@ -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.<br>
* <br>
*
* Project: <strong>envoy-client</strong><br>
* File: <strong>General.java</strong><br>
* Created: <strong>21 Dec 2019</strong><br>
*
* @author Maximilian K&auml;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;
}
}

View File

@ -57,6 +57,7 @@ public class SettingsScreen extends JDialog {
public SettingsScreen() {
// Initialize settings pages
Map<String, Class<? extends SettingsPanel>> panels = new HashMap<>();
panels.put("General", General.class);
panels.put("Color Themes", ThemeCustomizationPanel.class);
setBounds(10, 10, 450, 650);