diff --git a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java index 755b5b1..ad7fe5a 100644 --- a/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java +++ b/src/main/java/envoy/client/ui/settings/ThemeCustomizationPanel.java @@ -31,12 +31,12 @@ public class ThemeCustomizationPanel extends SettingsPanel { private static final long serialVersionUID = -8697897390666456624L; - private JPanel colorsPanel = new JPanel(); - private JButton createNewThemeButton = new JButton("Create New Theme"); + 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 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 final Insets insets = new Insets(5, 5, 5, 5); @@ -50,14 +50,15 @@ public class ThemeCustomizationPanel extends SettingsPanel { * @since Envoy v0.2-alpha */ public ThemeCustomizationPanel() { - temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); + temporaryTheme = new Theme("temporaryTheme", + Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme())); GridBagLayout gbl_themeLayout = new GridBagLayout(); - gbl_themeLayout.columnWidths = new int[] { 1, 1 }; - gbl_themeLayout.rowHeights = new int[] { 1, 1 }; - gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 }; - gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0 }; + gbl_themeLayout.columnWidths = new int[] { 1, 1 }; + gbl_themeLayout.rowHeights = new int[] { 1, 1 }; + gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 }; + gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0 }; setLayout(gbl_themeLayout); @@ -74,70 +75,41 @@ public class ThemeCustomizationPanel extends SettingsPanel { }); GridBagConstraints gbc_themes = new GridBagConstraints(); - gbc_themes.fill = GridBagConstraints.HORIZONTAL; - gbc_themes.gridx = 0; - gbc_themes.gridy = 0; - gbc_themes.anchor = GridBagConstraints.NORTHWEST; - gbc_themes.insets = insets; + gbc_themes.fill = GridBagConstraints.HORIZONTAL; + gbc_themes.gridwidth = 2; + gbc_themes.gridx = 0; + gbc_themes.gridy = 0; + gbc_themes.anchor = GridBagConstraints.NORTHWEST; + gbc_themes.insets = new Insets(10, 10, 20, 10); add(themes, gbc_themes); // colorsPanel.setLayout(new BoxLayout(colorsPanel, BoxLayout.Y_AXIS)); // colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT); - + GridBagLayout gbl_colorCustomizations = new GridBagLayout(); - gbl_colorCustomizations.columnWidths = new int[] { 1, 1 }; - gbl_colorCustomizations.rowHeights = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1}; - gbl_colorCustomizations.columnWeights = new double[] { 1.0, 1.0 }; - gbl_colorCustomizations.rowWeights = new double[] { 1, 1, 1, 1, 1, 1, 1, 1}; + gbl_colorCustomizations.columnWidths = new int[] { 1, 1 }; + gbl_colorCustomizations.rowHeights = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 }; + gbl_colorCustomizations.columnWeights = new double[] { 1, 1 }; + gbl_colorCustomizations.rowWeights = new double[] { 1, 1, 1, 1, 1, 1, 1, 1 }; colorsPanel.setLayout(gbl_colorCustomizations); - - + Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()); buildCustomizeElements(theme); GridBagConstraints gbc_colorsPanel = new GridBagConstraints(); - gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL; - gbc_colorsPanel.gridx = 0; - gbc_colorsPanel.gridy = 1; - gbc_colorsPanel.gridwidth = 2; - gbc_colorsPanel.anchor = GridBagConstraints.NORTHWEST; - gbc_colorsPanel.insets = insets; + gbc_colorsPanel.fill = GridBagConstraints.HORIZONTAL; + gbc_colorsPanel.gridx = 0; + gbc_colorsPanel.gridy = 1; + gbc_colorsPanel.gridwidth = 2; + gbc_colorsPanel.anchor = GridBagConstraints.NORTHWEST; + gbc_colorsPanel.insets = insets; add(colorsPanel, gbc_colorsPanel); - - createNewThemeButton.setEnabled(false); - createNewThemeButton.setBackground(theme.getInteractableBackgroundColor()); - createNewThemeButton.setForeground(theme.getInteractableForegroundColor()); colorsPanel.setBackground(theme.getCellColor()); - createNewThemeButton.addActionListener((evt) -> { - try { - String name = JOptionPane.showInputDialog("Enter a name for the new theme"); - 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())); - - createNewThemeButton.setEnabled(false); - themes.addItem(themeArray[themeArray.length - 1]); - - } catch (Exception e) { - logger.info("New theme couldn't be created! " + e); - e.printStackTrace(); - } - }); - - GridBagConstraints gbc_createNewTheme = new GridBagConstraints(); - gbc_createNewTheme.gridx = 0; - gbc_createNewTheme.gridy = 10; - - colorsPanel.add(createNewThemeButton, gbc_createNewTheme); - // Apply current theme applyTheme(theme); @@ -148,6 +120,27 @@ public class ThemeCustomizationPanel extends SettingsPanel { @Override public ActionListener getOkButtonAction() { return (evt) -> { + if (themeChanged) { + try { + String name = JOptionPane.showInputDialog("Enter a name for the new theme"); + 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); + + } catch (Exception e) { + logger.info("New theme couldn't be created! " + e); + e.printStackTrace(); + } + themeChanged = false; + } + Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName()); logger.log(Level.FINER, "Setting theme: " + selectedTheme.getThemeName()); @@ -166,11 +159,8 @@ public class ThemeCustomizationPanel extends SettingsPanel { setBackground(theme.getCellColor()); // themes - themes.setBackground(theme.getBackgroundColor()); - themes.setForeground(getInvertedColor(theme.getBackgroundColor())); - - createNewThemeButton.setBackground(theme.getInteractableBackgroundColor()); - createNewThemeButton.setForeground(theme.getInteractableForegroundColor()); + themes.setBackground(theme.getInteractableBackgroundColor()); + themes.setForeground(theme.getInteractableForegroundColor()); colorsPanel.setBackground(theme.getCellColor()); } @@ -180,20 +170,15 @@ public class ThemeCustomizationPanel extends SettingsPanel { colorsPanel.removeAll(); buildCustomizeElements(theme); - colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT); - - GridBagConstraints gbc_createNewTheme = new GridBagConstraints(); - gbc_createNewTheme.gridx = 0; - gbc_createNewTheme.gridy = 10; - - colorsPanel.add(createNewThemeButton, gbc_createNewTheme); } private void buildCustomizeElements(Theme theme) { buildCustomizeElement(theme, theme.getBackgroundColor(), "Background", "backgroundColor", 1); buildCustomizeElement(theme, theme.getCellColor(), "Cells", "cellColor", 2); - buildCustomizeElement(theme, theme.getInteractableForegroundColor(), "Interactable Foreground", "interactableForegroundColor", 3); - buildCustomizeElement(theme, theme.getInteractableBackgroundColor(), "Interactable Background", "interactableBackgroundColor", 4); + 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.getSelectionColor(), "Selection", "selectionColor", 7); @@ -202,8 +187,8 @@ public class ThemeCustomizationPanel extends SettingsPanel { } private void buildCustomizeElement(Theme theme, Color color, String name, String colorName, int gridy) { - JButton button = new JButton(); - JTextPane textPane = new JTextPane(); + JButton button = new JButton(); + JTextPane textPane = new JTextPane(); textPane.setFont(new Font("Arial", Font.PLAIN, 14)); textPane.setBackground(theme.getBackgroundColor()); @@ -222,7 +207,7 @@ public class ThemeCustomizationPanel extends SettingsPanel { // TODO: When Theme changed in same settings screen, color variable doesn't // update temporaryTheme.setColor(colorName, newColor); - createNewThemeButton.setEnabled(true); + themeChanged = true; } button.setBackground(newColor); @@ -233,24 +218,25 @@ public class ThemeCustomizationPanel extends SettingsPanel { }); GridBagConstraints gbc_textPane = new GridBagConstraints(); - gbc_textPane.fill = GridBagConstraints.BOTH; - gbc_textPane.gridx = 0; - gbc_textPane.gridy = gridy; - gbc_textPane.anchor = GridBagConstraints.CENTER; - gbc_textPane.insets = insets; + gbc_textPane.fill = GridBagConstraints.BOTH; + gbc_textPane.gridx = 0; + gbc_textPane.gridy = gridy; + gbc_textPane.anchor = GridBagConstraints.CENTER; + gbc_textPane.insets = insets; colorsPanel.add(textPane, gbc_textPane); - - + GridBagConstraints gbc_button = new GridBagConstraints(); - gbc_button.fill = GridBagConstraints.BOTH; - gbc_button.gridx = 1; - gbc_button.gridy = gridy; - gbc_button.anchor = GridBagConstraints.CENTER; - gbc_button.insets = insets; + gbc_button.fill = GridBagConstraints.BOTH; + gbc_button.gridx = 1; + gbc_button.gridy = gridy; + gbc_button.anchor = GridBagConstraints.CENTER; + gbc_button.insets = insets; colorsPanel.add(button, gbc_button); } - private Color getInvertedColor(Color color) { return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); } + private Color getInvertedColor(Color color) { + return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); + } }