129 lines
3.8 KiB
Java
129 lines
3.8 KiB
Java
package envoy.client.ui;
|
|
|
|
import java.awt.*;
|
|
import java.lang.reflect.Constructor;
|
|
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.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äfer
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
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. </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);
|
|
|
|
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());
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|