From 66cf42e0d92264e0e151ffbff41a751ef6e18af4 Mon Sep 17 00:00:00 2001 From: kske Date: Mon, 23 Dec 2019 15:35:27 +0100 Subject: [PATCH] Made PrimaryToggleSwitch a sub class of JButton --- .../envoy/client/ui/PrimaryToggleSwitch.java | 68 ++++--------------- .../envoy/client/ui/settings/General.java | 2 - 2 files changed, 14 insertions(+), 56 deletions(-) diff --git a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java index 7319fe6..d6a23cc 100644 --- a/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java +++ b/src/main/java/envoy/client/ui/PrimaryToggleSwitch.java @@ -1,9 +1,9 @@ package envoy.client.ui; -import java.awt.*; +import java.awt.Dimension; +import java.awt.Graphics; import javax.swing.JButton; -import javax.swing.JPanel; import envoy.client.Settings; import envoy.client.SettingsItem; @@ -19,11 +19,9 @@ import envoy.client.SettingsItem; * @author Maximilian Käfer * @since Envoy v0.3-alpha */ -public class PrimaryToggleSwitch extends JPanel { +public class PrimaryToggleSwitch extends JButton { - private final JButton b = new JButton(); - - private boolean currentState; + private boolean state; private static final long serialVersionUID = -721155303106833184L; @@ -34,61 +32,23 @@ public class PrimaryToggleSwitch extends JPanel { * @since Envoy v0.3-alpha */ public PrimaryToggleSwitch(SettingsItem settingsItem) { - 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)); + setBorderPainted(false); + setFocusPainted(false); + setContentAreaFilled(false); - 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(settingsItem.get()); - - b.addActionListener((evt) -> { settingsItem.set(!currentState); setState(!currentState); revalidate(); repaint(); }); - - repaint(); + addActionListener((evt) -> { state = !state; settingsItem.set(state); revalidate(); repaint(); }); } @Override 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); + g.setColor(state ? Color.GREEN : Color.LIGHT_GRAY); + g.fillRect(0, 0, getWidth(), getHeight()); + g.setColor(state ? Color.LIGHT_GRAY + : 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; - } -} +} \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/settings/General.java b/src/main/java/envoy/client/ui/settings/General.java index e4b5ae3..9b6339a 100644 --- a/src/main/java/envoy/client/ui/settings/General.java +++ b/src/main/java/envoy/client/ui/settings/General.java @@ -4,10 +4,8 @@ import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.event.ActionListener; -import java.util.logging.Level; import java.util.logging.Logger; -import javax.swing.JOptionPane; import javax.swing.JTextPane; import envoy.client.Settings;