/*
* PanelSheduleIndividual.java
*
*/
package clips.shedule.template;
import beans.shedule.week.TimeOffset;
import cli_fmw.delegate.DelegateLine2;
import TimeTable.Day;
import TimeTable.DayOfWeek;
import TimeTable.JTimeTable;
import TimeTable.relocators.SimpleRectangleEngine;
import TimeTable.scaler.TimeScaler;
import TimeTable.scaler.TimeScalerTraditional;
import clips.delegate.client.CollaboratorLocal;
import cli_fmw.main.ClipsException;
import cli_fmw.main.PageContainer;
import cli_fmw.delegate.directory.DirectoryItem;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import clips.delegate.shedule.individual.SheduleIndividualLocal;
import clips.delegate.shedule.week.SheduleWeekLocal;
import cli_fmw.delegate.utils.TimeLocal;
import cli_fmw.main.ContentStateListener;
import cli_fmw.main.DirectoryItemNotFoundException;
import cli_fmw.main.Persistentable;
import clips.delegate.shedule.individual.WeekIndex;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.PrintCreators;
import cli_fmw.main.Printable;
import cli_fmw.report.TableReportOptions;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.chooser.ChoosePanelComboException;
import cli_fmw.utils.chooser.ChoosePanelComboListener;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.directory.ro.DirectoryCollaborator;
import clips.delegate.directory.ro.DirectoryCollaboratorItem;
import framework.utils.Converter;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.border.EtchedBorder;
import javax.swing.table.TableModel;
/**
*
* @author Axe Ilshat
*/
public class PanelSheduleTemplate extends PageGeneric
implements ChoosePanelComboListener, Persistentable, Printable {
private final SheduleWeekLocal swl = SheduleWeekLocal.getInstance();
private final JTimeTable tt[] = new JTimeTable[7];
private SheduleIndividualLocal sil;
WeekCountComboListener weekCountComboListener = new WeekCountComboListener();
WeekButtons weekButtons = new WeekButtons();
private Integer currentWeek;
DirectoryCollaborator dirColl;
/**
* Creates new form PanelSheduleIndividual
*/
public PanelSheduleTemplate(PageContainer container) throws ClipsException {
super(container);
initComponents();
for (int i = 1; i < 5; i++) {
weekCountCombo.addItem(new Integer(i));
}
dirColl = DirectoryLocator.getDirectory(DirectoryCollaborator.class, false);
panelSelColl.setOnlyShedule(true);
panelSelColl.addContentStateListener(new ContentStateListener() {
@Override
public void contentStateChanged() {
DirectoryCollaboratorItem item = panelSelColl.getSelectedItem();
int id = item == null ? 0 : item.getID();
try {
chooseDone(0, id);
} catch (ChoosePanelComboException ex) {
try {
int collID = sil.getCollaborator().getID();
panelSelColl.setSelectedCollaborator(collID == 0 ? null : dirColl.getItemFromID(collID));
} catch (DirectoryItemNotFoundException ex1) {
MessageBox.showException(ex1);
}
}
}
});
createTimeTable();
StateSaver.attachTo(this);
}
/**
* Creates new form PanelSheduleIndividual
* @param container
* @param collaborator
* @throws ClipsException
*/
public PanelSheduleTemplate(PageContainer container, CollaboratorLocal collaborator)
throws ClipsException {
super(container);
initComponents();
for (int i = 1; i < 5; i++) {
weekCountCombo.addItem(new Integer(i));
}
createTimeTable();
try {
chooseDone(0, collaborator.getID());
} catch (ChoosePanelComboException ex) {
throw new ClipsException("Access denied",ex);
}
StateSaver.attachTo(this);
}
/**
* Just creates components
*/
private void createTimeTable() {
GridLayout ga = new GridLayout(1, 7);
mainTimeTablePanel.setLayout(ga);
SimpleRectangleEngine sre = new SimpleRectangleEngine();
DayOfWeek dayOfWeek = DayOfWeek.createEurope(0);
TimeOffset shiftBegin = swl.getTimeBegin(dayOfWeek);
TimeOffset shiftEnd = swl.getTimeEnd(dayOfWeek);
ruler.setShiftBegin(shiftBegin.getMilliseconds());
ruler.setShiftEnd(shiftEnd.getMilliseconds());
TimeScaler scaler = new TimeScalerTraditional(
shiftBegin.getHours(), shiftEnd.getHours(), 0.25f);
scaler.setMinHourHeight(25);
ruler.setScaler(scaler);
ruler.setFont(rulerLabel.getFont());
for (int i = 0; i < tt.length; i++) {
JPanel panel = new JPanel();
mainTimeTablePanel.add(panel);
BorderLayout bl = new BorderLayout();
panel.setLayout(bl);
//bl.setVgap(10);
JLabel label = new JLabel(TimeLocal.WEEK[i]);
label.setHorizontalAlignment(JLabel.CENTER);
label.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
panel.add(label, BorderLayout.NORTH);
DayOfWeek dof = DayOfWeek.createEurope(i);
TimeOffset dayShiftBegin = swl.getTimeBegin(dof);
TimeOffset dayShiftEnd = swl.getTimeEnd(dof);
tt[i] = new JTimeTable(new Day(2007, 0, i + 1), sre, scaler);
tt[i].setEnabled(false);
tt[i].setShiftBegin(dayShiftBegin.getMilliseconds());
tt[i].setShiftEnd(dayShiftEnd.getMilliseconds());
tt[i].setCanExceed24(true);
panel.add(tt[i]);
}
}
/**
* returns title for the page
* @return String, HUD title for page
*/
@Override
public String getPageTitle() {
return "Шаблон индивидуального расписания";
}
/**
* Inits time table model with current calloborator and zero week
* @throws cli_fmw.delegate.ClipsException
*/
void initTimeTableModel(WeekIndex weekIndex) throws ClipsException {
System.out.println("change week to " + weekIndex);
weekCountCombo.setEnabled(true);
lbCurrentWeek.setEnabled(true);
jLabel4.setEnabled(true);
SheduleTemplateTableModel model = new SheduleTemplateTableModel(sil, weekIndex, getAuditManager());
boolean canChange = (sil == null) ? false : sil.canModifyPlan();
for (int i = 0; i < 7; i++) {
DayOfWeek dof = DayOfWeek.createEurope(i);
TimeOffset dayShiftBegin = swl.getTimeBegin(dof);
TimeOffset dayShiftEnd = swl.getTimeEnd(dof);
tt[i].setDay(weekIndex.getDay(dof));
tt[i].setShiftBegin(dayShiftBegin.getMilliseconds());
tt[i].setShiftEnd(dayShiftEnd.getMilliseconds());
tt[i].setModel(model);
tt[i].setEnabled(true);
tt[i].setReadOnly(!canChange);
}
}
/** 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() {
zoompnl = new javax.swing.JPanel();
zoom = new javax.swing.JSlider();
zoomLbl = new javax.swing.JLabel();
maiPanel = new javax.swing.JPanel();
menuPanel = new javax.swing.JPanel();
panelSelColl = new clips.accountant.PanelCollaboratorSelect();
weeks = new javax.swing.JPanel();
weekPanel = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
weekCountCombo = new javax.swing.JComboBox();
middle = new javax.swing.JPanel();
buttonPanel = new javax.swing.JPanel();
lbCurrentWeek = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
mainPanel = new javax.swing.JPanel();
rulePanel = new javax.swing.JPanel();
rulerLabel = new javax.swing.JLabel();
ruler = new TimeTable.JTableRuler();
mainTimeTablePanel = new javax.swing.JPanel();
zoompnl.setLayout(new java.awt.BorderLayout());
zoom.setMaximum(120);
zoom.setMinimum(25);
zoom.setEnabled(false);
zoom.setMaximumSize(new java.awt.Dimension(32767, 0));
zoom.setMinimumSize(new java.awt.Dimension(34, 0));
zoompnl.add(zoom, java.awt.BorderLayout.CENTER);
zoomLbl.setText("Увеличение:");
zoomLbl.setEnabled(false);
zoompnl.add(zoomLbl, java.awt.BorderLayout.WEST);
maiPanel.setLayout(new java.awt.BorderLayout(0, 10));
weeks.setLayout(new java.awt.BorderLayout(20, 5));
weekPanel.setMaximumSize(new java.awt.Dimension(32767, 33));
weekPanel.setLayout(new java.awt.BorderLayout(5, 5));
jLabel4.setText("Чередуемость недель");
jLabel4.setEnabled(false);
weekPanel.add(jLabel4, java.awt.BorderLayout.WEST);
weekCountCombo.setEnabled(false);
weekCountCombo.setPreferredSize(new java.awt.Dimension(50, 19));
weekPanel.add(weekCountCombo, java.awt.BorderLayout.CENTER);
weeks.add(weekPanel, java.awt.BorderLayout.WEST);
middle.setLayout(new java.awt.BorderLayout(10, 0));
buttonPanel.setLayout(new javax.swing.BoxLayout(buttonPanel, javax.swing.BoxLayout.LINE_AXIS));
middle.add(buttonPanel, java.awt.BorderLayout.CENTER);
weeks.add(middle, java.awt.BorderLayout.CENTER);
lbCurrentWeek.setEnabled(false);
weeks.add(lbCurrentWeek, java.awt.BorderLayout.EAST);
javax.swing.GroupLayout menuPanelLayout = new javax.swing.GroupLayout(menuPanel);
menuPanel.setLayout(menuPanelLayout);
menuPanelLayout.setHorizontalGroup(
menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, menuPanelLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(weeks, javax.swing.GroupLayout.PREFERRED_SIZE, 863, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
.addGroup(menuPanelLayout.createSequentialGroup()
.addComponent(panelSelColl, javax.swing.GroupLayout.PREFERRED_SIZE, 395, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(492, Short.MAX_VALUE))
);
menuPanelLayout.setVerticalGroup(
menuPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(menuPanelLayout.createSequentialGroup()
.addComponent(panelSelColl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(weeks, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
maiPanel.add(menuPanel, java.awt.BorderLayout.NORTH);
mainPanel.setLayout(new java.awt.BorderLayout());
rulePanel.setLayout(new java.awt.BorderLayout());
rulerLabel.setText(" ");
rulerLabel.setBorder(javax.swing.BorderFactory.createEtchedBorder());
rulePanel.add(rulerLabel, java.awt.BorderLayout.NORTH);
javax.swing.GroupLayout rulerLayout = new javax.swing.GroupLayout(ruler);
ruler.setLayout(rulerLayout);
rulerLayout.setHorizontalGroup(
rulerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 36, Short.MAX_VALUE)
);
rulerLayout.setVerticalGroup(
rulerLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 627, Short.MAX_VALUE)
);
rulePanel.add(ruler, java.awt.BorderLayout.CENTER);
mainPanel.add(rulePanel, java.awt.BorderLayout.WEST);
javax.swing.GroupLayout mainTimeTablePanelLayout = new javax.swing.GroupLayout(mainTimeTablePanel);
mainTimeTablePanel.setLayout(mainTimeTablePanelLayout);
mainTimeTablePanelLayout.setHorizontalGroup(
mainTimeTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 893, Short.MAX_VALUE)
);
mainTimeTablePanelLayout.setVerticalGroup(
mainTimeTablePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 648, Short.MAX_VALUE)
);
mainPanel.add(mainTimeTablePanel, java.awt.BorderLayout.CENTER);
jScrollPane1.setViewportView(mainPanel);
maiPanel.add(jScrollPane1, java.awt.BorderLayout.CENTER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(maiPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 863, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(maiPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
/**
*
* @param weekCount
*/
private class WeekButtons implements ActionListener {
private ArrayList<JToggleButton> buttons = new ArrayList<JToggleButton>();
private WeekIndex currentWeekIndex;
public WeekButtons() {
for(int i=0; i<4; i++) {
JToggleButton btn = new JToggleButton("Неделя " + (i+1));
btn.addActionListener(this);
buttons.add(btn);
}
}
public WeekIndex getCurrentWeekIndex() {
return currentWeekIndex;
}
public void init(int weekCount) throws ClipsException {
if(weekCount > 4) {
weekCount = 4;
}
updateCurrentWeekIndex(weekCount);
for(JToggleButton btn: buttons) {
buttonPanel.remove(btn);
}
for(int i=0; i<weekCount; i++) {
buttonPanel.add(buttons.get(i));
buttons.get(i).setSelected(i==0);
}
currentWeekIndex = new WeekIndex(0);
weekCountCombo.invalidate();
buttonPanel.invalidate();
weeks.invalidate();
weekPanel.invalidate();
weekPanel.repaint();
buttonPanel.repaint();
}
@Override
public void actionPerformed(ActionEvent arg0) {
int index = buttons.indexOf(arg0.getSource());
if(index != -1) {
try {
WeekIndex wi= new WeekIndex(index);
initTimeTableModel(wi);
currentWeekIndex = wi;
for(int i=0; i<buttons.size(); i++) {
buttons.get(i).setSelected(i==index);
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
private void updateCurrentWeekIndex(int weekCount) throws ClipsException {
WeekIndex wi = new WeekIndex();
currentWeek = Math.abs(wi.getIndex() % weekCount + 1);
lbCurrentWeek.setText("Сейчас идет "+ currentWeek + " неделя");
weeks.invalidate();
}
}
/**
*
*/
class WeekCountComboListener implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
try {
Integer weekCount = (Integer) weekCountCombo.getSelectedItem();
sil.setWeekCount(weekCount);
weekButtons.init(weekCount);
initTimeTableModel(weekButtons.getCurrentWeekIndex());
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
public boolean canSelectionChange(DirectoryItem selected) {
return true;
}
/**
* Called when new collaborator selected
* @param selected
*/
@Override
public void chooseDone(int type, int id) throws ChoosePanelComboException {
try {
if (sil == null && id == 0) {
return;
}
DirectoryCollaborator colls = DirectoryLocator.getDirectory(DirectoryCollaborator.class, false);
DirectoryCollaboratorItem collaborator = colls.getItemFromID(id);
if(sil == null || sil.getCollaborator() != collaborator) {
if(sil != null && sil.isDirty()) {
int ans = MessageBox.showConfirmYesNoCancel(MessageBox.C_SAVE_CHANGES);
if(ans == MessageBox.ANSWER_YES) {
sil.save1();
} else if(ans == MessageBox.ANSWER_CANCEL) {
throw new ChoosePanelComboException();
}
}
// init data source
sil = new SheduleIndividualLocal(collaborator);
sil.addContentStateListener(getContainer());
// init week buttons
weekCountCombo.removeActionListener(weekCountComboListener);
weekCountCombo.setSelectedItem(sil.getWeekCount());
weekCountCombo.addActionListener(weekCountComboListener);
weekButtons.init(sil.getWeekCount());
zoom.setEnabled(true);
zoomLbl.setEnabled(true);
initTimeTableModel(weekButtons.getCurrentWeekIndex());
fireContainerEventStateChanged();
}
}
catch (ChoosePanelComboException ex){
throw ex;
}
catch (Exception ex) {
MessageBox.showExceptionOnly(ex);
throw new ChoosePanelComboException();
}
}
/**
*
* @return
*/
@Override
public boolean isDirty() {
return sil == null ? false : sil.isDirty();
}
/**
*
* @throws ClipsException
* @throws cli_fmw.delegate.exception.ClipsException
*/
@Override
public void save() throws ClipsException {
if(sil != null) {
sil.save1();
}
}
@Override
public void restore() {
if(sil != null) {
sil.restore();
try {
int weekCount = sil.getWeekCount();
weekButtons.init(weekCount);
weekCountCombo.removeActionListener(weekCountComboListener);
weekCountCombo.setSelectedItem(weekCount);
weekCountCombo.addActionListener(weekCountComboListener);
initTimeTableModel(weekButtons.getCurrentWeekIndex());
} catch (ClipsException ex) {
ex.printStackTrace();
}
}
}
@Override
public DelegateLine2 getDelegate() {
return null;
}
@Override
public boolean readyForPrint() {
return !(isDirty() || sil == null);
}
@Override
public void print() {
if (readyForPrint()) {
TableModel model = new PrintTableModel(sil, swl);
String title = "Плановое расписание сотрудника: " + sil.getCollaborator().getTitle();
HashMap<String, Object> data = new HashMap<String, Object>();
try {
Calendar c = TimeLocal.getCurrentTime();
Date d = c.getTime();
String curWeek = Converter.dateToString(d);
curWeek += ", " + c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, new Locale("ru"));
curWeek += " (" + currentWeek + " неделя)";
data.put("date", curWeek);
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
TableReportOptions ops = new TableReportOptions(TableReportOptions.PageOrentation.horizontal);
PrintCreators.createGeneratedReport(title, ops, data.entrySet(), null, model, getClass());
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel buttonPanel;
private javax.swing.JLabel jLabel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lbCurrentWeek;
private javax.swing.JPanel maiPanel;
private javax.swing.JPanel mainPanel;
private javax.swing.JPanel mainTimeTablePanel;
private javax.swing.JPanel menuPanel;
private javax.swing.JPanel middle;
private clips.accountant.PanelCollaboratorSelect panelSelColl;
private javax.swing.JPanel rulePanel;
private TimeTable.JTableRuler ruler;
private javax.swing.JLabel rulerLabel;
private javax.swing.JComboBox weekCountCombo;
private javax.swing.JPanel weekPanel;
private javax.swing.JPanel weeks;
private javax.swing.JSlider zoom;
private javax.swing.JLabel zoomLbl;
private javax.swing.JPanel zoompnl;
// End of variables declaration//GEN-END:variables
}