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 141b2371cc
commit 5090e81b56
6 changed files with 154 additions and 74 deletions

View File

@ -28,7 +28,7 @@ public class Settings {
private boolean enterToSend = true;
private Map<String, Theme> themes;
private String currentTheme;
private int currentOnCloseMode;
private boolean currentOnCloseMode;
/**
* Required to save the settings.
@ -65,7 +65,7 @@ public class Settings {
private void load() {
setEnterToSend(prefs.getBoolean("enterToSend", true));
setCurrentTheme(prefs.get("theme", "dark"));
setCurrentOnCloseMode(prefs.getInt("onCloseMode", 1));
setCurrentOnCloseMode(prefs.getBoolean("onCloseMode", true));
// Load themes from theme file
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(themeFile))) {
@ -96,7 +96,7 @@ public class Settings {
public void save() throws IOException {
prefs.put("theme", currentTheme);
prefs.putBoolean("enterToSend", isEnterToSend());
prefs.putInt("onCloseMode", currentOnCloseMode);
prefs.putBoolean("onCloseMode", currentOnCloseMode);
// Save themes to theme file
themeFile.createNewFile();
@ -163,7 +163,7 @@ public class Settings {
* @return the current on close mode.
* @since Envoy v0.3-alpha
*/
public int getCurrentOnCloseMode() { return currentOnCloseMode; }
public boolean getCurrentOnCloseMode() { return currentOnCloseMode; }
/**
* Sets the current on close mode.
@ -171,5 +171,5 @@ public class Settings {
* @param currentOnCloseMode the on close mode that should be set.
* @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
* @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
* set.
* </br>
* 0 = ExitOnClose </br>
* 1 = HideOnClose
* true = ExitOnClose </br>
* false = HideOnClose
* @since Envoy 0.3-alpha
*/
public OnCloseChangeEvent(int closeMode) { this.closeMode = closeMode; }
public OnCloseChangeEvent(boolean closeMode) { this.closeMode = closeMode; }
@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;
JButton b = new JButton("");
private boolean currentState;
private int variable;
private boolean variable;
private static final Logger logger = EnvoyLog.getLogger(PrimaryToggleSwitch.class.getSimpleName());
/**
@ -69,7 +69,7 @@ public class PrimaryToggleSwitch extends JPanel {
b.addActionListener((evt) -> {
try {
Class<?> c = Class.forName(eventName);
Class[] types = { int.class };
Class[] types = { boolean.class };
Constructor constructor = c.getConstructor(types);
Object[] parameters = { variable };
@ -113,7 +113,7 @@ public class PrimaryToggleSwitch extends JPanel {
add(b, gbc_toggleButton);
currentState = true;
variable = 1;
variable = true;
} else {
GridBagConstraints gbc_toggleButton = new GridBagConstraints();
gbc_toggleButton.anchor = GridBagConstraints.EAST;
@ -122,7 +122,7 @@ public class PrimaryToggleSwitch extends JPanel {
add(b, gbc_toggleButton);
currentState = false;
variable = 0;
variable = false;
}
}
}

View File

@ -132,7 +132,7 @@ public class Startup {
new StatusTrayIcon(chatWindow).show();
// 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);
} else {
chatWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

View File

@ -10,6 +10,7 @@ import javax.swing.JOptionPane;
import javax.swing.JTextPane;
import envoy.client.Settings;
import envoy.client.event.EnterToSendEvent;
import envoy.client.event.EventBus;
import envoy.client.event.OnCloseChangeEvent;
import envoy.client.ui.PrimaryToggleSwitch;
@ -31,12 +32,17 @@ public class General extends SettingsPanel {
private static final long serialVersionUID = -7470848775130754239L;
private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName());
private int state;
private Theme theme;
private boolean onCloseState;
private boolean enterToSend;
PrimaryToggleSwitch toggleSwitch;
JTextPane onCloseModeText = new JTextPane();
JTextPane onCloseModeState = new JTextPane();
JTextPane onCloseModeTextPane = 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
@ -45,62 +51,35 @@ public class General extends SettingsPanel {
* @since Envoy 0.3-alpha
*/
public General() {
Theme 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");
}
theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
setBackground(theme.getCellColor());
GridBagLayout gbl_general = new GridBagLayout();
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.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);
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
gbc_toggleSwitch.gridx = 1;
gbc_toggleSwitch.gridy = 0;
add(toggleSwitch, gbc_toggleSwitch);
if (state == 0) {
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);
createSettingElement(0,
"envoy.client.event.OnCloseChangeEvent",
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,
"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.
* @since Envoy v0.3-alpha
*/
public void changeOnClose(int state) {
System.out.println(state);
this.state = state;
public void changeOnClose(boolean state) {
this.onCloseState = state;
if (state == 0) {
onCloseModeState.setText("OFF");
if (state == false) {
onCloseModeStatePane.setText("OFF");
} else {
onCloseModeState.setText("ON");
onCloseModeStatePane.setText("ON");
}
this.revalidate();
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
public ActionListener getOkButtonAction() {
return (evt) -> {
if (state != Settings.getInstance().getCurrentOnCloseMode()) {
if (onCloseState != Settings.getInstance().getCurrentOnCloseMode()) {
try {
Settings.getInstance().setCurrentOnCloseMode(state);
Settings.getInstance().setCurrentOnCloseMode(onCloseState);
JOptionPane.showMessageDialog(null, "The changes will take effect the next time the program is started.");
} catch (Exception e) {
logger.info("Close mode could not be changed! " + e);
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();
}
}
};
}