Improve the code yoooooo 🥰

This commit is contained in:
Anas Elgarhy 2022-08-20 15:40:14 +02:00
parent 7cda03f2db
commit 1fc6c282c8
4 changed files with 43 additions and 104 deletions

View file

@ -3,7 +3,6 @@ package com.anas.intellij.plugins.ayah;
import com.anas.alqurancloudapi.Ayah;
import com.anas.intellij.plugins.ayah.audio.AudioPlayer;
import com.anas.intellij.plugins.ayah.dialogs.AyahDetailsDialog;
import com.anas.intellij.plugins.ayah.dialogs.AyahDialog;
import com.anas.intellij.plugins.ayah.settings.AyahSettingsState;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
@ -64,7 +63,7 @@ public class NotificationTimerTask extends TimerTask {
@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
LOGGER.info("Details action performed");
new AyahDialog(randomAyah).setVisible(true);
new AyahDetailsDialog(randomAyah).setVisible(true);
}
});

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.anas.intellij.plugins.ayah.dialogs.AyahDialog">
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.anas.intellij.plugins.ayah.dialogs.AyahDetailsDialog">
<grid id="cbd77" binding="contentPane" layout-manager="GridLayoutManager" row-count="3" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="10" bottom="10" right="10"/>
<constraints>

View file

@ -1,62 +1,56 @@
package com.anas.intellij.plugins.ayah.dialogs;
import com.anas.alqurancloudapi.Ayah;
import com.anas.intellij.plugins.ayah.settings.PanelBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextArea;
import net.miginfocom.swing.MigLayout;
import org.jetbrains.annotations.Nullable;
import com.anas.intellij.plugins.ayah.audio.AudioPlayer;
import com.anas.intellij.plugins.ayah.settings.AyahSettingsState;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @date: 8/20/22
*/
public class AyahDetailsDialog extends DialogWrapper {
private final JBTextArea ayahTextArea;
private JBLabel surahNameLabel;
private JBLabel numberOfAyahInSurahLabel;
private JBLabel tanzelLabel;
private final JButton playButton;
public class AyahDetailsDialog extends JDialog {
private JPanel contentPane;
private JButton playButton;
private JButton buttonCancel;
private JTextArea ayahTextArea;
private JLabel surahNameLabel;
private JLabel numberOfAyahInSuarhLabel;
private JLabel ayahRevelationType;
{
ayahTextArea = new JBTextArea();
playButton = new JButton("Play");
}
public AyahDetailsDialog(final Project project, final Ayah ayah) {
super(project, false);
setTitle("Ayah Details");
initializeDataFiles(ayah);
public AyahDetailsDialog(final Ayah ayah) {
setContentPane(contentPane);
setModal(true);
setSize(500, 300);
setModal(false);
setOKButtonText("");
init();
}
setResizable(false);
setLocationRelativeTo(null);
getRootPane().setDefaultButton(playButton);
private void initializeDataFiles(final Ayah ayah) {
ayahTextArea.setText(ayah.getText());
ayahTextArea.setEditable(false);
final var surah = ayah.getSurah();
surahNameLabel = new JBLabel(surah.getName());
tanzelLabel = new JBLabel(surah.getRevelationType().getArabicName() + " " + surah.getRevelationType().toString());
numberOfAyahInSurahLabel = new JBLabel("Number: " + ayah.getNumberInSurah());
surahNameLabel.setText(ayah.getSurah().getName());
numberOfAyahInSuarhLabel.setText("Number: " + ayah.getNumberInSurah());
ayahRevelationType.setText(ayah.getSurah().getRevelationType().getArabicName());
addListeners(ayah);
}
@Override
protected @Nullable JComponent createCenterPanel() {
return new PanelBuilder()
.setLayout(new MigLayout("fill"))
.addComponent(ayahTextArea, "grow, push, span")
.addComponent(surahNameLabel, "grow, push")
.addComponent(numberOfAyahInSurahLabel, "grow, push")
.addComponent(tanzelLabel, "grow, push, wrap")
.addComponent(playButton, "grow, push")
.build();
private void addListeners(final Ayah ayah) {
playButton.addActionListener(e ->
new AudioPlayer(AyahSettingsState.getInstance().getVolume(), ayah.getAudioUrl()).play());
buttonCancel.addActionListener(l -> dispose());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(l -> dispose(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
}

View file

@ -1,54 +0,0 @@
package com.anas.intellij.plugins.ayah.dialogs;
import com.anas.alqurancloudapi.Ayah;
import com.anas.intellij.plugins.ayah.audio.AudioPlayer;
import com.anas.intellij.plugins.ayah.settings.AyahSettingsState;
import javax.swing.*;
import java.awt.event.*;
public class AyahDialog extends JDialog {
private JPanel contentPane;
private JButton playButton;
private JButton buttonCancel;
private JTextArea ayahTextArea;
private JLabel surahNameLabel;
private JLabel numberOfAyahInSuarhLabel;
private JLabel ayahRevelationType;
public AyahDialog(final Ayah ayah) {
setContentPane(contentPane);
setModal(true);
setSize(500, 300);
setResizable(false);
setLocationRelativeTo(null);
getRootPane().setDefaultButton(playButton);
ayahTextArea.setText(ayah.getText());
surahNameLabel.setText(ayah.getSurah().getName());
numberOfAyahInSuarhLabel.setText("Number: " + ayah.getNumberInSurah());
ayahRevelationType.setText(ayah.getSurah().getRevelationType().getArabicName());
addListeners(ayah);
}
private void addListeners(final Ayah ayah) {
playButton.addActionListener(e ->
new AudioPlayer(AyahSettingsState.getInstance().getVolume(), ayah.getAudioUrl()).play());
buttonCancel.addActionListener(l -> dispose());
// call onCancel() when cross is clicked
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
// call onCancel() on ESCAPE
contentPane.registerKeyboardAction(l -> dispose(),
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
}