Enter to Send and Revising

* Revised style and improved object architecture
* Added Enter to Send mechanism.
This commit is contained in:
DieGurke 2019-12-23 00:03:22 +01:00
parent 56afbc5422
commit b0b9f63861
6 changed files with 154 additions and 74 deletions

View File

@ -28,7 +28,7 @@ public class Settings {
private boolean enterToSend = true; private boolean enterToSend = true;
private Map<String, Theme> themes; private Map<String, Theme> themes;
private String currentTheme; private String currentTheme;
private int currentOnCloseMode; private boolean currentOnCloseMode;
/** /**
* Required to save the settings. * Required to save the settings.
@ -65,7 +65,7 @@ public class Settings {
private void load() { private void load() {
setEnterToSend(prefs.getBoolean("enterToSend", true)); setEnterToSend(prefs.getBoolean("enterToSend", true));
setCurrentTheme(prefs.get("theme", "dark")); setCurrentTheme(prefs.get("theme", "dark"));
setCurrentOnCloseMode(prefs.getInt("onCloseMode", 1)); setCurrentOnCloseMode(prefs.getBoolean("onCloseMode", true));
// Load themes from theme file // Load themes from theme file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) { try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) {
@ -96,7 +96,7 @@ public class Settings {
public void save() throws IOException { public void save() throws IOException {
prefs.put("theme", currentTheme); prefs.put("theme", currentTheme);
prefs.putBoolean("enterToSend", isEnterToSend()); prefs.putBoolean("enterToSend", isEnterToSend());
prefs.putInt("onCloseMode", currentOnCloseMode); prefs.putBoolean("onCloseMode", currentOnCloseMode);
// Save themes to theme file // Save themes to theme file
themeFile.createNewFile(); themeFile.createNewFile();
@ -163,7 +163,7 @@ public class Settings {
* @return the current on close mode. * @return the current on close mode.
* @since Envoy v0.3-alpha * @since Envoy v0.3-alpha
*/ */
public int getCurrentOnCloseMode() { return currentOnCloseMode; } public boolean getCurrentOnCloseMode() { return currentOnCloseMode; }
/** /**
* Sets the current on close mode. * Sets the current on close mode.
@ -171,5 +171,5 @@ public class Settings {
* @param currentOnCloseMode the on close mode that should be set. * @param currentOnCloseMode the on close mode that should be set.
* @since Envoy v0.3-alpha * @since Envoy v0.3-alpha
*/ */
public void setCurrentOnCloseMode(int currentOnCloseMode) { this.currentOnCloseMode = currentOnCloseMode; } public void setCurrentOnCloseMode(boolean currentOnCloseMode) { this.currentOnCloseMode = currentOnCloseMode; }
} }

View File

@ -0,0 +1,27 @@
package envoy.client.event;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>EnterToSendEvent.java</strong><br>
* Created: <strong>22 Dec 2019</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy v0.3-alpha
*/
public class EnterToSendEvent implements Event<Boolean> {
private boolean mode;
/**
* @param mode This is the enter to sent mode when sending messages.
* </br>
* true = Enter to Send Messages </br>
* false = Enter to do a line break
* @since Envoy 0.3-alpha
*/
public EnterToSendEvent(boolean mode) { this.mode = mode; }
@Override
public Boolean get() { return mode; }
}

View File

@ -8,21 +8,21 @@ package envoy.client.event;
* @author Maximilian K&auml;fer * @author Maximilian K&auml;fer
* @since Envoy v0.3-alpha * @since Envoy v0.3-alpha
*/ */
public class OnCloseChangeEvent implements Event<Integer> { public class OnCloseChangeEvent implements Event<Boolean> {
private Integer closeMode; private boolean closeMode;
/** /**
* @param closeMode This is the on close mode for the client, that should be * @param closeMode This is the on close mode for the client, that should be
* set. * set.
* </br> * </br>
* 0 = ExitOnClose </br> * true = ExitOnClose </br>
* 1 = HideOnClose * false = HideOnClose
* @since Envoy 0.3-alpha * @since Envoy 0.3-alpha
*/ */
public OnCloseChangeEvent(int closeMode) { this.closeMode = closeMode; } public OnCloseChangeEvent(boolean closeMode) { this.closeMode = closeMode; }
@Override @Override
public Integer get() { return closeMode; } public Boolean get() { return closeMode; }
} }

View File

@ -28,7 +28,7 @@ public class PrimaryToggleSwitch extends JPanel {
private static final long serialVersionUID = -721155303106833184L; private static final long serialVersionUID = -721155303106833184L;
JButton b = new JButton(""); JButton b = new JButton("");
private boolean currentState; private boolean currentState;
private int variable; private boolean variable;
private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
/** /**
@ -69,7 +69,7 @@ public class PrimaryToggleSwitch extends JPanel {
b.addActionListener((evt) -> { b.addActionListener((evt) -> {
try { try {
Class<?> c = Class.forName(eventName); Class<?> c = Class.forName(eventName);
Class[] types = { int.class }; Class[] types = { boolean.class };
Constructor constructor = c.getConstructor(types); Constructor constructor = c.getConstructor(types);
Object[] parameters = { variable }; Object[] parameters = { variable };
@ -113,7 +113,7 @@ public class PrimaryToggleSwitch extends JPanel {
add(b, gbc_toggleButton); add(b, gbc_toggleButton);
currentState = true; currentState = true;
variable = 1; variable = true;
} else { } else {
GridBagConstraints gbc_toggleButton = new GridBagConstraints(); GridBagConstraints gbc_toggleButton = new GridBagConstraints();
gbc_toggleButton.anchor = GridBagConstraints.EAST; gbc_toggleButton.anchor = GridBagConstraints.EAST;
@ -122,7 +122,7 @@ public class PrimaryToggleSwitch extends JPanel {
add(b, gbc_toggleButton); add(b, gbc_toggleButton);
currentState = false; currentState = false;
variable = 0; variable = false;
} }
} }
} }

View File

@ -132,7 +132,7 @@ public class Startup {
new StatusTrayIcon(chatWindow).show(); new StatusTrayIcon(chatWindow).show();
// If the tray icon is supported, hide the chat window on close // If the tray icon is supported, hide the chat window on close
if (Settings.getInstance().getCurrentOnCloseMode() == 1) { if (Settings.getInstance().getCurrentOnCloseMode() == true) {
chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
} else { } else {
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

View File

@ -10,6 +10,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.EnterToSendEvent;
import envoy.client.event.EventBus; import envoy.client.event.EventBus;
import envoy.client.event.OnCloseChangeEvent; import envoy.client.event.OnCloseChangeEvent;
import envoy.client.ui.PrimaryToggleSwitch; import envoy.client.ui.PrimaryToggleSwitch;
@ -31,12 +32,17 @@ public class General extends SettingsPanel {
private static final long serialVersionUID = -7470848775130754239L; private static final long serialVersionUID = -7470848775130754239L;
private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName()); private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName());
private Theme theme;
private int state; private boolean onCloseState;
private boolean enterToSend;
PrimaryToggleSwitch toggleSwitch; PrimaryToggleSwitch toggleSwitch;
JTextPane onCloseModeText = new JTextPane(); JTextPane onCloseModeTextPane = new JTextPane();
JTextPane onCloseModeState = new JTextPane(); JTextPane onCloseModeStatePane = new JTextPane();
PrimaryToggleSwitch toggleSwitchEnterToSend;
JTextPane enterToSendTextPane = new JTextPane();
JTextPane enterToSendStatePane = new JTextPane();
/** /**
* This is the constructor for the General class. Here the user can set general * This is the constructor for the General class. Here the user can set general
@ -45,62 +51,35 @@ public class General extends SettingsPanel {
* @since Envoy 0.3-alpha * @since Envoy 0.3-alpha
*/ */
public General() { public General() {
Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
state = Settings.getInstance().getCurrentOnCloseMode();
if (state == 1) {
toggleSwitch = new PrimaryToggleSwitch(false, "envoy.client.event.OnCloseChangeEvent");
} else {
toggleSwitch = new PrimaryToggleSwitch(true, "envoy.client.event.OnCloseChangeEvent");
}
setBackground(theme.getCellColor()); setBackground(theme.getCellColor());
GridBagLayout gbl_general = new GridBagLayout(); GridBagLayout gbl_general = new GridBagLayout();
gbl_general.columnWidths = new int[] { 1, 1 }; gbl_general.columnWidths = new int[] { 1, 1 };
gbl_general.rowHeights = new int[] { 1, 1, 1 }; gbl_general.rowHeights = new int[] { 1, 1, 1, 1, 1 };
gbl_general.columnWeights = new double[] { 1.0, 0.1 }; gbl_general.columnWeights = new double[] { 1.0, 0.1 };
gbl_general.rowWeights = new double[] { 0.02, 0.0005, 1.0 }; gbl_general.rowWeights = new double[] { 0.02, 0.0005, 0.02, 0.0005, 1.0 };
setLayout(gbl_general); setLayout(gbl_general);
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); createSettingElement(0,
gbc_toggleSwitch.gridx = 1; "envoy.client.event.OnCloseChangeEvent",
gbc_toggleSwitch.gridy = 0; Settings.getInstance().getCurrentOnCloseMode(),
toggleSwitch,
add(toggleSwitch, gbc_toggleSwitch); onCloseModeStatePane,
onCloseModeTextPane,
if (state == 0) { "Client runs in the background, when window is closed");
onCloseModeState.setText("OFF");
} else {
onCloseModeState.setText("ON");
}
onCloseModeState.setBackground(theme.getCellColor());
onCloseModeState.setForeground(theme.getUserNameColor());
GridBagConstraints gbc_onCloseModeState = new GridBagConstraints();
gbc_onCloseModeState.anchor = GridBagConstraints.NORTH;
gbc_onCloseModeState.gridx = 1;
gbc_onCloseModeState.gridy = 1;
add(onCloseModeState, gbc_onCloseModeState);
onCloseModeText.setText("Client runs in the background, when window is closed");
onCloseModeText.setBackground(theme.getBackgroundColor());
// TODO: Change to inverted color.
onCloseModeText.setForeground(theme.getUserNameColor());
GridBagConstraints gbc_onCloseModeText = new GridBagConstraints();
gbc_onCloseModeText.fill = GridBagConstraints.BOTH;
gbc_onCloseModeText.gridx = 0;
gbc_onCloseModeText.gridy = 0;
gbc_onCloseModeText.gridheight = 2;
gbc_onCloseModeText.insets = new Insets(5, 5, 5, 5);
add(onCloseModeText, gbc_onCloseModeText);
EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get())); EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get()));
createSettingElement(2,
"envoy.client.event.EnterToSendEvent",
Settings.getInstance().isEnterToSend(),
toggleSwitchEnterToSend,
enterToSendStatePane,
enterToSendTextPane,
"Press Enter to send messages");
EventBus.getInstance().register(EnterToSendEvent.class, (evt) -> changeEnterToSend(((EnterToSendEvent) evt).get()));
} }
/** /**
@ -110,31 +89,105 @@ public class General extends SettingsPanel {
* or off. * or off.
* @since Envoy v0.3-alpha * @since Envoy v0.3-alpha
*/ */
public void changeOnClose(int state) { public void changeOnClose(boolean state) {
System.out.println(state); this.onCloseState = state;
this.state = state;
if (state == 0) { if (state == false) {
onCloseModeState.setText("OFF"); onCloseModeStatePane.setText("OFF");
} else { } else {
onCloseModeState.setText("ON"); onCloseModeStatePane.setText("ON");
} }
this.revalidate(); this.revalidate();
this.repaint(); this.repaint();
} }
/**
* This method changes the enter to send a message setting.
*
* @param state This is the integer that defines weather the toggleSwitch is on
* or off.
* @since Envoy v0.3-alpha
*/
public void changeEnterToSend(boolean state) {
this.enterToSend = state;
if (state == false) {
enterToSendStatePane.setText("OFF");
} else {
enterToSendStatePane.setText("ON");
}
this.revalidate();
this.repaint();
}
private void createSettingElement(int gridy, String eventPath, boolean state, PrimaryToggleSwitch toggleSwitch, JTextPane stateText,
JTextPane descriptionText, String text) {
if (state == true) {
toggleSwitch = new PrimaryToggleSwitch(false, eventPath);
} else {
toggleSwitch = new PrimaryToggleSwitch(true, eventPath);
}
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
gbc_toggleSwitch.gridx = 1;
gbc_toggleSwitch.gridy = gridy;
add(toggleSwitch, gbc_toggleSwitch);
if (state == false) {
stateText.setText("OFF");
} else {
stateText.setText("ON");
}
stateText.setBackground(theme.getCellColor());
stateText.setForeground(theme.getUserNameColor());
GridBagConstraints gbc_stateText = new GridBagConstraints();
gbc_stateText.anchor = GridBagConstraints.NORTH;
gbc_stateText.gridx = 1;
gbc_stateText.gridy = gridy + 1;
add(stateText, gbc_stateText);
descriptionText.setText(text);
descriptionText.setBackground(theme.getBackgroundColor());
// TODO: Change to inverted color.
descriptionText.setForeground(theme.getUserNameColor());
GridBagConstraints gbc_descriptionText = new GridBagConstraints();
gbc_descriptionText.fill = GridBagConstraints.BOTH;
gbc_descriptionText.gridx = 0;
gbc_descriptionText.gridy = gridy;
gbc_descriptionText.gridheight = 2;
gbc_descriptionText.insets = new Insets(5, 5, 5, 5);
add(descriptionText, gbc_descriptionText);
}
@Override @Override
public ActionListener getOkButtonAction() { public ActionListener getOkButtonAction() {
return (evt) -> { return (evt) -> {
if (state != Settings.getInstance().getCurrentOnCloseMode()) { if (onCloseState != Settings.getInstance().getCurrentOnCloseMode()) {
try { try {
Settings.getInstance().setCurrentOnCloseMode(state); Settings.getInstance().setCurrentOnCloseMode(onCloseState);
JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started."); JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started.");
} catch (Exception e) { } catch (Exception e) {
logger.info("Close mode could not be changed! " + e); logger.info("Close mode could not be changed! " + e);
e.printStackTrace(); e.printStackTrace();
} }
} }
if (enterToSend != Settings.getInstance().isEnterToSend()) {
try {
Settings.getInstance().setEnterToSend(enterToSend);
;
JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started.");
} catch (Exception e) {
logger.info("Enter to send mode could not be changed! " + e);
e.printStackTrace();
}
}
}; };
} }