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

509 lines
18 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.LayoutManager;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Arrays;
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;
/**
* 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<String>();
private JPanel buttonPane = new JPanel();
private JPanel themeContent = new JPanel();
private String[] themeArray = { Settings.getInstance().getThemes().get("dark").getThemeName(),
Settings.getInstance().getThemes().get("light").getThemeName() };
private JComboBox<String> themes = new JComboBox<String>(themeArray);
private GridBagConstraints gbc_themeContent = new GridBagConstraints();
private Theme selectedTheme = Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme());
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 boolean colorChanged = false;
private Theme temporaryTheme;
private static SettingsScreen settingsScreen;
// TODO: Add a JPanel with all the Information necessary:
// change (Picture,Username, Email, Password) and toggle(light/dark mode,
// "ctrl+enter"/"enter"
// to send a message directly)
/**
* Opens the settings screen.<br>
*
* @since Envoy v0.1-alpha
*/
public static void open() {
settingsScreen = new SettingsScreen();
settingsScreen.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
settingsScreen.setModal(true);
settingsScreen.setVisible(true);
}
/**
* Builds the settings screen.
*
* @since Envoy v0.1-alpha
*/
private SettingsScreen() {
System.out.println(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();
System.out.println(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());
options.setSelectionForeground(theme.getUserNameColor());
options.setSelectionBackground(theme.getSelectionColor());
options.setForeground(theme.getUserNameColor());
options.setBackground(theme.getCellColor());
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();
themeContent.setForeground(theme.getUserNameColor());
themeContent.setBackground(theme.getCellColor());
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.setBackground(theme.getUserNameColor());
themes.setForeground(theme.getBackgroundColor());
themes.setSelectedItem(Settings.getInstance().getCurrentTheme());
themes.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
String selectedValue = (String) themes.getSelectedItem();
System.out.println(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((LayoutManager) new BoxLayout(colorsPanel, BoxLayout.Y_AXIS));
colorsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getBackgroundColor(), "Background", 1);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getCellColor(), "Cells", 2);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableForegroundColor(),
"Interactable Foreground",
3);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableBackgroundColor(),
"Interactable Background",
4);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getMessageColorChat(), "Messages Chat", 5);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getDateColorChat(), "Date Chat", 6);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getSelectionColor(), "Selection", 7);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getTypingMessageColor(), "Typing Message", 8);
buildCustomizeElement(new JPanel(), new JButton(), new JTextPane(), theme, theme.getUserNameColor(), "User Names", 9);
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");
System.out.println(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();
// TODO: Create new Theme
} catch (Exception e) {
System.err.println("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().setUsername(Settings.getInstance().getUsername());// still temporary
Settings.getInstance().setEmail(Settings.getInstance().getEmail());// still temporary value
Settings.getInstance().setEnterToSend(Settings.getInstance().isEnterToSend());// still temporary
Settings.getInstance().setCurrentTheme(selectedTheme.getThemeName());
System.out.println(selectedTheme.getThemeName());
changeSettingsScreenColors(Settings.getInstance().getCurrentTheme());
updateColorVariables(Settings.getInstance().getCurrentTheme());
Settings.getInstance().save();
revalidate();
repaint();
} catch (Exception e) {
System.err.println("Something went wrong when changing the setting");
settingsScreen.dispose();
}
});
}
}
changeSettingsScreenColors(Settings.getInstance().getCurrentTheme());
}
private void changeSettingsScreenColors(String key) {
Theme theme = Settings.getInstance().getThemes().get(key);
// 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.getUserNameColor());
themes.setForeground(theme.getBackgroundColor());
createNewThemeButton.setBackground(theme.getInteractableBackgroundColor());
createNewThemeButton.setForeground(theme.getInteractableForegroundColor());
colorsPanel.setBackground(theme.getCellColor());
}
private void updateColorVariables(String key) {
Theme theme = Settings.getInstance().getThemes().get(key);
temporaryTheme = new Theme("temporaryTheme",
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getBackgroundColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getCellColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableForegroundColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getInteractableBackgroundColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getMessageColorChat(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getDateColorChat(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getSelectionColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getTypingMessageColor(),
Settings.getInstance().getThemes().get(Settings.getInstance().getCurrentTheme()).getUserNameColor());
colorsPanel.removeAll();
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getBackgroundColor(),
"Background",
1);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getCellColor(),
"Cells",
2);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableForegroundColor(),
"Interactable Foreground",
3);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getInteractableBackgroundColor(),
"Interactable Background",
4);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getMessageColorChat(),
"Messages Chat",
5);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getDateColorChat(),
"Date Chat",
6);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getSelectionColor(),
"Selection",
7);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getTypingMessageColor(),
"Typing Message",
8);
buildCustomizeElement(new JPanel(),
new JButton(),
new JTextPane(),
theme,
theme.getUserNameColor(),
"User Names",
9);
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(theme.getUserNameColor());
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()) {
System.out.println("New Color");
System.out.println(color.getRGB());
// TODO: When Theme changed in same settings screen, color variable doesnt
// update.
Color[] colorsArray = temporaryTheme.getAllColors();
for (int i = 0; i < colorsArray.length; i++) {
if (color.getRGB() == colorsArray[i].getRGB()) {
temporaryTheme.setColor(i, newColor);
colorChanged = true;
createNewThemeButton.setEnabled(true);
break;
}
}
button.setBackground(newColor);
}
} catch (Exception e) {
System.err.println("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);
}
}