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") @SuppressWarnings("unchecked")
public void setCurrentOnCloseMode(boolean currentOnCloseMode) { ((SettingsItem<Boolean>) items.get("onCloseMode")).set(currentOnCloseMode); } 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 * @return a {@code Map<String, Theme>} of all themes with their names as keys
* @since Envoy v0.2-alpha * @since Envoy v0.2-alpha

View File

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

View File

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