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/settings/SettingsScreen.java

173 lines
5.6 KiB
Java

package envoy.client.ui.settings;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import envoy.client.data.Settings;
import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.Theme;
import envoy.client.ui.primary.PrimaryButton;
import envoy.event.EventBus;
import envoy.util.EnvoyLog;
/**
* This class provides the GUI to change the user specific settings.<br>
* <br>
* 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
* @since Envoy Client v0.2-alpha
*/
public class SettingsScreen extends JDialog {
private static final long serialVersionUID = 0L;
private final JPanel contentPanel = new JPanel();
// Settings panel list
private final DefaultListModel<String> optionsListModel = new DefaultListModel<>();
private final JList<String> options = new JList<>(optionsListModel);
// OK and cancel buttons
private final JPanel buttonPane = new JPanel();
private final PrimaryButton cancelButton = new PrimaryButton("Cancel");
private final Insets insets = new Insets(5, 5, 5, 5);
private SettingsPanel settingsPanel;
private static final Logger logger = EnvoyLog.getLogger(SettingsScreen.class);
/**
* Initializes the settings screen.
*
* @since Envoy Client v0.1-alpha
*/
public SettingsScreen() {
// Initialize settings pages
Map<String, Class<? extends SettingsPanel>> panels = new HashMap<>();
panels.put("General", GeneralSettingsPanel.class);
panels.put("Color Themes", ThemeCustomizationPanel.class);
setBounds(10, 10, 450, 650);
getContentPane().setLayout(new BorderLayout());
{
// ContentPane
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);
// Constraints for the settings panel
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 1;
gbc_panel.gridy = 0;
gbc_panel.anchor = GridBagConstraints.PAGE_START;
gbc_panel.insets = insets;
options.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
options.addListSelectionListener((listSelectionEvent) -> {
if (!listSelectionEvent.getValueIsAdjusting()) {
// Get selected settings panel
final String option = options.getSelectedValue();
logger.log(Level.FINEST, "Selected settings panel: " + option);
// Remove previous settings panel
if (settingsPanel != null) contentPanel.remove(settingsPanel);
try {
settingsPanel = panels.get(option).getDeclaredConstructor(getClass()).newInstance(this);
// Add selected settings panel
contentPanel.add(settingsPanel, gbc_panel);
revalidate();
repaint();
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
logger.log(Level.SEVERE, "Failed to invoke constructor of SettingsPanel " + option, e);
}
}
});
options.setFont(new Font("Arial", Font.PLAIN, 14));
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 = insets;
panels.keySet().forEach(name -> optionsListModel.addElement(name));
contentPanel.add(options, gbc_optionsList);
// ButtonPane
GridBagLayout gbl_buttonPane = new GridBagLayout();
gbl_buttonPane.columnWidths = new int[] { 1, 1 };
gbl_buttonPane.rowHeights = new int[] { 25 };
gbl_buttonPane.columnWeights = new double[] { 1.0, 1.0 };
gbl_buttonPane.rowWeights = new double[] { 0.0 };
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 = insets;
gbc_cancelButton.gridx = 0;
gbc_cancelButton.gridy = 0;
buttonPane.add(cancelButton, gbc_cancelButton);
cancelButton.addActionListener((evt) -> { dispose(); });
}
}
// Apply current theme
applyTheme(Settings.getInstance().getCurrentTheme());
// Respond to theme changes
EventBus.getInstance().register(ThemeChangeEvent.class, evt -> applyTheme(evt.get()));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModal(true);
}
private void applyTheme(Theme theme) {
// JDialog
setBackground(theme.getBackgroundColor());
// contentPanel
contentPanel.setBackground(theme.getBackgroundColor());
// buttonPane
buttonPane.setBackground(theme.getCellColor());
// cancelButton
cancelButton.setBackground(theme.getInteractableBackgroundColor());
cancelButton.setForeground(theme.getInteractableForegroundColor());
// options
options.setSelectionForeground(theme.getUserNameColor());
options.setSelectionBackground(theme.getSelectionColor());
options.setForeground(theme.getUserNameColor());
options.setBackground(theme.getCellColor());
}
}