Added new theme name choose window

* Added NewThemeScreen Class
OptionsScreen when name conflict
* Added functionality to overwrite themes.
This commit is contained in:
DieGurke 2019-12-26 21:46:35 +01:00
parent 83ddbf5360
commit fd40cadf6c
3 changed files with 309 additions and 38 deletions

View File

@ -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}.
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>NewThemeScreen.java</strong><br>
* Created: <strong>26 Dec 2019</strong><br>
*
* @author Maximilian K&auml;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}. <br>
* 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();
}
});
}
}

View File

@ -162,7 +162,8 @@ public class SettingsScreen extends JDialog {
EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme(((ThemeChangeEvent) evt).get())); EventBus.getInstance().register(ThemeChangeEvent.class, (evt) -> applyTheme(((ThemeChangeEvent) evt).get()));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModal(true); //setModal(true);
//TODO: check modalitly
} }
private void applyTheme(Theme theme) { private void applyTheme(Theme theme) {

View File

@ -5,7 +5,6 @@ import java.awt.event.ActionListener;
import java.awt.event.ItemEvent; import java.awt.event.ItemEvent;
import java.awt.event.ItemListener; import java.awt.event.ItemListener;
import java.util.Arrays; import java.util.Arrays;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -119,36 +118,15 @@ public class ThemeCustomizationPanel extends SettingsPanel {
return (evt) -> { return (evt) -> {
if (themeChanged) { if (themeChanged) {
try { try {
String name = ""; new NewThemeScreen(this).setVisible(true);
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);
} catch (Exception e) { } catch (Exception e) {
logger.info("New theme couldn't be created! " + e); logger.info("New theme couldn't be created! " + e);
e.printStackTrace(); e.printStackTrace();
} }
themeChanged = false; 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();
}; };
} }
@ -234,15 +212,47 @@ public class ThemeCustomizationPanel extends SettingsPanel {
colorsPanel.add(button, gbc_button); colorsPanel.add(button, gbc_button);
} }
private String newName () { private void updateCurrentTheme() {
String name = JOptionPane.showInputDialog("Enter a name for the new theme"); Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName());
for (Map.Entry<String, Theme> entry : Settings.getInstance().getThemes().entrySet()) { logger.log(Level.FINER, "Setting theme: " + selectedTheme.getThemeName());
System.out.println(entry.getKey().toString());
if(entry.getKey().equalsIgnoreCase(name)) { final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
JOptionPane.showMessageDialog(getParent(), "Name is already used! Please choose another one."); applyTheme(currentTheme);
return ""; updateColorVariables(currentTheme);
} EventBus.getInstance().dispatch(new ThemeChangeEvent(currentTheme));
} temporaryTheme = new Theme("temporaryTheme", currentTheme);
return name;
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();
} }
} }