package clips.shedule.individual;
import TimeTable.Day;
import TimeTable.DayOfWeek;
import beans.shedule.prorumble.SheduleProrumbleBean;
import cli_fmw.main.ClipsException;
import cli_fmw.utils.ErrorValue;
import clips.delegate.shedule.SheduleHelper;
import clips.delegate.shedule.SheduledDay;
import clips.delegate.shedule.exception.SheduleExceptionLocal;
import clips.delegate.shedule.individual.SheduleIndividualData;
import clips.delegate.shedule.individual.SheduleIndividualLocal;
import clips.delegate.shedule.individual.WeekIndex;
import clips.delegate.shedule.prorumble.SheduleProrumbleLocal;
import clips.shedule.individual.PanelSheduleIndividual.DayUI;
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;
/**
*
* @author axe
*/
class PrintTableModel extends AbstractTableModel {
private final ComboOption options[];
private final String cols[];
private final WeekIndex currentWeekIndex;
private final SheduleIndividualLocal sil;
private final SheduleProrumbleLocal spl;
public PrintTableModel(ComboOption[] options, String cols[], WeekIndex currentWeekIndex,
SheduleIndividualLocal sil, SheduleProrumbleLocal spl) {
this.options = options;
this.cols = cols;
this.currentWeekIndex = currentWeekIndex;
this.sil = sil;
this.spl = spl;
}
@Override
public int getRowCount() {
return 2;
}
@Override
public int getColumnCount() {
return 7;
}
@Override
@SuppressWarnings(value = "unchecked")
public Object getValueAt(int rowIndex, int columnIndex) {
ComboOption option = options[columnIndex];
String toScreen = "";
if (rowIndex == 0) {
toScreen = option.toString();
} else if (option == PanelSheduleIndividual.planOption
|| option == PanelSheduleIndividual.exceptionOption) {
try {
DayOfWeek dayOfWeek = DayOfWeek.createEurope(columnIndex);
Iterator<SheduleIndividualData> it;
if (option == PanelSheduleIndividual.planOption) {
int week = currentWeekIndex.getIndex() % sil.getWeekCount();
it = sil.daySelector(new WeekIndex(week), dayOfWeek);
} else {
it = sil.daySelector(currentWeekIndex, dayOfWeek);
}
Day day = currentWeekIndex.getDay(dayOfWeek);
SheduledDay sheduledDay = new SheduleHelper(spl, sil).getSheduledDay(day);
if (!sheduledDay.isWorking()) {
String desc = sheduledDay.getDescription();
toScreen += (desc == null) ?
"Выходной день\n" : "Выходной день:"+ sheduledDay.getDescription() + "\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);
}
}
return toScreen;
}
@Override
public String getColumnName(int column) {
return cols[column];
}
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;
}
}
}
}