From 83ddbf5360999137e8861d0ef71039f92a633940 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Wed, 25 Dec 2019 21:43:59 +0100 Subject: [PATCH 1/5] Fixed possible name conflict when creating new themes. --- .../ui/settings/ThemeCustomizationPanel.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index c07bc56..19cceb0 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -5,6 +5,7 @@ import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Arrays; +import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -118,7 +119,10 @@ public class ThemeCustomizationPanel extends SettingsPanel { return (evt) -> { if (themeChanged) { try { - String name = JOptionPane.showInputDialog("Enter a name for the new theme"); + String name = ""; + while (name == "") { + name = newName(); + } logger.log(Level.FINEST, name); Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); themeArray = Arrays.copyOf(themeArray, themeArray.length + 1); @@ -229,4 +233,16 @@ public class ThemeCustomizationPanel extends SettingsPanel { colorsPanel.add(button, gbc_button); } + + private String newName () { + String name = JOptionPane.showInputDialog("Enter a name for the new theme"); + for (Map.Entry entry : Settings.getInstance().getThemes().entrySet()) { + System.out.println(entry.getKey().toString()); + if(entry.getKey().equalsIgnoreCase(name)) { + JOptionPane.showMessageDialog(getParent(), "Name is already used! Please choose another one."); + return ""; + } + } + return name; + } } \ No newline at end of file From fd40cadf6c63cdd91cc921d5832ba72b629e40b9 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Thu, 26 Dec 2019 21:46:35 +0100 Subject: [PATCH 2/5] Added new theme name choose window * Added NewThemeScreen Class OptionsScreen when name conflict * Added functionality to overwrite themes. --- .../client/ui/settings/NewThemeScreen.java | 260 ++++++++++++++++++ .../client/ui/settings/SettingsScreen.java | 3 +- .../ui/settings/ThemeCustomizationPanel.java | 84 +++--- 3 files changed, 309 insertions(+), 38 deletions(-) create mode 100644 src/main/java/envoy/client/ui/settings/NewThemeScreen.java diff --git a/src/main/java/envoy/client/ui/settings/NewThemeScreen.java b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java new file mode 100644 index 0000000..48df3b0 --- /dev/null +++ b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java @@ -0,0 +1,260 @@ +package envoy.client.ui.settings; + +import java.awt.*; +import java.util.logging.Logger; + +import javax.swing.*; + +import envoy.client.Settings; +import envoy.client.ui.PrimaryButton; +import envoy.client.ui.PrimaryTextArea; +import envoy.client.ui.Theme; +import envoy.client.util.EnvoyLog; + +/** + * Displays window where you can choose a name for the new {@link Theme}. + *
+ * Project: envoy-client
+ * File: NewThemeScreen.java
+ * Created: 26 Dec 2019
+ * + * @author Maximilian Käfer + * @since Envoy v0.3-alpha + */ +public class NewThemeScreen extends JDialog { + + private final JPanel standardPanel = new JPanel(); + private final JPanel secondaryPanel = new JPanel(); + private JTextPane text = new JTextPane(); + private PrimaryTextArea nameEnterTextArea = new PrimaryTextArea(4); + private PrimaryButton confirmButton = new PrimaryButton("Confirm"); + + private JTextPane errorText = new JTextPane(); + private PrimaryButton otherName = new PrimaryButton("Other Name"); + private PrimaryButton overwrite = new PrimaryButton("Overwrite"); + + private static final Logger logger = EnvoyLog.getLogger(NewThemeScreen.class.getSimpleName()); + private static final long serialVersionUID = 2369985550946300976L; + + /** + * Creates a window, where you can choose a name for a new {@link Theme}.
+ * There are two versions of this Window. The first one is responsible for + * choosing the name, the second one appears, the the name already exists. + * + * @param parentClass The class, where this constructor is invoked. + * @since Envoy v0.3-alpha + */ + public NewThemeScreen(ThemeCustomizationPanel parentClass) { + setTitle("New Theme"); + setDimensions(true, parentClass); + setVisible(true); + setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); + setModal(true); + // TODO: check modalitly + // setModalityType(DEFAULT_MODALITY_TYPE); + + Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); + + getContentPane().setLayout(new BorderLayout()); + { + standardPanel.setBackground(theme.getBackgroundColor()); + secondaryPanel.setBackground(theme.getBackgroundColor()); + loadStandardContent(theme, parentClass); + } + } + + private void setDimensions(boolean isStandard, ThemeCustomizationPanel parentClass) { + if (isStandard == true) { + setPreferredSize(new Dimension(300, 170)); + setMinimumSize(new Dimension(300, 170)); + setMaximumSize(new Dimension(300, 170)); + setBounds(parentClass.getLocation().x + (parentClass.getSize().width), + parentClass.getLocation().y + (parentClass.getSize().height / 2), + 300, + 170); + } else { + setPreferredSize(new Dimension(300, 225)); + setMinimumSize(new Dimension(300, 225)); + setMaximumSize(new Dimension(300, 225)); + setBounds(parentClass.getLocation().x + (parentClass.getSize().width), + parentClass.getLocation().y + (parentClass.getSize().height / 2), + 300, + 225); + } + } + + private void loadStandardContent(Theme theme, ThemeCustomizationPanel parentClass) { + getContentPane().removeAll(); + + // ContentPane + GridBagLayout gbl_contentPanel = new GridBagLayout(); + + gbl_contentPanel.columnWidths = new int[] { 1, 1 }; + gbl_contentPanel.rowHeights = new int[] { 1, 1, 1 }; + gbl_contentPanel.columnWeights = new double[] { 1, 1 }; + gbl_contentPanel.rowWeights = new double[] { 1, 1, 1 }; + + getContentPane().add(standardPanel, BorderLayout.CENTER); + standardPanel.setLayout(gbl_contentPanel); + + // text.setFont(new Font()); + text.setText("Please enter a name for the new Theme"); + text.setAlignmentX(CENTER_ALIGNMENT); + text.setBackground(theme.getCellColor()); + text.setForeground(theme.getUserNameColor()); + text.setEditable(false); + + GridBagConstraints gbc_text = new GridBagConstraints(); + gbc_text.fill = GridBagConstraints.HORIZONTAL; + gbc_text.gridx = 0; + gbc_text.gridy = 0; + gbc_text.gridwidth = 2; + gbc_text.insets = new Insets(5, 5, 5, 5); + + standardPanel.add(text, gbc_text); + + nameEnterTextArea.setBackground(theme.getCellColor()); + nameEnterTextArea.setForeground(theme.getTypingMessageColor()); + nameEnterTextArea.setText(""); + nameEnterTextArea.setEditable(true); + + GridBagConstraints gbc_input = new GridBagConstraints(); + gbc_input.fill = GridBagConstraints.HORIZONTAL; + gbc_input.gridx = 0; + gbc_input.gridy = 1; + gbc_input.gridwidth = 2; + gbc_input.insets = new Insets(5, 5, 5, 5); + + standardPanel.add(nameEnterTextArea, gbc_input); + + confirmButton.setBackground(theme.getInteractableBackgroundColor()); + confirmButton.setForeground(theme.getInteractableForegroundColor()); + + GridBagConstraints gbc_confirmButton = new GridBagConstraints(); + gbc_confirmButton.gridx = 0; + gbc_confirmButton.gridy = 2; + gbc_confirmButton.gridwidth = 2; + gbc_confirmButton.insets = new Insets(5, 5, 5, 5); + + standardPanel.add(confirmButton, gbc_confirmButton); + + confirmButton.addActionListener((evt) -> { + if (!nameEnterTextArea.getText().isEmpty()) try { + if (Settings.getInstance().getThemes().containsKey(nameEnterTextArea.getText())) { + // load other panel + setDimensions(false, parentClass); + loadSecondaryPage(theme, parentClass); + } else { + parentClass.newTheme(nameEnterTextArea.getText()); + dispose(); + } + } catch (Exception e) { + logger.info("" + "Name could not be set! " + e); + e.printStackTrace(); + } + }); + } + + private void loadSecondaryPage(Theme theme, ThemeCustomizationPanel parentClass) { + // ContentPane + getContentPane().removeAll(); + + GridBagLayout gbl_secondaryPanel = new GridBagLayout(); + + gbl_secondaryPanel.columnWidths = new int[] { 1, 1 }; + gbl_secondaryPanel.rowHeights = new int[] { 1, 1, 1, 1 }; + gbl_secondaryPanel.columnWeights = new double[] { 1, 1 }; + gbl_secondaryPanel.rowWeights = new double[] { 1, 1, 1, 1 }; + + getContentPane().add(secondaryPanel, BorderLayout.CENTER); + secondaryPanel.setLayout(gbl_secondaryPanel); + + // text.setFont(new Font()); + text.setText("Please enter a name for the new Theme"); + text.setAlignmentX(CENTER_ALIGNMENT); + text.setBackground(theme.getCellColor()); + text.setForeground(theme.getUserNameColor()); + text.setEditable(false); + + GridBagConstraints gbc_text = new GridBagConstraints(); + gbc_text.fill = GridBagConstraints.HORIZONTAL; + gbc_text.gridx = 0; + gbc_text.gridy = 0; + gbc_text.gridwidth = 2; + gbc_text.insets = new Insets(5, 5, 5, 5); + + secondaryPanel.add(text, gbc_text); + + nameEnterTextArea.setBackground(theme.getCellColor()); + nameEnterTextArea.setForeground(theme.getTypingMessageColor()); + nameEnterTextArea.setEditable(false); + + GridBagConstraints gbc_input = new GridBagConstraints(); + gbc_input.fill = GridBagConstraints.HORIZONTAL; + gbc_input.gridx = 0; + gbc_input.gridy = 1; + gbc_input.gridwidth = 2; + gbc_input.insets = new Insets(5, 5, 5, 5); + + secondaryPanel.add(nameEnterTextArea, gbc_input); + + errorText.setText("The name does already exist. Choose another one or overwrite the old theme."); + errorText.setAlignmentX(CENTER_ALIGNMENT); + errorText.setBackground(theme.getCellColor()); + errorText.setForeground(theme.getUserNameColor()); + errorText.setEditable(false); + + GridBagConstraints gbc_errorText = new GridBagConstraints(); + gbc_errorText.fill = GridBagConstraints.HORIZONTAL; + gbc_errorText.gridx = 0; + gbc_errorText.gridy = 2; + gbc_errorText.gridwidth = 2; + gbc_errorText.insets = new Insets(5, 5, 5, 5); + + secondaryPanel.add(errorText, gbc_errorText); + + otherName.setBackground(theme.getInteractableBackgroundColor()); + otherName.setForeground(theme.getInteractableForegroundColor()); + + GridBagConstraints gbc_otherName = new GridBagConstraints(); + gbc_otherName.gridx = 0; + gbc_otherName.gridy = 3; + gbc_otherName.insets = new Insets(5, 5, 5, 5); + + secondaryPanel.add(otherName, gbc_otherName); + + overwrite.setBackground(theme.getInteractableBackgroundColor()); + overwrite.setForeground(theme.getInteractableForegroundColor()); + + GridBagConstraints gbc_overwrite = new GridBagConstraints(); + gbc_overwrite.gridx = 1; + gbc_overwrite.gridy = 3; + gbc_overwrite.insets = new Insets(5, 5, 5, 5); + + secondaryPanel.add(overwrite, gbc_overwrite); + + otherName.addActionListener((evt) -> { + try { + setDimensions(true, parentClass); + loadStandardContent(theme, parentClass); + + } catch (Exception e) { + logger.info("Window could not be updated! " + e); + e.printStackTrace(); + } + + }); + + overwrite.addActionListener((evt) -> { + try { + dispose(); + parentClass.overwriteTheme(nameEnterTextArea.getText()); + + } catch (Exception e) { + logger.info("Error while overwriting the theme! " + e); + e.printStackTrace(); + } + + }); + } +} diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index b37bf9a..022f87f 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -162,7 +162,8 @@ public class SettingsScreen extends JDialog { EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme(((ThemeChangeEvent) evt).get())); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - setModal(true); + //setModal(true); + //TODO: check modalitly } private void applyTheme(Theme theme) { diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 19cceb0..08d2357 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -5,7 +5,6 @@ import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.util.Arrays; -import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -119,36 +118,15 @@ public class ThemeCustomizationPanel extends SettingsPanel { return (evt) -> { if (themeChanged) { try { - String name = ""; - while (name == "") { - name = newName(); - } - logger.log(Level.FINEST, name); - Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); - themeArray = Arrays.copyOf(themeArray, themeArray.length + 1); - themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(name).getThemeName(); - - temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); - - themes.addItem(themeArray[themeArray.length - 1]); - themes.setSelectedIndex(themeArray.length - 1); - + new NewThemeScreen(this).setVisible(true); } catch (Exception e) { logger.info("New theme couldn't be created! " + e); e.printStackTrace(); } themeChanged = false; + } else { + updateCurrentTheme(); } - - Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); - logger.log(Level.FINER, "Setting theme: " + selectedTheme.getThemeName()); - - final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); - updateColorVariables(currentTheme); - EventBus.getInstance().dispatch(new ThemeChangeEvent(currentTheme)); - - revalidate(); - repaint(); }; } @@ -198,8 +176,8 @@ public class ThemeCustomizationPanel extends SettingsPanel { button.addActionListener((evt) -> { try { - java.awt.Color c = JColorChooser.showDialog(null, "Choose a color", color); - Color newColor = new Color(c.getRGB()); + java.awt.Color c = JColorChooser.showDialog(null, "Choose a color", color); + Color newColor = new Color(c.getRGB()); if (newColor.getRGB() != color.getRGB()) { logger.log(Level.FINEST, "New Color: " + String.valueOf(color.getRGB())); // TODO: When Theme changed in same settings screen, color variable doesn't @@ -233,16 +211,48 @@ public class ThemeCustomizationPanel extends SettingsPanel { colorsPanel.add(button, gbc_button); } + + private void updateCurrentTheme() { + Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); + logger.log(Level.FINER, "Setting theme: " + selectedTheme.getThemeName()); + + final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); + applyTheme(currentTheme); + updateColorVariables(currentTheme); + EventBus.getInstance().dispatch(new ThemeChangeEvent(currentTheme)); + temporaryTheme = new Theme("temporaryTheme", currentTheme); + + revalidate(); + repaint(); + } - private String newName () { - String name = JOptionPane.showInputDialog("Enter a name for the new theme"); - for (Map.Entry entry : Settings.getInstance().getThemes().entrySet()) { - System.out.println(entry.getKey().toString()); - if(entry.getKey().equalsIgnoreCase(name)) { - JOptionPane.showMessageDialog(getParent(), "Name is already used! Please choose another one."); - return ""; - } - } - return name; + /** + * Adds a new {@link Theme} to the theme map. + * + * @param name The name of the new {@link Theme}. + * @since Envoy v0.3-alpha + */ + public void newTheme(String name) { + logger.log(Level.FINEST, name); + Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); + themeArray = Arrays.copyOf(themeArray, themeArray.length + 1); + themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(name).getThemeName(); + + themes.addItem(themeArray[themeArray.length - 1]); + themes.setSelectedIndex(themeArray.length - 1); + + updateCurrentTheme(); + } + + /** + * Overwrites a specific {@link Theme} located in the theme map and sets the selected {@link Theme} to this new {@link Theme}. + * + * @param key The name of the {@link Theme} to be overwritten. + * @since Envoy v0.3-alpha + */ + public void overwriteTheme(String key) { + Settings.getInstance().getThemes().replace(key, new Theme(key, temporaryTheme)); + selectedTheme = Settings.getInstance().getThemes().get(key); + updateCurrentTheme(); } } \ No newline at end of file From 81e21c36528bd9b8aa7ba5fd2a39a71a439dd633 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Fri, 27 Dec 2019 13:30:40 +0100 Subject: [PATCH 3/5] Fixed Javadoc comment --- src/main/java/envoy/client/ui/settings/NewThemeScreen.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/settings/NewThemeScreen.java b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java index 48df3b0..3fb2db0 100644 --- a/src/main/java/envoy/client/ui/settings/NewThemeScreen.java +++ b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java @@ -39,7 +39,7 @@ public class NewThemeScreen extends JDialog { /** * Creates a window, where you can choose a name for a new {@link Theme}.
* There are two versions of this Window. The first one is responsible for - * choosing the name, the second one appears, the the name already exists. + * choosing the name, the second one appears, if the name already exists. * * @param parentClass The class, where this constructor is invoked. * @since Envoy v0.3-alpha From 0785732f15baeb32ffd3659ee96d48372c22f3ad Mon Sep 17 00:00:00 2001 From: kske Date: Fri, 27 Dec 2019 16:56:48 +0200 Subject: [PATCH 4/5] Added instantaneous theme change, fixed dialog modality --- src/main/java/envoy/client/Settings.java | 7 + src/main/java/envoy/client/ui/Color.java | 2 + .../ui/settings/GeneralSettingsPanel.java | 5 +- .../client/ui/settings/NewThemeScreen.java | 110 +++++--------- .../client/ui/settings/SettingsPanel.java | 10 ++ .../client/ui/settings/SettingsScreen.java | 5 +- .../ui/settings/ThemeCustomizationPanel.java | 141 +++++++----------- 7 files changed, 120 insertions(+), 160 deletions(-) diff --git a/src/main/java/envoy/client/Settings.java b/src/main/java/envoy/client/Settings.java index 199653d..cdecc33 100644 --- a/src/main/java/envoy/client/Settings.java +++ b/src/main/java/envoy/client/Settings.java @@ -186,4 +186,11 @@ public class Settings { * @since Envoy v0.2-alpha */ public void setThemes(Map themes) { this.themes = themes; } + + /** + * @param themeName the name of the {@link Theme} to get + * @return the {@link Theme} with the specified name + * @since Envoy v0.3-alpha + */ + public Theme getTheme(String themeName) { return themes.get(themeName); } } \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/Color.java b/src/main/java/envoy/client/ui/Color.java index 33be64b..18d6ca6 100644 --- a/src/main/java/envoy/client/ui/Color.java +++ b/src/main/java/envoy/client/ui/Color.java @@ -79,6 +79,8 @@ public class Color extends java.awt.Color { private static final long serialVersionUID = -9166233199998257344L; + public Color(java.awt.Color other) { this(other.getRGB()); } + public Color(int rgb) { super(rgb); } public Color(int rgba, boolean hasalpha) { super(rgba, hasalpha); } diff --git a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java index ee8f619..d76d558 100644 --- a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java @@ -37,9 +37,12 @@ public class GeneralSettingsPanel extends SettingsPanel { * 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 0.3-alpha */ - public GeneralSettingsPanel() { + public GeneralSettingsPanel(SettingsScreen parent) { + super(parent); theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); setBackground(theme.getCellColor()); diff --git a/src/main/java/envoy/client/ui/settings/NewThemeScreen.java b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java index 48df3b0..e3c9301 100644 --- a/src/main/java/envoy/client/ui/settings/NewThemeScreen.java +++ b/src/main/java/envoy/client/ui/settings/NewThemeScreen.java @@ -1,15 +1,16 @@ package envoy.client.ui.settings; import java.awt.*; -import java.util.logging.Logger; +import java.util.function.Consumer; -import javax.swing.*; +import javax.swing.JDialog; +import javax.swing.JPanel; +import javax.swing.JTextPane; import envoy.client.Settings; import envoy.client.ui.PrimaryButton; import envoy.client.ui.PrimaryTextArea; import envoy.client.ui.Theme; -import envoy.client.util.EnvoyLog; /** * Displays window where you can choose a name for the new {@link Theme}. @@ -33,57 +34,47 @@ public class NewThemeScreen extends JDialog { private PrimaryButton otherName = new PrimaryButton("Other Name"); private PrimaryButton overwrite = new PrimaryButton("Overwrite"); - private static final Logger logger = EnvoyLog.getLogger(NewThemeScreen.class.getSimpleName()); - private static final long serialVersionUID = 2369985550946300976L; + private final Consumer newThemeAction, modifyThemeAction; + + private static final long serialVersionUID = 2369985550946300976L; /** * Creates a window, where you can choose a name for a new {@link Theme}.
* There are two versions of this Window. The first one is responsible for * choosing the name, the second one appears, the the name already exists. * - * @param parentClass The class, where this constructor is invoked. + * @param parent the dialog is launched with its location relative to this {@link SettingsScreen} + * @param newThemeAction is executed when a new theme name is entered + * @param modifyThemeAction is executed when an existing theme name is entered and confirmed * @since Envoy v0.3-alpha */ - public NewThemeScreen(ThemeCustomizationPanel parentClass) { + public NewThemeScreen(SettingsScreen parent, Consumer newThemeAction, Consumer modifyThemeAction) { + this.newThemeAction = newThemeAction; + this.modifyThemeAction = modifyThemeAction; + + setLocationRelativeTo(parent); setTitle("New Theme"); - setDimensions(true, parentClass); - setVisible(true); - setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setModal(true); - // TODO: check modalitly - // setModalityType(DEFAULT_MODALITY_TYPE); + + setDimensions(true); + setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); getContentPane().setLayout(new BorderLayout()); - { - standardPanel.setBackground(theme.getBackgroundColor()); - secondaryPanel.setBackground(theme.getBackgroundColor()); - loadStandardContent(theme, parentClass); - } + standardPanel.setBackground(theme.getBackgroundColor()); + secondaryPanel.setBackground(theme.getBackgroundColor()); + loadStandardContent(theme); } - private void setDimensions(boolean isStandard, ThemeCustomizationPanel parentClass) { - if (isStandard == true) { - setPreferredSize(new Dimension(300, 170)); - setMinimumSize(new Dimension(300, 170)); - setMaximumSize(new Dimension(300, 170)); - setBounds(parentClass.getLocation().x + (parentClass.getSize().width), - parentClass.getLocation().y + (parentClass.getSize().height / 2), - 300, - 170); - } else { - setPreferredSize(new Dimension(300, 225)); - setMinimumSize(new Dimension(300, 225)); - setMaximumSize(new Dimension(300, 225)); - setBounds(parentClass.getLocation().x + (parentClass.getSize().width), - parentClass.getLocation().y + (parentClass.getSize().height / 2), - 300, - 225); - } + private void setDimensions(boolean isStandard) { + Dimension size = isStandard ? new Dimension(300, 170) : new Dimension(300, 225); + setPreferredSize(size); + setMinimumSize(size); + setMaximumSize(size); } - private void loadStandardContent(Theme theme, ThemeCustomizationPanel parentClass) { + private void loadStandardContent(Theme theme) { getContentPane().removeAll(); // ContentPane @@ -139,23 +130,18 @@ public class NewThemeScreen extends JDialog { standardPanel.add(confirmButton, gbc_confirmButton); confirmButton.addActionListener((evt) -> { - if (!nameEnterTextArea.getText().isEmpty()) try { - if (Settings.getInstance().getThemes().containsKey(nameEnterTextArea.getText())) { - // load other panel - setDimensions(false, parentClass); - loadSecondaryPage(theme, parentClass); - } else { - parentClass.newTheme(nameEnterTextArea.getText()); - dispose(); - } - } catch (Exception e) { - logger.info("" + "Name could not be set! " + e); - e.printStackTrace(); + if (!nameEnterTextArea.getText().isEmpty()) if (Settings.getInstance().getThemes().containsKey(nameEnterTextArea.getText())) { + // load other panel + setDimensions(false); + loadSecondaryPage(theme); + } else { + newThemeAction.accept(nameEnterTextArea.getText()); + dispose(); } }); } - private void loadSecondaryPage(Theme theme, ThemeCustomizationPanel parentClass) { + private void loadSecondaryPage(Theme theme) { // ContentPane getContentPane().removeAll(); @@ -233,28 +219,8 @@ public class NewThemeScreen extends JDialog { secondaryPanel.add(overwrite, gbc_overwrite); - otherName.addActionListener((evt) -> { - try { - setDimensions(true, parentClass); - loadStandardContent(theme, parentClass); + otherName.addActionListener((evt) -> { setDimensions(true); loadStandardContent(theme); }); - } catch (Exception e) { - logger.info("Window could not be updated! " + e); - e.printStackTrace(); - } - - }); - - overwrite.addActionListener((evt) -> { - try { - dispose(); - parentClass.overwriteTheme(nameEnterTextArea.getText()); - - } catch (Exception e) { - logger.info("Error while overwriting the theme! " + e); - e.printStackTrace(); - } - - }); + overwrite.addActionListener((evt) -> { modifyThemeAction.accept(nameEnterTextArea.getText()); dispose(); }); } -} +} \ No newline at end of file diff --git a/src/main/java/envoy/client/ui/settings/SettingsPanel.java b/src/main/java/envoy/client/ui/settings/SettingsPanel.java index 9b8f239..80469c5 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/SettingsPanel.java @@ -18,8 +18,18 @@ import javax.swing.JPanel; */ public abstract class SettingsPanel extends JPanel { + protected final SettingsScreen parent; + private static final long serialVersionUID = -3069212622468626050L; + /** + * 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} diff --git a/src/main/java/envoy/client/ui/settings/SettingsScreen.java b/src/main/java/envoy/client/ui/settings/SettingsScreen.java index 022f87f..611100f 100644 --- a/src/main/java/envoy/client/ui/settings/SettingsScreen.java +++ b/src/main/java/envoy/client/ui/settings/SettingsScreen.java @@ -93,7 +93,7 @@ public class SettingsScreen extends JDialog { if (settingsPanel != null) contentPanel.remove(settingsPanel); try { - settingsPanel = panels.get(option).getDeclaredConstructor().newInstance(); + settingsPanel = panels.get(option).getDeclaredConstructor(getClass()).newInstance(this); // Add selected settings panel contentPanel.add(settingsPanel, gbc_panel); @@ -162,8 +162,7 @@ public class SettingsScreen extends JDialog { EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme(((ThemeChangeEvent) evt).get())); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); - //setModal(true); - //TODO: check modalitly + setModal(true); } private void applyTheme(Theme theme) { diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 08d2357..9b1a85a 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -4,7 +4,6 @@ import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; -import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; @@ -33,10 +32,11 @@ public class ThemeCustomizationPanel extends SettingsPanel { private JPanel colorsPanel = new JPanel(); - private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]); - private JComboBox themes = new JComboBox<>(themeArray); - private Theme temporaryTheme, selectedTheme; - private boolean themeChanged = false; + private DefaultComboBoxModel themesModel = new DefaultComboBoxModel<>( + Settings.getInstance().getThemes().keySet().toArray(new String[0])); + private JComboBox themes = new JComboBox<>(themesModel); + private Theme temporaryTheme; + private boolean themeChanged; private final Insets insets = new Insets(5, 5, 5, 5); @@ -48,9 +48,12 @@ public class ThemeCustomizationPanel extends SettingsPanel { * the current {@link Theme} and create new themes as part of the * {@link SettingsScreen}. * + * @param parent the {@link SettingsScreen} as a part of which this + * {@link SettingsPanel} is displayed * @since Envoy v0.2-alpha */ - public ThemeCustomizationPanel() { + public ThemeCustomizationPanel(SettingsScreen parent) { + super(parent); temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); GridBagLayout gbl_themeLayout = new GridBagLayout(); @@ -64,16 +67,6 @@ public class ThemeCustomizationPanel extends SettingsPanel { themes.setSelectedItem(Settings.getInstance().getCurrentTheme()); - themes.addItemListener(new ItemListener() { - - @Override - public void itemStateChanged(ItemEvent e) { - String selectedValue = (String) themes.getSelectedItem(); - logger.log(Level.FINEST, selectedValue); - selectedTheme = Settings.getInstance().getThemes().get(selectedValue); - } - }); - GridBagConstraints gbc_themes = new GridBagConstraints(); gbc_themes.fill = GridBagConstraints.HORIZONTAL; gbc_themes.gridwidth = 2; @@ -106,26 +99,53 @@ public class ThemeCustomizationPanel extends SettingsPanel { add(colorsPanel, gbc_colorsPanel); colorsPanel.setBackground(theme.getCellColor()); + // Apply theme upon selection + themes.addItemListener(new ItemListener() { + + @Override + public void itemStateChanged(ItemEvent e) { + String selectedValue = (String) themes.getSelectedItem(); + logger.log(Level.FINEST, "Selected theme: " + selectedValue); + + final Theme currentTheme = Settings.getInstance().getTheme(selectedValue); + Settings.getInstance().setCurrentTheme(selectedValue); + EventBus.getInstance().dispatch(new ThemeChangeEvent(currentTheme)); + } + }); + // Apply current theme applyTheme(theme); // Respond to theme changes - EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme(((ThemeChangeEvent) evt).get())); + EventBus.getInstance() + .register(ThemeChangeEvent.class, + (evt) -> { + final Theme currentTheme = ((ThemeChangeEvent) evt).get(); + temporaryTheme = new Theme("temporaryTheme", currentTheme); + applyTheme(currentTheme); + }); } @Override public ActionListener getOkButtonAction() { return (evt) -> { if (themeChanged) { - try { - new NewThemeScreen(this).setVisible(true); - } catch (Exception e) { - logger.info("New theme couldn't be created! " + e); - e.printStackTrace(); - } + 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)); + themes.setSelectedItem(name); + }).setVisible(true); themeChanged = false; - } else { - updateCurrentTheme(); } }; } @@ -139,13 +159,16 @@ public class ThemeCustomizationPanel extends SettingsPanel { themes.setBackground(theme.getInteractableBackgroundColor()); themes.setForeground(theme.getInteractableForegroundColor()); colorsPanel.setBackground(theme.getCellColor()); + + // Color panel + updateColorVariables(theme); + + revalidate(); + repaint(); } private void updateColorVariables(Theme theme) { - temporaryTheme = new Theme("temporaryTheme", theme); - colorsPanel.removeAll(); - buildCustomizeElements(theme); } @@ -155,7 +178,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { buildCustomizeElement(theme, theme.getInteractableForegroundColor(), "Interactable Foreground", "interactableForegroundColor", 3); buildCustomizeElement(theme, theme.getInteractableBackgroundColor(), "Interactable Background", "interactableBackgroundColor", 4); buildCustomizeElement(theme, theme.getMessageColorChat(), "Messages Chat", "messageColorChat", 5); - buildCustomizeElement(theme, theme.getDateColorChat(), "Date Chat", "dateColorCat", 6); + buildCustomizeElement(theme, theme.getDateColorChat(), "Date Chat", "dateColorChat", 6); buildCustomizeElement(theme, theme.getSelectionColor(), "Selection", "selectionColor", 7); buildCustomizeElement(theme, theme.getTypingMessageColor(), "Typing Message", "typingMessageColor", 8); buildCustomizeElement(theme, theme.getUserNameColor(), "User Names", "userNameColor", 9); @@ -175,21 +198,15 @@ public class ThemeCustomizationPanel extends SettingsPanel { button.setPreferredSize(new Dimension(25, 25)); button.addActionListener((evt) -> { - try { - java.awt.Color c = JColorChooser.showDialog(null, "Choose a color", color); - Color newColor = new Color(c.getRGB()); - if (newColor.getRGB() != color.getRGB()) { - logger.log(Level.FINEST, "New Color: " + String.valueOf(color.getRGB())); - // TODO: When Theme changed in same settings screen, color variable doesn't - // update + java.awt.Color c = JColorChooser.showDialog(null, "Choose a color", color); + if (c != null) { + Color newColor = new Color(c); + if (!color.equals(newColor)) { + logger.log(Level.FINEST, "New Color: " + newColor); temporaryTheme.setColor(colorName, newColor); themeChanged = true; } button.setBackground(newColor); - - } catch (Exception e) { - logger.info("An error occured while opening Color Chooser: " + e); - e.printStackTrace(); } }); @@ -211,48 +228,4 @@ public class ThemeCustomizationPanel extends SettingsPanel { colorsPanel.add(button, gbc_button); } - - private void updateCurrentTheme() { - Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); - logger.log(Level.FINER, "Setting theme: " + selectedTheme.getThemeName()); - - final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); - applyTheme(currentTheme); - updateColorVariables(currentTheme); - EventBus.getInstance().dispatch(new ThemeChangeEvent(currentTheme)); - temporaryTheme = new Theme("temporaryTheme", currentTheme); - - revalidate(); - repaint(); - } - - /** - * Adds a new {@link Theme} to the theme map. - * - * @param name The name of the new {@link Theme}. - * @since Envoy v0.3-alpha - */ - public void newTheme(String name) { - logger.log(Level.FINEST, name); - Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme)); - themeArray = Arrays.copyOf(themeArray, themeArray.length + 1); - themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(name).getThemeName(); - - themes.addItem(themeArray[themeArray.length - 1]); - themes.setSelectedIndex(themeArray.length - 1); - - updateCurrentTheme(); - } - - /** - * Overwrites a specific {@link Theme} located in the theme map and sets the selected {@link Theme} to this new {@link Theme}. - * - * @param key The name of the {@link Theme} to be overwritten. - * @since Envoy v0.3-alpha - */ - public void overwriteTheme(String key) { - Settings.getInstance().getThemes().replace(key, new Theme(key, temporaryTheme)); - selectedTheme = Settings.getInstance().getThemes().get(key); - updateCurrentTheme(); - } } \ No newline at end of file From b21b21269555876cc5242d098566f553974063cb Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Fri, 27 Dec 2019 17:04:02 +0100 Subject: [PATCH 5/5] Added a v --- .../java/envoy/client/ui/settings/GeneralSettingsPanel.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java index d76d558..adafc2e 100644 --- a/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java +++ b/src/main/java/envoy/client/ui/settings/GeneralSettingsPanel.java @@ -39,7 +39,7 @@ public class GeneralSettingsPanel extends SettingsPanel { * * @param parent the {@link SettingsScreen} as a part of which this * {@link SettingsPanel} is displayed - * @since Envoy 0.3-alpha + * @since Envoy v0.3-alpha */ public GeneralSettingsPanel(SettingsScreen parent) { super(parent);