diff --git a/src/main/java/ui/view/pane/settings/DebugSettingsComponent.java b/src/main/java/ui/view/pane/settings/DebugSettingsComponent.java new file mode 100644 index 0000000..cfe9618 --- /dev/null +++ b/src/main/java/ui/view/pane/settings/DebugSettingsComponent.java @@ -0,0 +1,37 @@ +package ui.view.pane.settings; + +import settings.debug.DebugSettings; + +import javax.swing.*; +import java.awt.*; + +import static java.awt.BorderLayout.CENTER; + +class DebugSettingsComponent implements SettingsComponent { + private final JComponent component; + + DebugSettingsComponent(DebugSettings debugSettings) { + this.component = new JPanel(new BorderLayout()); + + JCheckBox loggingCheckbox = new JCheckBox("Enabled"); + loggingCheckbox.setSelected(debugSettings.logging()); + loggingCheckbox.addActionListener(e -> debugSettings.setLogging(loggingCheckbox.isSelected())); + + component.add(loggingCheckbox, CENTER); + } + + @Override + public String title() { + return "Logging"; + } + + @Override + public String description() { + return "Use this setting to enable or disable logging"; + } + + @Override + public JComponent component() { + return component; + } +} diff --git a/src/main/java/ui/view/pane/settings/DebugSettingsPanel.java b/src/main/java/ui/view/pane/settings/DebugSettingsPanel.java deleted file mode 100644 index b86c125..0000000 --- a/src/main/java/ui/view/pane/settings/DebugSettingsPanel.java +++ /dev/null @@ -1,47 +0,0 @@ -package ui.view.pane.settings; - -import settings.debug.DebugSettings; -import ui.view.component.HeaderLabel; - -import javax.swing.*; -import java.awt.*; - -import static java.awt.GridBagConstraints.FIRST_LINE_START; -import static java.awt.GridBagConstraints.HORIZONTAL; - -class DebugSettingsPanel extends JPanel { - DebugSettingsPanel(DebugSettings debugSettings) { - GridBagLayout layout = new GridBagLayout(); - layout.columnWidths = new int[]{0, 20}; - layout.rowHeights = new int[]{0, 10, 0, 5, 0}; - setLayout(layout); - - JCheckBox loggingCheckbox = new JCheckBox("Enabled"); - loggingCheckbox.setSelected(debugSettings.logging()); - loggingCheckbox.addActionListener(e -> debugSettings.setLogging(loggingCheckbox.isSelected())); - - GridBagConstraints constraints = new GridBagConstraints(); - - constraints.gridx = 0; - constraints.gridy = 0; - constraints.anchor = FIRST_LINE_START; - constraints.gridwidth = 3; - JLabel headerLabel = new HeaderLabel("Logging"); - add(headerLabel, constraints); - - constraints.gridx = 0; - constraints.gridy = 2; - constraints.anchor = FIRST_LINE_START; - constraints.gridwidth = 3; - constraints.weightx = 1.0; - constraints.fill = HORIZONTAL; - JLabel descriptionLabel = new JLabel("Use this setting to enable or disable logging"); - add(descriptionLabel, constraints); - - constraints.gridx = 0; - constraints.gridy = 4; - constraints.anchor = FIRST_LINE_START; - constraints.gridwidth = 3; - add(loggingCheckbox, constraints); - } -} diff --git a/src/main/java/ui/view/pane/settings/DefaultSaveLocationSettingsPanel.java b/src/main/java/ui/view/pane/settings/DefaultSaveLocationSettingsComponent.java similarity index 71% rename from src/main/java/ui/view/pane/settings/DefaultSaveLocationSettingsPanel.java rename to src/main/java/ui/view/pane/settings/DefaultSaveLocationSettingsComponent.java index f4fd8ca..9a83bea 100644 --- a/src/main/java/ui/view/pane/settings/DefaultSaveLocationSettingsPanel.java +++ b/src/main/java/ui/view/pane/settings/DefaultSaveLocationSettingsComponent.java @@ -1,7 +1,6 @@ package ui.view.pane.settings; import settings.defaultsavelocation.DefaultSaveLocationSettings; -import ui.view.component.HeaderLabel; import ui.view.component.filechooser.FileChooser; import javax.swing.*; @@ -10,19 +9,16 @@ import java.util.Optional; import static java.awt.GridBagConstraints.FIRST_LINE_START; -import static java.awt.GridBagConstraints.HORIZONTAL; import static ui.view.component.filechooser.ChooseMode.DIRECTORIES_ONLY; -class DefaultSaveLocationSettingsPanel extends JPanel { - private final JLabel headerLabel = new HeaderLabel("Default save location for BChecks"); - private final JLabel descriptionLabel = new JLabel("Use this setting to define where BChecks should be saved to by default. If you don't use this setting, you'll be asked where to save the BCheck to every time that you save one"); +class DefaultSaveLocationSettingsComponent implements SettingsComponent { + private final JPanel component = new JPanel(); private final JButton chooseFileButton = new JButton("Choose directory"); private final JCheckBox useSettingCheckbox = new JCheckBox("Enabled"); - - private final DefaultSaveLocationSettings defaultSaveLocationSettings; private final JTextField pathField = new JTextField(); + private final DefaultSaveLocationSettings defaultSaveLocationSettings; - DefaultSaveLocationSettingsPanel(DefaultSaveLocationSettings defaultSaveLocationSettings) { + DefaultSaveLocationSettingsComponent(DefaultSaveLocationSettings defaultSaveLocationSettings) { this.defaultSaveLocationSettings = defaultSaveLocationSettings; initialiseUi(); @@ -41,8 +37,8 @@ private void initialiseUi() { private void setupLayout() { GridBagLayout layout = new GridBagLayout(); layout.columnWidths = new int[]{0, 20, 0}; - layout.rowHeights = new int[]{0, 10, 0, 5, 0, 5, 0}; - setLayout(layout); + layout.rowHeights = new int[]{0, 5, 0}; + component.setLayout(layout); } private void setupPathLabel() { @@ -81,8 +77,7 @@ private void setupUseSettingCheckbox() { pathField.setVisible(true); defaultSaveLocationSettings.setUseSetting(true); - } - else { + } else { chooseFileButton.setVisible(false); pathField.setVisible(false); @@ -98,33 +93,33 @@ private void addElements() { constraints.gridx = 0; constraints.gridy = 0; constraints.anchor = FIRST_LINE_START; - constraints.gridwidth = 3; - add(headerLabel, constraints); - - constraints.gridx = 0; - constraints.gridy = 2; - constraints.anchor = FIRST_LINE_START; - constraints.gridwidth = 3; - constraints.weightx = 1.0; - constraints.fill = HORIZONTAL; - add(descriptionLabel, constraints); - - constraints.gridx = 0; - constraints.gridy = 4; - constraints.anchor = FIRST_LINE_START; - constraints.gridwidth = 3; - add(useSettingCheckbox, constraints); + component.add(useSettingCheckbox, constraints); constraints = new GridBagConstraints(); constraints.gridx = 0; - constraints.gridy = 6; + constraints.gridy = 2; constraints.anchor = FIRST_LINE_START; - add(pathField, constraints); + component.add(pathField, constraints); constraints = new GridBagConstraints(); constraints.gridx = 2; - constraints.gridy = 6; + constraints.gridy = 2; constraints.anchor = FIRST_LINE_START; - add(chooseFileButton, constraints); + component.add(chooseFileButton, constraints); + } + + @Override + public String title() { + return "Default save location for BChecks"; + } + + @Override + public String description() { + return "Use this setting to define where BChecks should be saved to by default. If you don't use this setting, you'll be asked where to save the BCheck to every time that you save one"; + } + + @Override + public JComponent component() { + return component; } } diff --git a/src/main/java/ui/view/pane/settings/GitHubSettingsPanel.java b/src/main/java/ui/view/pane/settings/GitHubSettingsComponent.java similarity index 71% rename from src/main/java/ui/view/pane/settings/GitHubSettingsPanel.java rename to src/main/java/ui/view/pane/settings/GitHubSettingsComponent.java index 1eb012c..fd6bd0f 100644 --- a/src/main/java/ui/view/pane/settings/GitHubSettingsPanel.java +++ b/src/main/java/ui/view/pane/settings/GitHubSettingsComponent.java @@ -1,7 +1,6 @@ package ui.view.pane.settings; import settings.github.GitHubSettings; -import ui.view.component.HeaderLabel; import ui.view.listener.SingleHandlerDocumentListener; import javax.swing.*; @@ -9,9 +8,8 @@ import static java.awt.GridBagConstraints.FIRST_LINE_START; -class GitHubSettingsPanel extends JPanel { - private final JLabel headerLabel = new HeaderLabel("GitHub configuration"); - private final JLabel descriptionLabelFirstLine = new JLabel("Use these settings to define which GitHub repo the extension looks at to find BChecks."); +class GitHubSettingsComponent implements SettingsComponent { + private final JPanel component = new JPanel(); private final JLabel descriptionLabelSecondLine = new JLabel("If the repo isn't public, you'll need to specify an API key too. You can look at GitHub's documentation to find out how to create one."); private final JLabel descriptionLabelThirdLine = new JLabel("If you're using the same API key across multiple applications, you might exceed GitHub's rate limit, meaning that this extension will no longer work until the rate limit resets."); private final JLabel descriptionLabelFourthLine = new JLabel("Once you've changed these settings, you'll need to refresh the store for them to take effect."); @@ -22,7 +20,7 @@ class GitHubSettingsPanel extends JPanel { private final GitHubSettings gitHubSettings; - GitHubSettingsPanel(GitHubSettings gitHubSettings) { + GitHubSettingsComponent(GitHubSettings gitHubSettings) { this.gitHubSettings = gitHubSettings; initialiseUi(); @@ -40,9 +38,9 @@ private void initialiseUi() { private void setupLayout() { GridBagLayout layout = new GridBagLayout(); layout.columnWidths = new int[]{0, 20, 0}; - layout.rowHeights = new int[]{0, 10, 0, 5, 0, 5, 0, 5, 0, 30, 0, 5, 0}; + layout.rowHeights = new int[]{0, 5, 0, 5, 0, 30, 0, 5, 0}; - setLayout(layout); + component.setLayout(layout); } private void setupRepoNameField() { @@ -63,58 +61,61 @@ private void setupApiKeyField() { private void addElements() { GridBagConstraints constraints = new GridBagConstraints(); - constraints.anchor = FIRST_LINE_START; - constraints.gridx = 0; constraints.gridy = 0; constraints.gridwidth = 3; - add(headerLabel, constraints); + component.add(descriptionLabelSecondLine, constraints); constraints = new GridBagConstraints(); constraints.anchor = FIRST_LINE_START; constraints.gridy = 2; constraints.gridwidth = 3; - add(descriptionLabelFirstLine, constraints); + component.add(descriptionLabelThirdLine, constraints); constraints = new GridBagConstraints(); constraints.anchor = FIRST_LINE_START; constraints.gridy = 4; constraints.gridwidth = 3; - add(descriptionLabelSecondLine, constraints); + component.add(descriptionLabelFourthLine, constraints); constraints = new GridBagConstraints(); constraints.anchor = FIRST_LINE_START; constraints.gridy = 6; - constraints.gridwidth = 3; - add(descriptionLabelThirdLine, constraints); - - constraints = new GridBagConstraints(); - constraints.anchor = FIRST_LINE_START; - constraints.gridy = 8; - constraints.gridwidth = 3; - add(descriptionLabelFourthLine, constraints); - - constraints = new GridBagConstraints(); - constraints.anchor = FIRST_LINE_START; - constraints.gridy = 10; - add(repoNameDescription, constraints); + component.add(repoNameDescription, constraints); constraints = new GridBagConstraints(); constraints.anchor = FIRST_LINE_START; constraints.gridx = 2; - constraints.gridy = 10; + constraints.gridy = 6; constraints.weightx = 1; - add(repoNameField, constraints); + component.add(repoNameField, constraints); constraints = new GridBagConstraints(); constraints.anchor = FIRST_LINE_START; - constraints.gridy = 12; - add(apiKeyDescription, constraints); + constraints.gridy = 8; + component.add(apiKeyDescription, constraints); constraints = new GridBagConstraints(); constraints.anchor = FIRST_LINE_START; constraints.gridx = 2; - constraints.gridy = 12; - add(apiKeyField, constraints); + constraints.gridy = 8; + constraints.insets = new Insets(0, 0, 10, 0); + + component.add(apiKeyField, constraints); + } + + @Override + public String title() { + return "GitHub configuration"; + } + + @Override + public String description() { + return "Use these settings to define which GitHub repo the extension looks at to find BChecks."; + } + + @Override + public JComponent component() { + return component; } } diff --git a/src/main/java/ui/view/pane/settings/Settings.java b/src/main/java/ui/view/pane/settings/Settings.java index c0e6e26..8c1292d 100644 --- a/src/main/java/ui/view/pane/settings/Settings.java +++ b/src/main/java/ui/view/pane/settings/Settings.java @@ -3,85 +3,36 @@ import settings.controller.SettingsController; import javax.swing.*; -import javax.swing.border.EmptyBorder; import java.awt.*; +import java.util.List; -import static java.awt.Color.LIGHT_GRAY; import static java.awt.GridBagConstraints.FIRST_LINE_START; import static java.awt.GridBagConstraints.HORIZONTAL; public class Settings extends JPanel { - private final JComponent defaultSaveLocationSettingsPanel; - private final JComponent gitHubSettingsPanel; - private final JComponent debugSettingsPanel; public Settings(SettingsController settingsController) { setLayout(new GridBagLayout()); - this.defaultSaveLocationSettingsPanel = new DefaultSaveLocationSettingsPanel(settingsController.defaultSaveLocationSettings()); - this.gitHubSettingsPanel = new GitHubSettingsPanel(settingsController.gitHubSettings()); - this.debugSettingsPanel = new DebugSettingsPanel(settingsController.debugSettings()); - - initialiseUi(); - } - - private void initialiseUi() { - GridBagConstraints constraints = new GridBagConstraints(); - constraints.gridx = 0; - constraints.gridy = 0; - constraints.anchor = FIRST_LINE_START; - constraints.weightx = 1.0; - constraints.fill = HORIZONTAL; - - defaultSaveLocationSettingsPanel.setBorder(new EmptyBorder(15, 15, 15, 15)); - - add(defaultSaveLocationSettingsPanel, constraints); - - constraints = new GridBagConstraints(); - constraints.gridx = 0; - constraints.gridy = 1; - constraints.anchor = FIRST_LINE_START; - constraints.weightx = 1.0; - constraints.fill = HORIZONTAL; - - add(createSeparator(), constraints); - - constraints = new GridBagConstraints(); - constraints.gridx = 0; - constraints.gridy = 2; - constraints.anchor = FIRST_LINE_START; - constraints.weightx = 1.0; - constraints.fill = HORIZONTAL; - - gitHubSettingsPanel.setBorder(new EmptyBorder(15, 15, 25, 15)); - - add(gitHubSettingsPanel, constraints); - - constraints = new GridBagConstraints(); - constraints.gridx = 0; - constraints.gridy = 3; - constraints.anchor = FIRST_LINE_START; - constraints.weightx = 1.0; - constraints.fill = HORIZONTAL; - - add(createSeparator(), constraints); - - constraints = new GridBagConstraints(); - constraints.gridx = 0; - constraints.gridy = 4; - constraints.anchor = FIRST_LINE_START; - constraints.weightx = 1.0; - constraints.weighty = 1.0; - constraints.fill = HORIZONTAL; - - debugSettingsPanel.setBorder(new EmptyBorder(15, 15, 15, 15)); - - add(debugSettingsPanel, constraints); - } - - private static JSeparator createSeparator() { - JSeparator separator = new JSeparator(); - separator.setForeground(LIGHT_GRAY); - return separator; + List settingsComponentList = List.of( + new DefaultSaveLocationSettingsComponent(settingsController.defaultSaveLocationSettings()), + new GitHubSettingsComponent(settingsController.gitHubSettings()), + new DebugSettingsComponent(settingsController.debugSettings()) + ); + + for (int i = 0; i < settingsComponentList.size(); i++) { + SettingsComponent settingsComponent = settingsComponentList.get(i); + boolean isLast = i + 1 == settingsComponentList.size(); + + GridBagConstraints constraints = new GridBagConstraints(); + constraints.gridx = 0; + constraints.gridy = i; + constraints.anchor = FIRST_LINE_START; + constraints.weightx = 1.0; + constraints.weighty = isLast ? 1.0 : 0; + constraints.fill = HORIZONTAL; + + add(new SettingsPanel(settingsComponent), constraints); + } } } diff --git a/src/main/java/ui/view/pane/settings/SettingsComponent.java b/src/main/java/ui/view/pane/settings/SettingsComponent.java new file mode 100644 index 0000000..6b532b7 --- /dev/null +++ b/src/main/java/ui/view/pane/settings/SettingsComponent.java @@ -0,0 +1,11 @@ +package ui.view.pane.settings; + +import javax.swing.*; + +interface SettingsComponent { + String title(); + + String description(); + + JComponent component(); +} diff --git a/src/main/java/ui/view/pane/settings/SettingsPanel.java b/src/main/java/ui/view/pane/settings/SettingsPanel.java new file mode 100644 index 0000000..f94b840 --- /dev/null +++ b/src/main/java/ui/view/pane/settings/SettingsPanel.java @@ -0,0 +1,50 @@ +package ui.view.pane.settings; + +import ui.view.component.HeaderLabel; + +import javax.swing.*; +import java.awt.*; + +import static java.awt.Color.LIGHT_GRAY; +import static java.awt.GridBagConstraints.FIRST_LINE_START; +import static java.awt.GridBagConstraints.HORIZONTAL; + +class SettingsPanel extends JPanel { + SettingsPanel(SettingsComponent settingsComponent) { + setLayout(new GridBagLayout()); + + GridBagConstraints constraints = new GridBagConstraints(); + constraints.gridx = 0; + constraints.gridy = 0; + constraints.anchor = FIRST_LINE_START; + constraints.insets = new Insets(15, 15, 10 ,15); + + add(new HeaderLabel(settingsComponent.title()), constraints); + + constraints = new GridBagConstraints(); + constraints.gridx = 0; + constraints.gridy = 1; + constraints.anchor = FIRST_LINE_START; + constraints.insets = new Insets(0, 15, 5 ,15); + add(new JLabel(settingsComponent.description()), constraints); + + constraints = new GridBagConstraints(); + constraints.gridx = 0; + constraints.gridy = 2; + constraints.anchor = FIRST_LINE_START; + constraints.insets = new Insets(0, 15, 0 ,15); + constraints.weightx = 1.0; + constraints.weighty = 1.0; + add(settingsComponent.component(), constraints); + + JSeparator separator = new JSeparator(); + separator.setForeground(LIGHT_GRAY); + + constraints.gridx = 0; + constraints.gridy = 3; + constraints.anchor = FIRST_LINE_START; + constraints.insets = new Insets(15, 0, 0 ,0); + constraints.fill = HORIZONTAL; + add(separator, constraints); + } +}