From c58cfaf14822dc96ee1b91a12f4eb01fbb879370 Mon Sep 17 00:00:00 2001 From: Tergel TSAGAAN Date: Tue, 12 Jan 2021 14:45:38 +0100 Subject: [PATCH] automatic day/slot selection when adding --- src/main/java/GUI/Agenda/Agenda.java | 24 ++++++++++++++++++++++-- src/main/java/GUI/Agenda/TableModel.java | 14 ++++++++++---- src/main/java/GUI/ProjectionHandler.java | 7 +++++++ 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/main/java/GUI/Agenda/Agenda.java b/src/main/java/GUI/Agenda/Agenda.java index 337eda3..89563e2 100644 --- a/src/main/java/GUI/Agenda/Agenda.java +++ b/src/main/java/GUI/Agenda/Agenda.java @@ -3,6 +3,7 @@ package GUI.Agenda; import DB.Competition; import DB.Projection; import DB.Slot; +import Exceptions.NotFoundInTable; import Exceptions.ProjectionNotSpecified; import GUI.ProjectionHandler; import GUI.Types.CompetType; @@ -15,6 +16,7 @@ import java.awt.*; import java.awt.event.MouseEvent; import java.sql.Date; import java.sql.Time; +import java.time.LocalDate; import java.util.ArrayList; import java.util.Arrays; @@ -27,6 +29,9 @@ public class Agenda extends JPanel { private CompetType competionType; private Agenda agenda; private ArrayList slots; + private Slot currentSlot; + private LocalDate currentDay; + public Agenda(JPanel agendaPanel, int currentPage, CompetType competionType) { this.agendaPanel = agendaPanel; this.slots = competionType.getSlots(); @@ -78,7 +83,6 @@ public class Agenda extends JPanel { public int getSize() { return headers.length; } - public Object getElementAt(int index) { return headers[index]; } @@ -113,7 +117,11 @@ public class Agenda extends JPanel { @Override public void mousePressed(MouseEvent mouseEvent) { - Object currentCellValue = table.getValueAt(table.rowAtPoint(mouseEvent.getPoint()), table.getTableHeader().columnAtPoint(mouseEvent.getPoint())); + int row = table.rowAtPoint(mouseEvent.getPoint()); + int column = table.getTableHeader().columnAtPoint(mouseEvent.getPoint()); + currentDay = dm.getColumn(column); + currentSlot = (Slot) lm.getElementAt(row); + Object currentCellValue = table.getValueAt(row, column); Projection projection = null; if (currentCellValue.getClass() == Projection.class) projection = (Projection) currentCellValue; @@ -194,4 +202,16 @@ public class Agenda extends JPanel { public int getCurrentPage() { return this.currentPage; } + + public Competition getCompetition() throws NotFoundInTable { + return new Competition(competionType.getCompetition()); + } + + public Slot getSlot() { + return currentSlot; + } + + public LocalDate getDate() { + return currentDay; + } } diff --git a/src/main/java/GUI/Agenda/TableModel.java b/src/main/java/GUI/Agenda/TableModel.java index 6a14608..3512ee1 100644 --- a/src/main/java/GUI/Agenda/TableModel.java +++ b/src/main/java/GUI/Agenda/TableModel.java @@ -1,25 +1,27 @@ package GUI.Agenda; import javax.swing.table.AbstractTableModel; +import java.time.LocalDate; import java.util.Arrays; public class TableModel extends AbstractTableModel { - private String[] columnNames; + private LocalDate[] columnNames; private Object[][] data; private int headerSize; private int day; public TableModel(int day, int currentPage, int headerSize) { this.day = day; - columnNames = new String[this.day]; + columnNames = new LocalDate[this.day]; data = new Object[5][this.day]; for (int i = 0; i < this.day; i++) { - this.columnNames[i] = String.format("2021-05-%d", 10 + (i + 1) + (5 * (currentPage))); + this.columnNames[i] = LocalDate.of(2021, 05, (i + 11) + (5 * (currentPage))); } for (int i = 0; i < 5; i++) { Arrays.fill(data[i], ""); } } + @Override public int getRowCount() { return data.length; @@ -30,9 +32,13 @@ public class TableModel extends AbstractTableModel { return columnNames.length; } + public LocalDate getColumn(int column) { + return columnNames[column]; + } + @Override public String getColumnName(int column) { - return columnNames[column]; + return columnNames[column].toString(); } @Override diff --git a/src/main/java/GUI/ProjectionHandler.java b/src/main/java/GUI/ProjectionHandler.java index 728225c..eb3d007 100644 --- a/src/main/java/GUI/ProjectionHandler.java +++ b/src/main/java/GUI/ProjectionHandler.java @@ -1,6 +1,7 @@ package GUI; import DB.*; +import Exceptions.NotFoundInTable; import Exceptions.ProjectionNotSpecified; import GUI.Agenda.Agenda; import GUI.Types.ProjectionType; @@ -44,6 +45,12 @@ public class ProjectionHandler extends JDialog { throw new ProjectionNotSpecified(); this.projectionType = projectionType; this.agenda = agenda; + try { + competitionComboBox.getModel().setSelectedItem(agenda.getCompetition()); + } catch (NotFoundInTable ignored) { + } + dayComboBox.getModel().setSelectedItem(agenda.getDate()); + slotComboBox.getModel().setSelectedItem(agenda.getSlot()); createUIComponents(); }