From a9e867237ad8b2636c7c3f59e755b7e973677efc Mon Sep 17 00:00:00 2001 From: Tergel TSAGAAN Date: Wed, 13 Jan 2021 15:37:51 +0100 Subject: [PATCH] dispose dialog when escape handling multiple projection on same slot and room --- src/main/java/DB/Projection.java | 4 + src/main/java/GUI/Agenda/Agenda.java | 31 +++++-- .../GUI/Agenda/Model/AgendaTableModel.java | 93 +++++++++++++++++++ .../Agenda/Model/ProjectionTableModel.java | 47 ++++++++++ src/main/java/GUI/Agenda/TableModel.java | 57 ------------ src/main/java/GUI/ProjectionHandler.java | 12 ++- src/main/java/GUI/ProjectionSelecter.form | 81 ++++++++++++++++ src/main/java/GUI/ProjectionSelecter.java | 83 +++++++++++++++++ 8 files changed, 340 insertions(+), 68 deletions(-) create mode 100644 src/main/java/GUI/Agenda/Model/AgendaTableModel.java create mode 100644 src/main/java/GUI/Agenda/Model/ProjectionTableModel.java delete mode 100644 src/main/java/GUI/Agenda/TableModel.java create mode 100644 src/main/java/GUI/ProjectionSelecter.form create mode 100644 src/main/java/GUI/ProjectionSelecter.java diff --git a/src/main/java/DB/Projection.java b/src/main/java/DB/Projection.java index 4b0943f..384cc63 100644 --- a/src/main/java/DB/Projection.java +++ b/src/main/java/DB/Projection.java @@ -117,4 +117,8 @@ public class Projection extends Table { public String toString() { return getMovie().getName(); } + + public String[] toArray() { + return new String[]{getMovie().toString(), getCompetition().toString(), getRoom().toString(), getStartDate().toString(), getSlot().toString()}; + } } diff --git a/src/main/java/GUI/Agenda/Agenda.java b/src/main/java/GUI/Agenda/Agenda.java index cc3b6e3..842d9f5 100644 --- a/src/main/java/GUI/Agenda/Agenda.java +++ b/src/main/java/GUI/Agenda/Agenda.java @@ -6,7 +6,9 @@ import DB.Projection; import DB.Slot; import Exceptions.NotFoundInTable; import Exceptions.ProjectionNotSpecified; +import GUI.Agenda.Model.AgendaTableModel; import GUI.ProjectionHandler; +import GUI.ProjectionSelecter; import GUI.Types.CompetType; import GUI.Types.ProjectionType; @@ -83,11 +85,12 @@ public class Agenda extends JPanel { public int getSize() { return headers.length; } + public Object getElementAt(int index) { return headers[index]; } }; - TableModel dm = new TableModel(day, currentPage); + AgendaTableModel dm = new AgendaTableModel(day, currentPage); table = new JTable(dm) { @Override public boolean getScrollableTracksViewportHeight() { @@ -98,7 +101,7 @@ public class Agenda extends JPanel { table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); table.setRowHeight(100); - Agenda tmp = this; + agenda = this; table.addMouseListener(new MouseInputAdapter() { @Override public void mouseReleased(MouseEvent mouseEvent) { @@ -110,7 +113,7 @@ public class Agenda extends JPanel { Projection projection = null; if (currentCellValue.getClass() == Projection.class) projection = (Projection) currentCellValue; - PopUpProjection popUpProjection = new PopUpProjection(projection, tmp); + PopUpProjection popUpProjection = new PopUpProjection(projection, agenda); popUpProjection.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY()); } } @@ -126,13 +129,15 @@ public class Agenda extends JPanel { if (currentCellValue.getClass() == Projection.class) projection = (Projection) currentCellValue; if (mouseEvent.getClickCount() == 2 && table.getSelectedRow() != -1) { - if (projection == null) { + if (projection == null && currentCellValue.getClass() != String.class) { + ProjectionSelecter dialog = new ProjectionSelecter((ArrayList) currentCellValue, agenda); + dialog.pack(); + dialog.setVisible(true); + } else if (projection == null) openDialog(ProjectionType.ADD); - } else { - openDialog(ProjectionType.EDIT, projection); - } + else openDialog(ProjectionType.EDIT, projection); } else if (mouseEvent.isPopupTrigger()) { - PopUpProjection popUpProjection = new PopUpProjection(projection, tmp); + PopUpProjection popUpProjection = new PopUpProjection(projection, agenda); popUpProjection.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY()); } } @@ -196,7 +201,15 @@ public class Agenda extends JPanel { break; } } - table.setValueAt(projection, row, column); + Object tableValue = table.getValueAt(row, column); + if (tableValue.getClass() == Projection.class) { + Projection[] projections = {(Projection) tableValue, projection}; + table.setValueAt(projections, row, column); + } else if (tableValue.getClass() != String.class) { + ((AgendaTableModel) table.getModel()).addValueAt(projection, row, column); + } else { + table.setValueAt(projection, row, column); + } } public int getCurrentPage() { diff --git a/src/main/java/GUI/Agenda/Model/AgendaTableModel.java b/src/main/java/GUI/Agenda/Model/AgendaTableModel.java new file mode 100644 index 0000000..53b1ffb --- /dev/null +++ b/src/main/java/GUI/Agenda/Model/AgendaTableModel.java @@ -0,0 +1,93 @@ +package GUI.Agenda.Model; + +import Config.Config; +import DB.Projection; + +import javax.swing.table.AbstractTableModel; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Arrays; + +public class AgendaTableModel extends AbstractTableModel { + private LocalDate[] columnNames; + private Object[][] data; + private int day; + + public AgendaTableModel(int day, int currentPage) { + this.day = day; + columnNames = new LocalDate[this.day]; + data = new Object[5][this.day]; + + LocalDate startDate = Config.getStartDate(); + + for (int i = 0; i < this.day; i++) { + this.columnNames[i] = startDate.plusDays(i + 5 * (currentPage)); + } + for (int i = 0; i < 5; i++) { + Arrays.fill(data[i], ""); + } + } + + @Override + public int getRowCount() { + return data.length; + } + + @Override + public int getColumnCount() { + return columnNames.length; + } + + public LocalDate getColumn(int column) { + return columnNames[column]; + } + + @Override + public String getColumnName(int column) { + return columnNames[column].toString(); + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + Object dataObject = data[rowIndex][columnIndex]; + if (dataObject.getClass() == DB.Projection[].class) { + ArrayList projections = new ArrayList() { + @Override + public String toString() { + StringBuilder builder = new StringBuilder(""); + for (int i = 0; i < size(); i++) { + builder.append(String.format("%s
", get(i))); + } + builder.append(""); + return builder.toString(); + } + }; + Projection[] dataArray = (Projection[]) dataObject; + projections.addAll(Arrays.asList(dataArray)); + return projections; + } + return data[rowIndex][columnIndex]; + } + + public void addValueAt(Projection projection, int rowIndex, int columnIndex) { + Projection[] projections = (Projection[]) data[rowIndex][columnIndex]; + ArrayList projectionsArrayList = new ArrayList<>(Arrays.asList(projections)) { + @Override + public String toString() { + StringBuilder builder = new StringBuilder(""); + for (int i = 0; i < size(); i++) { + builder.append(String.format("%s
", get(i))); + } + builder.append(""); + return builder.toString(); + } + }; + projectionsArrayList.add(projection); + setValueAt(projectionsArrayList, rowIndex, columnIndex); + } + + @Override + public void setValueAt(Object aValue, int rowIndex, int columnIndex) { + data[rowIndex][columnIndex] = aValue; + } +} diff --git a/src/main/java/GUI/Agenda/Model/ProjectionTableModel.java b/src/main/java/GUI/Agenda/Model/ProjectionTableModel.java new file mode 100644 index 0000000..3c3ca6d --- /dev/null +++ b/src/main/java/GUI/Agenda/Model/ProjectionTableModel.java @@ -0,0 +1,47 @@ +package GUI.Agenda.Model; + +import DB.Projection; + +import javax.swing.table.AbstractTableModel; +import java.util.ArrayList; + +public class ProjectionTableModel extends AbstractTableModel { + private String[] columnNames = {"Movie", "Competition", "Room", "Start", "Slot"}; + private Projection[] data; + + public ProjectionTableModel(ArrayList projections) { + data = new Projection[projections.size()]; + for (int i = 0; i < projections.size(); i++) { + data[i] = projections.get(i); + } + } + + @Override + public int getRowCount() { + return data.length; + } + + @Override + public int getColumnCount() { + return columnNames.length; + } + + @Override + public String getColumnName(int column) { + return columnNames[column]; + } + + public Projection getRowAt(int row) { + return (Projection) data[row]; + } + + @Override + public Object getValueAt(int rowIndex, int columnIndex) { + return data[rowIndex].toArray()[columnIndex]; + } + + @Override + public void setValueAt(Object aValue, int rowIndex, int columnIndex) { + data[rowIndex] = (Projection) aValue; + } +} diff --git a/src/main/java/GUI/Agenda/TableModel.java b/src/main/java/GUI/Agenda/TableModel.java deleted file mode 100644 index 4f8b0b3..0000000 --- a/src/main/java/GUI/Agenda/TableModel.java +++ /dev/null @@ -1,57 +0,0 @@ -package GUI.Agenda; - -import Config.Config; - -import javax.swing.table.AbstractTableModel; -import java.time.LocalDate; -import java.util.Arrays; - -public class TableModel extends AbstractTableModel { - private LocalDate[] columnNames; - private Object[][] data; - private int day; - - public TableModel(int day, int currentPage) { - this.day = day; - columnNames = new LocalDate[this.day]; - data = new Object[5][this.day]; - - LocalDate startDate = Config.getStartDate(); - - for (int i = 0; i < this.day; i++) { - this.columnNames[i] = startDate.plusDays(i + 5 * (currentPage)); - } - for (int i = 0; i < 5; i++) { - Arrays.fill(data[i], ""); - } - } - - @Override - public int getRowCount() { - return data.length; - } - - @Override - public int getColumnCount() { - return columnNames.length; - } - - public LocalDate getColumn(int column) { - return columnNames[column]; - } - - @Override - public String getColumnName(int column) { - return columnNames[column].toString(); - } - - @Override - public Object getValueAt(int rowIndex, int columnIndex) { - return data[rowIndex][columnIndex]; - } - - @Override - public void setValueAt(Object aValue, int rowIndex, int columnIndex) { - data[rowIndex][columnIndex] = aValue; - } -} diff --git a/src/main/java/GUI/ProjectionHandler.java b/src/main/java/GUI/ProjectionHandler.java index 7e0e48d..0821b41 100644 --- a/src/main/java/GUI/ProjectionHandler.java +++ b/src/main/java/GUI/ProjectionHandler.java @@ -9,8 +9,7 @@ import GUI.Types.ProjectionType; import javax.swing.*; import java.awt.*; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; +import java.awt.event.*; import java.sql.Date; import java.time.LocalDate; import java.util.ArrayList; @@ -97,6 +96,15 @@ public class ProjectionHandler extends JDialog { 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 updateCompetitions() { diff --git a/src/main/java/GUI/ProjectionSelecter.form b/src/main/java/GUI/ProjectionSelecter.form new file mode 100644 index 0000000..c4f865d --- /dev/null +++ b/src/main/java/GUI/ProjectionSelecter.form @@ -0,0 +1,81 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/main/java/GUI/ProjectionSelecter.java b/src/main/java/GUI/ProjectionSelecter.java new file mode 100644 index 0000000..3797405 --- /dev/null +++ b/src/main/java/GUI/ProjectionSelecter.java @@ -0,0 +1,83 @@ +package GUI; + +import DB.Projection; +import GUI.Agenda.Agenda; +import GUI.Agenda.Model.ProjectionTableModel; +import GUI.Types.ProjectionType; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.ArrayList; + +public class ProjectionSelecter extends JDialog { + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonCancel; + private JTable table1; + private JScrollPane scrollPane; + private Projection currentProjection; + private Agenda agenda; + + public ProjectionSelecter(ArrayList projections, Agenda agenda) { + this.agenda = agenda; + setContentPane(contentPane); + setModal(true); + setLocationRelativeTo(null); + setTitle("Projection Selecter"); + getRootPane().setDefaultButton(buttonOK); + ProjectionTableModel pTM = new ProjectionTableModel(projections); + table1.setModel(pTM); + buttonOK.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onOK(); + } + }); + + buttonCancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }); + table1.addMouseListener( + new MouseAdapter() { + @Override + public void mousePressed(MouseEvent e) { + JTable table = (JTable) e.getSource(); + Point point = e.getPoint(); + int row = table.rowAtPoint(point); + if (e.getClickCount() == 2 && table.getSelectedRow() != -1) { + currentProjection = ((ProjectionTableModel) table.getModel()).getRowAt(row); + agenda.openDialog(ProjectionType.EDIT, currentProjection); + dispose(); + } + } + } + ); + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent e) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction(new ActionListener() { + public void actionPerformed(ActionEvent e) { + onCancel(); + } + }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); + } + + private void onOK() { + agenda.openDialog(ProjectionType.EDIT, currentProjection); + dispose(); + } + + private void onCancel() { + // add your code here if necessary + dispose(); + } + +}