/*
* DialogAddress.java
*
* Created on 24 Январь 2008 г., 10:08
*/
package clips.userseach;
import beans.contract.entity.Polis;
import beans.contract.entity.PolisDetails;
import cli_fmw.address.DialogAddressM;
import cli_fmw.delegate.client.Address;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.main.ClipsException;
import cli_fmw.main.audit.AuditManager;
import cli_fmw.report.panels.model.DirectoryObjectComboBoxModel;
import cli_fmw.report.panels.model.FilteredComboBoxModel;
import cli_fmw.report.panels.model.TextFieldModel;
import cli_fmw.report.panels.model.TextFieldModelLimited;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.ModalDialog;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.client.ClientLocal;
import clips.delegate.config.ConfigLocal;
import clips.delegate.contract.ContractFactoryLocal;
import clips.delegate.contract.ContractLocal;
import clips.delegate.contract.PolisData;
import clips.delegate.directory.simple.insurer.DirectoryInsurer;
import clips.delegate.directory.simple.insurer.DirectoryInsurerItem;
import clips.delegate.directory.simple.regionIdCode.DirectoryRegionIdCode;
import clips.delegate.directory.simple.regionIdCode.DirectoryRegionIdCodeItem;
import clips.login.UserInfo;
import framework.beans.address.entities.AddressDetails;
import framework.utils.FormatChecker;
import java.awt.Frame;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import org.infotechservice.ICalendar.DateEvent;
import org.infotechservice.ICalendar.DateListener;
/**
* Диалог, позволяющий редактировать адрес (либо клиента либо предприятия)
* @author lacoste
*/
public class DialogPolisOMI extends ModalDialog {
private DirectoryRegionIdCode dirRegionId;
private DirectoryInsurer dirInsurer;
private PolisDetails details;
private PolisDetails originalDetails;
private ContractLocal contract;
private ClientLocal client;
private Boolean initNew = null;
private boolean validNumber;
/**
*
* @param parent
* @param pd not null
* @throws cli_fmw.main.ClipsException
*/
public DialogPolisOMI(Frame parent, PolisData pd, AuditManager am) throws ClipsException {
super(parent, "Ввод полиса ОМС", am);
if (pd == null){
throw new IllegalArgumentException("Полис ОМС не указан");
}
initComponents();
details = pd.getDetails().clone();
contract = pd.getContract();
originalDetails = pd.getDetails();
dirRegionId = DirectoryLocator.getDirectory(DirectoryRegionIdCode.class);
//КомбоМодель страховых компаний
dirInsurer = DirectoryLocator.getDirectory(DirectoryInsurer.class);
initAll();
setEnabledComponets();
StateSaver.attachTo(this);
}
public PolisData getPolisOMI() throws ClipsException {
if (originalDetails.equals(details)){
throw new UnsupportedOperationException("Данные полиса не изменялись");
} else {
if (initNew == null){
throw new RuntimeException("Действие не определено");
}
if (initNew){
details.id = 0;
}
return new PolisData(contract, client, details);
}
}
private void initAll() throws ClipsException {
initInsurers();
initContractsOMS();
initNumber();
initTillDate();
initFromDate();
initRegions();
initSeries();
initAddress();
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel4 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel11 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jPanel12 = new javax.swing.JPanel();
cbContract = new javax.swing.JComboBox();
cbInsurer = new javax.swing.JComboBox();
jPanel3 = new javax.swing.JPanel();
cbRegion = new javax.swing.JComboBox();
tfSeries = new javax.swing.JTextField();
tfNumber = new javax.swing.JTextField();
jPanel5 = new javax.swing.JPanel();
jPanel6 = new javax.swing.JPanel();
dchDateFrom = new org.infotechservice.ICalendar.components.ICDateChooser();
jLabel7 = new javax.swing.JLabel();
jPanel7 = new javax.swing.JPanel();
jLabel9 = new javax.swing.JLabel();
dchDateTill = new org.infotechservice.ICalendar.components.ICDateChooser();
jPanel1 = new javax.swing.JPanel();
tfAddress = new javax.swing.JTextField();
btAddress = new javax.swing.JButton();
panelOkCancel = new javax.swing.JPanel();
btCreate = new javax.swing.JButton();
btCancel = new javax.swing.JButton();
btUpdate = new javax.swing.JButton();
btFormal = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(400, 213));
jPanel4.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
jPanel4.setLayout(new java.awt.BorderLayout());
jPanel2.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
jPanel2.setLayout(new java.awt.BorderLayout());
jPanel11.setLayout(new java.awt.GridLayout(0, 1));
jLabel1.setText("Контракт");
jPanel11.add(jLabel1);
jLabel4.setText("Страховая компания");
jPanel11.add(jLabel4);
jLabel6.setText("Код региона");
jPanel11.add(jLabel6);
jLabel5.setText("Серия полиса");
jPanel11.add(jLabel5);
jLabel2.setText("Номер полиса");
jPanel11.add(jLabel2);
jLabel3.setText("Срок действия");
jPanel11.add(jLabel3);
jLabel8.setText("Адрес");
jPanel11.add(jLabel8);
jPanel2.add(jPanel11, java.awt.BorderLayout.WEST);
jPanel12.setLayout(new java.awt.GridLayout(0, 1, 0, 3));
jPanel12.add(cbContract);
cbInsurer.setEditable(true);
jPanel12.add(cbInsurer);
jPanel3.setLayout(new java.awt.BorderLayout(5, 0));
jPanel3.add(cbRegion, java.awt.BorderLayout.CENTER);
jPanel12.add(jPanel3);
jPanel12.add(tfSeries);
jPanel12.add(tfNumber);
jPanel5.setLayout(new java.awt.GridLayout(1, 0, 20, 0));
jPanel6.setLayout(new java.awt.BorderLayout(5, 0));
dchDateFrom.setMinSelectableDate(new java.util.Date(-2208995919000L));
jPanel6.add(dchDateFrom, java.awt.BorderLayout.CENTER);
jLabel7.setText("от");
jPanel6.add(jLabel7, java.awt.BorderLayout.WEST);
jPanel5.add(jPanel6);
jPanel7.setLayout(new java.awt.BorderLayout(5, 0));
jLabel9.setText("до");
jPanel7.add(jLabel9, java.awt.BorderLayout.WEST);
dchDateTill.setMinSelectableDate(new java.util.Date(-2208995919000L));
jPanel7.add(dchDateTill, java.awt.BorderLayout.CENTER);
jPanel5.add(jPanel7);
jPanel12.add(jPanel5);
jPanel1.setLayout(new java.awt.BorderLayout(5, 0));
tfAddress.setEditable(false);
jPanel1.add(tfAddress, java.awt.BorderLayout.CENTER);
btAddress.setText("...");
btAddress.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddressActionPerformed(evt);
}
});
jPanel1.add(btAddress, java.awt.BorderLayout.LINE_END);
jPanel12.add(jPanel1);
jPanel2.add(jPanel12, java.awt.BorderLayout.CENTER);
jPanel4.add(jPanel2, java.awt.BorderLayout.NORTH);
btCreate.setText("Создать");
btCreate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCreateActionPerformed(evt);
}
});
btCancel.setText("Отмена");
btCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCancelActionPerformed(evt);
}
});
btUpdate.setText("Обновить");
btUpdate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btUpdateActionPerformed(evt);
}
});
btFormal.setText("Формальный");
btFormal.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btFormalActionPerformed(evt);
}
});
javax.swing.GroupLayout panelOkCancelLayout = new javax.swing.GroupLayout(panelOkCancel);
panelOkCancel.setLayout(panelOkCancelLayout);
panelOkCancelLayout.setHorizontalGroup(
panelOkCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelOkCancelLayout.createSequentialGroup()
.addContainerGap()
.addComponent(btFormal)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE)
.addComponent(btCreate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btUpdate)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btCancel)
.addContainerGap())
);
panelOkCancelLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCancel, btCreate, btUpdate});
panelOkCancelLayout.setVerticalGroup(
panelOkCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelOkCancelLayout.createSequentialGroup()
.addContainerGap(14, Short.MAX_VALUE)
.addGroup(panelOkCancelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btCancel)
.addComponent(btUpdate)
.addComponent(btCreate)
.addComponent(btFormal)))
);
jPanel4.add(panelOkCancel, java.awt.BorderLayout.SOUTH);
getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btCreateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCreateActionPerformed
initNew = Boolean.TRUE;
setDlgResult(DLG_OK);
dispose();
}//GEN-LAST:event_btCreateActionPerformed
private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
setDlgResult(DLG_CANCEL);
dispose();
}//GEN-LAST:event_btCancelActionPerformed
private void btAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddressActionPerformed
try {
Address a;
if (details.addressDetail != null){
a = new Address(details.addressDetail);
}else{
a = new Address(new AddressDetails());
}
Address address = UserInfo.get().getCollaborator().getLpu().getAddress();
DialogAddressM dialog = new DialogAddressM(this.getOwner(), a, address, getAuditManager());
dialog.setVisible(true);
if (dialog.getDlgResult() == ModalDialog.DLG_OK) {
a = dialog.getAddress();
details.addressDetail = a.getDetails();
tfAddress.setText(a.toStringShort());
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btAddressActionPerformed
private void btUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUpdateActionPerformed
initNew = Boolean.FALSE;
setDlgResult(DLG_OK);
dispose();
}//GEN-LAST:event_btUpdateActionPerformed
private void btFormalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btFormalActionPerformed
try {
DirectoryInsurerItem defaultInsurer = UserInfo.get().getCollaborator().getLpu().getDefaultInsurer();
DirectoryRegionIdCodeItem configRegionCode = ConfigLocal.getInstance().getConfigRegionCode();
cbInsurer.setSelectedItem(defaultInsurer);
cbRegion.setSelectedItem(configRegionCode);
tfSeries.setText("");
tfNumber.setText(Polis.FICTIVE_POLIS_NUMBER);
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btFormalActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btAddress;
private javax.swing.JButton btCancel;
private javax.swing.JButton btCreate;
private javax.swing.JButton btFormal;
private javax.swing.JButton btUpdate;
private javax.swing.JComboBox cbContract;
private javax.swing.JComboBox cbInsurer;
private javax.swing.JComboBox cbRegion;
private org.infotechservice.ICalendar.components.ICDateChooser dchDateFrom;
private org.infotechservice.ICalendar.components.ICDateChooser dchDateTill;
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.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel11;
private javax.swing.JPanel jPanel12;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JPanel jPanel5;
private javax.swing.JPanel jPanel6;
private javax.swing.JPanel jPanel7;
private javax.swing.JPanel panelOkCancel;
private javax.swing.JTextField tfAddress;
private javax.swing.JTextField tfNumber;
private javax.swing.JTextField tfSeries;
// End of variables declaration//GEN-END:variables
private void initContractsOMS() throws ClipsException {
//Комбомодель контрактов ОМС
List<ContractLocal> contractsOMI = new ContractFactoryLocal(getAuditManager()).findContractsOMI();
cbContract.setModel(new DefaultComboBoxModel(contractsOMI.toArray()));
if (details.contractID == 0){
details.contractID = contractsOMI.get(0).getID();
}
for (ContractLocal contractLocal : contractsOMI) {
if (contractLocal.getID() == details.contractID){
cbContract.setSelectedItem(contractsOMI);
}
}
}
private void initAddress() throws ClipsException{
tfAddress.setDocument(new TextFieldModel(tfAddress) {
@Override
protected void setModelTextChecked(String text) throws ClipsException, ParseException, NumberFormatException {
}
@Override
protected String getModelTextChecked() throws ClipsException {
return (new Address(details.addressDetail)).toStringShort();
}
});
if (details.addressDetail != null){
tfAddress.setText((new Address(details.addressDetail)).toStringShort());
}
}
private void initNumber() {
TextFieldModelLimited textFieldModel = new TextFieldModelLimited(tfNumber) {
private String text = "";
private void setNumber(String number) throws ParseException{
text = number;
FormatChecker.checkPolisOMS(text);
details.number = number;
}
@Override
protected void setModelTextChecked(String text) throws ClipsException, ParseException, NumberFormatException {
setNumber(text);
}
@Override
protected String getModelTextChecked() throws ClipsException {
return text;
}
};
textFieldModel.setWidth(16);
tfNumber.setDocument(textFieldModel);
if (details.number != null){
tfNumber.setText(details.number);
}
}
private void initSeries() {
tfSeries.setDocument(new TextFieldModel(tfSeries) {
@Override
protected void setModelTextChecked(String text) throws ClipsException, ParseException, NumberFormatException {
details.series = text;
setEnabledComponets();
}
@Override
protected String getModelTextChecked() throws ClipsException {
return details.series;
}
});
if (details.series != null){
tfSeries.setText(details.series);
}
}
private void initTillDate() {
dchDateTill.addDateListener(new DateListener() {
@Override
public void dateChanged(DateEvent evt) {
details.tillDate = dchDateTill.getDate();
setEnabledComponets();
}
});
}
private void initFromDate() {
dchDateFrom.addDateListener(new DateListener() {
@Override
public void dateChanged(DateEvent evt) {
details.fromDate = dchDateFrom.getDate();
setEnabledComponets();
}
});
}
private void initRegions() throws ClipsException{
DirectoryObjectComboBoxModel<PolisDetails, DirectoryRegionIdCodeItem> model =
new DirectoryObjectComboBoxModel<PolisDetails, DirectoryRegionIdCodeItem>(cbRegion, dirRegionId) {
@Override
public DirectoryRegionIdCodeItem getSelectedDirectoryItem() throws ClipsException {
return getDir().getItemFromID(getObject().regionCodeId);
}
@Override
public void setSelectedDirectoryItem(DirectoryRegionIdCodeItem item) throws ClipsException {
getObject().regionCodeId = item.getID();
setEnabledComponets();
}
};
model.setObject(details);
}
private void initInsurers(){
DirectoryObjectComboBoxModel<PolisDetails, DirectoryInsurerItem> model = new DirectoryObjectComboBoxModel<PolisDetails, DirectoryInsurerItem>(cbInsurer, dirInsurer) {
@Override
public DirectoryInsurerItem getSelectedDirectoryItem() throws ClipsException {
return getDir().getItemFromID(getObject().insurerID);
}
@Override
public void setSelectedDirectoryItem(DirectoryInsurerItem item) throws ClipsException {
getObject().insurerID = item.getID();
setEnabledComponets();
}
};
model.setObject(details);
FilteredComboBoxModel filteredModel = new FilteredComboBoxModel(cbInsurer, new FilteredComboBoxModel.MaskChecker(), true) {
@Override
public Object addItem(String txt) {
System.out.println("addItem: " + txt);
int res = MessageBox.showConfirmYesNo(MessageBox.C_ADD_INSURER_ANOTHER_REGION);
if (res == MessageBox.ANSWER_YES) {
try {
DirectoryInsurerItem insurerItem = dirInsurer.initNewItem(txt);
dirInsurer.getItems().append(insurerItem);
return insurerItem;
} catch (ClipsException ex) {
MessageBox.showException(ex);
return null;
}
} else {
return null;
}
}
};
filteredModel.setContainModel(model);
}
private void setEnabledComponets(){
boolean hasRegionInsurer = details.regionCodeId != 0 && details.insurerID != 0;
boolean changed = !originalDetails.equals(details);
btCreate.setEnabled(hasRegionInsurer && changed && validNumber);
btUpdate.setEnabled(hasRegionInsurer && changed && validNumber);
}
}