Create the bse settings ui and Improve the code 🥰

This commit is contained in:
Anas Elgarhy 2022-08-19 17:38:28 +02:00
parent 17c1d2b083
commit 2de9769dd0
9 changed files with 277 additions and 29 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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;
}
}

View file

@ -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: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @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<AyahSettingsState> {
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 PersistentStateComponent<AyahSettingsS
public void loadState(@NotNull final AyahSettingsState state) {
XmlSerializerUtil.copyBean(state, this);
}
public BasmalhOnStart getBasmalhOnStart() {
return basmalhOnStart;
}
public void setBasmalhOnStart(final BasmalhOnStart basmalhOnStart) {
this.basmalhOnStart = basmalhOnStart;
}
public long getIntervalTimeBetweenNotifications() {
return intervalTimeBetweenNotifications;
}
public void setIntervalTimeBetweenNotifications(final int intervalTimeBetweenNotifications) {
this.intervalTimeBetweenNotifications = intervalTimeBetweenNotifications;
}
public boolean isAutoPlayAudio() {
return autoPlayAudio;
}
public void setAutoPlayAudio(final boolean autoPlayAudio) {
this.autoPlayAudio = autoPlayAudio;
}
public String getPlayerId() {
return playerId;
}
public void setPlayerId(final String playerId) {
this.playerId = playerId;
}
}

View file

@ -1,19 +1,51 @@
package com.anas.intellij.plugins.ayah.settings;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/**
* @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @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;
}
}

View file

@ -0,0 +1,41 @@
package com.anas.intellij.plugins.ayah.settings;
import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
/**
* @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @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;
}
}

View file

@ -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: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @date: 8/19/22
*/
public class SettingsComponent {
private final JPanel panel;
private final JBCheckBox basmalhOnStartCheckBox;
private final JBCheckBox autoPlayBasmalhCheckBox;
private final JComboBox<String> basmalhPlayerIdComboBox;
private final JSpinner notificationsIntervalSpinner;
private SpinnerNumberModel notificationsIntervalSpinnerModel;
private final JBCheckBox notificationsAudioCheckBox;
private final JComboBox<String> 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;
}
}

View file

@ -1,8 +0,0 @@
package com.anas.intellij.plugins.ayah.settings;
/**
* @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @date: 8/19/22
*/
public class SettingsPanel {
}

View file

@ -34,6 +34,6 @@
displayName="Ayah Plugin Settings" />
<applicationService serviceImplementation="com.anas.intellij.plugins.ayah.settings.AyahSettingsState" />
<notificationGroup id="com.anas.intellij.plugins.ayah.notificationGroup" displayType="BALLOON" />
<notificationGroup id="Random ayah from the quran" displayType="BALLOON" />
</extensions>
</idea-plugin>