142 lines
4.3 KiB
Java
142 lines
4.3 KiB
Java
package envoy.client.ui.settings;
|
|
|
|
import java.awt.GridBagConstraints;
|
|
import java.awt.GridBagLayout;
|
|
import java.awt.Insets;
|
|
import java.awt.event.ActionListener;
|
|
import java.util.logging.Logger;
|
|
|
|
import javax.swing.JOptionPane;
|
|
import javax.swing.JTextPane;
|
|
|
|
import envoy.client.Settings;
|
|
import envoy.client.event.EventBus;
|
|
import envoy.client.event.OnCloseChangeEvent;
|
|
import envoy.client.ui.PrimaryToggleSwitch;
|
|
import envoy.client.ui.Theme;
|
|
import envoy.client.util.EnvoyLog;
|
|
|
|
/**
|
|
* Displays GUI components that allow general settings regarding the client.<br>
|
|
* <br>
|
|
*
|
|
* Project: <strong>envoy-client</strong><br>
|
|
* File: <strong>General.java</strong><br>
|
|
* Created: <strong>21 Dec 2019</strong><br>
|
|
*
|
|
* @author Maximilian Käfer
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public class General extends SettingsPanel {
|
|
|
|
private static final long serialVersionUID = -7470848775130754239L;
|
|
private static final Logger logger = EnvoyLog.getLogger(General.class.getSimpleName());
|
|
|
|
private int state;
|
|
|
|
PrimaryToggleSwitch toggleSwitch;
|
|
JTextPane onCloseModeText = new JTextPane();
|
|
JTextPane onCloseModeState = new JTextPane();
|
|
|
|
/**
|
|
* This is the constructor for the General class. Here the user can set general
|
|
* settings for the client.
|
|
*
|
|
* @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");
|
|
}
|
|
|
|
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.columnWeights = new double[] { 1.0, 0.1 };
|
|
gbl_general.rowWeights = new double[] { 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);
|
|
|
|
EventBus.getInstance().register(OnCloseChangeEvent.class, (evt) -> changeOnClose(((OnCloseChangeEvent) evt).get()));
|
|
}
|
|
|
|
/**
|
|
* This method changes the on close mode of the client.
|
|
*
|
|
* @param state This is the integer that defines weather the toggleSwitch is on
|
|
* or off.
|
|
* @since Envoy v0.3-alpha
|
|
*/
|
|
public void changeOnClose(int state) {
|
|
System.out.println(state);
|
|
this.state = state;
|
|
|
|
if (state == 0) {
|
|
onCloseModeState.setText("OFF");
|
|
} else {
|
|
onCloseModeState.setText("ON");
|
|
}
|
|
this.revalidate();
|
|
this.repaint();
|
|
}
|
|
|
|
@Override
|
|
public ActionListener getOkButtonAction() {
|
|
return (evt) -> {
|
|
if (state != Settings.getInstance().getCurrentOnCloseMode()) {
|
|
try {
|
|
Settings.getInstance().setCurrentOnCloseMode(state);
|
|
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();
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
}
|