From c6a7f697b9ccb4e650fbbae44b87eaa95f2499e1 Mon Sep 17 00:00:00 2001 From: Tergel TSAGAAN Date: Mon, 11 Jan 2021 16:39:13 +0100 Subject: [PATCH] Remove edit from GUI menu bar and add exception to ProjectionHandler --- .../Exceptions/ProjectionNotSpecified.java | 7 ++++++ src/main/java/GUI/Agenda/Agenda.java | 14 +++++++----- src/main/java/GUI/GUI.java | 22 +++---------------- src/main/java/GUI/ProjectionHandler.java | 10 ++++----- 4 files changed, 24 insertions(+), 29 deletions(-) create mode 100644 src/main/java/Exceptions/ProjectionNotSpecified.java diff --git a/src/main/java/Exceptions/ProjectionNotSpecified.java b/src/main/java/Exceptions/ProjectionNotSpecified.java new file mode 100644 index 0000000..1f70946 --- /dev/null +++ b/src/main/java/Exceptions/ProjectionNotSpecified.java @@ -0,0 +1,7 @@ +package Exceptions; + +public class ProjectionNotSpecified extends Exception { + public ProjectionNotSpecified() { + super("Can't edit without a specified projection !"); + } +} diff --git a/src/main/java/GUI/Agenda/Agenda.java b/src/main/java/GUI/Agenda/Agenda.java index 1c5b770..b72229c 100644 --- a/src/main/java/GUI/Agenda/Agenda.java +++ b/src/main/java/GUI/Agenda/Agenda.java @@ -2,6 +2,7 @@ package GUI.Agenda; import DB.Competition; import DB.Projection; +import Exceptions.ProjectionNotSpecified; import GUI.ProjectionHandler; import GUI.Types.CompetType; import GUI.Types.ProjectionType; @@ -159,13 +160,16 @@ public class Agenda extends JPanel { } } - protected void openDialog(ProjectionType projectionType) { - ProjectionHandler dialog = new ProjectionHandler(projectionType, this); - dialog.pack(); - dialog.setVisible(true); + public void openDialog(ProjectionType projectionType) { + try { + ProjectionHandler dialog = new ProjectionHandler(projectionType, this); + dialog.pack(); + dialog.setVisible(true); + } catch (ProjectionNotSpecified ignored) { + } } - protected void openDialog(ProjectionType projectionType, Projection projection) { + public void openDialog(ProjectionType projectionType, Projection projection) { ProjectionHandler dialog = new ProjectionHandler(projectionType, this, projection); dialog.pack(); dialog.setVisible(true); diff --git a/src/main/java/GUI/GUI.java b/src/main/java/GUI/GUI.java index b939d2c..6417f6f 100644 --- a/src/main/java/GUI/GUI.java +++ b/src/main/java/GUI/GUI.java @@ -20,17 +20,12 @@ public class GUI extends JFrame implements ActionListener { private JMenuBar menuBar = new JMenuBar(); private JMenu menuFichier = new JMenu("File"); private JMenuItem menuItemAddProj = new JMenuItem("Add projection"); - private JMenuItem menuItemEditProj = new JMenuItem("Edit projection"); private JMenuItem menuItemRemoveProj = new JMenuItem("Remove projection"); private int currentPage; private Agenda agenda; String headers[] = {"Matin", "Midi", "Milieu
Après-midi", "Fin
Après-midi", "Soirée"}; private CompetType currentCompetition = CompetType.LM; - public static void main(String[] args) { - new GUI(); - } - public GUI() { super(); construct(); @@ -39,7 +34,6 @@ public class GUI extends JFrame implements ActionListener { private void renderMenu() { menuBar.add(menuFichier); menuFichier.add(menuItemAddProj); - menuFichier.add(menuItemEditProj); menuFichier.add(menuItemRemoveProj); setJMenuBar(menuBar); } @@ -63,7 +57,6 @@ public class GUI extends JFrame implements ActionListener { LMButton.addActionListener(this); UCRButton.addActionListener(this); menuItemAddProj.addActionListener(this); - menuItemEditProj.addActionListener(this); menuItemRemoveProj.addActionListener(this); setVisible(true); } @@ -77,8 +70,7 @@ public class GUI extends JFrame implements ActionListener { this.currentPage = agenda.getCurrentPage(); this.repaint(); this.revalidate(); - } - else if (source == nextButton) { + } else if (source == nextButton) { this.currentPage++; Agenda agenda = new Agenda(agendaPanel, this.currentPage, this.currentCompetition); this.currentPage = agenda.getCurrentPage(); @@ -104,17 +96,9 @@ public class GUI extends JFrame implements ActionListener { this.repaint(); this.revalidate(); } else if (source == this.menuItemAddProj) { - ProjectionHandler dialog = new ProjectionHandler(ProjectionType.ADD, agenda); - dialog.pack(); - dialog.setVisible(true); - } else if (source == this.menuItemEditProj) { - ProjectionHandler dialog = new ProjectionHandler(ProjectionType.EDIT, agenda); - dialog.pack(); - dialog.setVisible(true); + agenda.openDialog(ProjectionType.ADD); } else if (source == this.menuItemRemoveProj) { - ProjectionHandler dialog = new ProjectionHandler(ProjectionType.REMOVE, agenda); - dialog.pack(); - dialog.setVisible(true); + agenda.openDialog(ProjectionType.REMOVE); } } } diff --git a/src/main/java/GUI/ProjectionHandler.java b/src/main/java/GUI/ProjectionHandler.java index 93b6568..680dd4a 100644 --- a/src/main/java/GUI/ProjectionHandler.java +++ b/src/main/java/GUI/ProjectionHandler.java @@ -1,6 +1,7 @@ package GUI; import DB.*; +import Exceptions.ProjectionNotSpecified; import GUI.Agenda.Agenda; import GUI.Types.ProjectionType; @@ -38,7 +39,9 @@ public class ProjectionHandler extends JDialog { private Agenda agenda; private Projection projection = null; - public ProjectionHandler(ProjectionType projectionType, Agenda agenda) { + public ProjectionHandler(ProjectionType projectionType, Agenda agenda) throws ProjectionNotSpecified { + if (projectionType == ProjectionType.EDIT) + throw new ProjectionNotSpecified(); this.projectionType = projectionType; this.agenda = agenda; createUIComponents(); @@ -167,10 +170,7 @@ public class ProjectionHandler extends JDialog { new Projection(date, competition, room, movie, slot); break; case EDIT: - if (projection != null) - projection.delete(); - else - (Projection.find(date, competition, room, movie, slot)).delete(); + projection.delete(); new Projection(date, competition, room, movie, slot); break; case REMOVE: