This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/client/ui/SettingsScreen.java

459 lines
16 KiB
Java

package envoy.client.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import envoy.client.Settings;
import envoy.client.util.EnvoyLog;
import envoy.client.event.EventBus;
import envoy.client.event.ThemeChangeEvent;
/**
* This class provides the GUI to change the user specific settings.
*
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsScreen.java</strong><br>
* Created: <strong>31 Oct 2019</strong><br>
*
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @author Kai S. K. Engelbart
*/
public class SettingsScreen extends JDialog {
private static final long serialVersionUID = -4476913491263077107L;
private final JPanel contentPanel = new JPanel();
private DefaultListModel<String> optionsListModel = new DefaultListModel<>();
private final JList<String> options = new JList<>();
private JPanel buttonPane = new JPanel();
private JPanel themeContent = new JPanel();
private String[] themeArray = Settings.getInstance().getThemes().keySet().toArray(new String[0]);
private JComboBox<String> themes = new JComboBox<>(themeArray);
private GridBagConstraints gbc_themeContent = new GridBagConstraints();
private JButton createNewThemeButton = new JButton("Create New Theme");
private JPanel colorsPanel = new JPanel();
private JButton okButton = new JButton("Save");
private JButton cancelButton = new JButton("Cancel");
private static int space = 5;
private Theme temporaryTheme, selectedTheme;
private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class.getSimpleName());
/**
* Builds the settings screen.
*
* @since Envoy v0.1-alpha
*/
public SettingsScreen() {
logger.info(Settings.getInstance().getCurrentTheme());
setBounds(10, 10, 450, 650);
getContentPane().setLayout(new BorderLayout());
{
createNewThemeButton.setEnabled(false);
temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
// Content pane
GridBagLayout gbl_contentPanel = new GridBagLayout();
gbl_contentPanel.columnWidths = new int[] { 1, 1 };
gbl_contentPanel.rowHeights = new int[] { 1 };
gbl_contentPanel.columnWeights = new double[] { 0.05, 1.0 };
gbl_contentPanel.rowWeights = new double[] { 1.0 };
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(gbl_contentPanel);
options.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
options.addListSelectionListener((listSelectionEvent) -> {
if (!listSelectionEvent.getValueIsAdjusting()) {
@SuppressWarnings("unchecked")
final JList<String> selectedOption = (JList<String>) listSelectionEvent.getSource();
final String option = selectedOption.getSelectedValue();
logger.log(Level.FINEST, option);
switch (option) {
case "Color Themes":
setContent(themeContent, gbc_themeContent);
getContentPane().repaint();
getContentPane().revalidate();
break;
}
}
});
options.setFont(new Font("Arial", Font.PLAIN, 14));
Theme theme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
GridBagConstraints gbc_optionsList = new GridBagConstraints();
gbc_optionsList.fill = GridBagConstraints.BOTH;
gbc_optionsList.gridx = 0;
gbc_optionsList.gridy = 0;
gbc_optionsList.anchor = GridBagConstraints.PAGE_START;
gbc_optionsList.insets = new Insets(space, space, space, space);
optionsListModel.addElement("Color Themes");
options.setModel(optionsListModel);
contentPanel.add(options, gbc_optionsList);
// Theme content
gbc_themeContent = new GridBagConstraints();
gbc_themeContent.fill = GridBagConstraints.BOTH;
gbc_themeContent.gridx = 1;
gbc_themeContent.gridy = 0;
gbc_themeContent.anchor = GridBagConstraints.PAGE_START;
gbc_themeContent.insets = new Insets(space, space, space, space);
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 };
themeContent.setLayout(gbl_themeLayout);
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.gridx = 0;
gbc_themes.gridy = 0;
gbc_themes.anchor = GridBagConstraints.NORTHWEST;
gbc_themes.insets = new Insets(space, space, space, space);
themeContent.add(themes, gbc_themes);
colorsPanel.setLayout(new BoxLayout(colorsPanel, BoxLayout.Y_AXIS));
colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getBackgroundColor(), "Background", 0);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getCellColor(), "Cells", 1);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableForegroundColor(),
"Interactable Foreground",
2);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableBackgroundColor(),
"Interactable Background",
3);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getMessageColorChat(), "Messages Chat", 4);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getDateColorChat(), "Date Chat", 5);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getSelectionColor(), "Selection", 6);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getTypingMessageColor(), "Typing Message", 7);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getUserNameColor(), "User Names", 8);
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 = new Insets(space, 0, 0, 0);
themeContent.add(colorsPanel, gbc_colorsPanel);
createNewThemeButton.setBackground(theme.getInteractableBackgroundColor());
createNewThemeButton.setForeground(theme.getInteractableForegroundColor());
colorsPanel.setBackground(theme.getCellColor());
createNewThemeButton.addActionListener((evt) -> {
try {
String s = JOptionPane.showInputDialog("Enter a name for the new theme");
logger.log(Level.FINEST, s);
Settings.getInstance()
.addNewThemeToMap(new Theme(s, temporaryTheme.getBackgroundColor(), temporaryTheme.getCellColor(),
temporaryTheme.getInteractableForegroundColor(), temporaryTheme.getInteractableBackgroundColor(),
temporaryTheme.getMessageColorChat(), temporaryTheme.getDateColorChat(), temporaryTheme.getSelectionColor(),
temporaryTheme.getTypingMessageColor(), temporaryTheme.getUserNameColor()));
themeArray = Arrays.copyOf(themeArray, themeArray.length + 1);
themeArray[themeArray.length - 1] = Settings.getInstance().getThemes().get(s).getThemeName();
temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
createNewThemeButton.setEnabled(false);
themes.addItem(themeArray[themeArray.length - 1]);
contentPanel.revalidate();
contentPanel.repaint();
} 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);
// ButtonPane-------------------------------------------------------
GridBagLayout gbl_buttonPane = new GridBagLayout();
gbl_buttonPane.columnWidths = new int[] { 100, 250, 100, 0 };
gbl_buttonPane.rowHeights = new int[] { 25, 0 };
gbl_buttonPane.columnWeights = new double[] { 0.0, 0.0, 0.0, Double.MIN_VALUE };
gbl_buttonPane.rowWeights = new double[] { 0.0, Double.MIN_VALUE };
getContentPane().add(buttonPane, BorderLayout.SOUTH);
buttonPane.setLayout(gbl_buttonPane);
{
cancelButton.setActionCommand("Cancel");
cancelButton.setBorderPainted(false);
GridBagConstraints gbc_cancelButton = new GridBagConstraints();
gbc_cancelButton.anchor = GridBagConstraints.NORTHWEST;
gbc_cancelButton.insets = new Insets(space, space, space, space);
gbc_cancelButton.gridx = 0;
gbc_cancelButton.gridy = 0;
buttonPane.add(cancelButton, gbc_cancelButton);
cancelButton.addActionListener((evt) -> { dispose(); });
}
{
okButton.setActionCommand("OK");
okButton.setBorderPainted(false);
GridBagConstraints gbc_okButton = new GridBagConstraints();
gbc_okButton.anchor = GridBagConstraints.NORTHEAST;
gbc_okButton.fill = GridBagConstraints.EAST;
gbc_okButton.insets = new Insets(space, space, space, space);
gbc_okButton.gridx = 2;
gbc_okButton.gridy = 0;
buttonPane.add(okButton, gbc_okButton);
getRootPane().setDefaultButton(okButton);
okButton.addActionListener((evt) -> {
try {
Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary
Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName());
logger.log(Level.FINER, selectedTheme.getThemeName());
final Theme currentTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
changeSettingsScreenColors(currentTheme);
updateColorVariables(currentTheme);
EventBus.getInstance().dispatch(new ThemeChangeEvent(currentTheme));
Settings.getInstance().save();
revalidate();
repaint();
} catch (Exception e) {
logger.warning("Something went wrong when changing the setting");
JOptionPane.showMessageDialog(this, "Something went wrong when changing the setting");
dispose();
}
});
}
}
changeSettingsScreenColors(Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModal(true);
}
private void changeSettingsScreenColors(Theme theme) {
// whole JDialog
setBackground(theme.getBackgroundColor());
// contentPanel
contentPanel.setBackground(theme.getBackgroundColor());
// buttonPane
buttonPane.setBackground(theme.getCellColor());
// cancelButton
cancelButton.setBackground(theme.getInteractableBackgroundColor());
cancelButton.setForeground(theme.getInteractableForegroundColor());
// okButton
okButton.setBackground(theme.getInteractableBackgroundColor());
okButton.setForeground(theme.getInteractableForegroundColor());
// options
options.setSelectionForeground(theme.getUserNameColor());
options.setSelectionBackground(theme.getSelectionColor());
options.setForeground(theme.getUserNameColor());
options.setBackground(theme.getCellColor());
// themeContent
themeContent.setForeground(theme.getUserNameColor());
themeContent.setBackground(theme.getCellColor());
// themes
themes.setBackground(theme.getBackgroundColor());
themes.setForeground(getInvertedColor(theme.getBackgroundColor()));
createNewThemeButton.setBackground(theme.getInteractableBackgroundColor());
createNewThemeButton.setForeground(theme.getInteractableForegroundColor());
colorsPanel.setBackground(theme.getCellColor());
}
private void updateColorVariables(Theme theme) {
temporaryTheme = new Theme("temporaryTheme", theme);
colorsPanel.removeAll();
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getBackgroundColor(),
"Background",
0);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getCellColor(),
"Cells",
1);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableForegroundColor(),
"Interactable Foreground",
2);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableBackgroundColor(),
"Interactable Background",
3);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getMessageColorChat(),
"Messages Chat",
4);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getDateColorChat(),
"Date Chat",
5);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getSelectionColor(),
"Selection",
6);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getTypingMessageColor(),
"Typing Message",
7);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getUserNameColor(),
"User Names",
8);
GridBagConstraints gbc_createNewTheme = new GridBagConstraints();
gbc_createNewTheme.gridx = 0;
gbc_createNewTheme.gridy = 10;
colorsPanel.add(createNewThemeButton, gbc_createNewTheme);
}
private void setContent(JPanel content, GridBagConstraints layout) { contentPanel.add(content, layout); }
private void buildCustomizeElement(JPanel panel, JButton button, JTextPane textPane, Theme theme, Color color, String name, int yIndex) {
textPane.setFont(new Font("Arial", Font.PLAIN, 14));
textPane.setBackground(theme.getBackgroundColor());
textPane.setForeground(getInvertedColor(theme.getBackgroundColor()));
textPane.setText(name);
textPane.setEditable(false);
button.setBackground(color);
button.setPreferredSize(new Dimension(25, 25));
button.addActionListener((evt) -> {
try {
Color newColor = JColorChooser.showDialog(null, "Choose a color", color);
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 doesnt
// update.
temporaryTheme.setColor(yIndex, newColor);
createNewThemeButton.setEnabled(true);
}
button.setBackground(newColor);
} catch (Exception e) {
logger.info("An error occured while opening Color Chooser: " + e);
e.printStackTrace();
}
});
panel.add(textPane);
panel.add(button);
panel.setBackground(theme.getCellColor());
panel.setAlignmentX(Component.LEFT_ALIGNMENT);
colorsPanel.add(panel);
}
private Color getInvertedColor(Color color) { return new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue()); }
}