/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.shedule.week;
import TimeTable.DayOfWeek;
import TimeTable.JTimeTable;
import TimeTable.model.TimeTableModelDefault;
import TimeTable.spans.SpanException;
import TimeTable.spans.TimeSpan;
import TimeTable.spans.TimeSpanAlphBtn;
import beans.shedule.week.TimeOffset;
import cli_fmw.main.MainWindow;
import clips.delegate.shedule.week.SheduleWeekLocal;
import cli_fmw.utils.MessageBox;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
/**
*
* @author Axe Ilshat
*/
public class SheduleWeekTimeTableModel extends TimeTableModelDefault {
private SheduleWeekLocal swl;
private TimeSpan spans[] = new TimeSpan[7];
/**
* Creates a new instance of SheduleWeekTableModel
* @param swl - week delegate
* @throws java.lang.Exception
*/
public SheduleWeekTimeTableModel(SheduleWeekLocal swl) {
this.swl = swl;
GregorianCalendar gc = (GregorianCalendar) GregorianCalendar.getInstance();
gc.set(2007, 0, 1, 0, 0, 0);
gc.set(Calendar.MILLISECOND, 0);
long point = gc.getTimeInMillis();
for(int i=0; i<7; i++) {
DayOfWeek dow = DayOfWeek.createEurope(i);
if(!swl.isWeekend(dow)) {
TimeOffset begin = swl.getTimeBegin(dow);
TimeOffset end = swl.getTimeEnd(dow);
spans[i] = new TimeSpanAlphBtn(
new Date(point + begin.getMilliseconds()), begin.getMinutesTill(end));
addTimeSpan(spans[i]);
System.out.println("model item #" + i + ": " + spans[i].getBegin() + " LOADED");
}
point += 24L * 3600 * 1000;
}
}
/**
* Defines day of week from time span
* @param span
* @return int 0-6
* @throws java.lang.Exception
*/
protected DayOfWeek getDayOfWeek(TimeSpan span) {
for(int i=0; i<spans.length; i++) {
if(spans[i] == span) {
return DayOfWeek.createEurope(i);
}
}
throw new RuntimeException("Cannot define day of week");
}
/**
* Подвтерждает у пользователя и удалеет спан, информирует таблицы
* @param dayOfWeek
* @param span
*/
protected void removeSpanWithConformation(DayOfWeek dayOfWeek, TimeSpan span) {
if(MessageBox.showConfirmYesNo(MessageBox.C_DEL_WORK) == MessageBox.ANSWER_YES) {
swl.setWeekend(dayOfWeek);
spans[dayOfWeek.getEuropean()] = null;
removeTimeSpan(span);
}
}
/**
* Показывает диалог подробной информации о спане
* @param dayOfWeek
* @param span
*/
protected boolean showSpanDetails(DayOfWeek dayOfWeek, TimeSpan span) {
GregorianCalendar base = (GregorianCalendar) GregorianCalendar.getInstance();
base.set(2007, 0, dayOfWeek.getEuropean()+1, 0, 0, 0);
base.set(Calendar.MILLISECOND, 0);
TimeSpanDialog dlg = new TimeSpanDialog(MainWindow.mainWindow, base.getTime(),
span.getBegin(), span.getLength(),
span.canBeResized(), span.canBeMoved(),
span.canBeDeleted(), 5, span.getMinimalTime());
dlg.setVisible(true);
if(dlg.getDlgResult() == TimeSpanDialog.DLG_DELETED) {
removeSpanWithConformation(dayOfWeek, span);
return true;
} else if(dlg.getDlgResult() == TimeSpanDialog.DLG_OK) {
Date timeBegin = dlg.getTimeBegin() ;
Calendar cb = GregorianCalendar.getInstance();
cb.setTime(timeBegin);
try {
span.set(dlg.getTimeBegin(), dlg.getDuration());
int beginTime = (int)(cb.getTimeInMillis()- base.getTimeInMillis())/(60*1000);
swl.setWorkingDay(dayOfWeek, new TimeOffset(beginTime), dlg.getDuration());
} catch (SpanException ex) {
MessageBox.showException(ex);
} catch (Exception ex) {
}
return true;
}
return false;
}
@Override
public void spanResized(TimeSpan span, JTimeTable table) {
DayOfWeek dayOfWeek = getDayOfWeek(span);
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
cal.set(2007, 0, dayOfWeek.getEuropean()+1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
TimeOffset timeBegin = new TimeOffset((int)((span.getBegin().getTime() - cal.getTimeInMillis()) / (60*1000)));
swl.setWorkingDay(dayOfWeek, timeBegin, span.getLength());
}
@Override
public void spanMoved(TimeSpan span, JTimeTable table) {
DayOfWeek dayOfWeek = getDayOfWeek(span);
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
cal.set(2007, 0, dayOfWeek.getEuropean()+1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
TimeOffset timeBegin = new TimeOffset((int)((span.getBegin().getTime() - cal.getTimeInMillis()) / (60*1000)));
swl.setWorkingDay(dayOfWeek, timeBegin, span.getLength());
}
@Override
public void mouseOverSpan(TimeSpan span, JTimeTable table) {
}
@Override
public void mouseLeftSpan(TimeSpan span, JTimeTable table) {
}
@Override
public void mouseEnterSpan(TimeSpan span, JTimeTable table) {
}
/**
* Функция вызывается при двойном щелчке на спане
* @param span
* @param table
*/
@Override
public void spanActivated(TimeSpan span, JTimeTable table) {
DayOfWeek dayOfWeek = getDayOfWeek(span);
showSpanDetails(dayOfWeek, span);
}
/**
* Функция вызывается при нажатии клавиши контестного меню на активном span'e
* @param span
* @param table
* @param x - позиция курсора мыши
* @param y - позиция курсора мыши
*/
@Override
public void spanContextMenu(TimeSpan span, JTimeTable table, int x, int y) {
DayOfWeek dayOfWeek = getDayOfWeek(span);
GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
cal.set(2007, 0, dayOfWeek.getEuropean()+1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
JPopupMenu popup = new JPopupMenu();
//details
JMenuItem menuItem = new JMenuItem("Подробнее...");
menuItem.addActionListener(new DetailsSpanAction(dayOfWeek, span));
popup.add(menuItem);
//delete
menuItem = new JMenuItem("Удалить");
menuItem.addActionListener(new DeleteSpanAction(dayOfWeek, span));
popup.add(menuItem);
popup.show(table, x, y);
}
/**
* Функция вызывается при нажатии клавиши контестного меню
* @param table - таблица
* @param x - позиция курсора мыши
* @param y - позиция курсора мыши
*/
@Override
public void contextMenu(JTimeTable table, int x, int y) {
DayOfWeek dayOfWeek = table.getDay().getDayOfWeek();
if(spans[dayOfWeek.getEuropean()] == null) {
JPopupMenu popup = new JPopupMenu();
JMenuItem menuItem = new JMenuItem("Добавить время работы...");
menuItem.addActionListener(new AddNewSpanAction(dayOfWeek));
popup.add(menuItem);
popup.show(table, x, y);
}
}
/**
* Context menu action
*/
class AddNewSpanAction implements ActionListener {
DayOfWeek dayOfWeek;
public AddNewSpanAction(DayOfWeek dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}
@Override
public void actionPerformed(ActionEvent e) {
Calendar cal = GregorianCalendar.getInstance();
cal.set(2007, 0, dayOfWeek.getEuropean()+1, 0, 0, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.HOUR_OF_DAY, 7);
Date begin = cal.getTime();
TimeSpanAlphBtn span = new TimeSpanAlphBtn(begin, 8*60);
span.setCanBeDeleted(false);
if(showSpanDetails(dayOfWeek, span)) {
spans[dayOfWeek.getEuropean()] = span;
span.setCanBeDeleted(true);
addTimeSpan(span);
}
}
}
/**
* Context menu action
*/
class DeleteSpanAction implements ActionListener {
DayOfWeek dayOfWeek;
TimeSpan span;
public DeleteSpanAction (DayOfWeek dayOfWeek, TimeSpan span) {
this.dayOfWeek = dayOfWeek;
this.span = span;
}
@Override
public void actionPerformed(ActionEvent e) {
removeSpanWithConformation(dayOfWeek, span);
};
}
/**
* Context menu action
*/
class DetailsSpanAction implements ActionListener {
DayOfWeek dayOfWeek;
TimeSpan span;
public DetailsSpanAction (DayOfWeek dayOfWeek, TimeSpan span) {
this.dayOfWeek = dayOfWeek;
this.span = span;
}
@Override
public void actionPerformed(ActionEvent e) {
showSpanDetails(dayOfWeek, span);
};
}
}