/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.shedule.template;
import TimeTable.JTimeTable;
import TimeTable.model.TimeTableModelDefault;
import TimeTable.spans.SpanException;
import TimeTable.spans.TimeSpan;
import cli_fmw.main.ClipsException;
import clips.delegate.shedule.individual.SheduleIndividualLocal;
import TimeTable.DayOfWeek;
import cli_fmw.main.MainWindow;
import cli_fmw.main.audit.AuditManager;
import clips.delegate.shedule.individual.SheduleIndividualData;
import clips.delegate.shedule.individual.WeekIndex;
import cli_fmw.utils.SelectorEditable;
import cli_fmw.utils.MessageBox;
import clips.shedule.week.TimeSpanDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
/**
*
* @author Axe Ilshat
*/
public class SheduleTemplateTableModel extends TimeTableModelDefault {
private SelectorEditable<SheduleIndividualData> currentList;
protected SheduleIndividualLocal sil;
protected WeekIndex currentweekIndex;
private AuditManager am;
/**
* Creates a new instance of SheduleWeekTableModel
* @param sil
* @param weekIndex
* @throws ClipsException
*/
public SheduleTemplateTableModel(SheduleIndividualLocal sil, WeekIndex weekIndex, AuditManager am)
throws ClipsException {
this.sil = sil;
this.am = am;
rebuildContent(weekIndex);
}
/**
* Очищает список работ и загружает его заново
* @param index
* @throws ClipsException
*/
protected void rebuildContent(WeekIndex index) throws ClipsException {
removeAllTimeSpans();
currentList = sil.weekSelector(index);
for(int i=0; i<currentList.size(); i++) {
TimeSpanWork span = new TimeSpanWork(currentList.get(i));
addTimeSpan(span);
}
fireTimeModelEvent();
currentweekIndex = index;
System.out.println(getClass().getCanonicalName() + " Rebuld content: " + currentList.size());
}
public void rebuildContent() throws ClipsException {
rebuildContent(currentweekIndex);
}
/**
* Подтверждает у пользователя и удалеет спан, информирует таблицы
* @param span
*/
protected void removeSpanWithConformation(TimeSpanWork span) {
if(MessageBox.showConfirmYesNo(MessageBox.C_DEL_WORK) == MessageBox.ANSWER_YES) {
SheduleIndividualData work = span.getWork();
currentList.remove(work);
removeTimeSpan(span);
}
}
/**
* Показывает диалог подробной информации о спане
* @param span
* @return
*/
protected boolean showSpanDetails(TimeSpanWork span) {
try {
SheduleIndividualData job = span.getWork();
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
gc.setTimeInMillis(job.getBegin().getTime());
int begin = gc.get(GregorianCalendar.HOUR_OF_DAY)*60 + gc.get(GregorianCalendar.MINUTE);
int end = begin + job.getWorkDuration();
IndividualTimeSpanDialog dlg = new IndividualTimeSpanDialog(MainWindow.mainWindow, begin, end,
span.canBeResized(), span.canBeMoved(), span.canBeDeleted(),
5, span.getMinimalTime()/(60*1000), job.getType(), sil.getCollaborator(), job.getCabinet(), am);
dlg.setVisible(true);
if (dlg.getDlgResult() == IndividualTimeSpanDialog.DLG_DELETED) {
removeSpanWithConformation(span);
return true;
} else if (dlg.getDlgResult() == IndividualTimeSpanDialog.DLG_OK) {
SheduleIndividualData saved = (SheduleIndividualData) job.clone();
gc.set(GregorianCalendar.HOUR_OF_DAY, dlg.getTimeBegin()/60);
gc.set(GregorianCalendar.MINUTE, dlg.getTimeBegin()%60);
job.setBegin(gc.getTime());
job.setWorkDuration(dlg.getTimeEnd() - dlg.getTimeBegin());
job.setType(dlg.getWorkType());
job.setCabinet(dlg.getCabinet());
try {
span.updateSpan();
} catch (SpanException ex) {
MessageBox.showException(ex);
job.setBegin(saved.getBegin());
job.setWorkDuration(saved.getWorkDuration());
job.setType(saved.getType());
job.setCabinet(saved.getCabinet());
}
return true;
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
return false;
}
/**
* Вызывается контролом при изменении размеров спана
* @param span
* @param table
*/
@Override
public void spanResized(TimeSpan span, JTimeTable table) {
TimeSpanWork jobSpan = (TimeSpanWork)span;
SheduleIndividualData job = jobSpan.getWork();
job.setBegin(span.getBegin());
job.setWorkDuration(span.getLength());
}
/**
* Вызывается контролом при перемещении спана
* @param span
* @param table
*/
@Override
public void spanMoved(TimeSpan span, JTimeTable table) {
TimeSpanWork jobSpan = (TimeSpanWork)span;
SheduleIndividualData job = jobSpan.getWork();
job.setBegin(span.getBegin());
job.setWorkDuration(span.getLength());
}
/**
* Функция вызывается при двойном щелчке на спане
* @param span
* @param table
*/
@Override
public void spanActivated(TimeSpan span, JTimeTable table) {
showSpanDetails((TimeSpanWork)span);
}
/**
* Функция вызывается при нажатии клавиши контестного меню на активном span'e
* @param span
* @param table
* @param x - позиция курсора мыши
* @param y - позиция курсора мыши
*/
@Override
public void spanContextMenu(TimeSpan span, JTimeTable table, int x, int y) {
TimeSpanWork jobSpan = (TimeSpanWork)span;
JPopupMenu popup = new JPopupMenu();
//details
JMenuItem menuItem = new JMenuItem("Подробнее о работе...");
menuItem.addActionListener(new SpanAction(jobSpan, SpanAction.DETAILS));
popup.add(menuItem);
//delete
menuItem = new JMenuItem("Удалить");
menuItem.addActionListener(new SpanAction(jobSpan, SpanAction.DELETE));
popup.add(menuItem);
popup.show(table, x, y);
}
/**
* Функция вызывается при нажатии клавиши контестного меню
* @param table - таблица
* @param x - позиция курсора мыши
* @param y - позиция курсора мыши
*/
@Override
public void contextMenu(JTimeTable table, int x, int y) {
JPopupMenu popup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Добавить работу...");
DayOfWeek dayOfWeek = table.getDay().getDayOfWeek();
menuItem.addActionListener(new AddNewSpanAction(dayOfWeek));
popup.add(menuItem);
popup.show(table, x, y);
}
/**
* Context menu action
*/
protected class AddNewSpanAction implements ActionListener {
DayOfWeek day;
public AddNewSpanAction(DayOfWeek day) {
this.day = day;
}
@Override
public void actionPerformed(ActionEvent e) {
try {
IndividualTimeSpanDialog dlg = new IndividualTimeSpanDialog(MainWindow.mainWindow, 9*60, 21*60,
true, true, false, 5, 10, null, sil.getCollaborator(), null, am);
dlg.setVisible(true);
if (dlg.getDlgResult() == TimeSpanDialog.DLG_OK) {
SheduleIndividualData work = new SheduleIndividualData(sil.getCollaborator());
Calendar cal = currentweekIndex.getBegin();
cal.add(Calendar.DAY_OF_MONTH, day.getEuropean());
cal.add(Calendar.MINUTE, dlg.getTimeBegin());
work.setBegin(cal.getTime());
work.setWorkDuration(dlg.getTimeEnd() - dlg.getTimeBegin());
work.setDefaultServiceDuration(0);
work.setType(dlg.getWorkType());
work.setCabinet(dlg.getCabinet());
currentList.append(work);
TimeSpanWork span = new TimeSpanWork(work);
addTimeSpan(span);
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
/**
* Context menu action
*/
protected class SpanAction implements ActionListener {
public static final int DETAILS = 0;
public static final int DELETE = 1;
public static final int ADDNEW = 2;
TimeSpanWork jobSpan;
int action;
public SpanAction (TimeSpanWork jobSpan, int action) {
this.jobSpan = jobSpan;
this.action = action;
}
@Override
public void actionPerformed(ActionEvent e) {
switch(action) {
case DETAILS:
showSpanDetails(jobSpan);
break;
case DELETE:
removeSpanWithConformation(jobSpan);
break;
}
};
}
}