Polished settings mechanism
* Set initial value of onCloseMode setting to true * Added setting change handlers * Applying settings changes immediately * Made PrimaryToggleSwitch round
This commit is contained in:
		| @@ -102,7 +102,7 @@ public class Settings { | ||||
|  | ||||
| 	private void supplementDefaults() { | ||||
| 		items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key.")); | ||||
| 		items.putIfAbsent("onCloseMode", new SettingsItem<>(false, "Hide on close", "Hides the chat window when it is closed.")); | ||||
| 		items.putIfAbsent("onCloseMode", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed.")); | ||||
| 		items.putIfAbsent("currentTheme", new SettingsItem<>("dark", null)); | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -3,6 +3,7 @@ package envoy.client; | ||||
| import java.io.Serializable; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.function.Consumer; | ||||
|  | ||||
| import javax.swing.JComponent; | ||||
|  | ||||
| @@ -21,12 +22,14 @@ public class SettingsItem<T> implements Serializable { | ||||
| 	private Class<? extends JComponent>	componentClass; | ||||
| 	private String						userFriendlyName, description; | ||||
|  | ||||
| 	transient private Consumer<T> changeHandler; | ||||
|  | ||||
| 	private static final Map<Class<?>, Class<? extends JComponent>> componentClasses = new HashMap<>(); | ||||
| 	 | ||||
|  | ||||
| 	private static final long serialVersionUID = 2146837835556852218L; | ||||
|  | ||||
| 	static { | ||||
| 		componentClasses.put(boolean.class, PrimaryToggleSwitch.class); | ||||
| 		componentClasses.put(Boolean.class, PrimaryToggleSwitch.class); | ||||
| 	} | ||||
|  | ||||
| 	public SettingsItem(T value, String userFriendlyName, String description) { | ||||
| @@ -53,7 +56,11 @@ public class SettingsItem<T> implements Serializable { | ||||
| 	/** | ||||
| 	 * @param value the value to set | ||||
| 	 */ | ||||
| 	public void set(T value) { this.value = value; } | ||||
| 	public void set(T value) { | ||||
| 		if(changeHandler != null && value != this.value) | ||||
| 			changeHandler.accept(value); | ||||
| 		this.value = value; | ||||
| 	} | ||||
|  | ||||
| 	/** | ||||
| 	 * @return the componentClass | ||||
| @@ -84,4 +91,9 @@ public class SettingsItem<T> implements Serializable { | ||||
| 	 * @param description the description to set | ||||
| 	 */ | ||||
| 	public void setDescription(String description) { this.description = description; } | ||||
|  | ||||
| 	/** | ||||
| 	 * @param changeHandler the changeHandler to set | ||||
| 	 */ | ||||
| 	public void setChangeHandler(Consumer<T> changeHandler) { this.changeHandler = changeHandler; changeHandler.accept(value); } | ||||
| } | ||||
| @@ -1,27 +0,0 @@ | ||||
| package envoy.client.event; | ||||
|  | ||||
| /** | ||||
|  * Encapsulates a change to the {@code enterToSend} setting.<br> | ||||
|  * <br> | ||||
|  * Project: <strong>envoy-client</strong><br> | ||||
|  * File: <strong>EnterToSendEvent.java</strong><br> | ||||
|  * Created: <strong>22 Dec 2019</strong><br> | ||||
|  * | ||||
|  * @author Maximilian Käfer | ||||
|  * @since Envoy v0.3-alpha | ||||
|  */ | ||||
| public class EnterToSendEvent implements Event<Boolean> { | ||||
|  | ||||
| 	private boolean mode; | ||||
|  | ||||
| 	/** | ||||
| 	 * Initializes an {@link EnterToSendEvent}. | ||||
| 	 *  | ||||
| 	 * @param mode the state of the {@code enterToSend} setting | ||||
| 	 * @since Envoy 0.3-alpha | ||||
| 	 */ | ||||
| 	public EnterToSendEvent(boolean mode) { this.mode = mode; } | ||||
|  | ||||
| 	@Override | ||||
| 	public Boolean get() { return mode; } | ||||
| } | ||||
| @@ -1,27 +0,0 @@ | ||||
| package envoy.client.event; | ||||
|  | ||||
| /** | ||||
|  * Encapsulates a change to the {@code currentOnCloseMode} setting.<br> | ||||
|  * <br> | ||||
|  * Project: <strong>envoy-client</strong><br> | ||||
|  * File: <strong>OnCloseChangeEvent.java</strong><br> | ||||
|  * Created: <strong>22 Dec 2019</strong><br> | ||||
|  * | ||||
|  * @author Maximilian Käfer | ||||
|  * @since Envoy v0.3-alpha | ||||
|  */ | ||||
| public class OnCloseChangeEvent implements Event<Boolean> { | ||||
|  | ||||
| 	private boolean closeMode; | ||||
|  | ||||
| 	/** | ||||
| 	 * Initializes an {@link OnCloseChangeEvent}. | ||||
| 	 *  | ||||
| 	 * @param closeMode the state of the {@code currentOnCloseMode} setting | ||||
| 	 * @since Envoy 0.3-alpha | ||||
| 	 */ | ||||
| 	public OnCloseChangeEvent(boolean closeMode) { this.closeMode = closeMode; } | ||||
|  | ||||
| 	@Override | ||||
| 	public Boolean get() { return closeMode; } | ||||
| } | ||||
| @@ -47,14 +47,13 @@ public class PrimaryToggleSwitch extends JButton { | ||||
| 	@Override | ||||
| 	public void paintComponent(Graphics g) { | ||||
| 		g.setColor(state ? Color.GREEN : Color.LIGHT_GRAY); | ||||
| 		g.fillRect(0, 0, getWidth(), getHeight()); | ||||
| 		g.fillRoundRect(0, 0, getWidth(), getHeight(), 25, 25); | ||||
|  | ||||
| 		g.setColor(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor()); | ||||
|  | ||||
| 		if (state) { | ||||
| 			g.fillRect(25, 0, 25, 25); | ||||
| 		} else { | ||||
| 			g.fillRect(0, 0, 25, 25); | ||||
| 		} | ||||
| 		if (state) | ||||
| 			g.fillRoundRect(25, 0, 25, 25, 25, 25); | ||||
| 		else | ||||
| 			g.fillRoundRect(0, 0, 25, 25, 25, 25); | ||||
| 	} | ||||
| } | ||||
| @@ -143,8 +143,7 @@ public class Startup { | ||||
| 					new StatusTrayIcon(chatWindow).show(); | ||||
|  | ||||
| 					// If the tray icon is supported and corresponding settings is set, hide the chat window on close | ||||
| 					if (Settings.getInstance().getCurrentOnCloseMode()) | ||||
| 						chatWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); | ||||
| 					Settings.getInstance().getItems().get("onCloseMode").setChangeHandler((onCloseMode) -> chatWindow.setDefaultCloseOperation((boolean) onCloseMode ? JFrame.HIDE_ON_CLOSE : JFrame.EXIT_ON_CLOSE));  | ||||
| 				} catch (EnvoyException e) { | ||||
| 					logger.warning("The StatusTrayIcon is not supported on this platform!"); | ||||
| 				} | ||||
|   | ||||
| @@ -4,13 +4,14 @@ import java.awt.GridBagConstraints; | ||||
| import java.awt.GridBagLayout; | ||||
| import java.awt.Insets; | ||||
| import java.awt.event.ActionListener; | ||||
| import java.util.logging.Level; | ||||
| import java.util.logging.Logger; | ||||
| 
 | ||||
| import javax.swing.JComponent; | ||||
| import javax.swing.JTextPane; | ||||
| 
 | ||||
| import envoy.client.Settings; | ||||
| import envoy.client.SettingsItem; | ||||
| import envoy.client.ui.PrimaryToggleSwitch; | ||||
| import envoy.client.ui.Theme; | ||||
| import envoy.client.util.EnvoyLog; | ||||
| 
 | ||||
| @@ -24,12 +25,13 @@ import envoy.client.util.EnvoyLog; | ||||
|  * @author Maximilian Käfer | ||||
|  * @since Envoy v0.3-alpha | ||||
|  */ | ||||
| public class General extends SettingsPanel { | ||||
| public class GeneralSettingsPanel extends SettingsPanel { | ||||
| 
 | ||||
| 	private Theme theme; | ||||
| 
 | ||||
| 	private static final Logger	logger				= EnvoyLog.getLogger(General.class.getSimpleName()); | ||||
| 	private static final long	serialVersionUID	= -7470848775130754239L; | ||||
| 	private static final String[]	items				= { "onCloseMode", "enterToSend" }; | ||||
| 	private static final Logger		logger				= EnvoyLog.getLogger(GeneralSettingsPanel.class.getSimpleName()); | ||||
| 	private static final long		serialVersionUID	= -7470848775130754239L; | ||||
| 
 | ||||
| 	/** | ||||
| 	 * This is the constructor for the General class. Here the user can set general | ||||
| @@ -37,7 +39,7 @@ public class General extends SettingsPanel { | ||||
| 	 *  | ||||
| 	 * @since Envoy 0.3-alpha | ||||
| 	 */ | ||||
| 	public General() { | ||||
| 	public GeneralSettingsPanel() { | ||||
| 		theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); | ||||
| 
 | ||||
| 		setBackground(theme.getCellColor()); | ||||
| @@ -50,21 +52,24 @@ public class General extends SettingsPanel { | ||||
| 
 | ||||
| 		setLayout(gbl_general); | ||||
| 
 | ||||
| 		createSettingElement(0, (SettingsItem<Boolean>) Settings.getInstance().getItems().get("onCloseMode")); | ||||
| 
 | ||||
| 		createSettingElement(1, (SettingsItem<Boolean>) Settings.getInstance().getItems().get("enterToSend")); | ||||
| 		for (int i = 0; i < items.length; i++) | ||||
| 			try { | ||||
| 				createSettingElement(i, Settings.getInstance().getItems().get(items[i])); | ||||
| 			} catch (SecurityException | ReflectiveOperationException e) { | ||||
| 				logger.log(Level.WARNING, "Could not create settings item", e); | ||||
| 			} | ||||
| 	} | ||||
| 
 | ||||
| 	private void createSettingElement(int gridy, SettingsItem<Boolean> settingsItem) { | ||||
| 	private void createSettingElement(int gridy, SettingsItem<?> settingsItem) throws SecurityException, ReflectiveOperationException { | ||||
| 		JTextPane descriptionText = new JTextPane(); | ||||
| 
 | ||||
| 		PrimaryToggleSwitch toggleSwitch = new PrimaryToggleSwitch(settingsItem); | ||||
| 		JComponent settingComponent = settingsItem.getComponent(); | ||||
| 
 | ||||
| 		GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); | ||||
| 		gbc_toggleSwitch.gridx	= 1; | ||||
| 		gbc_toggleSwitch.gridy	= gridy; | ||||
| 
 | ||||
| 		add(toggleSwitch, gbc_toggleSwitch); | ||||
| 		add(settingComponent, gbc_toggleSwitch); | ||||
| 
 | ||||
| 		descriptionText.setText(settingsItem.getDescription()); | ||||
| 		descriptionText.setBackground(theme.getBackgroundColor()); | ||||
| @@ -57,7 +57,7 @@ public class SettingsScreen extends JDialog { | ||||
| 	public SettingsScreen() { | ||||
| 		// Initialize settings pages | ||||
| 		Map<String, Class<? extends SettingsPanel>> panels = new HashMap<>(); | ||||
| 		panels.put("General", General.class); | ||||
| 		panels.put("General", GeneralSettingsPanel.class); | ||||
| 		panels.put("Color Themes", ThemeCustomizationPanel.class); | ||||
|  | ||||
| 		setBounds(10, 10, 450, 650); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user