From 2de9769dd057e30eab175b46cbdb4c8b7c71ed17 Mon Sep 17 00:00:00 2001 From: Anas Elgarhy Date: Fri, 19 Aug 2022 17:38:28 +0200 Subject: [PATCH] =?UTF-8?q?Create=20the=20bse=20settings=20ui=20and=20Impr?= =?UTF-8?q?ove=20the=20code=20=F0=9F=A5=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../plugins/ayah/AyahStartupActivity.java | 6 +- .../settings/AyahSettingsConfigurable.java | 17 ++- .../ayah/settings/AyahSettingsState.java | 48 ++++++- .../plugins/ayah/settings/BasmalhOnStart.java | 46 +++++- .../plugins/ayah/settings/PanelBuilder.java | 41 ++++++ .../ayah/settings/SettingsComponent.java | 136 ++++++++++++++++++ .../plugins/ayah/settings/SettingsPanel.java | 8 -- src/main/resources/META-INF/plugin.xml | 2 +- 9 files changed, 277 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/anas/intellij/plugins/ayah/settings/PanelBuilder.java create mode 100644 src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsComponent.java delete mode 100644 src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsPanel.java diff --git a/build.gradle.kts b/build.gradle.kts index 690c1c6..41e6bb7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -13,7 +13,7 @@ repositories { dependencies { implementation("com.github.anas-elgarhy:alquran-cloud-api:0.4.0-v1") - implementation("org.projectlombok:lombok:1.18.24") + implementation("com.miglayout:miglayout-swing:11.0") } // Configure Gradle IntelliJ Plugin diff --git a/src/main/java/com/anas/intellij/plugins/ayah/AyahStartupActivity.java b/src/main/java/com/anas/intellij/plugins/ayah/AyahStartupActivity.java index 6685327..53d8888 100644 --- a/src/main/java/com/anas/intellij/plugins/ayah/AyahStartupActivity.java +++ b/src/main/java/com/anas/intellij/plugins/ayah/AyahStartupActivity.java @@ -15,9 +15,7 @@ import java.io.IOException; */ public class AyahStartupActivity implements StartupActivity { @Override - public void runActivity(@NotNull Project project) { - System.out.println("Hello World"); - + public void runActivity(@NotNull final Project project) { // Messages.showDialog(project, "Hi yoo", "My First Message Yooo", new String[]{"Ok"}, 0, Messages.getInformationIcon()); /* new Notification("com.anas.intellij.plugins.ayah.notificationGroup", "My First Notification", @@ -26,7 +24,7 @@ public class AyahStartupActivity implements StartupActivity { try { final var rAyah = Ayah.getRandomAyah(); NotificationGroupManager.getInstance() - .getNotificationGroup("com.anas.intellij.plugins.ayah.notificationGroup") + .getNotificationGroup("Random ayah from the quran") .createNotification(rAyah.getSurah().getName(), rAyah.getText(), NotificationType.INFORMATION).notify(project); } catch (final IOException e) { diff --git a/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsConfigurable.java b/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsConfigurable.java index 3f2ce6b..89055c9 100644 --- a/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsConfigurable.java +++ b/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsConfigurable.java @@ -13,6 +13,8 @@ import javax.swing.*; */ public class AyahSettingsConfigurable implements Configurable { + private SettingsComponent settingsComponent; + @Nls(capitalization = Nls.Capitalization.Title) @Override public String getDisplayName() { @@ -21,16 +23,27 @@ public class AyahSettingsConfigurable implements Configurable { @Override public @Nullable JComponent createComponent() { - return null; + settingsComponent = new SettingsComponent(); + return settingsComponent.getPanel(); } @Override public boolean isModified() { - return false; + return settingsComponent.isModified(); } @Override public void apply() throws ConfigurationException { } + + @Override + public void reset() { + settingsComponent.reset(); + } + + @Override + public void disposeUIResources() { + settingsComponent = null; + } } diff --git a/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsState.java b/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsState.java index d52201f..639ef71 100644 --- a/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsState.java +++ b/src/main/java/com/anas/intellij/plugins/ayah/settings/AyahSettingsState.java @@ -5,7 +5,6 @@ import com.intellij.openapi.components.PersistentStateComponent; import com.intellij.openapi.components.State; import com.intellij.openapi.components.Storage; import com.intellij.util.xmlb.XmlSerializerUtil; -import lombok.*; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,23 +12,27 @@ import org.jetbrains.annotations.Nullable; * @author: Anas Elgarhy * @date: 8/19/22 */ -@Getter -@Setter -@NoArgsConstructor(access = AccessLevel.PRIVATE) @State( name = "com.anas.intellij.plugins.ayah.settings.AyahSettingsState", storages = @Storage("ayah.xml") ) public class AyahSettingsState implements PersistentStateComponent { private BasmalhOnStart basmalhOnStart; - private long intervalTimeBetweenNotifications; - private boolean autoPlay; + private int intervalTimeBetweenNotifications; // in minutes + private boolean autoPlayAudio; private String playerId; public static AyahSettingsState getInstance() { return ApplicationManager.getApplication().getService(AyahSettingsState.class); } + private AyahSettingsState() { + basmalhOnStart = new BasmalhOnStart(); + intervalTimeBetweenNotifications = 30; // 30 minutes + autoPlayAudio = false; + playerId = null; + } + @Override public @Nullable AyahSettingsState getState() { @@ -40,4 +43,37 @@ public class AyahSettingsState implements PersistentStateComponentAnas Elgarhy * @date: 8/19/22 */ -@Getter -@Setter -@NoArgsConstructor public class BasmalhOnStart { private boolean isActive; private boolean isNotificationActive; private boolean isSoundActive; private String playerId; + + public BasmalhOnStart() { + isActive = true; + isNotificationActive = true; + isSoundActive = false; + playerId = null; + } + + public boolean isActive() { + return isActive; + } + + public void setActive(final boolean active) { + isActive = active; + } + + public boolean isNotificationActive() { + return isNotificationActive; + } + + public void setNotificationActive(final boolean notificationActive) { + isNotificationActive = notificationActive; + } + + public boolean isSoundActive() { + return isSoundActive; + } + + public void setSoundActive(final boolean soundActive) { + isSoundActive = soundActive; + } + + public String getPlayerId() { + return playerId; + } + + public void setPlayerId(final String playerId) { + this.playerId = playerId; + } } diff --git a/src/main/java/com/anas/intellij/plugins/ayah/settings/PanelBuilder.java b/src/main/java/com/anas/intellij/plugins/ayah/settings/PanelBuilder.java new file mode 100644 index 0000000..a437722 --- /dev/null +++ b/src/main/java/com/anas/intellij/plugins/ayah/settings/PanelBuilder.java @@ -0,0 +1,41 @@ +package com.anas.intellij.plugins.ayah.settings; + +import javax.swing.*; +import javax.swing.border.Border; +import java.awt.*; + +/** + * @author: Anas Elgarhy + * @date: 8/19/22 + */ +public class PanelBuilder { + private final JPanel panel; + + public PanelBuilder() { + panel = new JPanel(); + } + + public PanelBuilder setBorder(final Border border) { + panel.setBorder(border); + return this; + } + + public PanelBuilder setLayout(final LayoutManager layout) { + panel.setLayout(layout); + return this; + } + + public PanelBuilder addComponent(final Component component) { + panel.add(component); + return this; + } + + public PanelBuilder addComponent(final JComponent component, final Object constraints) { + panel.add(component, constraints); + return this; + } + + public JPanel build() { + return panel; + } +} diff --git a/src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsComponent.java b/src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsComponent.java new file mode 100644 index 0000000..0f156c7 --- /dev/null +++ b/src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsComponent.java @@ -0,0 +1,136 @@ +package com.anas.intellij.plugins.ayah.settings; + + +import com.anas.alqurancloudapi.edition.Edition; +import com.anas.alqurancloudapi.edition.EditionFormat; +import com.intellij.ui.components.JBCheckBox; +import com.intellij.ui.components.JBLabel; +import com.intellij.util.ui.FormBuilder; +import net.miginfocom.swing.MigLayout; + +import javax.swing.*; +import java.io.IOException; + +/** + * The settings UI. + * + * @author: Anas Elgarhy + * @date: 8/19/22 + */ +public class SettingsComponent { + private final JPanel panel; + private final JBCheckBox basmalhOnStartCheckBox; + private final JBCheckBox autoPlayBasmalhCheckBox; + private final JComboBox basmalhPlayerIdComboBox; + private final JSpinner notificationsIntervalSpinner; + private SpinnerNumberModel notificationsIntervalSpinnerModel; + private final JBCheckBox notificationsAudioCheckBox; + private final JComboBox ayahPlayerIdComboBox; + + { + basmalhOnStartCheckBox = new JBCheckBox("Basmalh on start"); + autoPlayBasmalhCheckBox = new JBCheckBox("Auto play basmalh audio"); + basmalhPlayerIdComboBox = new JComboBox<>(); + notificationsIntervalSpinner = new JSpinner(); + notificationsAudioCheckBox = new JBCheckBox("Notifications audio"); + ayahPlayerIdComboBox = new JComboBox<>(); + } + + public SettingsComponent() { + panel = FormBuilder.createFormBuilder() + .addComponent(new PanelBuilder() + .setLayout(new MigLayout("fill")) + .addComponent( + new PanelBuilder() + .setBorder(BorderFactory.createTitledBorder("Basmalh on start")) + .setLayout(new MigLayout()) + .addComponent(basmalhOnStartCheckBox, "span, grow") + .addComponent(autoPlayBasmalhCheckBox) + .addComponent(new JBLabel("Basmalh player"), + "gap unrelated") + .addComponent(basmalhPlayerIdComboBox, + "grow, wrap") + .build(), + "span, grow, wrap" + ) + .addComponent(new JBLabel("Notifications interval"), + "gap unrelated") + .addComponent(notificationsIntervalSpinner, "grow") + .addComponent(new JBLabel("Minutes"), "gap 1, wrap") + .addComponent(notificationsAudioCheckBox, "grow") + .addComponent(new JBLabel("Ayah player"), "gap unrelated") + .addComponent(ayahPlayerIdComboBox, "grow, wrap") + .build() + ) + .getPanel(); + + setup(); + } + + private void setup() { + final var settings = AyahSettingsState.getInstance(); + notificationsIntervalSpinnerModel = new SpinnerNumberModel(settings.getIntervalTimeBetweenNotifications(), + 1, Integer.MAX_VALUE, 1); + setupComboboxes(settings); + notificationsIntervalSpinner.setModel(notificationsIntervalSpinnerModel); + basmalhOnStartCheckBox.setSelected(settings.getBasmalhOnStart().isActive()); + autoPlayBasmalhCheckBox.setSelected(settings.getBasmalhOnStart().isSoundActive()); + autoPlayBasmalhCheckBox.setEnabled(settings.getBasmalhOnStart().isActive()); + notificationsAudioCheckBox.setSelected(settings.isAutoPlayAudio()); + addListeners(); + } + + private void addListeners() { + basmalhOnStartCheckBox.addActionListener(e -> + autoPlayBasmalhCheckBox.setEnabled(basmalhOnStartCheckBox.isSelected())); + + ayahPlayerIdComboBox.addActionListener(e -> + basmalhPlayerIdComboBox.setEnabled(basmalhOnStartCheckBox.isSelected())); + + notificationsAudioCheckBox.addActionListener(e -> + ayahPlayerIdComboBox.setEnabled(notificationsAudioCheckBox.isSelected())); + } + + private void setupComboboxes(final AyahSettingsState settings) { + if (settings.getBasmalhOnStart().getPlayerId() != null) { + basmalhPlayerIdComboBox.setSelectedItem(settings.getBasmalhOnStart().getPlayerId()); + } + if (settings.getPlayerId() != null) { + ayahPlayerIdComboBox.setSelectedItem(settings.getPlayerId()); + } + + try { + final var editions = Edition.getEditions(EditionFormat.AUDIO); + for (final var edition : editions) { + basmalhPlayerIdComboBox.addItem(edition.getEnglishName()); + ayahPlayerIdComboBox.addItem(edition.getEnglishName()); + } + } catch (final IOException e) { + e.printStackTrace(); + basmalhPlayerIdComboBox.addItem("Error can't get editions, please check internet connection"); + ayahPlayerIdComboBox.addItem("Error can't get editions, please check internet connection"); + } + + basmalhPlayerIdComboBox.setEnabled(settings.getBasmalhOnStart().isActive()); + } + + public boolean isModified() { + final var settings = AyahSettingsState.getInstance(); + return settings.getBasmalhOnStart().getPlayerId() != null && + !settings.getBasmalhOnStart().getPlayerId().equals(basmalhPlayerIdComboBox.getSelectedItem()) || + settings.getPlayerId() != null && + !settings.getPlayerId().equals(ayahPlayerIdComboBox.getSelectedItem()) || + settings.getIntervalTimeBetweenNotifications() != notificationsIntervalSpinnerModel.getNumber().intValue() || + settings.getBasmalhOnStart().isActive() != basmalhOnStartCheckBox.isSelected() || + settings.getBasmalhOnStart().isSoundActive() != autoPlayBasmalhCheckBox.isSelected() || + settings.isAutoPlayAudio() != notificationsAudioCheckBox.isSelected(); + } + + public void reset() { + setup(); + } + + public JPanel getPanel() { + return panel; + } +} diff --git a/src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsPanel.java b/src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsPanel.java deleted file mode 100644 index 1541363..0000000 --- a/src/main/java/com/anas/intellij/plugins/ayah/settings/SettingsPanel.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.anas.intellij.plugins.ayah.settings; - -/** - * @author: Anas Elgarhy - * @date: 8/19/22 - */ -public class SettingsPanel { -} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index c982fb2..5ec5fbc 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -34,6 +34,6 @@ displayName="Ayah Plugin Settings" /> - + \ No newline at end of file