Made PrimaryToggleSwitch a sub class of JButton
This commit is contained in:
parent
3020125334
commit
66cf42e0d9
@ -1,9 +1,9 @@
|
|||||||
package envoy.client.ui;
|
package envoy.client.ui;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.Dimension;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JPanel;
|
|
||||||
|
|
||||||
import envoy.client.Settings;
|
import envoy.client.Settings;
|
||||||
import envoy.client.SettingsItem;
|
import envoy.client.SettingsItem;
|
||||||
@ -19,11 +19,9 @@ import envoy.client.SettingsItem;
|
|||||||
* @author Maximilian Käfer
|
* @author Maximilian Käfer
|
||||||
* @since Envoy v0.3-alpha
|
* @since Envoy v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public class PrimaryToggleSwitch extends JPanel {
|
public class PrimaryToggleSwitch extends JButton {
|
||||||
|
|
||||||
private final JButton b = new JButton();
|
private boolean state;
|
||||||
|
|
||||||
private boolean currentState;
|
|
||||||
|
|
||||||
private static final long serialVersionUID = -721155303106833184L;
|
private static final long serialVersionUID = -721155303106833184L;
|
||||||
|
|
||||||
@ -34,61 +32,23 @@ public class PrimaryToggleSwitch extends JPanel {
|
|||||||
* @since Envoy v0.3-alpha
|
* @since Envoy v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public PrimaryToggleSwitch(SettingsItem<Boolean> settingsItem) {
|
public PrimaryToggleSwitch(SettingsItem<Boolean> settingsItem) {
|
||||||
setEnabled(true);
|
|
||||||
setVisible(true);
|
|
||||||
|
|
||||||
setPreferredSize(new Dimension(50, 25));
|
setPreferredSize(new Dimension(50, 25));
|
||||||
setMinimumSize(new Dimension(50, 25));
|
setMinimumSize(new Dimension(50, 25));
|
||||||
setMaximumSize(new Dimension(50, 25));
|
setMaximumSize(new Dimension(50, 25));
|
||||||
|
|
||||||
b.setPreferredSize(new Dimension(25, 25));
|
setBorderPainted(false);
|
||||||
b.setMinimumSize(new Dimension(25, 25));
|
setFocusPainted(false);
|
||||||
b.setMaximumSize(new Dimension(25, 25));
|
setContentAreaFilled(false);
|
||||||
|
|
||||||
b.setBackground(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor());
|
addActionListener((evt) -> { state = !state; settingsItem.set(state); revalidate(); repaint(); });
|
||||||
|
|
||||||
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(settingsItem.get());
|
|
||||||
|
|
||||||
b.addActionListener((evt) -> { settingsItem.set(!currentState); setState(!currentState); revalidate(); repaint(); });
|
|
||||||
|
|
||||||
repaint();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void paintComponent(Graphics g) {
|
public void paintComponent(Graphics g) {
|
||||||
g.setColor(Color.LIGHT_GRAY);
|
g.setColor(state ? Color.GREEN : Color.LIGHT_GRAY);
|
||||||
g.fillRect(0, 0, 50, 25);
|
g.fillRect(0, 0, getWidth(), getHeight());
|
||||||
g.setColor(Color.GREEN);
|
g.setColor(state ? Color.LIGHT_GRAY
|
||||||
g.fillRect(0, 0, 25, 25);
|
: Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor());
|
||||||
|
g.fillRect(0, getWidth() / 2, getWidth(), getHeight());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
/**
|
|
||||||
* This method sets the state of this {@link PrimaryToggleSwitch}.
|
|
||||||
*
|
|
||||||
* @param state {@code true} to enable the switch, {@code false} to disable it
|
|
||||||
* @since Envoy 0.3-alpha
|
|
||||||
*/
|
|
||||||
private void setState(boolean state) {
|
|
||||||
GridBagConstraints gbc_toggleButton = new GridBagConstraints();
|
|
||||||
|
|
||||||
if (state) {
|
|
||||||
gbc_toggleButton.anchor = GridBagConstraints.WEST;
|
|
||||||
gbc_toggleButton.gridx = 0;
|
|
||||||
} else {
|
|
||||||
gbc_toggleButton.anchor = GridBagConstraints.EAST;
|
|
||||||
gbc_toggleButton.gridx = 1;
|
|
||||||
}
|
|
||||||
gbc_toggleButton.gridy = 0;
|
|
||||||
add(b, gbc_toggleButton);
|
|
||||||
|
|
||||||
currentState = state;
|
|
||||||
}
|
|
||||||
}
|
|
@ -4,10 +4,8 @@ import java.awt.GridBagConstraints;
|
|||||||
import java.awt.GridBagLayout;
|
import java.awt.GridBagLayout;
|
||||||
import java.awt.Insets;
|
import java.awt.Insets;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import javax.swing.JOptionPane;
|
|
||||||
import javax.swing.JTextPane;
|
import javax.swing.JTextPane;
|
||||||
|
|
||||||
import envoy.client.Settings;
|
import envoy.client.Settings;
|
||||||
|
Reference in New Issue
Block a user