Added GeneralSettingsPane, added JavaFX support in SettingsItem

Also removed the old settings ui components.
This commit is contained in:
Kai S. K. Engelbart 2020-04-18 12:03:32 +02:00
parent f18525476e
commit 2cba9352df
11 changed files with 47 additions and 832 deletions

View File

@ -103,7 +103,7 @@ public class Settings {
private void supplementDefaults() {
items.putIfAbsent("enterToSend", new SettingsItem<>(true, "Enter to send", "Sends a message by pressing the enter key."));
items.putIfAbsent("onCloseMode", new SettingsItem<>(true, "Hide on close", "Hides the chat window when it is closed."));
items.putIfAbsent("currentTheme", new SettingsItem<>("dark", null));
items.putIfAbsent("currentTheme", new SettingsItem<>("dark", "Current Theme Name", "The name of the currently selected theme."));
}
/**

View File

@ -4,10 +4,11 @@ import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import javax.swing.JComponent;
import envoy.client.ui.primary.PrimaryToggleSwitch;
import javafx.scene.Node;
/**
* Encapsulates a persistent value that is directly or indirectly mutable by the
@ -23,18 +24,18 @@ import envoy.client.ui.primary.PrimaryToggleSwitch;
*/
public class SettingsItem<T> implements Serializable {
private T value;
private Class<? extends JComponent> componentClass;
private String userFriendlyName, description;
private T value;
private String userFriendlyName, description;
transient private Consumer<T> changeHandler;
private transient Consumer<T> changeHandler;
private transient Function<SettingsItem<?>, Node> nodeCreator;
private static final Map<Class<?>, Class<? extends JComponent>> componentClasses = new HashMap<>();
private static final Map<Class<?>, Function<SettingsItem<?>, Node>> nodeCreators = new HashMap<>();
private static final long serialVersionUID = 0L;
private static final long serialVersionUID = 1L;
static {
componentClasses.put(Boolean.class, PrimaryToggleSwitch.class);
}
/**
@ -48,37 +49,11 @@ public class SettingsItem<T> implements Serializable {
* @since Envoy Client v0.3-alpha
*/
public SettingsItem(T value, String userFriendlyName, String description) {
this(value, componentClasses.get(value.getClass()));
this.value = value;
this.userFriendlyName = userFriendlyName;
this.description = description;
}
/**
* Initializes a {@link SettingsItem}. The default value's class will be mapped
* to a specific {@link JComponent}. The mapping can also be disables if this
* parameter is {@code null}. In that case a {@link NullPointerException} will
* be thrown if the method {@link SettingsItem#getComponent()} is called.
*
* @param value the default value
* @param componentClass the class of the {@link JComponent} to represent this
* {@link SettingsItem} with
* @since Envoy Client v0.3-alpha
*/
public SettingsItem(T value, Class<? extends JComponent> componentClass) {
this.value = value;
this.componentClass = componentClass;
}
/**
* @return an instance of the {@link JComponent} that represents this
* {@link SettingsItem}
* @throws ReflectiveOperationException if the component initialization failed
* @throws SecurityException if the component initialization failed
* @since Envoy Client v0.3-alpha
*/
public JComponent getComponent() throws ReflectiveOperationException, SecurityException {
if (componentClass == null) throw new NullPointerException("Component class is null");
return componentClass.getConstructor(SettingsItem.class).newInstance(this);
if (nodeCreators.containsKey(value.getClass())) nodeCreator = nodeCreators.get(value.getClass());
}
/**
@ -99,18 +74,6 @@ public class SettingsItem<T> implements Serializable {
this.value = value;
}
/**
* @return the componentClass
* @since Envoy Client v0.3-alpha
*/
public Class<? extends JComponent> getComponentClass() { return componentClass; }
/**
* @param componentClass the componentClass to set
* @since Envoy Client v0.3-alpha
*/
public void setComponentClass(Class<? extends JComponent> componentClass) { this.componentClass = componentClass; }
/**
* @return the userFriendlyName
* @since Envoy Client v0.3-alpha
@ -147,4 +110,8 @@ public class SettingsItem<T> implements Serializable {
this.changeHandler = changeHandler;
changeHandler.accept(value);
}
public boolean hasNodeCreator() { return nodeCreator != null; }
public Node getNode() { return nodeCreator.apply(this); }
}

View File

@ -0,0 +1,21 @@
package envoy.client.ui;
import envoy.client.data.Settings;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>GeneralSettingsPane.java</strong><br>
* Created: <strong>18.04.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public class GeneralSettingsPane extends SettingsPane {
private static final Settings settings = Settings.getInstance();
/**
* @since Envoy Client v0.1-beta
*/
public GeneralSettingsPane() { super("General"); }
}

View File

@ -1,7 +1,6 @@
package envoy.client.ui;
import javafx.scene.Node;
import javafx.scene.layout.Pane;
/**
* Project: <strong>envoy-client</strong><br>
@ -11,9 +10,15 @@ import javafx.scene.Node;
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public abstract class SettingsPane extends Node {
public abstract class SettingsPane extends Pane {
protected String title;
public abstract String getTitle();
protected SettingsPane(String title) { this.title = title; }
/**
* @return the title of this settings pane
* @since Envoy Client v0.1-beta
*/
public String getTitle() { return title; }
}

View File

@ -3,8 +3,6 @@ package envoy.client.ui;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import envoy.client.data.Settings;
/**
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsSceneController.java</strong><br>
@ -13,7 +11,7 @@ import envoy.client.data.Settings;
* @author Kai S. K. Engelbart
* @since Envoy Client v0.1-beta
*/
public class SettingsSceneController {
public final class SettingsSceneController {
@FXML
private ListView<SettingsPane> settingsList;
@ -21,8 +19,6 @@ public class SettingsSceneController {
@FXML
private TitledPane titledPane;
private static final Settings settings = Settings.getInstance();
@FXML
private void initialize() {
settingsList.setCellFactory(listView -> new ListCell<>() {

View File

@ -1,89 +0,0 @@
package envoy.client.ui.settings;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComponent;
import javax.swing.JTextPane;
import envoy.client.data.Settings;
import envoy.client.data.SettingsItem;
import envoy.client.ui.Theme;
import envoy.util.EnvoyLog;
/**
* Displays GUI components that allow general settings regarding the client.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>GeneralSettingsPanel.java</strong><br>
* Created: <strong>21 Dec 2019</strong><br>
*
* @author Maximilian K&auml;fer
* @since Envoy Client v0.3-alpha
*/
public class GeneralSettingsPanel extends SettingsPanel {
private Theme theme;
private static final String[] items = { "onCloseMode", "enterToSend" };
private static final Logger logger = EnvoyLog.getLogger(GeneralSettingsPanel.class);
private static final long serialVersionUID = 0L;
/**
* This is the constructor for the General class. Here the user can set general
* settings for the client.
*
* @param parent the {@link SettingsScreen} as a part of which this
* {@link SettingsPanel} is displayed
* @since Envoy Client v0.3-alpha
*/
public GeneralSettingsPanel(SettingsScreen parent) {
super(parent);
theme = Settings.getInstance().getCurrentTheme();
setBackground(theme.getCellColor());
GridBagLayout gbl_general = new GridBagLayout();
gbl_general.columnWidths = new int[] { 1, 1 };
gbl_general.rowHeights = new int[] { 1, 1, 1 };
gbl_general.columnWeights = new double[] { 1.0, 0.1 };
gbl_general.rowWeights = new double[] { 0.02, 0.02, 1.0 };
setLayout(gbl_general);
for (int i = 0; i < items.length; i++)
try {
createSettingElement(i, Settings.getInstance().getItems().get(items[i]));
} catch (SecurityException | ReflectiveOperationException e) {
logger.log(Level.WARNING, "Could not create settings item", e);
}
}
private void createSettingElement(int gridy, SettingsItem<?> settingsItem) throws SecurityException, ReflectiveOperationException {
JTextPane descriptionText = new JTextPane();
JComponent settingComponent = settingsItem.getComponent();
GridBagConstraints gbc_toggleSwitch = new GridBagConstraints();
gbc_toggleSwitch.gridx = 1;
gbc_toggleSwitch.gridy = gridy;
add(settingComponent, gbc_toggleSwitch);
descriptionText.setText(settingsItem.getDescription());
descriptionText.setBackground(theme.getBackgroundColor());
descriptionText.setForeground(theme.getBackgroundColor().invert());
descriptionText.setEditable(false);
GridBagConstraints gbc_descriptionText = new GridBagConstraints();
gbc_descriptionText.fill = GridBagConstraints.BOTH;
gbc_descriptionText.gridx = 0;
gbc_descriptionText.gridy = gridy;
gbc_descriptionText.insets = new Insets(5, 5, 5, 5);
add(descriptionText, gbc_descriptionText);
}
}

View File

@ -1,228 +0,0 @@
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.data.Settings;
import envoy.client.ui.Theme;
import envoy.client.ui.primary.PrimaryButton;
import envoy.client.ui.primary.PrimaryTextArea;
/**
* 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 Client 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 = 0L;
/**
* 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 Client 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().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

@ -1,30 +0,0 @@
package envoy.client.ui.settings;
import javax.swing.JPanel;
/**
* Serves as an interface between {@link SettingsScreen} and different
* {@link JPanel}s with actual settings that are defined as sub classes of this
* class.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>SettingsPanel.java</strong><br>
* Created: <strong>20 Dec 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @since Envoy Client v0.2-alpha
*/
public abstract class SettingsPanel extends JPanel {
protected final SettingsScreen parent;
private static final long serialVersionUID = 0L;
/**
* 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; }
}

View File

@ -1,172 +0,0 @@
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());
}
}

View File

@ -1,246 +0,0 @@
package envoy.client.ui.settings;
import java.awt.*;
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.Color;
import envoy.client.ui.Theme;
import envoy.client.ui.primary.PrimaryButton;
import envoy.event.EventBus;
import envoy.util.EnvoyLog;
/**
* Displays GUI components that allow changing the current {@Theme} and creating
* new ones.<br>
* <br>
* Project: <strong>envoy-client</strong><br>
* File: <strong>ThemeCustomizationPanel.java</strong><br>
* Created: <strong>20 Dec 2019</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Client v0.2-alpha
*/
public class ThemeCustomizationPanel extends SettingsPanel {
private JPanel colorsPanel = new JPanel();
private DefaultComboBoxModel<String> themesModel;
private JComboBox<String> themes;
private Theme temporaryTheme;
private PrimaryButton createThemeButton = new PrimaryButton("Create Theme");
private boolean themeChanged;
private final Insets insets = new Insets(5, 5, 5, 5);
private static final Logger logger = EnvoyLog.getLogger(ThemeCustomizationPanel.class);
private static final long serialVersionUID = 0L;
/**
* Initializes a {@link ThemeCustomizationPanel} that enables the user to change
* 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 Client v0.2-alpha
*/
public ThemeCustomizationPanel(SettingsScreen parent) {
super(parent);
temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getCurrentTheme());
var themeNames = Settings.getInstance().getThemes().keySet().toArray(new String[0]);
String currentThemeName = Settings.getInstance().getCurrentThemeName();
for (int i = 0; i < themeNames.length; i++)
if (currentThemeName.equals(themeNames[i])) {
themeNames[i] = themeNames[0];
themeNames[0] = currentThemeName;
break;
}
themesModel = new DefaultComboBoxModel<>(themeNames);
themes = new JComboBox<>(themesModel);
GridBagLayout gbl_themeLayout = new GridBagLayout();
gbl_themeLayout.columnWidths = new int[] { 1, 1 };
gbl_themeLayout.rowHeights = new int[] { 1, 1, 1 };
gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 };
gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0, 0.01 };
setLayout(gbl_themeLayout);
themes.setSelectedItem(Settings.getInstance().getCurrentTheme());
GridBagConstraints gbc_themes = new GridBagConstraints();
gbc_themes.fill = GridBagConstraints.HORIZONTAL;
gbc_themes.gridwidth = 2;
gbc_themes.gridx = 0;
gbc_themes.gridy = 0;
gbc_themes.anchor = GridBagConstraints.NORTHWEST;
gbc_themes.insets = new Insets(10, 10, 20, 10);
add(themes, gbc_themes);
GridBagLayout gbl_colorCustomizations = new GridBagLayout();
gbl_colorCustomizations.columnWidths = new int[] { 1, 1 };
gbl_colorCustomizations.rowHeights = new int[] { 1, 1, 1, 1, 1, 1, 1, 1, 1 };
gbl_colorCustomizations.columnWeights = new double[] { 1, 1 };
gbl_colorCustomizations.rowWeights = new double[] { 1, 1, 1, 1, 1, 1, 1, 1 };
colorsPanel.setLayout(gbl_colorCustomizations);
Theme theme = Settings.getInstance().getCurrentTheme();
buildCustomizeElements(theme);
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 = insets;
add(colorsPanel, gbc_colorsPanel);
createThemeButton.addActionListener((evt) -> {
if (themeChanged) {
new NewThemeScreen(parent, name -> {
// Create new theme
logger.log(Level.FINEST, name);
Settings.getInstance().addNewThemeToMap(new Theme(name, temporaryTheme));
// Add new theme name to combo box
themesModel.addElement(name);
// Select new theme name
themes.setSelectedIndex(themesModel.getSize() - 1);
}, name -> {
// Modify theme
Settings.getInstance().getThemes().replace(name, new Theme(name, temporaryTheme));
if (themes.getSelectedItem().equals(name))
EventBus.getInstance().dispatch(new ThemeChangeEvent(Settings.getInstance().getTheme(name)));
else themes.setSelectedItem(name);
}).setVisible(true);
themeChanged = false;
}
});
GridBagConstraints gbc_createThemeButton = new GridBagConstraints();
gbc_createThemeButton.fill = GridBagConstraints.HORIZONTAL;
gbc_createThemeButton.gridx = 0;
gbc_createThemeButton.gridy = 2;
gbc_createThemeButton.anchor = GridBagConstraints.CENTER;
gbc_createThemeButton.insets = insets;
add(createThemeButton, gbc_createThemeButton);
colorsPanel.setBackground(theme.getCellColor());
// Apply theme upon selection
themes.addItemListener(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 -> {
final Theme currentTheme = evt.get();
temporaryTheme = new Theme("temporaryTheme", currentTheme);
applyTheme(currentTheme);
});
}
private void applyTheme(Theme theme) {
// themeContent
setForeground(theme.getUserNameColor());
setBackground(theme.getCellColor());
// createThemeButton
createThemeButton.setForeground(theme.getInteractableForegroundColor());
createThemeButton.setBackground(theme.getInteractableBackgroundColor());
// themes
themes.setBackground(theme.getInteractableBackgroundColor());
themes.setForeground(theme.getInteractableForegroundColor());
colorsPanel.setBackground(theme.getCellColor());
// Color panel
updateColorVariables(theme);
revalidate();
repaint();
}
private void updateColorVariables(Theme theme) {
colorsPanel.removeAll();
buildCustomizeElements(theme);
}
private void buildCustomizeElements(Theme theme) {
buildCustomizeElement(theme, theme.getBackgroundColor(), "Background", "backgroundColor", 1);
buildCustomizeElement(theme, theme.getCellColor(), "Cells", "cellColor", 2);
buildCustomizeElement(theme, theme.getInteractableForegroundColor(), "Interactable Foreground", "interactableForegroundColor", 3);
buildCustomizeElement(theme, theme.getInteractableBackgroundColor(), "Interactable Background", "interactableBackgroundColor", 4);
buildCustomizeElement(theme, theme.getTextColor(), "Text Color", "textColor", 5);
buildCustomizeElement(theme, theme.getDateColor(), "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);
}
private void buildCustomizeElement(Theme theme, Color color, String name, String colorName, int gridy) {
JButton button = new JButton();
JTextPane textPane = new JTextPane();
textPane.setFont(new Font("Arial", Font.PLAIN, 14));
textPane.setBackground(theme.getBackgroundColor());
textPane.setForeground(theme.getBackgroundColor().invert());
textPane.setText(name);
textPane.setEditable(false);
button.setBackground(color);
button.setPreferredSize(new Dimension(25, 25));
button.addActionListener((evt) -> {
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);
}
});
GridBagConstraints gbc_textPane = new GridBagConstraints();
gbc_textPane.fill = GridBagConstraints.BOTH;
gbc_textPane.gridx = 0;
gbc_textPane.gridy = gridy;
gbc_textPane.anchor = GridBagConstraints.CENTER;
gbc_textPane.insets = insets;
colorsPanel.add(textPane, gbc_textPane);
GridBagConstraints gbc_button = new GridBagConstraints();
gbc_button.fill = GridBagConstraints.BOTH;
gbc_button.gridx = 1;
gbc_button.gridy = gridy;
gbc_button.anchor = GridBagConstraints.CENTER;
gbc_button.insets = insets;
colorsPanel.add(button, gbc_button);
}
}

View File

@ -1,9 +0,0 @@
/**
* This package contains user interface classes related to the settings screen.
*
* @author Kai S. K. Engelbart
* @author Leon Hofmeister
* @author Maximilian K&auml;fer
* @since Envoy Client v0.2-alpha
*/
package envoy.client.ui.settings;