Adjusting PrimaryToggleSwitch implementation to SettingsItem class

This commit is contained in:
Kai S. K. Engelbart 2019-12-23 14:51:52 +01:00
parent 035b3c02e4
commit 3d381f4351
3 changed files with 36 additions and 57 deletions

View File

@ -163,6 +163,16 @@ public class Settings {
@SuppressWarnings("unchecked")
public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem<Boolean>) items.get("onCloseMode")).set(currentOnCloseMode); }
/**
* @return the items
*/
public Map<String, SettingsItem<?>> getItems() { return items; }
/**
* @param items the items to set
*/
public void setItems(Map<String, SettingsItem<?>> items) { this.items = items; }
/**
* @return a {@code Map<String, Theme>} of all themes with their names as keys
* @since Envoy v0.2-alpha

View File

@ -1,16 +1,12 @@
package envoy.client.ui;
import java.awt.*;
import java.lang.reflect.Constructor;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JPanel;
import envoy.client.Settings;
import envoy.client.event.Event;
import envoy.client.event.EventBus;
import envoy.client.util.EnvoyLog;
import envoy.client.SettingsItem;
/**
* This Component can be used to toggle between two options. e.g. on and
@ -25,23 +21,19 @@ import envoy.client.util.EnvoyLog;
*/
public class PrimaryToggleSwitch extends JPanel {
private final JButton b = new JButton("");
private final JButton b = new JButton();
private boolean currentState;
private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
private static final long serialVersionUID = -721155303106833184L;
/**
* This is the constructor for the PrimaryToggleSwitch.
*
* @param initialState The state the toggleSwitch is standardly set to. </br>
* true: off </br>
* false: on
* @param eventClass the class of the event dispatched by this toggleSwitch
* @param settingsItem
* @since Envoy v0.3-alpha
*/
public PrimaryToggleSwitch(boolean initialState, Class<? extends Event<Boolean>> eventClass) {
public PrimaryToggleSwitch(SettingsItem<Boolean> settingsItem) {
setEnabled(true);
setVisible(true);
@ -63,20 +55,13 @@ public class PrimaryToggleSwitch extends JPanel {
setLayout(gbl_toggleSwitch);
setState(initialState);
setState(settingsItem.get());
b.addActionListener((evt) -> {
try {
// Dispatch event
Constructor<? extends Event<Boolean>> constructor = eventClass.getConstructor(boolean.class);
EventBus.getInstance().dispatch(constructor.newInstance(currentState));
setState(!currentState);
revalidate();
repaint();
} catch (ReflectiveOperationException | SecurityException e) {
logger.warning("An error occured while changing the setting: " + e);
}
settingsItem.set(!currentState);
setState(!currentState);
revalidate();
repaint();
});
repaint();
@ -96,7 +81,7 @@ public class PrimaryToggleSwitch extends JPanel {
* @param state {@code true} to enable the switch, {@code false} to disable it
* @since Envoy 0.3-alpha
*/
public void setState(boolean state) {
private void setState(boolean state) {
GridBagConstraints gbc_toggleButton = new GridBagConstraints();
if (state) {

View File

@ -11,7 +11,7 @@ import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import envoy.client.Settings;
import envoy.client.event.*;
import envoy.client.SettingsItem;
import envoy.client.ui.PrimaryToggleSwitch;
import envoy.client.ui.Theme;
import envoy.client.util.EnvoyLog;
@ -31,15 +31,7 @@ public class General extends SettingsPanel {
private Theme theme;
private boolean onCloseState = Settings.getInstance().getCurrentOnCloseMode();
private boolean enterToSend = Settings.getInstance().isEnterToSend();
private PrimaryToggleSwitch toggleSwitch;
private JTextPane onCloseModeTextPane = new JTextPane();
private JTextPane onCloseModeStatePane = new JTextPane();
private PrimaryToggleSwitch toggleSwitchEnterToSend;
private JTextPane enterToSendTextPane = new JTextPane();
private JTextPane enterToSendStatePane = new JTextPane();
private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName());
private static final long serialVersionUID = -7470848775130754239L;
@ -61,26 +53,16 @@ public class General extends SettingsPanel {
gbl_general.rowWeights = new double[] { 0.02, 0.0005, 0.02, 0.0005, 1.0 };
setLayout(gbl_general);
createSettingElement(0,
OnCloseChangeEvent.class,
Settings.getInstance().getCurrentOnCloseMode(),
toggleSwitch,
onCloseModeStatePane,
onCloseModeTextPane,
"Client runs in the background, when window is closed");
EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get()));
(SettingsItem<Boolean>) Settings.getInstance().getItems().get("onCloseMode"));
createSettingElement(2,
EnterToSendEvent.class,
Settings.getInstance().isEnterToSend(),
toggleSwitchEnterToSend,
enterToSendStatePane,
enterToSendTextPane,
"Press Enter to send messages");
EventBus.getInstance().register(EnterToSendEvent.class, (evt) -> changeEnterToSend(((EnterToSendEvent) evt).get()));
(SettingsItem<Boolean>) Settings.getInstance().getItems().get("enterToSend"));
}
// TODO: Move ON / OFF text to PrimaryToggleswitch
/**
* This method changes the on close mode of the client.
*
@ -91,7 +73,7 @@ public class General extends SettingsPanel {
public void changeOnClose(boolean state) {
this.onCloseState = state;
onCloseModeStatePane.setText(state ? "ON" : "OFF");
//onCloseModeStatePane.setText(state ? "ON" : "OFF");
revalidate();
repaint();
}
@ -106,14 +88,16 @@ public class General extends SettingsPanel {
public void changeEnterToSend(boolean state) {
this.enterToSend = state;
enterToSendStatePane.setText(state ? "ON" : "OFF");
//enterToSendStatePane.setText(state ? "ON" : "OFF");
revalidate();
repaint();
}
private void createSettingElement(int gridy, Class<? extends Event<Boolean>> eventClass, boolean state, PrimaryToggleSwitch toggleSwitch,
JTextPane stateText, JTextPane descriptionText, String text) {
toggleSwitch = new PrimaryToggleSwitch(state, eventClass);
private void createSettingElement(int gridy, SettingsItem<Boolean> settingsItem) {
JTextPane stateText = new JTextPane();
JTextPane descriptionText = new JTextPane();
PrimaryToggleSwitch toggleSwitch = new PrimaryToggleSwitch(settingsItem);
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
gbc_toggleSwitch.gridx = 1;
@ -121,7 +105,7 @@ public class General extends SettingsPanel {
add(toggleSwitch, gbc_toggleSwitch);
stateText.setText(state ? "ON" : "OFF");
stateText.setText(settingsItem.get() ? "ON" : "OFF");
stateText.setBackground(theme.getCellColor());
stateText.setForeground(theme.getUserNameColor());
stateText.setEditable(false);
@ -133,7 +117,7 @@ public class General extends SettingsPanel {
add(stateText, gbc_stateText);
descriptionText.setText(text);
descriptionText.setText(settingsItem.getDescription());
descriptionText.setBackground(theme.getBackgroundColor());
descriptionText.setForeground(theme.getBackgroundColor().invert());
descriptionText.setEditable(false);