Merge pull request #83 from informatik-ag-ngl/f/themeNameCollision

Theme name collision
This commit is contained in:
DieGurke 2019-12-27 17:04:25 +01:00 committed by GitHub
commit 9b142c8484
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 304 additions and 57 deletions

View File

@ -186,4 +186,11 @@ public class Settings {
* @since Envoy v0.2-alpha
*/
public void setThemes(Map<String, Theme> 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); }
}

View File

@ -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); }

View File

@ -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.
*
* @since Envoy 0.3-alpha
* @param parent the {@link SettingsScreen} as a part of which this
* {@link SettingsPanel} is displayed
* @since Envoy v0.3-alpha
*/
public GeneralSettingsPanel() {
public GeneralSettingsPanel(SettingsScreen parent) {
super(parent);
theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
setBackground(theme.getCellColor());

View File

@ -0,0 +1,226 @@
package envoy.client.ui.settings;
import java.awt.*;
import java.util.function.Consumer;
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;
/**
* 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 final Consumer<String> newThemeAction, modifyThemeAction;
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, if the name already exists.
*
* @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(SettingsScreen parent, Consumer<String> newThemeAction, Consumer<String> modifyThemeAction) {
this.newThemeAction = newThemeAction;
this.modifyThemeAction = modifyThemeAction;
setLocationRelativeTo(parent);
setTitle("New Theme");
setModal(true);
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);
}
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) {
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()) 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) {
// 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) -> { setDimensions(true); loadStandardContent(theme); });
overwrite.addActionListener((evt) -> { modifyThemeAction.accept(nameEnterTextArea.getText()); dispose(); });
}
}

View File

@ -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}

View File

@ -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);

View File

@ -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<String> themes = new JComboBox<>(themeArray);
private Theme temporaryTheme, selectedTheme;
private boolean themeChanged = false;
private DefaultComboBoxModel<String> themesModel = new DefaultComboBoxModel<>(
Settings.getInstance().getThemes().keySet().toArray(new String[0]));
private JComboBox<String> 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,45 +99,54 @@ 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 {
String name = JOptionPane.showInputDialog("Enter a name for the new theme");
new NewThemeScreen(parent, (name) -> {
// Create 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()));
// Add new theme name to combo box
themesModel.addElement(name);
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();
}
// 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;
}
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();
};
}
@ -157,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);
}
@ -173,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);
@ -193,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();
}
});