/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/*
* MesListPanel.java
*
* Created on Apr 28, 2009, 9:45:10 AM
*/
package clips.service.mes;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.main.MainWindow;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.report.panels.model.DirectoryObjectComboBoxModel;
import cli_fmw.report.panels.model.delegate.DelegateListModel;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.directory.simple.mes.DirectoryClinicType;
import clips.delegate.directory.simple.mes.DirectoryClinicTypeItem;
import clips.delegate.service.mes.MedicEconomicStandartLocal;
import clips.delegate.service.mes.MedicEconomicStandartSearchLocal;
import java.util.ArrayList;
/**
*
* @author finder
*/
public class MesListPanel extends PageGeneric implements Persistentable {
private static final long serialVersionUID = 1L;
private DirectoryObjectComboBoxModel<Object, DirectoryClinicTypeItem> mestypeModel;
private DelegateListModel<MedicEconomicStandartLocal> mainListModel;
private DirectoryClinicTypeItem selType;
private MedicEconomicStandartLocal curMes;
private MesEditPanel rightPanel;
/** Creates new form MesListPanel
* @param container
* @throws ClipsException
*/
public MesListPanel(PageContainer container) throws ClipsException {
super(container);
initComponents();
rightPanel = new MesEditPanel(MainWindow.mainWindow, null);
pnMesEditCointainer.add(rightPanel);
initModels();
StateSaver.attachTo(this);
}
private void initModels() throws ClipsException{
DirectoryClinicType type = DirectoryLocator.getDirectory(DirectoryClinicType.class);
mestypeModel = new DirectoryObjectComboBoxModel<Object, DirectoryClinicTypeItem>(cbMesType, type) {
private static final long serialVersionUID = 1L;
@Override
public boolean isSelectionVisible() {
return true;
}
@Override
public DirectoryClinicTypeItem getSelectedDirectoryItem() throws ClipsException {
return selType;
}
@Override
public void setSelectedDirectoryItem(DirectoryClinicTypeItem item) throws ClipsException {
setCurType(item);
}
};
mainListModel = new DelegateListModel<MedicEconomicStandartLocal>(new ArrayList<MedicEconomicStandartLocal>());
lsMain.setModel(mainListModel);
setCurType(null);
}
/** 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();
lsMain = new javax.swing.JList();
cbMesType = new javax.swing.JComboBox();
jPanel3 = new javax.swing.JPanel();
btAdd = new javax.swing.JButton();
btRemove = new javax.swing.JButton();
pnMesEditCointainer = new javax.swing.JPanel();
setLayout(new java.awt.GridLayout(1, 0));
jPanel1.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
jPanel1.setLayout(new java.awt.BorderLayout(4, 4));
lsMain.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]; }
});
lsMain.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
lsMainValueChanged(evt);
}
});
jScrollPane1.setViewportView(lsMain);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
cbMesType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
jPanel1.add(cbMesType, java.awt.BorderLayout.PAGE_START);
jPanel3.setBorder(null);
btAdd.setText("Добавить");
btAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddActionPerformed(evt);
}
});
btRemove.setText("Удалить");
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addComponent(btAdd)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btRemove)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btAdd, btRemove});
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btAdd)
.addComponent(btRemove))
);
jPanel1.add(jPanel3, java.awt.BorderLayout.PAGE_END);
jSplitPane1.setLeftComponent(jPanel1);
pnMesEditCointainer.setLayout(new java.awt.GridLayout(1, 0));
jSplitPane1.setRightComponent(pnMesEditCointainer);
add(jSplitPane1);
}// </editor-fold>//GEN-END:initComponents
private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
try {
MedicEconomicStandartLocal newMes = new MedicEconomicStandartLocal(getAuditManager());
mainListModel.addElement(newMes);
lsMain.setSelectedValue(newMes, true);
}
catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btAddActionPerformed
private void lsMainValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lsMainValueChanged
MedicEconomicStandartLocal newSel = (MedicEconomicStandartLocal)lsMain.getSelectedValue();
if (!setCurItem(newSel, false)){
lsMain.setSelectedValue(curMes, false);
}
}//GEN-LAST:event_lsMainValueChanged
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btAdd;
private javax.swing.JButton btRemove;
private javax.swing.JComboBox cbMesType;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JList lsMain;
private javax.swing.JPanel pnMesEditCointainer;
// End of variables declaration//GEN-END:variables
private void setCurType(DirectoryClinicTypeItem type){
if (!setCurItem(null, false)){
return;
}
mainListModel.removeAllElements();
if (type != null){
try {
ArrayList<MedicEconomicStandartLocal> list = new MedicEconomicStandartSearchLocal(getAuditManager()).getList(type);
mainListModel.addAll(list);
}
catch (ClipsException ex) {
MessageBox.showException(ex);
return;
}
}
selType = type;
updateButtons();
}
private boolean setCurItem(MedicEconomicStandartLocal mes, boolean forse){
if (mes == curMes){
return true;
}
if (!forse && curMes != null && curMes.isDirty()){
int res = MessageBox.showConfirmYesNoCancel(MessageBox.C_SAVE_CHANGES);
if (res == MessageBox.ANSWER_YES){
try {
save();
}
catch (ClipsException ex) {
MessageBox.showException(ex);
return false;
}
}
else if (res == MessageBox.ANSWER_NO){
restore();
}
else {
return false;
}
}
if (curMes != null){
curMes.removeContentStateListener(getContainer());
}
curMes = mes;
if (curMes != null) {
curMes.addContentStateListener(getContainer());
}
rightPanel.setObject(mes);
updateButtons();
getContainer().contentStateChanged();
return true;
}
private void updateButtons(){
btAdd.setEnabled(selType != null);
btRemove.setEnabled(curMes != null);
}
@Override
public String getPageTitle() {
return "Редактирование МЭСов";
}
@Override
public DelegateLine2 getDelegate() {
return null;
}
@Override
public boolean isDirty() {
return curMes == null? false: curMes.isDirty();
}
@Override
public void save() throws ClipsException {
if (curMes != null) {
curMes.save1();
}
}
@Override
public void restore() {
if (curMes != null) {
if (curMes.isNewlyCreated()){
remove();
}
else{
curMes.restore();
}
}
}
public void remove(){
try{
if (curMes != null) {
if (!curMes.isNewlyCreated()){
int res = MessageBox.showConfirmYesNo(MessageBox.C_DEL_OBJECT);
if (res != MessageBox.ANSWER_YES){
return;
}
}
MedicEconomicStandartLocal tmp = curMes;
setCurItem(null, false);
tmp.remove();
mainListModel.removeElement(tmp);
}
}
catch (ClipsException ex){
MessageBox.showException(ex);
}
}
}