package gui;
import java.awt.Dimension;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import events.Handling;
public class CalendarPanel extends JPanel {
private static final long serialVersionUID = 1L;
public JScrollPane calendarScrollPane;
public DefaultTableModel calendarTableModel;
private JTable calendarTable;
private static final String[] calendarTableTitle = new String[] { "Monday",
"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
private static JPanelRenderer myRenderer = new JPanelRenderer();
public CalendarPanel(Handling eventHandling) {
setLayout(null);
calendarScrollPane = new JScrollPane();
calendarTableModel = new DefaultTableModel(calendarTableTitle, 0);
calendarTable = new JTable(calendarTableModel);
calendarTable.setRowHeight(100);
calendarTable.getColumnModel().getColumn(0).setPreferredWidth(100);
calendarTable.getColumnModel().getColumn(1).setPreferredWidth(100);
calendarTable.getColumnModel().getColumn(2).setPreferredWidth(100);
calendarTable.getColumnModel().getColumn(3).setPreferredWidth(100);
calendarTable.getColumnModel().getColumn(4).setPreferredWidth(100);
calendarTable.getColumnModel().getColumn(5).setPreferredWidth(100);
calendarTable.getColumnModel().getColumn(6).setPreferredWidth(100);
calendarTable.getTableHeader().setResizingAllowed(true);
calendarTable.getColumnModel().getColumn(0).setCellRenderer(myRenderer);
calendarTable.getColumnModel().getColumn(1).setCellRenderer(myRenderer);
calendarTable.getColumnModel().getColumn(2).setCellRenderer(myRenderer);
calendarTable.getColumnModel().getColumn(3).setCellRenderer(myRenderer);
calendarTable.getColumnModel().getColumn(4).setCellRenderer(myRenderer);
calendarTable.getColumnModel().getColumn(5).setCellRenderer(myRenderer);
calendarTable.getColumnModel().getColumn(6).setCellRenderer(myRenderer);
calendarScrollPane.setViewportView(calendarTable);
Dimension scrollPaneSize = new Dimension(700, 400);
calendarScrollPane.setPreferredSize(scrollPaneSize);
add(calendarScrollPane);
calendarScrollPane.setBounds(20, 20, 700, 400);
generateCalendarFields();
}
public void generateCalendarFields() {
Calendar cal = new GregorianCalendar();
Vector<CalendarElement> daysOfWeek = new Vector<CalendarElement>(7);
CalendarElement newElement;
int run = 0;
int j;
int totalDays = 0;
for (int i = 0; i <= 5; i++) {
switch (i) {
case 0:
switch (cal.getActualMinimum(Calendar.DAY_OF_MONTH)) {
case Calendar.MONDAY:
for (j = 1 ; j <= 7 ; j++) {
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
}
run = 7;
totalDays = totalDays + run;
break;
case Calendar.TUESDAY:
for (int k = 0; k < 1; k++) {
newElement = new CalendarElement();
daysOfWeek.add(newElement);
}
for (j = 1 ; j <= 6 ; j++) {
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
}
run = 6;
totalDays = totalDays + run;
break;
case Calendar.WEDNESDAY:
for (int k = 0; k < 2; k++) {
newElement = new CalendarElement();
daysOfWeek.add(newElement);
}
for (j = 1 ; j <= 5 ; j++) {
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
}
run = 5;
totalDays = totalDays + run;
break;
case Calendar.THURSDAY:
for (int k = 0; k < 3; k++) {
newElement = new CalendarElement();
daysOfWeek.add(newElement);
}
for (j = 1 ; j <= 4 ; j++) {
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
}
run = 4;
totalDays = totalDays + run;
break;
case Calendar.FRIDAY:
for (int k = 0; k < 4; k++) {
newElement = new CalendarElement();
daysOfWeek.add(newElement);
}
for (j = 1 ; j <= 3 ; j++) {
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
}
run = 3;
totalDays = totalDays + run;
break;
case Calendar.SATURDAY:
for (int k = 0; k < 5; k++) {
newElement = new CalendarElement();
daysOfWeek.add(newElement);
}
for (j = 1 ; j <= 2 ; j++) {
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
}
run = 2;
totalDays = totalDays + run;
break;
case Calendar.SUNDAY:
for (int k = 0; k < 6; k++) {
newElement = new CalendarElement();
daysOfWeek.add(newElement);
}
for (j = 1 ; j <= 1; j++) {
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
}
run = 1;
totalDays = totalDays + run;
break;
default:
break;
}
break;
default:
daysOfWeek.removeAllElements();
if(totalDays <= (cal.getActualMaximum(Calendar.DAY_OF_MONTH) + 7)){
for (j = (totalDays + 1) ; j <= (totalDays + 7); j++) {
if(j <= cal.getActualMaximum(Calendar.DAY_OF_MONTH)){
newElement = new CalendarElement(j);
daysOfWeek.add(newElement);
} else{
newElement = new CalendarElement();
daysOfWeek.add(newElement);
}
}
totalDays = totalDays + 7;
}
break;
}
System.out.println("************* Woche: " + i);
for(int y = 0 ; y < daysOfWeek.size() ; y++){
System.out.println(daysOfWeek.elementAt(y).getDay());
}
calendarTableModel.addRow(daysOfWeek);
}
}
}