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