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
Tergel TSAGAAN c869010150 Merge branch 'master' of https://forge.univ-lyon1.fr/cannes-cpoa/projection-planning into master
 Conflicts:
	src/main/java/GUI/Agenda/Agenda.java
2021-01-14 08:31:03 +01:00

213 lines
8.5 KiB
Java

package GUI;
import Config.Config;
import DB.*;
import Exceptions.ProjectionNotSpecified;
import GUI.Agenda.Agenda;
import GUI.Theme.DangerContainedButtonUI;
import GUI.Theme.PrimaryContainedButtonUI;
import GUI.Theme.SecondaryContainedButtonUI;
import GUI.Types.ProjectionType;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
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;
private Projection projection = null;
public ProjectionHandler(ProjectionType projectionType, Agenda agenda) throws ProjectionNotSpecified {
if (projectionType == ProjectionType.EDIT)
throw new ProjectionNotSpecified();
this.projectionType = projectionType;
this.agenda = agenda;
competitionComboBox.getModel().setSelectedItem(agenda.getCompetition());
if (agenda.getDate() != null || agenda.getSlot() != null) {
updateMovies();
updateDates();
dayComboBox.getModel().setSelectedItem(agenda.getDate());
updateSlots();
slotComboBox.getModel().setSelectedItem(agenda.getSlot());
updateRooms();
}
createUI();
}
public ProjectionHandler(ProjectionType projectionType, Agenda agenda, Projection projection) {
this.projectionType = projectionType;
this.agenda = agenda;
this.projection = projection;
createUI();
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 createUI() {
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());
setTitle(projectionType.getTitle());
confirmButton.setText(projectionType.getText());
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());
contentPane.registerKeyboardAction(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
private void onCancel() {
dispose();
}
private void createUIComponents() {
confirmButton = new JButton();
if (projectionType == ProjectionType.REMOVE)
confirmButton.setUI(new DangerContainedButtonUI());
else
confirmButton.setUI(new PrimaryContainedButtonUI());
cancelButton = new JButton();
cancelButton.setUI(new SecondaryContainedButtonUI());
}
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 = Config.getStartDate().datesUntil(Config.getEndDate()).toArray(LocalDate[]::new);
if (competition.getDays() != 0 && dates.size() >= competition.getDays() && !(projectionType == ProjectionType.EDIT && projection.getCompetition().getName().equals(competition.getName())))
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;
Competition competition = (Competition) competitionComboBox.getSelectedItem();
Date date = Date.valueOf((LocalDate) dayComboBox.getSelectedItem());
if (projectionType == ProjectionType.REMOVE)
slots = Slot.getAll(competition, date);
else
slots = Slot.getAvailable(competition, date);
if (slots != null)
for (Slot s : slots)
slotComboBox.addItem(s);
}
}
private void updateRooms() {
roomComboBox.removeAllItems();
if (dayComboBox.getSelectedItem() != null) {
ArrayList<Room> rooms;
Movie movie = (Movie) filmComboBox.getSelectedItem();
Slot slot = (Slot) slotComboBox.getSelectedItem();
Date date = Date.valueOf((LocalDate) dayComboBox.getSelectedItem());
if (projectionType == ProjectionType.REMOVE)
rooms = Room.getAll(movie, slot, date);
else
rooms = Room.getAvailable(movie, slot, date);
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;
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:
projection.delete();
new Projection(date, competition, room, movie, slot);
break;
case REMOVE:
if (projection != null)
projection.delete();
else
(Projection.find(date, competition, room, movie, slot)).delete();
break;
}
this.agenda.refresh();
dispose();
}
}