/*
* PanelLaboratory.java
*
* Created on 10 Июль 2008 г., 17:19
*/
package clips.administrator.laboratory;
import beans.laboratory.LaboratoryDetails;
import clips.delegate.directory.filtered.DirectoryCheckupTypeItem;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.CheckableDocListener;
import clips.delegate.laboratory.LaboratoryLocal;
import cli_fmw.utils.SelectorEditableExceptional;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.main.Printable;
import cli_fmw.utils.MessageBox;
import cli_fmw.main.ClipsException;
import cli_fmw.main.PrintCreators;
import cli_fmw.report.panels.model.ObjectTextModel;
import cli_fmw.utils.table_config_states.StateSaver;
import cli_fmw.utils.table_config_states.TableState;
import clips.delegate.directory.complex.DirectoryLaboratory;
import clips.delegate.directory.complex.DirectoryLaboratoryItem;
import clips.login.UserInfo;
import framework.utils.ROList;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
/**
* Панель редактирования рабочих мест, назначения и удаления доступных панелей
* @author petr
*/
public class PanelLaboratory extends PageGeneric implements
Persistentable, ListSelectionListener, Printable {
private LaboratoryLocal labLocal;//делегат рабочее место
private DirectoryLaboratoryItem selected;//пункт справочника
class LabListModel extends DefaultListModel{
public void refreshElements(){
fireContentsChanged(this, 0, this.getSize() - 1);
}
}
private LabListModel labListModel;
private ObjectTextModel<DirectoryLaboratoryItem> tfLabNameModel;
/**
* Коструктор панели
* @param container контейнер
* @throws ClipsException
*/
public PanelLaboratory(PageContainer container) throws ClipsException {
super(container);
initComponents();
lstLabs.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);//выделять можно только 1 строку
lstLabs.addListSelectionListener(this);//добавим листенер (сама панель)
initList();
resizeColumn();
tfLabNameModel = new ObjectTextModel<DirectoryLaboratoryItem>(tfLabName, null) {
@Override
protected void setModelTextChecked(String text) throws ClipsException, ParseException, NumberFormatException {
if (text == null || text.trim().isEmpty()){
throw new ParseException("Пустое имя лаборатории", 0);
}
getObject().setTitle(text);
labListModel.refreshElements();
}
@Override
protected String getModelTextChecked() throws ClipsException {
return getObject().getTitle();
}
};
StateSaver.attachTo(this);
}
private void initList() throws ClipsException{
labListModel = new LabListModel();
DirectoryLaboratory ds = DirectoryLocator.getDirectory(DirectoryLaboratory.class);
ROList<DirectoryLaboratoryItem> list = ds.getFilteredItems(UserInfo.get().getCollaborator().getLpu());
for (DirectoryLaboratoryItem directoryLaboratoryItem : list) {
labListModel.addElement(directoryLaboratoryItem);
}
lstLabs.setModel(labListModel);
}
/** Creates new form PanelLaboratory */
/** 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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jSplitPane1 = new javax.swing.JSplitPane();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
lstLabs = new javax.swing.JList();
jPanel2 = new javax.swing.JPanel();
btAdd = new javax.swing.JButton();
btRemove = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
tblCheckupTypes = new cli_fmw.utils.sorted_table.SortedTable();
tfLabName = new javax.swing.JTextField();
jSplitPane1.setDividerLocation(300);
jPanel1.setLayout(new java.awt.BorderLayout());
jScrollPane1.setBorder(javax.swing.BorderFactory.createTitledBorder("Лаборатория"));
lstLabs.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
jScrollPane1.setViewportView(lstLabs);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
btAdd.setText("Добавить");
btAdd.setMaximumSize(new java.awt.Dimension(95, 25));
btAdd.setMinimumSize(new java.awt.Dimension(95, 25));
btAdd.setPreferredSize(new java.awt.Dimension(95, 25));
btAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddActionPerformed(evt);
}
});
jPanel2.add(btAdd);
btRemove.setText("Удалить");
btRemove.setEnabled(false);
btRemove.setMaximumSize(new java.awt.Dimension(95, 25));
btRemove.setMinimumSize(new java.awt.Dimension(95, 25));
btRemove.setPreferredSize(new java.awt.Dimension(95, 25));
btRemove.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btRemoveActionPerformed(evt);
}
});
jPanel2.add(btRemove);
jPanel1.add(jPanel2, java.awt.BorderLayout.SOUTH);
jSplitPane1.setLeftComponent(jPanel1);
jPanel3.setLayout(new java.awt.BorderLayout());
jScrollPane2.setViewportView(tblCheckupTypes);
jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);
tfLabName.setEnabled(false);
jPanel3.add(tfLabName, java.awt.BorderLayout.PAGE_START);
jSplitPane1.setRightComponent(jPanel3);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 498, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
if (isDirty()){
MessageBox.showWarning(MessageBox.W_NOT_CLOSE_EDITING);
return;
}
try {
LaboratoryDetails det = new LaboratoryDetails();
det.title = "новая лаборатория";
det.lpuId = UserInfo.get().getCollaborator().getLpu().getID();
DirectoryLaboratory dir = DirectoryLocator.getDirectory(DirectoryLaboratory.class);
DirectoryLaboratoryItem item = new DirectoryLaboratoryItem(det);
dir.getItems().append(item);
DirectoryLocator.getDirectory(DirectoryLaboratory.class, true);
initList();
}
catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btAddActionPerformed
private void btRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoveActionPerformed
try {
DirectoryLaboratory dir = DirectoryLocator.getDirectory(DirectoryLaboratory.class);
if (selected != null) {
save();
dir.getItems().remove(selected);
}
DirectoryLocator.getDirectory(DirectoryLaboratory.class, true);
initList();
} catch(ClipsException e) {
MessageBox.showException(e);
}
}//GEN-LAST:event_btRemoveActionPerformed
/**
* Функция обноавления таблицы
* @throws generic.ClipsException
*/
private void updateTable() throws ClipsException {
if (labLocal != null){
//создаём модель таблицы на основе селектора
TableModelLaboratory model = new TableModelLaboratory(labLocal);
tblCheckupTypes.setModel(model);//назначаем таблице модель
}
else{
tblCheckupTypes.setModel(new DefaultTableModel());
}
}
private void resizeColumn() {
TableState state = new TableState();
state.setPreferredWidth(TableModelLaboratory.COLUMN_CHECKUP_TYPE, 19);
state.setPreferredWidth(TableModelLaboratory.COLUMN_CHECKS, 1);
StateSaver.setDefaultState(this, tblCheckupTypes, state);
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btAdd;
private javax.swing.JButton btRemove;
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;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JList lstLabs;
private cli_fmw.utils.sorted_table.SortedTable tblCheckupTypes;
private javax.swing.JTextField tfLabName;
// End of variables declaration//GEN-END:variables
@Override
public String getPageTitle() {
return "Лаборатории";
}
@Override
public boolean isDirty() {
if (labLocal != null) {
return labLocal.isDirty();
}
return false;
}
@Override
public void save() throws ClipsException {
labLocal.save1();
}
@Override
public void restore() {
try {
labLocal.restore();
updateTable();
} catch (Exception ex) {
MessageBox.showException(ex);
}
}
/**
* Выбор рабочего места в листе
* @param e
*/
@Override
public void valueChanged(ListSelectionEvent e) {
onValueChange();
}
private void onValueChange(){
if (isDirty()){//если делегат грязный, то нельзя выбрать другой пункт
MessageBox.showWarning(MessageBox.W_NOT_CLOSE_EDITING);
lstLabs.removeListSelectionListener(this);
lstLabs.setSelectedValue(selected, true);
lstLabs.addListSelectionListener(this);
}else{
try {
selected = (DirectoryLaboratoryItem) lstLabs.getSelectedValue();
if (selected != null){
labLocal = new LaboratoryLocal(selected);
labLocal.addContentStateListener(getContainer());
}
else{
labLocal = null;
}
btRemove.setEnabled(selected != null);
tfLabNameModel.setObject(selected);
fireContainerEventStateChanged();
updateTable();
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
@Override
public boolean readyForPrint() {
return !(isDirty() | lstLabs.getSelectionModel().isSelectionEmpty());
}
@Override
public void print() {
if (readyForPrint()) {
try {
String title = "Список типов анализов, доступных лаборатории: " + labLocal.getLaboratory().getTitle();
HashMap<String, Object> data = new LinkedHashMap<String, Object>();
String cheсkups = "";
Iterator<DirectoryCheckupTypeItem> pp = labLocal.getCheckupTypes().iterator();
while (pp.hasNext()) {
cheсkups += " - " + pp.next().toString() + "\n";
}
if (cheсkups.isEmpty()) {
cheсkups += "У лаборатории нет доступных типов анализов";
}
data.put("cheсkups", cheсkups);
PrintCreators.createGeneratedReport(title, null, data.entrySet(), null, null, getClass());
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
@Override
public DelegateLine2 getDelegate() {
return null;
}
}