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/NewThemeScreen.java

229 lines
7.8 KiB
Java

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