/*
* PanelShedule.java
*
* Created on 29 Февраль 2008 г., 17:21
*/
package clips.shedule.reception.self;
import TimeTable.Day;
import TimeTable.DayOfWeek;
import TimeTable.JTableRuler;
import TimeTable.JTimeTable;
import TimeTable.model.TimeTableModel;
import TimeTable.scaler.TimeScaler;
import TimeTable.scaler.TimeScalerTraditional;
import cli_fmw.delegate.DelegateLine2;
import clips.delegate.service.SerRenLocal;
import clips.delegate.shedule.exception.SheduleExceptionLocal;
import clips.delegate.shedule.individual.SheduleIndividualLocal;
import clips.delegate.shedule.individual.WeekIndex;
import clips.delegate.shedule.prorumble.SheduleProrumbleData;
import clips.delegate.shedule.prorumble.SheduleProrumbleLocal;
import clips.delegate.shedule.reception.SheduleReceptionData;
import clips.delegate.shedule.reception.SheduleReceptionLocal;
import clips.delegate.shedule.week.SheduleWeekLocal;
import cli_fmw.delegate.utils.TimeLocal;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.main.Printable;
import clips.shedule.template.SheduleTemplateTableModel;
import clips.shedule.reception.SheduleReceptionTableModel;
import cli_fmw.utils.ErrorValue;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.Selector;
import cli_fmw.main.ClipsException;
import cli_fmw.main.PrintCreators;
import cli_fmw.main.PageGeneric;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import clips.delegate.shedule.SheduleHelper;
import clips.delegate.shedule.SheduledDay;
import clips.login.UserInfo;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import framework.utils.Converter;
/**
*
* @author axe
*/
public class PanelSelfReception extends PageGeneric
implements Persistentable, Printable {
private final static int PANELMODE_OK = 0;
private final static int PANELMODE_ERROR = 1;
private final static int PANELMODE_HOLIDAY = 2;
private final static int PANELMODE_NOSELECTION = 3;
private SheduleWeekLocal swl;
private SheduleReceptionLocal srl;
private SheduleExceptionLocal sel;
DirectoryCollaboratorItem collaborator;
private TimeScaler scaler;
private Calendar currentDay;
private CalendarProcess calendarProcess = new CalendarProcess();
private JLabel stubLabel, stubLabel2;
class WeekendException extends ClipsException {
WeekendException(String desc) {
super(desc);
}
}
public PanelSelfReception(PageContainer container) throws ClipsException {
super(container);
initComponents();
jScrollPane1.getVerticalScrollBar().setUnitIncrement(15);
stubLabel = new JLabel();
stubLabel.setHorizontalAlignment(JLabel.CENTER);
stubLabel2 = new JLabel();
stubLabel2.setHorizontalAlignment(JLabel.CENTER);
this.collaborator = UserInfo.get().getCollaborator();
swl = SheduleWeekLocal.getInstance();
sel = SheduleExceptionLocal.getInstance();
try {
initPanel(collaborator);
setPanelMode(PANELMODE_OK, null);
} catch (WeekendException ex) {
setPanelMode(PANELMODE_HOLIDAY, ex.getLocalizedMessage());
} catch (Exception ex) {
setPanelMode(PANELMODE_ERROR, ex.getLocalizedMessage());
MessageBox.showException(ex);
}
calendar.setCalendar(TimeLocal.getCurrentTime());
calendar.addPropertyChangeListener(calendarProcess);
StateSaver.attachTo(this);
}
@Override
public String getPageTitle() {
return "Мое расписание приема";
}
@Override
public boolean isDirty() {
return (srl != null && srl.isDirty());
}
@Override
public void save() throws ClipsException {
if(srl != null) {
srl.save1();
}
}
@Override
public void restore() {
if(srl != null) {
try {
srl.restore();
initPanel(collaborator);
setPanelMode(PANELMODE_OK, null);
} catch (WeekendException ex) {
setPanelMode(PANELMODE_HOLIDAY, ex.getLocalizedMessage());
} catch (Exception ex) {
setPanelMode(PANELMODE_ERROR, ex.getLocalizedMessage());
MessageBox.showException(ex);
}
}
}
private void setPanelMode(int mode, String description) {
boolean zoom = true;
boolean cal = true;
switch(mode) {
case PANELMODE_OK: {
break;
}
case PANELMODE_ERROR: {
stubPanels(description);
zoom = false;
cal = false;
break;
}
case PANELMODE_HOLIDAY: {
stubPanels(description);
zoom = false;
break;
}
case PANELMODE_NOSELECTION: {
stubPanels("");
zoom = false;
cal = false;
break;
}
}
calendar.setEnabled(cal);
hourScaler.setEnabled(zoom);
planPanel.revalidate();
realPanel.revalidate();
}
private void stubPanels(String description) {
realPlacement.removeAll();
planPlacement.removeAll();
stubLabel.setText(description);
planPlacement.setLayout(new GridLayout());
planPlacement.add(stubLabel);
stubLabel.repaint();
stubLabel2.setText(description);
realPlacement.setLayout(new GridLayout());
realPlacement.add(stubLabel2);
stubLabel2.repaint();
}
/**
* Вызывается в после изменения дня в календаре или текущего сотрудника.
* Создает модели для расписаний и загружает данные
* @throws cli_fmw.delegate.exception.ClipsException
*/
private void initPanel(DirectoryCollaboratorItem collaborator)
throws ClipsException {
Calendar cal = calendar.getCalendar();
int day = cal.get(Calendar.DAY_OF_MONTH);
int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
DayOfWeek dayOfWeek = DayOfWeek.create(cal);
scaler = new TimeScalerTraditional(
swl.getTimeBegin(dayOfWeek).getHours(),
swl.getTimeEnd(dayOfWeek).getHours(), 0.2f);
scaler.setMinHourHeight(hourScaler.getValue());
planPlacement.removeAll();
realPlacement.removeAll();
SheduleProrumbleLocal spl = new SheduleProrumbleLocal(collaborator);
SheduleIndividualLocal sil = new SheduleIndividualLocal(collaborator);
SheduleHelper sh = new SheduleHelper(spl, sil);
SheduledDay sheduledDay = sh.getSheduledDay(new Day(cal.getTimeInMillis()));
if(!sheduledDay.isWorking()) {
srl = null;
throw new WeekendException(sheduledDay.getDescription());
}
Day thisDay = new Day(year, month, day);
initPlanPanel(collaborator, thisDay, dayOfWeek, scaler, sheduledDay);
initReceptionPanel(collaborator, thisDay, scaler, sheduledDay);
}
/**
* Инициализирует панель плана
* @param collaborator
* @param day
* @param dayOfWeek
* @param scaler
* @param di
* @throws generic.ClipsException
*/
private void initPlanPanel(DirectoryCollaboratorItem collaborator,
Day day, DayOfWeek dayOfWeek, TimeScaler scaler, SheduledDay di) throws ClipsException {
SheduleProrumbleLocal spl = new SheduleProrumbleLocal(collaborator);
SheduleProrumbleData prorumble = spl.getProrumble(day);
if(prorumble != null) {
throw new WeekendException(prorumble.getType().toString());
}
TimeTableModel tableModel = null;
SheduleIndividualLocal sil = new SheduleIndividualLocal(collaborator);
WeekIndex weekIndex = new WeekIndex(day);
JTimeTable table = new JTimeTable();
if (sil.daySelector(weekIndex, dayOfWeek).hasNext()) {
tableModel = new SheduleTemplateTableModel(sil, weekIndex, getAuditManager());
table.setDay(day);
System.out.println("TimeTable set day to " + day + " week:" + weekIndex);
} else {
WeekIndex planWeekIndex = new WeekIndex(weekIndex.getIndex() % sil.getWeekCount());
tableModel = new SheduleTemplateTableModel(sil, planWeekIndex, getAuditManager());
Day planDay = planWeekIndex.getDay(dayOfWeek);
table.setDay(planDay);
System.out.println("PlanTimeTable set day to " + planDay + " week:" + planWeekIndex);
}
JTableRuler ruler = new JTableRuler(scaler);
table.setScaler(scaler);
table.setReadOnly(true);
int shiftBegin = di.getClinicShiftBegin().getMilliseconds();
int shiftEnd = di.getClinicShiftEnd().getMilliseconds();
table.setShiftBegin(shiftBegin);
table.setShiftEnd(shiftEnd);
ruler.setShiftBegin(shiftBegin);
ruler.setShiftEnd(shiftEnd);
table.setModel(tableModel);
planPlacement.setLayout(new BorderLayout(0, 0));
planPlacement.add(ruler, java.awt.BorderLayout.WEST);
planPlacement.add(table, java.awt.BorderLayout.CENTER);
}
/**
* Инициализирует панель приема
* @param collaborator
* @param day
* @param scaler
* @param di
* @throws generic.ClipsException
*/
private void initReceptionPanel(DirectoryCollaboratorItem collaborator, Day day,
TimeScaler scaler, SheduledDay di)
throws ClipsException {
srl = new SheduleReceptionLocal(day, collaborator);
srl.addContentStateListener(getContainer());
SheduleReceptionTableModel tableModel =
new SheduleReceptionTableModel(srl.selector(), null, day, collaborator, description, getAuditManager());
JTimeTable table = new JTimeTable();
JTableRuler ruler = new JTableRuler(scaler);
table.setScaler(scaler);
int shiftBegin = di.getClinicShiftBegin().getMilliseconds();
int shiftEnd = di.getClinicShiftEnd().getMilliseconds();
table.setShiftBegin(shiftBegin);
table.setShiftEnd(shiftEnd);
ruler.setShiftBegin(shiftBegin);
ruler.setShiftEnd(shiftEnd);
table.setDay(day);
table.setModel(tableModel);
realPlacement.setLayout(new BorderLayout(0, 0));
realPlacement.add(ruler, java.awt.BorderLayout.WEST);
realPlacement.add(table, java.awt.BorderLayout.CENTER);
Day today = new Day(TimeLocal.getCurrentTime().getTimeInMillis());
table.setReadOnly(today.getMilliseconds() > day.getMilliseconds());
System.out.println("today= " + today.getMilliseconds() + " day=" + day.getMilliseconds());
}
class CalendarProcess implements PropertyChangeListener {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (!evt.getPropertyName().equals("calendar")) {
return;
}
try {
if(isDirty()) {
if(currentDay != null) {
calendar.removePropertyChangeListener(calendarProcess);
calendar.setCalendar(currentDay);
calendar.addPropertyChangeListener(calendarProcess);
}
return;
}
initPanel(collaborator);
setPanelMode(PANELMODE_OK, null);
} catch (WeekendException ex) {
setPanelMode(PANELMODE_HOLIDAY, ex.getLocalizedMessage());
calendar.setEnabled(true);
} catch (Exception ex) {
setPanelMode(PANELMODE_ERROR, ex.getLocalizedMessage());
MessageBox.showExceptionOnly(ex);
}
}
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
content = new javax.swing.JPanel();
mainContent = new javax.swing.JPanel();
leftToolbar = new javax.swing.JPanel();
calendar = new com.toedter.calendar.JCalendar();
jLabel4 = new javax.swing.JLabel();
hourScaler = new javax.swing.JSlider();
jLabel5 = new javax.swing.JLabel();
jScrollPane2 = new javax.swing.JScrollPane();
description = new javax.swing.JTextArea();
jPanel1 = new javax.swing.JPanel();
head = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
body = new javax.swing.JPanel();
planPanel = new javax.swing.JPanel();
planPlacement = new javax.swing.JPanel();
realPanel = new javax.swing.JPanel();
realPlacement = new javax.swing.JPanel();
setLayout(new java.awt.BorderLayout(10, 0));
content.setLayout(new java.awt.BorderLayout());
mainContent.setLayout(new java.awt.BorderLayout(10, 0));
leftToolbar.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
calendar.setEnabled(false);
jLabel4.setText("Дата:");
hourScaler.setMaximum(400);
hourScaler.setMinimum(20);
hourScaler.addChangeListener(new javax.swing.event.ChangeListener() {
public void stateChanged(javax.swing.event.ChangeEvent evt) {
hourScalerStateChanged(evt);
}
});
jLabel5.setText("Увеличение:");
description.setLineWrap(true);
description.setBorder(javax.swing.BorderFactory.createTitledBorder("Примечания"));
jScrollPane2.setViewportView(description);
javax.swing.GroupLayout leftToolbarLayout = new javax.swing.GroupLayout(leftToolbar);
leftToolbar.setLayout(leftToolbarLayout);
leftToolbarLayout.setHorizontalGroup(
leftToolbarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(leftToolbarLayout.createSequentialGroup()
.addContainerGap()
.addGroup(leftToolbarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(calendar, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(hourScaler, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
.addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
leftToolbarLayout.setVerticalGroup(
leftToolbarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(leftToolbarLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(calendar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(hourScaler, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE)
.addGap(53, 53, 53))
);
mainContent.add(leftToolbar, java.awt.BorderLayout.EAST);
jPanel1.setLayout(new java.awt.BorderLayout());
head.setLayout(new java.awt.GridLayout(1, 0, 10, 0));
jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel2.setText("Работы");
jLabel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
head.add(jLabel2);
jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel3.setText("Расписание");
jLabel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jLabel3.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
head.add(jLabel3);
jPanel1.add(head, java.awt.BorderLayout.NORTH);
body.setLayout(new java.awt.GridLayout(1, 0, 10, 0));
planPanel.setLayout(new java.awt.BorderLayout());
planPlacement.setLayout(new java.awt.GridLayout(1, 0));
planPanel.add(planPlacement, java.awt.BorderLayout.CENTER);
body.add(planPanel);
realPanel.setLayout(new java.awt.BorderLayout());
realPlacement.setLayout(new java.awt.GridLayout(1, 0));
realPanel.add(realPlacement, java.awt.BorderLayout.CENTER);
body.add(realPanel);
jScrollPane1.setViewportView(body);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
mainContent.add(jPanel1, java.awt.BorderLayout.CENTER);
content.add(mainContent, java.awt.BorderLayout.CENTER);
add(content, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void hourScalerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_hourScalerStateChanged
int val = hourScaler.getValue();
scaler.setMinHourHeight(val);
for(int i=0; i<planPlacement.getComponentCount(); i++) {
Component c = planPlacement.getComponent(i);
if(c instanceof JTableRuler) {
JTableRuler ruler = (JTableRuler) c;
ruler.revalidate();
}
}
for(int i=0; i<realPlacement.getComponentCount(); i++) {
Component c = realPlacement.getComponent(i);
if(c instanceof JTableRuler) {
JTableRuler ruler = (JTableRuler) c;
ruler.revalidate();
}
}
planPanel.revalidate();
realPanel.revalidate();
}//GEN-LAST:event_hourScalerStateChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel body;
private com.toedter.calendar.JCalendar calendar;
private javax.swing.JPanel content;
private javax.swing.JTextArea description;
private javax.swing.JPanel head;
private javax.swing.JSlider hourScaler;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JPanel leftToolbar;
private javax.swing.JPanel mainContent;
private javax.swing.JPanel planPanel;
private javax.swing.JPanel planPlacement;
private javax.swing.JPanel realPanel;
private javax.swing.JPanel realPlacement;
// End of variables declaration//GEN-END:variables
@Override
public DelegateLine2 getDelegate() {
return null;
}
@Override
public boolean readyForPrint() {
return !isDirty();
}
@Override
public void print() {
try {
if (readyForPrint()) {
HashMap<String, Object> data = new LinkedHashMap<String, Object>();
String title = "Записи на прием сотрудника: " + collaborator.getTitle();
data.put("date", "Дата: " + Converter.dateToString(calendar.getDate()));
TableModel model = null;
if (srl.selector() == null || srl.selector().size() == 0) {
data.put("noData", "нет записей на прием");
} else {
model = new PrintTableModel();
}
PrintCreators.createGeneratedReport(title, null, data.entrySet(), null, model, getClass());
}
} catch (Exception ex) {
MessageBox.showExceptionOnly(ex);
}
}
class PrintTableModel extends AbstractTableModel {
private ArrayList<SheduleReceptionData> datas = new ArrayList<SheduleReceptionData>();
private final static int COL_TIME = 0;
private final static int COL_CLIENT = 1;
private final static int COL_DUTY = 2;
private final static int COLCOUNT = 3;
public PrintTableModel() throws ClipsException {
Selector<SheduleReceptionData> selector = srl.selector();
for (int i = 0; i < selector.size(); i++) {
datas.add(selector.get(i));
}
java.util.Collections.sort(datas, new SheduleReceptionDataComparator());
}
@Override
public int getRowCount() {
return datas.size();
}
@Override
public int getColumnCount() {
return COLCOUNT;
}
@Override
public String getColumnName(int column) {
if (column == COL_TIME) {
return "Интервал";
}
else if (column == COL_CLIENT) {
return "Пациент";
}
else if (column == COL_DUTY) {
return "Включенные услуги";
}
else {
return "Хммм, а это откуда?";
}
}
@Override
public Object getValueAt(int rowIndex, int col) {
try {
SheduleReceptionData data = datas.get(rowIndex);
if (col == COL_TIME) {
Calendar tb = GregorianCalendar.getInstance();
Calendar te = GregorianCalendar.getInstance();
tb.setTime(data.getBegin());
te.setTime(data.getEnd());
return 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));
}
else if (col == COL_CLIENT) {
return data.getClient(null).toString();
}
else if (col == COL_DUTY){
String services = "";
List<Integer> srl = data.getLinkedServicesList();
List<SerRenLocal> clientServices = data.getClient(null).getSerRenListToRender();
for (int i = 0; i < clientServices.size(); i++) {
SerRenLocal s = clientServices.get(i);
if (srl.contains(s.getID())) {
try {
services += " - " + s.getService().getTitle() + "\n";
} catch (ClipsException ex) {
ex.printStackTrace();
}
}
}
return services;
}
else {
return "Хммм, а это откуда?";
}
} catch (ClipsException ex) {
return new ErrorValue(ex);
}
}
class SheduleReceptionDataComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
SheduleReceptionData p1 = (SheduleReceptionData) o1;
SheduleReceptionData p2 = (SheduleReceptionData) o2;
if (p1.getBegin().after(p2.getBegin())) {
return 1;
} else {
return -1;
}
}
}
}
}