From 336288b71a5c5f547cd627f453de08bf05184f77 Mon Sep 17 00:00:00 2001 From: flifloo Date: Thu, 7 Jan 2021 11:20:11 +0100 Subject: [PATCH] Add remove support and contextual right click menu --- src/main/java/DB/Projection.java | 25 ++++- src/main/java/DB/Room.java | 18 +++- src/main/java/DB/Slot.java | 15 +++ src/main/java/GUI/Agenda/Agenda.java | 43 +++++---- src/main/java/GUI/Agenda/PopUpProjection.java | 50 ++++++++++ src/main/java/GUI/ProjectionHandler.java | 93 +++++++++++++------ src/main/java/GUI/Types/ProjectionType.java | 13 ++- 7 files changed, 209 insertions(+), 48 deletions(-) create mode 100644 src/main/java/GUI/Agenda/PopUpProjection.java diff --git a/src/main/java/DB/Projection.java b/src/main/java/DB/Projection.java index c4cddc6..4b0943f 100644 --- a/src/main/java/DB/Projection.java +++ b/src/main/java/DB/Projection.java @@ -92,8 +92,29 @@ public class Projection extends Table { return list; } + static public Projection find(Date date, Competition competition, Room room, Movie movie, Slot slot) { + Projection projection = null; + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT id FROM Projection WHERE startDate = ? AND CompetitionName = ? AND RoomName = ? AND MovieName = ? AND SlotId = ?"); + ps.setDate(1, date); + ps.setString(2, competition.getName()); + ps.setString(3, room.getName()); + ps.setString(4, movie.getName()); + ps.setInt(5, slot.getId()); + if (ps.execute()) { + ResultSet rs = ps.getResultSet(); + rs.next(); + projection = new Projection(rs.getInt("id")); + } + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return projection; + } + @Override - public boolean delete() { - return false; + public String toString() { + return getMovie().getName(); } } diff --git a/src/main/java/DB/Room.java b/src/main/java/DB/Room.java index 2943f5b..f175814 100644 --- a/src/main/java/DB/Room.java +++ b/src/main/java/DB/Room.java @@ -48,10 +48,26 @@ public class Room extends Table { return list; } + static public ArrayList getAll(Movie movie, Slot slot, Date date) { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT R.name FROM Room R INNER JOIN CompetitionRoom CR on R.name = CR.RoomName INNER JOIN Projection P on R.name = P.RoomName WHERE CR.CompetitionName = ? AND P.SlotId = ? AND P.startDate = ? GROUP BY R.name"); + ps.setString(1, movie.getCompetition().getName()); + ps.setInt(2, slot.getId()); + ps.setDate(3, date); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Room(rs.getString("name"))); + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return list; + } + static public ArrayList getAvailable(Movie movie, Slot slot, Date date) { ArrayList list = new ArrayList<>(); try { - PreparedStatement ps = DB.getConnection().prepareStatement("SELECT R.name FROM Room R INNER JOIN CompetitionRoom CR on R.name = CR.RoomName LEFT JOIN Projection P on R.name = P.RoomName LEFT JOIN Slot S on S.id = P.SlotId WHERE CR.CompetitionName = ? AND NOT (S.id = ? AND P.startDate = ?) OR P.id IS NULL GROUP BY R.name"); + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT R.name FROM Room R INNER JOIN CompetitionRoom CR on R.name = CR.RoomName LEFT JOIN Projection P on R.name = P.RoomName LEFT JOIN Slot S on S.id = P.SlotId WHERE CR.CompetitionName = ? AND (NOT (S.id = ? AND P.startDate = ?) OR P.id IS NULL) GROUP BY R.name"); ps.setString(1, movie.getCompetition().getName()); ps.setInt(2, slot.getId()); ps.setDate(3, date); diff --git a/src/main/java/DB/Slot.java b/src/main/java/DB/Slot.java index 7917ea9..1a4d974 100644 --- a/src/main/java/DB/Slot.java +++ b/src/main/java/DB/Slot.java @@ -66,6 +66,21 @@ public class Slot extends Table { return list; } + static public ArrayList getAll(Competition competition, Date date) { + ArrayList list = new ArrayList<>(); + try { + PreparedStatement ps = DB.getConnection().prepareStatement("SELECT S.id FROM Slot S INNER JOIN Projection P on S.id = P.SlotId WHERE S.CompetitionName = ? AND P.startDate = ? GROUP BY S.id"); + ps.setString(1, competition.getName()); + ps.setDate(2, date); + for (ResultSet rs = ps.executeQuery(); rs.next();) + list.add(new Slot(rs.getInt("id"))); + ps.close(); + } catch (SQLException | NullPointerException | NotFoundInTable e) { + return null; + } + return list; + } + static public ArrayList getAvailable(Competition competition, Date date) { ArrayList list = new ArrayList<>(); try { diff --git a/src/main/java/GUI/Agenda/Agenda.java b/src/main/java/GUI/Agenda/Agenda.java index 887e03b..47b80ec 100644 --- a/src/main/java/GUI/Agenda/Agenda.java +++ b/src/main/java/GUI/Agenda/Agenda.java @@ -1,7 +1,6 @@ package GUI.Agenda; -import DB.Competition; -import DB.Projection; +import DB.*; import GUI.ProjectionHandler; import GUI.Types.CompetType; import GUI.Types.ProjectionType; @@ -91,21 +90,25 @@ public class Agenda extends JPanel { table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setRowHeight(100); + Agenda tmp = this; table.addMouseListener(new MouseInputAdapter() { + @Override public void mousePressed(MouseEvent mouseEvent) { - JTable table = (JTable) mouseEvent.getSource(); - Point point = mouseEvent.getPoint(); - int row = table.getSelectedRow(); - int column = table.getSelectedColumn(); + Object currentCellValue = table.getValueAt(table.rowAtPoint(mouseEvent.getPoint()), table.getTableHeader().columnAtPoint(mouseEvent.getPoint())); + Projection projection = null; + if (currentCellValue.getClass() == Projection.class) + projection = (Projection) currentCellValue; if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) { - if (table.getValueAt(row, column) == "") { + if (projection == null) { openDialog(ProjectionType.ADD); } else { - openDialog(ProjectionType.EDIT); - + openDialog(ProjectionType.EDIT, projection); } - + } + else if (mouseEvent.isPopupTrigger()) { + PopUpProjection popUpProjection = new PopUpProjection(projection, tmp); + popUpProjection.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY()); } } }); @@ -136,20 +139,28 @@ public class Agenda extends JPanel { try { ArrayList projections = Projection.getAvailable(new Competition(this.competionType.getCompetition())); for (Projection projection : projections) { - addMovie(projection.getStartDate(), projection.getSlot().getStartTime(), projection.getMovie().getName()); + addMovie(projection); } } catch (Exception e) { } } - private void openDialog(ProjectionType projectionType) { - ProjectionHandler dialog = new ProjectionHandler(ProjectionType.ADD, this); - ; + protected void openDialog(ProjectionType projectionType) { + ProjectionHandler dialog = new ProjectionHandler(projectionType, this); dialog.pack(); dialog.setVisible(true); } - private void addMovie(Date startDate, Time time, String movieName) { + protected void openDialog(ProjectionType projectionType, Projection projection) { + ProjectionHandler dialog = new ProjectionHandler(projectionType, this, projection); + dialog.pack(); + dialog.setVisible(true); + } + + private void addMovie(Projection projection) { + Date startDate = projection.getStartDate(); + Time time = projection.getSlot().getStartTime(); + String movieName = projection.getMovie().getName(); int column = this.table.getColumn(startDate.toString()).getModelIndex(); int row = 0; String times[] = {"08:30:00", "11:30:00", "15:00:00", "18:00:00", "21:00:00"}; @@ -159,7 +170,7 @@ public class Agenda extends JPanel { break; } } - table.setValueAt(movieName, row, column); + table.setValueAt(projection, row, column); } public int getCurrentPage() { diff --git a/src/main/java/GUI/Agenda/PopUpProjection.java b/src/main/java/GUI/Agenda/PopUpProjection.java new file mode 100644 index 0000000..441ec7e --- /dev/null +++ b/src/main/java/GUI/Agenda/PopUpProjection.java @@ -0,0 +1,50 @@ +package GUI.Agenda; + +import DB.Projection; +import GUI.Types.ProjectionType; + +import javax.swing.*; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +public class PopUpProjection extends JPopupMenu implements ActionListener { + JMenuItem itemAdd; + JMenuItem itemEdit; + JMenuItem itemRemove; + Projection projection; + Agenda agenda; + public PopUpProjection(Projection projection, Agenda agenda) { + this.agenda = agenda; + itemAdd = new JMenuItem("Add"); + itemEdit = new JMenuItem("Edit"); + itemRemove = new JMenuItem("Remove"); + itemAdd.addActionListener(this); + itemEdit.addActionListener(this); + itemRemove.addActionListener(this); + if (projection != null) { + this.projection = projection; + add(itemEdit); + add(itemRemove); + } + else { + this.projection = null; + add(itemAdd); + } + } + + @Override + public void actionPerformed(ActionEvent actionEvent) { + Object source = actionEvent.getSource(); + if (source == itemAdd) { + agenda.openDialog(ProjectionType.ADD); + + } + else if (source == itemEdit) { + agenda.openDialog(ProjectionType.EDIT, projection); + + } + else if (source == itemRemove) { + agenda.openDialog(ProjectionType.REMOVE, projection); + } + } +} diff --git a/src/main/java/GUI/ProjectionHandler.java b/src/main/java/GUI/ProjectionHandler.java index da80f57..30e26a8 100644 --- a/src/main/java/GUI/ProjectionHandler.java +++ b/src/main/java/GUI/ProjectionHandler.java @@ -36,16 +36,37 @@ public class ProjectionHandler extends JDialog { private JComboBox roomComboBox; private ProjectionType projectionType; private Agenda agenda; + private Projection projection = null; public ProjectionHandler(ProjectionType projectionType, Agenda agenda) { this.projectionType = projectionType; this.agenda = agenda; + createUIComponents(); + } + + public ProjectionHandler(ProjectionType projectionType, Agenda agenda, Projection projection) { + this.projectionType = projectionType; + this.agenda = agenda; + this.projection = projection; + createUIComponents(); + competitionComboBox.getModel().setSelectedItem(projection.getCompetition()); + filmComboBox.getModel().setSelectedItem(projection.getMovie()); + dayComboBox.getModel().setSelectedItem(projection.getStartDate().toLocalDate()); + slotComboBox.getModel().setSelectedItem(projection.getSlot()); + roomComboBox.getModel().setSelectedItem(projection.getRoom()); + if (projectionType == ProjectionType.REMOVE) + for (JComboBox comboBox : Arrays.asList(competitionComboBox, filmComboBox, dayComboBox, slotComboBox, roomComboBox)) + comboBox.setEnabled(false); + } + + private void createUIComponents() { setContentPane(contentPane); setModal(true); setPreferredSize(new Dimension(500, 300)); ImageIcon img = new ImageIcon("src/main/java/GUI/Assets/Logo/logo_cannes.jpg"); setIconImage(img.getImage()); - handleDialogName(); + setTitle(projectionType.getTitle()); + confirmButton.setText(projectionType.getText()); setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { @@ -62,28 +83,6 @@ public class ProjectionHandler extends JDialog { confirmButton.addActionListener(actionEvent -> confirm()); } - private void handleDialogName() { - switch (this.projectionType) { - case ADD: { - setTitle("Add projection"); - confirmButton.setText("Add"); - break; - } - case EDIT: { - setTitle("Edit projection"); - confirmButton.setText("Edit"); - break; - } - case REMOVE: { - setTitle("Remove projection"); - confirmButton.setText("remove"); - break; - } - default: - break; - } - } - private void updateCompetitions() { ArrayList competitions = Competition.getAll(); if (competitions != null) @@ -120,7 +119,18 @@ public class ProjectionHandler extends JDialog { slotComboBox.removeAllItems(); roomComboBox.removeAllItems(); if (dayComboBox.getSelectedItem() != null) { - ArrayList slots = Slot.getAvailable((Competition) competitionComboBox.getSelectedItem(), Date.valueOf((LocalDate) dayComboBox.getSelectedItem())); + ArrayList slots = null; + Competition competition = (Competition) competitionComboBox.getSelectedItem(); + Date date = Date.valueOf((LocalDate) dayComboBox.getSelectedItem()); + switch (projectionType) { + case ADD: + slots = Slot.getAvailable(competition, date); + break; + case EDIT: + break; + case REMOVE: + slots = Slot.getAll(competition, date); + } if (slots != null) for (Slot s : slots) slotComboBox.addItem(s); @@ -130,7 +140,19 @@ public class ProjectionHandler extends JDialog { private void updateRooms() { roomComboBox.removeAllItems(); if (dayComboBox.getSelectedItem() != null) { - ArrayList rooms = Room.getAvailable((Movie) filmComboBox.getSelectedItem(), (Slot) slotComboBox.getSelectedItem(), Date.valueOf((LocalDate) dayComboBox.getSelectedItem())); + ArrayList rooms = null; + Movie movie = (Movie) filmComboBox.getSelectedItem(); + Slot slot = (Slot) slotComboBox.getSelectedItem(); + Date date = Date.valueOf((LocalDate) dayComboBox.getSelectedItem()); + switch (projectionType) { + case ADD: + rooms = Room.getAvailable(movie, slot, date); + break; + case EDIT: + break; + case REMOVE: + rooms = Room.getAll(movie, slot, date); + } if (rooms != null) for (Room r : rooms) roomComboBox.addItem(r); @@ -141,10 +163,25 @@ public class ProjectionHandler extends JDialog { for (JComboBox comboBox : Arrays.asList(competitionComboBox, filmComboBox, dayComboBox, slotComboBox, roomComboBox)) if (comboBox.getSelectedItem() == null) return; - if (projectionType == ProjectionType.ADD) - new Projection(Date.valueOf((LocalDate) dayComboBox.getSelectedItem()), (Competition) competitionComboBox.getSelectedItem(), (Room) roomComboBox.getSelectedItem(), (Movie) filmComboBox.getSelectedItem(), (Slot) slotComboBox.getSelectedItem()); + Date date = Date.valueOf((LocalDate) dayComboBox.getSelectedItem()); + Competition competition = (Competition) competitionComboBox.getSelectedItem(); + Room room = (Room) roomComboBox.getSelectedItem(); + Movie movie = (Movie) filmComboBox.getSelectedItem(); + Slot slot = (Slot) slotComboBox.getSelectedItem(); + switch (projectionType) { + case ADD: + new Projection(date, competition, room, movie, slot); + break; + case EDIT: + break; + case REMOVE: + if (projection != null) + projection.delete(); + else + (Projection.find(date, competition, room, movie, slot)).delete(); + break; + } this.agenda.refresh(); - dispose(); } } diff --git a/src/main/java/GUI/Types/ProjectionType.java b/src/main/java/GUI/Types/ProjectionType.java index bf84265..d445e30 100644 --- a/src/main/java/GUI/Types/ProjectionType.java +++ b/src/main/java/GUI/Types/ProjectionType.java @@ -1,5 +1,16 @@ package GUI.Types; +import lombok.Getter; + public enum ProjectionType { - ADD, EDIT, REMOVE + ADD("Add projection", "Add"), + EDIT("Edit projection", "Edit"), + REMOVE("Remove projection", "Remove"); + @Getter final private String title; + @Getter final private String text; + + ProjectionType(String title, String text) { + this.title = title; + this.text = text; + } } \ No newline at end of file