Made PrimaryToggleSwitch a sub class of JButton

This commit is contained in:
Kai S. K. Engelbart 2019-12-23 15:35:27 +01:00
parent 3020125334
commit 66cf42e0d9
2 changed files with 14 additions and 56 deletions

View File

@ -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<Boolean> 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;
}
}
}

View File

@ -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;