/*
* SheduleNewItemDialog.java
*
* Created on 3 Март 2008 г., 17:26
*/
package clips.shedule.reception;
import clips.delegate.service.SerRenLocal;
import cli_fmw.utils.ModalDialog;
import cli_fmw.main.ClipsException;
import cli_fmw.main.audit.AuditManager;
import cli_fmw.utils.table_config_states.StateSaver;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;
import javax.swing.AbstractSpinnerModel;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author axe
*/
public class SheduleItemDialog extends ModalDialog {
public final static int DLG_DELETED = DLG_NEXTRESULT;
private int result;
private boolean autoDuration = true;
ServicesTableModel serviceModel;
/** Creates new form SheduleNewItemDialog */
public SheduleItemDialog(java.awt.Frame parent, Iterator<SerRenLocal> services, AuditManager am) {
super(parent, "Запись в расписании", am);
initComponents();
Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle dlgSize = this.getBounds();
this.setLocation((scrSize.width - dlgSize.width) / 2, (scrSize.height - dlgSize.height) / 2);
DateTimeListener dtl = new DateTimeListener();
hBegin.setModel(new SpinnerNumberModel(0, 0, 23, 1));
hDuration.setModel(new SpinnerNumberModel(0, 0, 23, 1));
mBegin.setModel(new SpinnerNumberModel(0, 0, 59, 1));
mDuration.setModel(new SpinnerNumberModel(0, 0, 59, 1));
hDuration.addChangeListener(dtl);
mDuration.addChangeListener(dtl);
hBegin.addChangeListener(dtl);
mBegin.addChangeListener(dtl);
SpinnerSelectionListener ssl = new SpinnerSelectionListener();
hDuration.getEditor().addFocusListener(ssl);
mDuration.getEditor().addFocusListener(ssl);
hBegin.getEditor().addFocusListener(ssl);
mBegin.getEditor().addFocusListener(ssl);
if(services == null || !services.hasNext()) {
//100 баксов тому кто сделает что бы нормально работало
remove(servicePanel);
remove(bottomPanel);
remove(topPanel);
setLayout(new BorderLayout());
Dimension topsize = topPanel.getPreferredSize();
Dimension bottomsize = bottomPanel.getPreferredSize();
Dimension size = getSize();
size.height = topsize.height + bottomsize.height + 20;
add(bottomPanel, BorderLayout.CENTER);
add(topPanel, BorderLayout.NORTH);
setPreferredSize(size);
setSize(size);
} else {
serviceModel = new ServicesTableModel(services);
mainTable.setModel(serviceModel);
}
StateSaver.attachTo(this);
}
@Override
public int getDlgResult() {
return result;
}
class DateTimeListener implements ChangeListener {
@Override
public void stateChanged(ChangeEvent arg0) {
int start = ((Integer)hBegin.getValue())*60 + ((Integer)mBegin.getValue());
int duration = ((Integer)hDuration.getValue())*60 + ((Integer)mDuration.getValue());
okBtn.setEnabled((start+duration <= 24*60)
&& duration > 5 && start > 0);
}
}
/**
* HELPER CLASS
*/
class BoundModel extends AbstractSpinnerModel {
int min;
int max;
Integer value = new Integer(0);
BoundModel(int min, int max) {
this.min = min;
this.max = max;
}
@Override
public Object getValue() {
return value;
}
@Override
public void setValue(Object val) {
int newValue = (Integer)val;
if(newValue > max) {
value = max;
} else if (newValue < min) {
value = min;
} else {
value = newValue;
}
}
@Override
public Object getNextValue() {
int val = value + 1;
if(val > max) {
val = max;
}
return new Integer(val);
}
@Override
public Object getPreviousValue() {
int val = value - 1;
if(val < min) {
val = min;
}
return new Integer(val);
}
}
class SpinnerSelectionListener implements FocusListener {
@Override
public void focusGained(FocusEvent evt) {
JTextField tf = (JTextField) evt.getComponent();
tf.setSelectionStart(0);
tf.setSelectionEnd(tf.getText().length());
}
@Override
public void focusLost(FocusEvent evt) {
JTextField tf = (JTextField) evt.getComponent();
tf.setSelectionStart(0);
tf.setSelectionStart(0);
tf.setSelectionEnd(0);
}
}
/**
* Возвращает список услуг
* @return NULL or LIST (can be empty)
*/
public List<SerRenLocal> getServiceSelectedList() {
if(serviceModel != null) {
return serviceModel.getSelectedList();
}
return null;
}
public void setServiceSelectedList(List<Integer> services) {
if(serviceModel != null) {
serviceModel.setSelectedList(services);
}
}
/** 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() {
topPanel = new javax.swing.JPanel();
jPanel1 = new javax.swing.JPanel();
hBegin = new javax.swing.JSpinner();
jLabel3 = new javax.swing.JLabel();
mBegin = new javax.swing.JSpinner();
jLabel8 = new javax.swing.JLabel();
jLabel1 = new javax.swing.JLabel();
jPanel2 = new javax.swing.JPanel();
hDuration = new javax.swing.JSpinner();
jLabel6 = new javax.swing.JLabel();
mDuration = new javax.swing.JSpinner();
jLabel7 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
servicePanel = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
mainTable = new javax.swing.JTable();
jLabel4 = new javax.swing.JLabel();
bottomPanel = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
description = new javax.swing.JTextArea();
jLabel5 = new javax.swing.JLabel();
jPanel3 = new javax.swing.JPanel();
delBtn = new javax.swing.JButton();
okBtn = new javax.swing.JButton();
cancelBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
formKeyPressed(evt);
}
});
jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.LINE_AXIS));
jPanel1.add(hBegin);
jLabel3.setText(" часов ");
jPanel1.add(jLabel3);
jPanel1.add(mBegin);
jLabel8.setText(" минут");
jPanel1.add(jLabel8);
jLabel1.setText("Время: ");
jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.LINE_AXIS));
jPanel2.add(hDuration);
jLabel6.setText(" часов ");
jPanel2.add(jLabel6);
jPanel2.add(mDuration);
jLabel7.setText(" минут");
jPanel2.add(jLabel7);
jLabel2.setText("Длительность");
javax.swing.GroupLayout topPanelLayout = new javax.swing.GroupLayout(topPanel);
topPanel.setLayout(topPanelLayout);
topPanelLayout.setHorizontalGroup(
topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(topPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 491, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
topPanelLayout.setVerticalGroup(
topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(topPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(topPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);
mainTable.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
mainTable.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
mainTableMouseClicked(evt);
}
});
jScrollPane2.setViewportView(mainTable);
jLabel4.setText("Услуги:");
javax.swing.GroupLayout servicePanelLayout = new javax.swing.GroupLayout(servicePanel);
servicePanel.setLayout(servicePanelLayout);
servicePanelLayout.setHorizontalGroup(
servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, servicePanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE))
.addContainerGap())
);
servicePanelLayout.setVerticalGroup(
servicePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(servicePanelLayout.createSequentialGroup()
.addComponent(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
);
getContentPane().add(servicePanel, java.awt.BorderLayout.CENTER);
description.setColumns(20);
description.setLineWrap(true);
description.setRows(5);
jScrollPane1.setViewportView(description);
jLabel5.setText("Примечания для врача:");
delBtn.setText("Удалить");
delBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
delBtnActionPerformed(evt);
}
});
jPanel3.add(delBtn);
okBtn.setText(" ОК ");
okBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
okBtnActionPerformed(evt);
}
});
jPanel3.add(okBtn);
cancelBtn.setText("Отмена");
cancelBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelBtnActionPerformed(evt);
}
});
jPanel3.add(cancelBtn);
javax.swing.GroupLayout bottomPanelLayout = new javax.swing.GroupLayout(bottomPanel);
bottomPanel.setLayout(bottomPanelLayout);
bottomPanelLayout.setHorizontalGroup(
bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bottomPanelLayout.createSequentialGroup()
.addContainerGap()
.addGroup(bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5)
.addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, 682, Short.MAX_VALUE))
.addContainerGap())
);
bottomPanelLayout.setVerticalGroup(
bottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(bottomPanelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel5)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
getContentPane().add(bottomPanel, java.awt.BorderLayout.SOUTH);
pack();
}// </editor-fold>//GEN-END:initComponents
private void okBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okBtnActionPerformed
result = DLG_OK;
setVisible(false);
}//GEN-LAST:event_okBtnActionPerformed
private void cancelBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelBtnActionPerformed
result = DLG_CANCEL;
setVisible(false);
}//GEN-LAST:event_cancelBtnActionPerformed
private void delBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delBtnActionPerformed
result = DLG_DELETED;
setVisible(false);
}//GEN-LAST:event_delBtnActionPerformed
private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
if(evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
result = DLG_CANCEL;
setVisible(false);
}
}//GEN-LAST:event_formKeyPressed
private void mainTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mainTableMouseClicked
if(autoDuration && serviceModel != null) {
Iterator<SerRenLocal> i = serviceModel.getSelectedList().iterator();
int sum = 0;
while(i.hasNext()) {
SerRenLocal srl = i.next();
try {
sum += srl.getService().getDefaultDuration();
} catch (ClipsException ex) {
ex.printStackTrace();
}
}if(sum < 10) {
sum = 10;
}
hDuration.setValue(new Integer(sum/60));
mDuration.setValue(new Integer(sum%60));
}
}//GEN-LAST:event_mainTableMouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel bottomPanel;
private javax.swing.JButton cancelBtn;
public javax.swing.JButton delBtn;
public javax.swing.JTextArea description;
public javax.swing.JSpinner hBegin;
public javax.swing.JSpinner hDuration;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
public javax.swing.JSpinner mBegin;
public javax.swing.JSpinner mDuration;
private javax.swing.JTable mainTable;
public javax.swing.JButton okBtn;
private javax.swing.JPanel servicePanel;
private javax.swing.JPanel topPanel;
// End of variables declaration//GEN-END:variables
}