1
0
Fork 0

Add remove support and contextual right click menu

This commit is contained in:
Ethanell 2021-01-07 11:20:11 +01:00
parent ca7d3f1136
commit 336288b71a
7 changed files with 209 additions and 48 deletions

View file

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

View file

@ -48,10 +48,26 @@ public class Room extends Table {
return list;
}
static public ArrayList<Room> getAll(Movie movie, Slot slot, Date date) {
ArrayList<Room> 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<Room> getAvailable(Movie movie, Slot slot, Date date) {
ArrayList<Room> 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);

View file

@ -66,6 +66,21 @@ public class Slot extends Table {
return list;
}
static public ArrayList<Slot> getAll(Competition competition, Date date) {
ArrayList<Slot> 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<Slot> getAvailable(Competition competition, Date date) {
ArrayList<Slot> list = new ArrayList<>();
try {

View file

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

View file

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

View file

@ -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<Competition> competitions = Competition.getAll();
if (competitions != null)
@ -120,7 +119,18 @@ public class ProjectionHandler extends JDialog {
slotComboBox.removeAllItems();
roomComboBox.removeAllItems();
if (dayComboBox.getSelectedItem() != null) {
ArrayList<Slot> slots = Slot.getAvailable((Competition) competitionComboBox.getSelectedItem(), Date.valueOf((LocalDate) dayComboBox.getSelectedItem()));
ArrayList<Slot> 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<Room> rooms = Room.getAvailable((Movie) filmComboBox.getSelectedItem(), (Slot) slotComboBox.getSelectedItem(), Date.valueOf((LocalDate) dayComboBox.getSelectedItem()));
ArrayList<Room> 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();
}
}

View file

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