Fixes #121 (first displayed theme is current theme)

Additionally removed okButton from SettingsScreen
This commit is contained in:
delvh 2020-03-22 17:05:28 +01:00
parent 68c7344137
commit 156beaf44b
5 changed files with 146 additions and 160 deletions

View File

@ -173,7 +173,7 @@ public class ComponentList<E> extends JPanel {
* @throws java.util.NoSuchElementException if no selection is present * @throws java.util.NoSuchElementException if no selection is present
* @since Envoy v0.1-beta * @since Envoy v0.1-beta
*/ */
public int getSingleSelection() { return selection.iterator().next(); } public int getSingleSelection() { return selection.stream().findAny().get(); }
/** /**
* @return an arbitrary selected element * @return an arbitrary selected element

View File

@ -3,7 +3,6 @@ package envoy.client.ui.settings;
import java.awt.GridBagConstraints; import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; import java.awt.GridBagLayout;
import java.awt.Insets; import java.awt.Insets;
import java.awt.event.ActionListener;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -87,7 +86,4 @@ public class GeneralSettingsPanel extends SettingsPanel {
add(descriptionText, gbc_descriptionText); add(descriptionText, gbc_descriptionText);
} }
@Override
public ActionListener getOkButtonAction() { return evt -> {}; }
} }

View File

@ -1,7 +1,5 @@
package envoy.client.ui.settings; package envoy.client.ui.settings;
import java.awt.event.ActionListener;
import javax.swing.JPanel; import javax.swing.JPanel;
/** /**
@ -29,11 +27,4 @@ public abstract class SettingsPanel extends JPanel {
* {@link SettingsPanel} is displayed * {@link SettingsPanel} is displayed
*/ */
public SettingsPanel(SettingsScreen parent) { this.parent = parent; } public SettingsPanel(SettingsScreen parent) { this.parent = parent; }
/**
* @return an {@link ActionListener} that should be invoked when the OK button
* is pressed in the {@link SettingsScreen}
* @since Envoy v0.2-alpha
*/
public abstract ActionListener getOkButtonAction();
} }

View File

@ -40,7 +40,6 @@ public class SettingsScreen extends JDialog {
// OK and cancel buttons // OK and cancel buttons
private final JPanel buttonPane = new JPanel(); private final JPanel buttonPane = new JPanel();
private final PrimaryButton okButton = new PrimaryButton("Save");
private final PrimaryButton cancelButton = new PrimaryButton("Cancel"); private final PrimaryButton cancelButton = new PrimaryButton("Cancel");
private final Insets insets = new Insets(5, 5, 5, 5); private final Insets insets = new Insets(5, 5, 5, 5);
@ -138,21 +137,6 @@ public class SettingsScreen extends JDialog {
cancelButton.addActionListener((evt) -> { dispose(); }); 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 = insets;
gbc_okButton.gridx = 2;
gbc_okButton.gridy = 0;
buttonPane.add(okButton, gbc_okButton);
getRootPane().setDefaultButton(okButton);
// Invoke settings panel action on button press
okButton.addActionListener((evt) -> { if (settingsPanel != null) settingsPanel.getOkButtonAction().actionPerformed(evt); });
}
} }
// Apply current theme // Apply current theme
@ -179,10 +163,6 @@ public class SettingsScreen extends JDialog {
cancelButton.setBackground(theme.getInteractableBackgroundColor()); cancelButton.setBackground(theme.getInteractableBackgroundColor());
cancelButton.setForeground(theme.getInteractableForegroundColor()); cancelButton.setForeground(theme.getInteractableForegroundColor());
// okButton
okButton.setBackground(theme.getInteractableBackgroundColor());
okButton.setForeground(theme.getInteractableForegroundColor());
// options // options
options.setSelectionForeground(theme.getUserNameColor()); options.setSelectionForeground(theme.getUserNameColor());
options.setSelectionBackground(theme.getSelectionColor()); options.setSelectionBackground(theme.getSelectionColor());

View File

@ -1,7 +1,6 @@
package envoy.client.ui.settings; package envoy.client.ui.settings;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionListener;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
@ -11,6 +10,7 @@ import envoy.client.data.Settings;
import envoy.client.event.ThemeChangeEvent; import envoy.client.event.ThemeChangeEvent;
import envoy.client.ui.Color; import envoy.client.ui.Color;
import envoy.client.ui.Theme; import envoy.client.ui.Theme;
import envoy.client.ui.primary.PrimaryButton;
import envoy.event.EventBus; import envoy.event.EventBus;
import envoy.util.EnvoyLog; import envoy.util.EnvoyLog;
@ -30,13 +30,13 @@ public class ThemeCustomizationPanel extends SettingsPanel {
private JPanel colorsPanel = new JPanel(); private JPanel colorsPanel = new JPanel();
private DefaultComboBoxModel<String> themesModel = new DefaultComboBoxModel<>( private DefaultComboBoxModel<String> themesModel;
Settings.getInstance().getThemes().keySet().toArray(new String[0])); private JComboBox<String> themes;
private JComboBox<String> themes = new JComboBox<>(themesModel);
private Theme temporaryTheme; private Theme temporaryTheme;
private boolean themeChanged; private PrimaryButton createThemeButton = new PrimaryButton("Create Theme");
private final Insets insets = new Insets(5, 5, 5, 5); 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 Logger logger = EnvoyLog.getLogger(ThemeCustomizationPanel.class);
private static final long serialVersionUID = -8697897390666456624L; private static final long serialVersionUID = -8697897390666456624L;
@ -54,12 +54,22 @@ public class ThemeCustomizationPanel extends SettingsPanel {
super(parent); super(parent);
temporaryTheme = new Theme("temporaryTheme", Settings.getInstance().getCurrentTheme()); 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(); GridBagLayout gbl_themeLayout = new GridBagLayout();
gbl_themeLayout.columnWidths = new int[] { 1, 1 }; gbl_themeLayout.columnWidths = new int[] { 1, 1 };
gbl_themeLayout.rowHeights = new int[] { 1, 1 }; gbl_themeLayout.rowHeights = new int[] { 1, 1, 1 };
gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 }; gbl_themeLayout.columnWeights = new double[] { 1.0, 1.0 };
gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0 }; gbl_themeLayout.rowWeights = new double[] { 0.01, 1.0, 0.01 };
setLayout(gbl_themeLayout); setLayout(gbl_themeLayout);
@ -95,6 +105,37 @@ public class ThemeCustomizationPanel extends SettingsPanel {
gbc_colorsPanel.insets = insets; gbc_colorsPanel.insets = insets;
add(colorsPanel, gbc_colorsPanel); 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()); colorsPanel.setBackground(theme.getCellColor());
// Apply theme upon selection // Apply theme upon selection
@ -120,37 +161,15 @@ public class ThemeCustomizationPanel extends SettingsPanel {
}); });
} }
@Override
public ActionListener getOkButtonAction() {
return (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;
}
};
}
private void applyTheme(Theme theme) { private void applyTheme(Theme theme) {
// themeContent // themeContent
setForeground(theme.getUserNameColor()); setForeground(theme.getUserNameColor());
setBackground(theme.getCellColor()); setBackground(theme.getCellColor());
// createThemeButton
createThemeButton.setForeground(theme.getInteractableForegroundColor());
createThemeButton.setBackground(theme.getInteractableBackgroundColor());
// themes // themes
themes.setBackground(theme.getInteractableBackgroundColor()); themes.setBackground(theme.getInteractableBackgroundColor());
themes.setForeground(theme.getInteractableForegroundColor()); themes.setForeground(theme.getInteractableForegroundColor());