1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Projection_Planning/src/main/java/GUI/ProjectionHandler.java
2021-01-05 17:52:38 +01:00

150 lines
5.6 KiB
Java

package GUI;
import DB.*;
import GUI.Agenda.Agenda;
import GUI.Types.ProjectionType;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
public class ProjectionHandler extends JDialog {
private JPanel contentPane;
private JButton cancelButton;
private JButton confirmButton;
private JComboBox competitionComboBox;
private JComboBox dayComboBox;
private JComboBox slotComboBox;
private JPanel competitionPanel;
private JPanel slotPanel;
private JPanel dayPanel;
private JLabel competitionLabel;
private JLabel slotLabel;
private JLabel dayLabel;
private JPanel filmPanel;
private JLabel filmLabel;
private JComboBox filmComboBox;
private JPanel roomPanel;
private JLabel roomLabel;
private JComboBox roomComboBox;
private ProjectionType projectionType;
private Agenda agenda;
public ProjectionHandler(ProjectionType projectionType, Agenda agenda) {
this.projectionType = projectionType;
this.agenda = agenda;
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();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose();
}
});
setLocationRelativeTo(null);
updateCompetitions();
competitionComboBox.addActionListener(actionEvent -> updateMovies());
filmComboBox.addActionListener(actionEvent -> updateDates());
dayComboBox.addActionListener(actionEvent -> updateSlots());
slotComboBox.addActionListener(actionEvent -> updateRooms());
cancelButton.addActionListener(actionEvent -> dispose());
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)
for (Competition c : competitions)
competitionComboBox.addItem(c);
}
private void updateMovies() {
filmComboBox.removeAllItems();
dayComboBox.removeAllItems();
slotComboBox.removeAllItems();
roomComboBox.removeAllItems();
ArrayList<Movie> movies = Movie.getAvailable((Competition) competitionComboBox.getSelectedItem());
if (movies != null)
for (Movie m : movies)
filmComboBox.addItem(m);
}
private void updateDates() {
dayComboBox.removeAllItems();
slotComboBox.removeAllItems();
roomComboBox.removeAllItems();
Competition competition = (Competition) competitionComboBox.getSelectedItem();
Set<Date> dates = competition.getProjections().stream().map(p -> p.getStartDate()).collect(Collectors.toSet());
LocalDate[] localDates = LocalDate.of(2021, 5, 11).datesUntil(LocalDate.of(2021, 5, 22)).toArray(LocalDate[]::new);
if (competition.getDays() != 0 && dates.size() >= competition.getDays())
localDates = dates.stream().map(Date::toLocalDate).toArray(LocalDate[]::new);
for (LocalDate date : localDates)
dayComboBox.addItem(date);
}
private void updateSlots() {
slotComboBox.removeAllItems();
roomComboBox.removeAllItems();
if (dayComboBox.getSelectedItem() != null) {
ArrayList<Slot> slots = Slot.getAvailable((Competition) competitionComboBox.getSelectedItem(), Date.valueOf((LocalDate) dayComboBox.getSelectedItem()));
if (slots != null)
for (Slot s : slots)
slotComboBox.addItem(s);
}
}
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()));
if (rooms != null)
for (Room r : rooms)
roomComboBox.addItem(r);
}
}
private void confirm() {
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());
this.agenda.refresh();
dispose();
}
}