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/Agenda/Agenda.java
Tergel TSAGAAN 28d2b300f1 fix popup doesn't appear when left click
added cell selection when displaying popup
2021-01-07 14:38:53 +01:00

193 lines
7.2 KiB
Java

package GUI.Agenda;
import DB.Competition;
import DB.Projection;
import GUI.ProjectionHandler;
import GUI.Types.CompetType;
import GUI.Types.ProjectionType;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.sql.Date;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
public class Agenda extends JPanel {
private JPanel agendaPanel;
private String headers[];
private int totalDay;
private int currentPage;
private JTable table;
private CompetType competionType;
private Agenda agenda;
public Agenda(JPanel agendaPanel, int currentPage, CompetType competionType) {
this.agendaPanel = agendaPanel;
this.headers = competionType.getHeaders();
this.totalDay = 11;
this.currentPage = currentPage;
this.competionType = competionType;
this.agendaPanel.removeAll();
int totalPages[] = dayToPage();
if (this.currentPage >= totalPages.length) {
this.currentPage = totalPages.length - 1;
} else if (this.currentPage < 0) {
this.currentPage = 0;
}
JScrollPane scroll = constructAgenda(totalPages[this.currentPage]);
agendaPanel.add(scroll);
}
public void refresh() {
this.agendaPanel.removeAll();
int totalPages[] = dayToPage();
if (this.currentPage >= totalPages.length) {
this.currentPage = totalPages.length - 1;
} else if (this.currentPage < 0) {
this.currentPage = 0;
}
JScrollPane scroll = constructAgenda(totalPages[this.currentPage]);
agendaPanel.add(scroll);
agendaPanel.repaint();
agendaPanel.revalidate();
}
private int[] dayToPage() {
int day = this.totalDay;
int count = 0;
while (day >= 5) {
day = day - 5;
count++;
}
int rest = totalDay - 5 * count;
int totalPages = rest < 5 ? count + 1 : count;
int pages[] = new int[totalPages];
Arrays.fill(pages, 5);
if (rest < 5) pages[totalPages - 1] = rest;
return pages;
}
private JScrollPane constructAgenda(int day) {
ListModel lm = new AbstractListModel() {
public int getSize() {
return headers.length;
}
public Object getElementAt(int index) {
return headers[index];
}
};
TableModel dm = new TableModel(day, currentPage, headers.length);
table = new JTable(dm) {
@Override
public boolean getScrollableTracksViewportHeight() {
return true;
}
};
table.setCellSelectionEnabled(true);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
table.setRowHeight(100);
Agenda tmp = this;
table.addMouseListener(new MouseInputAdapter() {
@Override
public void mouseReleased(MouseEvent mouseEvent) {
if (mouseEvent.isPopupTrigger()) {
int row = table.rowAtPoint(mouseEvent.getPoint());
int column = table.getTableHeader().columnAtPoint(mouseEvent.getPoint());
table.changeSelection(row, column, false, false);
Object currentCellValue = table.getValueAt(row, column);
Projection projection = null;
if (currentCellValue.getClass() == Projection.class)
projection = (Projection) currentCellValue;
PopUpProjection popUpProjection = new PopUpProjection(projection, tmp);
popUpProjection.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
}
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
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 (projection == null) {
openDialog(ProjectionType.ADD);
} else {
openDialog(ProjectionType.EDIT, projection);
}
} else if (mouseEvent.isPopupTrigger()) {
PopUpProjection popUpProjection = new PopUpProjection(projection, tmp);
popUpProjection.show(mouseEvent.getComponent(), mouseEvent.getX(), mouseEvent.getY());
}
}
});
TableColumnModel columnModel = table.getColumnModel();
int columnCount = columnModel.getColumnCount();
for (int i = 0; i < columnCount; i++) {
table.getColumnModel().getColumn(i).setPreferredWidth(200);
}
JList rowHeader = new JList(lm);
rowHeader.setFixedCellWidth(100);
rowHeader.setFixedCellHeight(100);
rowHeader.setCellRenderer(new RowHeaderRenderer(table));
JScrollPane scroll = new JScrollPane(table);
scroll.setRowHeaderView(rowHeader);
scroll.setBorder(BorderFactory.createEmptyBorder());
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
this.agendaPanel.setPreferredSize(new Dimension(this.agendaPanel.getWidth(), headers.length * 100 + 20));
updateMovies();
return scroll;
}
private void updateMovies() {
try {
ArrayList<Projection> projections = Projection.getAvailable(new Competition(this.competionType.getCompetition()));
for (Projection projection : projections) {
addMovie(projection);
}
} catch (Exception e) {
}
}
protected void openDialog(ProjectionType projectionType) {
ProjectionHandler dialog = new ProjectionHandler(projectionType, this);
dialog.pack();
dialog.setVisible(true);
}
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"};
for (int i = 0; i < times.length; i++) {
if (times[i].equals(time.toString())) {
row = i;
break;
}
}
table.setValueAt(projection, row, column);
}
public int getCurrentPage() {
return this.currentPage;
}
}