From 156beaf44b24ee567d7078f3e9d88eeae2575d60 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 22 Mar 2020 17:05:28 +0100 Subject: [PATCH] Fixes #121 (first displayed theme is current theme) Additionally removed okButton from SettingsScreen --- .../envoy/client/ui/list/ComponentList.java | 6 +- .../ui/settings/GeneralSettingsPanel.java | 182 +++++++++--------- .../client/ui/settings/SettingsPanel.java | 11 +- .../client/ui/settings/SettingsScreen.java | 20 -- .../ui/settings/ThemeCustomizationPanel.java | 87 +++++---- 5 files changed, 146 insertions(+), 160 deletions(-) diff --git a/src/main/java/envoy/client/ui/list/ComponentList.java b/src/main/java/envoy/client/ui/list/ComponentList.java index 44d98de..68b92a2 100644 --- a/src/main/java/envoy/client/ui/list/ComponentList.java +++ b/src/main/java/envoy/client/ui/list/ComponentList.java @@ -110,7 +110,7 @@ public class ComponentList extends JPanel { /** * Removes the current selection. - * + * * @since Envoy v0.1-alpha */ public void clearSelection() { @@ -173,7 +173,7 @@ public class ComponentList extends JPanel { * @throws java.util.NoSuchElementException if no selection is present * @since Envoy v0.1-beta */ - public int getSingleSelection() { return selection.iterator().next(); } + public int getSingleSelection() { return selection.stream().findAny().get(); } /** * @return an arbitrary selected element @@ -235,7 +235,7 @@ public class ComponentList extends JPanel { /** * Sets a new selection mode. The current selection will be cleared during this * action. - * + * * @param selectionMode the selection mode to set * @return this component list * @since Envoy v0.1-beta diff --git a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java index fbc3952..ce40e91 100644 --- a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java @@ -1,93 +1,89 @@ -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.Level; -import java.util.logging.Logger; - -import javax.swing.JComponent; -import javax.swing.JTextPane; - -import envoy.client.data.Settings; -import envoy.client.data.SettingsItem; -import envoy.client.ui.Theme; -import envoy.util.EnvoyLog; - -/** - * Displays GUI components that allow general settings regarding the client.
- *
- * Project: envoy-client
- * File: GeneralSettingsPanel.java
- * Created: 21 Dec 2019
- * - * @author Maximilian Käfer - * @since Envoy v0.3-alpha - */ -public class GeneralSettingsPanel extends SettingsPanel { - - private Theme theme; - - private static final String[] items = { "onCloseMode", "enterToSend" }; - private static final Logger logger = EnvoyLog.getLogger(GeneralSettingsPanel.class); - private static final long serialVersionUID = -7470848775130754239L; - - /** - * This is the constructor for the General class. Here the user can set general - * settings for the client. - * - * @param parent the {@link SettingsScreen} as a part of which this - * {@link SettingsPanel} is displayed - * @since Envoy v0.3-alpha - */ - public GeneralSettingsPanel(SettingsScreen parent) { - super(parent); - theme = 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.columnWeights = new double[] { 1.0, 0.1 }; - gbl_general.rowWeights = new double[] { 0.02, 0.02, 1.0 }; - - setLayout(gbl_general); - - 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 settingsItem) throws SecurityException, ReflectiveOperationException { - JTextPane descriptionText = new JTextPane(); - - JComponent settingComponent = settingsItem.getComponent(); - - GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); - gbc_toggleSwitch.gridx = 1; - gbc_toggleSwitch.gridy = gridy; - - add(settingComponent, gbc_toggleSwitch); - - descriptionText.setText(settingsItem.getDescription()); - descriptionText.setBackground(theme.getBackgroundColor()); - descriptionText.setForeground(theme.getBackgroundColor().invert()); - descriptionText.setEditable(false); - - GridBagConstraints gbc_descriptionText = new GridBagConstraints(); - gbc_descriptionText.fill = GridBagConstraints.BOTH; - gbc_descriptionText.gridx = 0; - gbc_descriptionText.gridy = gridy; - gbc_descriptionText.insets = new Insets(5, 5, 5, 5); - - add(descriptionText, gbc_descriptionText); - } - - @Override - public ActionListener getOkButtonAction() { return evt -> {}; } -} +package envoy.client.ui.settings; + +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.swing.JComponent; +import javax.swing.JTextPane; + +import envoy.client.data.Settings; +import envoy.client.data.SettingsItem; +import envoy.client.ui.Theme; +import envoy.util.EnvoyLog; + +/** + * Displays GUI components that allow general settings regarding the client.
+ *
+ * Project: envoy-client
+ * File: GeneralSettingsPanel.java
+ * Created: 21 Dec 2019
+ * + * @author Maximilian Käfer + * @since Envoy v0.3-alpha + */ +public class GeneralSettingsPanel extends SettingsPanel { + + private Theme theme; + + private static final String[] items = { "onCloseMode", "enterToSend" }; + private static final Logger logger = EnvoyLog.getLogger(GeneralSettingsPanel.class); + private static final long serialVersionUID = -7470848775130754239L; + + /** + * This is the constructor for the General class. Here the user can set general + * settings for the client. + * + * @param parent the {@link SettingsScreen} as a part of which this + * {@link SettingsPanel} is displayed + * @since Envoy v0.3-alpha + */ + public GeneralSettingsPanel(SettingsScreen parent) { + super(parent); + theme = 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.columnWeights = new double[] { 1.0, 0.1 }; + gbl_general.rowWeights = new double[] { 0.02, 0.02, 1.0 }; + + setLayout(gbl_general); + + 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 settingsItem) throws SecurityException, ReflectiveOperationException { + JTextPane descriptionText = new JTextPane(); + + JComponent settingComponent = settingsItem.getComponent(); + + GridBagConstraints gbc_toggleSwitch = new GridBagConstraints(); + gbc_toggleSwitch.gridx = 1; + gbc_toggleSwitch.gridy = gridy; + + add(settingComponent, gbc_toggleSwitch); + + descriptionText.setText(settingsItem.getDescription()); + descriptionText.setBackground(theme.getBackgroundColor()); + descriptionText.setForeground(theme.getBackgroundColor().invert()); + descriptionText.setEditable(false); + + GridBagConstraints gbc_descriptionText = new GridBagConstraints(); + gbc_descriptionText.fill = GridBagConstraints.BOTH; + gbc_descriptionText.gridx = 0; + gbc_descriptionText.gridy = gridy; + gbc_descriptionText.insets = new Insets(5, 5, 5, 5); + + add(descriptionText, gbc_descriptionText); + } +} diff --git a/src/main/java/envoy/client/ui/settings/SettingsPanel.java b/src/main/java/envoy/client/ui/settings/SettingsPanel.java index 80469c5..421b774 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/SettingsPanel.java @@ -1,7 +1,5 @@ package envoy.client.ui.settings; -import java.awt.event.ActionListener; - import javax.swing.JPanel; /** @@ -24,16 +22,9 @@ public abstract class SettingsPanel extends JPanel { /** * Initializes a {@link SettingsPanel}. - * + * * @param parent the {@link SettingsScreen} as a part of which this * {@link SettingsPanel} is displayed */ public SettingsPanel(SettingsScreen parent) { this.parent = parent; } - - /** - * @return an {@link ActionListener} that should be invoked when the OK button - * is pressed in the {@link SettingsScreen} - * @since Envoy v0.2-alpha - */ - public abstract ActionListener getOkButtonAction(); } diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index 22a24a4..0c98084 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -40,7 +40,6 @@ public class SettingsScreen extends JDialog { // OK and cancel buttons private final JPanel buttonPane = new JPanel(); - private final PrimaryButton okButton = new PrimaryButton("Save"); private final PrimaryButton cancelButton = new PrimaryButton("Cancel"); private final Insets insets = new Insets(5, 5, 5, 5); @@ -138,21 +137,6 @@ public class SettingsScreen extends JDialog { cancelButton.addActionListener((evt) -> { dispose(); }); } - { - okButton.setActionCommand("OK"); - okButton.setBorderPainted(false); - GridBagConstraints gbc_okButton = new GridBagConstraints(); - gbc_okButton.anchor = GridBagConstraints.NORTHEAST; - gbc_okButton.fill = GridBagConstraints.EAST; - gbc_okButton.insets = insets; - gbc_okButton.gridx = 2; - gbc_okButton.gridy = 0; - buttonPane.add(okButton, gbc_okButton); - getRootPane().setDefaultButton(okButton); - - // Invoke settings panel action on button press - okButton.addActionListener((evt) -> { if (settingsPanel != null) settingsPanel.getOkButtonAction().actionPerformed(evt); }); - } } // Apply current theme @@ -179,10 +163,6 @@ public class SettingsScreen extends JDialog { cancelButton.setBackground(theme.getInteractableBackgroundColor()); cancelButton.setForeground(theme.getInteractableForegroundColor()); - // okButton - okButton.setBackground(theme.getInteractableBackgroundColor()); - okButton.setForeground(theme.getInteractableForegroundColor()); - // options options.setSelectionForeground(theme.getUserNameColor()); options.setSelectionBackground(theme.getSelectionColor()); diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 997dae5..a68a6a5 100755 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -1,7 +1,6 @@ package envoy.client.ui.settings; import java.awt.*; -import java.awt.event.ActionListener; import java.util.logging.Level; import java.util.logging.Logger; @@ -11,6 +10,7 @@ import envoy.client.data.Settings; import envoy.client.event.ThemeChangeEvent; import envoy.client.ui.Color; import envoy.client.ui.Theme; +import envoy.client.ui.primary.PrimaryButton; import envoy.event.EventBus; import envoy.util.EnvoyLog; @@ -30,13 +30,13 @@ public class ThemeCustomizationPanel extends SettingsPanel { private JPanel colorsPanel = new JPanel(); - private DefaultComboBoxModel themesModel = new DefaultComboBoxModel<>( - Settings.getInstance().getThemes().keySet().toArray(new String[0])); - private JComboBox themes = new JComboBox<>(themesModel); + private DefaultComboBoxModel themesModel; + private JComboBox themes; private Theme temporaryTheme; - private boolean themeChanged; + private PrimaryButton createThemeButton = new PrimaryButton("Create Theme"); - private final Insets insets = new Insets(5, 5, 5, 5); + private boolean themeChanged; + private final Insets insets = new Insets(5, 5, 5, 5); private static final Logger logger = EnvoyLog.getLogger(ThemeCustomizationPanel.class); private static final long serialVersionUID = -8697897390666456624L; @@ -54,12 +54,22 @@ public class ThemeCustomizationPanel extends SettingsPanel { super(parent); temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getCurrentTheme()); + var themeNames = Settings.getInstance().getThemes().keySet().toArray(new String[0]); + String currentThemeName = Settings.getInstance().getCurrentThemeName(); + for (int i = 0; i < themeNames.length; i++) + if (currentThemeName.equals(themeNames[i])) { + themeNames[i] = themeNames[0]; + themeNames[0] = currentThemeName; + break; + } + themesModel = new DefaultComboBoxModel<>(themeNames); + themes = new JComboBox<>(themesModel); GridBagLayout gbl_themeLayout = new GridBagLayout(); gbl_themeLayout.columnWidths = new int[] { 1, 1 }; - gbl_themeLayout.rowHeights = new int[] { 1, 1 }; + gbl_themeLayout.rowHeights = new int[] { 1, 1, 1 }; gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 }; - gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0 }; + gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0, 0.01 }; setLayout(gbl_themeLayout); @@ -95,6 +105,37 @@ public class ThemeCustomizationPanel extends SettingsPanel { gbc_colorsPanel.insets = insets; add(colorsPanel, gbc_colorsPanel); + + createThemeButton.addActionListener((evt) -> { + if (themeChanged) { + new NewThemeScreen(parent, name -> { + // Create new theme + logger.log(Level.FINEST, name); + Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); + + // Add new theme name to combo box + themesModel.addElement(name); + + // Select new theme name + themes.setSelectedIndex(themesModel.getSize() - 1); + }, name -> { + // Modify theme + Settings.getInstance().getThemes().replace(name, new Theme(name, temporaryTheme)); + if (themes.getSelectedItem().equals(name)) + EventBus.getInstance().dispatch(new ThemeChangeEvent(Settings.getInstance().getTheme(name))); + else themes.setSelectedItem(name); + }).setVisible(true); + themeChanged = false; + } + }); + GridBagConstraints gbc_createThemeButton = new GridBagConstraints(); + gbc_createThemeButton.fill = GridBagConstraints.HORIZONTAL; + gbc_createThemeButton.gridx = 0; + gbc_createThemeButton.gridy = 2; + gbc_createThemeButton.anchor = GridBagConstraints.CENTER; + gbc_createThemeButton.insets = insets; + add(createThemeButton, gbc_createThemeButton); + colorsPanel.setBackground(theme.getCellColor()); // Apply theme upon selection @@ -120,37 +161,15 @@ public class ThemeCustomizationPanel extends SettingsPanel { }); } - @Override - public ActionListener getOkButtonAction() { - return (evt) -> { - if (themeChanged) { - new NewThemeScreen(parent, name -> { - // Create new theme - logger.log(Level.FINEST, name); - Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); - - // Add new theme name to combo box - themesModel.addElement(name); - - // Select new theme name - themes.setSelectedIndex(themesModel.getSize() - 1); - }, name -> { - // Modify theme - Settings.getInstance().getThemes().replace(name, new Theme(name, temporaryTheme)); - if (themes.getSelectedItem().equals(name)) - EventBus.getInstance().dispatch(new ThemeChangeEvent(Settings.getInstance().getTheme(name))); - else themes.setSelectedItem(name); - }).setVisible(true); - themeChanged = false; - } - }; - } - private void applyTheme(Theme theme) { // themeContent setForeground(theme.getUserNameColor()); setBackground(theme.getCellColor()); + // createThemeButton + createThemeButton.setForeground(theme.getInteractableForegroundColor()); + createThemeButton.setBackground(theme.getInteractableBackgroundColor()); + // themes themes.setBackground(theme.getInteractableBackgroundColor()); themes.setForeground(theme.getInteractableForegroundColor());