package clips.shedule.template;
import TimeTable.DayOfWeek;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.ErrorValue;
import cli_fmw.utils.MessageBox;
import clips.delegate.shedule.individual.SheduleIndividualData;
import clips.delegate.shedule.individual.SheduleIndividualLocal;
import clips.delegate.shedule.individual.WeekIndex;
import clips.delegate.shedule.week.SheduleWeekLocal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import javax.swing.table.AbstractTableModel;
class PrintTableModel extends AbstractTableModel {
private final SheduleIndividualLocal sil;
private final SheduleWeekLocal swl;
public PrintTableModel(SheduleIndividualLocal sil, SheduleWeekLocal swl) {
this.sil = sil;
this.swl = swl;
}
@Override
public int getRowCount() {
try {
return sil.getWeekCount();
} catch (ClipsException ex) {
MessageBox.showException(ex);
return 0;
}
}
@Override
public int getColumnCount() {
return 8;
}
@Override
public String getColumnName(int column) {
if (column == 0) {
return "Неделя";
}
DayOfWeek dayOfWeek = DayOfWeek.createEurope(column - 1);
return dayOfWeek.getTitle();
}
@Override
@SuppressWarnings(value = {"unchecked"})
public Object getValueAt(int rowIndex, int col) {
if (col == 0) {
return "Неделя " + (rowIndex + 1);
} else {
try {
DayOfWeek dayOfWeek = DayOfWeek.createEurope(col - 1);
Iterator<SheduleIndividualData> it = sil.daySelector(new WeekIndex(rowIndex), dayOfWeek);
String toScreen = "";
if (swl.isWeekend(dayOfWeek)) {
toScreen += "Выходной день\n";
}
if (!it.hasNext()) {
toScreen += "Нет работ";
} else {
Calendar tb = GregorianCalendar.getInstance();
Calendar te = GregorianCalendar.getInstance();
ArrayList<SheduleIndividualData> datas = new ArrayList<SheduleIndividualData>();
while (it.hasNext()) {
datas.add(it.next());
}
Collections.sort(datas, new SheduleIndividualDataComparator());
for (int i = 0; i < datas.size(); i++) {
SheduleIndividualData data = datas.get(i);
Date begin = data.getBegin();
int duration = data.getWorkDuration();
tb.setTime(begin);
te.setTime(begin);
te.add(Calendar.MINUTE, duration);
String timeTxt = String.format("%02d:%02d-%02d:%02d", tb.get(Calendar.HOUR_OF_DAY), tb.get(Calendar.MINUTE), te.get(Calendar.HOUR_OF_DAY), te.get(Calendar.MINUTE));
String type = data.getType().getTitle();
String cabinet = data.getCabinet().toString();
toScreen = toScreen + type + "\n" + timeTxt + "\n(" + cabinet + ") \n\n";
}
}
return toScreen;
} catch (ClipsException ex) {
return new ErrorValue(ex);
}
}
}
class SheduleIndividualDataComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
SheduleIndividualData p1 = (SheduleIndividualData) o1;
SheduleIndividualData p2 = (SheduleIndividualData) o2;
if (p1.getBegin().after(p2.getBegin())) {
return 1;
} else {
return -1;
}
}
}
}