/*
* PanelEnterpriseEdit.java
*
* Created on 23 Июль 2008 г., 12:00
*/
package clips.directory.editors.enterprise;
import beans.directory.enterprise.EnterpriseDetails;
import cli_fmw.address.DialogAddressM;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.delegate.client.Address;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.directory.dialogs.DirectoryDialogTreeModel;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Printable;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.SelectorEditableExceptional;
import cli_fmw.main.ClipsException;
import cli_fmw.main.ContentStateListener;
import cli_fmw.main.PrintCreators;
import cli_fmw.report.panels.model.ObjectTextModel;
import cli_fmw.utils.ModalDialog;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.config.ConfigLocal;
import clips.delegate.directory.complex.DirectoryEnterprise;
import clips.delegate.directory.complex.DirectoryEnterpriseItem;
import clips.delegate.directory.ro.DirectoryCollaborator;
import clips.login.UserInfo;
import java.awt.Frame;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.tree.TreePath;
/**
*
* @author lacoste
*/
public class PanelEnterpriseEdit extends PageGeneric implements Printable {
private DirectoryEnterpriseItem enterprise;
private DirectoryCollaborator collaborators;
DirectoryEnterprise enterprises;
ContentStateListener listener;
/** Creates new form PanelEnterpriseEdit */
public PanelEnterpriseEdit(PageContainer container) throws ClipsException {
super(container);
initComponents();
jPanel7.setVisible(false);
collaborators = DirectoryLocator.getDirectory(DirectoryCollaborator.class, false);
enterprises = DirectoryLocator.getDirectory(DirectoryEnterprise.class, false);
setTree();
panelSelColl.setWithNull(true);
listener = new ContentStateListener() {
@Override
public void contentStateChanged() {
try {
if (enterprise != null) {
enterprise.setCollaborator(panelSelColl.getSelectedItem());
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
};
StateSaver.attachTo(this);
}
private void setTree() throws ClipsException {
DirectoryDialogTreeModel model =
new DirectoryDialogTreeModel(enterprises.toArray(false), "Предприятия");
treeEnterprises.setModel(model);
}
private void setInnerPanel() throws ClipsException {
jPanel7.setVisible(true);
tfTitle.setText(enterprise.getTitle());
taAddress.setText(enterprise.getAddress().toString());
tfOkved.setText(enterprise.getOkved());
panelSelColl.removeContentStateListener(listener);
panelSelColl.setSelectedCollaborator(enterprise.getCollaborator());
panelSelColl.addContentStateListener(listener);
tfOkved.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent arg0) {
setField(arg0.getDocument());
}
@Override
public void removeUpdate(DocumentEvent arg0) {
setField(arg0.getDocument());
}
@Override
public void changedUpdate(DocumentEvent arg0) {
}
private void setField(Document doc) {
try {
enterprise.setOkved(doc.getText(0, doc.getLength()));
} catch (ClipsException ex) {
MessageBox.showException(ex);
} catch (BadLocationException ex) {
MessageBox.showException(ex);
}
}
});
}
/** 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() {
java.awt.GridBagConstraints gridBagConstraints;
jSplitPane1 = new javax.swing.JSplitPane();
jPanel1 = new javax.swing.JPanel();
jPanel7 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
taAddress = new javax.swing.JTextArea();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
tfTitle = new javax.swing.JTextField();
btSetTitle = new javax.swing.JButton();
jPanel4 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
btAddress = new javax.swing.JButton();
jPanel8 = new javax.swing.JPanel();
jLabel3 = new javax.swing.JLabel();
tfOkved = new javax.swing.JTextField();
jPanel3 = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
panelSelColl = new clips.accountant.PanelCollaboratorSelect();
jPanel5 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
treeEnterprises = new javax.swing.JTree();
jPanel6 = new javax.swing.JPanel();
btDelEnterprise = new javax.swing.JButton();
btAddEnterprise = new javax.swing.JButton();
jSplitPane1.setDividerLocation(230);
jPanel7.setLayout(new java.awt.GridBagLayout());
jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
taAddress.setColumns(20);
taAddress.setEditable(false);
taAddress.setLineWrap(true);
taAddress.setRows(4);
taAddress.setWrapStyleWord(true);
jScrollPane2.setViewportView(taAddress);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
jPanel7.add(jScrollPane2, gridBagConstraints);
jPanel2.setLayout(new java.awt.BorderLayout(5, 0));
jLabel1.setText("Предприятие: ");
jPanel2.add(jLabel1, java.awt.BorderLayout.LINE_START);
tfTitle.setPreferredSize(null);
jPanel2.add(tfTitle, java.awt.BorderLayout.CENTER);
btSetTitle.setText("Изменить");
btSetTitle.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btSetTitleActionPerformed(evt);
}
});
jPanel2.add(btSetTitle, java.awt.BorderLayout.LINE_END);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(0, 2, 2, 0);
jPanel7.add(jPanel2, gridBagConstraints);
jPanel4.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
jLabel2.setText("Адрес предприятия: ");
jPanel4.add(jLabel2);
btAddress.setText("Задать/Изменить");
btAddress.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddressActionPerformed(evt);
}
});
jPanel4.add(btAddress);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 3;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
jPanel7.add(jPanel4, gridBagConstraints);
jPanel8.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT, 0, 0));
jLabel3.setText("Код ОКВЭД: ");
jPanel8.add(jLabel3);
tfOkved.setColumns(20);
jPanel8.add(tfOkved);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 2, 2, 0);
jPanel7.add(jPanel8, gridBagConstraints);
jLabel4.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(jLabel4)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panelSelColl, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE))
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(panelSelColl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
);
jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jLabel4, panelSelColl});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
gridBagConstraints.insets = new java.awt.Insets(0, 2, 2, 0);
jPanel7.add(jPanel3, gridBagConstraints);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel7, javax.swing.GroupLayout.DEFAULT_SIZE, 629, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(275, Short.MAX_VALUE))
);
jSplitPane1.setRightComponent(jPanel1);
jPanel5.setLayout(new java.awt.BorderLayout(0, 5));
jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 382));
treeEnterprises.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
treeEnterprisesValueChanged(evt);
}
});
jScrollPane1.setViewportView(treeEnterprises);
jPanel5.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jPanel6.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
jPanel6.setLayout(new java.awt.GridLayout(1, 0, 5, 5));
btDelEnterprise.setText("Удалить");
btDelEnterprise.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btDelEnterpriseActionPerformed(evt);
}
});
jPanel6.add(btDelEnterprise);
btAddEnterprise.setText("Добавить");
btAddEnterprise.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddEnterpriseActionPerformed(evt);
}
});
jPanel6.add(btAddEnterprise);
jPanel5.add(jPanel6, java.awt.BorderLayout.PAGE_END);
jSplitPane1.setLeftComponent(jPanel5);
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, 865, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void btAddressActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddressActionPerformed
try {
Address address = UserInfo.get().getCollaborator().getLpu().getAddress();
Address ad = new Address(enterprise.getAddress().getDetails());
DialogAddressM dlg = new DialogAddressM((Frame) getContainer(), ad, address, getAuditManager());
dlg.setVisible(true);
if (dlg.getDlgResult() == ModalDialog.DLG_OK){
enterprise.setAddress(dlg.getAddress());
taAddress.setText(enterprise.getAddress().toString());
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btAddressActionPerformed
private void treeEnterprisesValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeEnterprisesValueChanged
try {
if (treeEnterprises.getLastSelectedPathComponent() instanceof DirectoryEnterpriseItem) {
enterprise = (DirectoryEnterpriseItem) treeEnterprises.getLastSelectedPathComponent();
if (enterprise.getID() != 0) {
setInnerPanel();
}
else {
jPanel7.setVisible(false);
}
}
else {
jPanel7.setVisible(false);
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_treeEnterprisesValueChanged
private void btSetTitleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSetTitleActionPerformed
try {
enterprise.setTitle(tfTitle.getText());
treeEnterprises.updateUI();
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btSetTitleActionPerformed
private void btAddEnterpriseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddEnterpriseActionPerformed
try {
EnterpriseDetails d = new EnterpriseDetails();
d.id = 0;
d.title = "Введите название";
DirectoryEnterpriseItem item = new DirectoryEnterpriseItem(d);
if (treeEnterprises.getLastSelectedPathComponent() instanceof DirectoryEnterpriseItem) {
DirectoryEnterpriseItem parent = (DirectoryEnterpriseItem)
treeEnterprises.getLastSelectedPathComponent();
d.parentItem = parent.getID();
parent.getItems().append(item);
}
else {
d.parentItem = 0;
enterprises.getItems().append(item);
}
TreePath path = treeEnterprises.getSelectionPath();
setTree();
treeEnterprises.expandPath(path);
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_btAddEnterpriseActionPerformed
private void btDelEnterpriseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDelEnterpriseActionPerformed
try {
if (treeEnterprises.getLastSelectedPathComponent() == treeEnterprises.getModel().getRoot() || treeEnterprises.getLastSelectedPathComponent() == null) {
return;
}
DirectoryEnterpriseItem item = (DirectoryEnterpriseItem) treeEnterprises.getLastSelectedPathComponent();
if (item.getItems().size() > 0) {
int answer = MessageBox.showConfirmYesNo(MessageBox.C_DELETE_CHIELD_OBJECTS);
if (answer == MessageBox.ANSWER_NO) {
return;
}
}
deleteEnterprise(item);
setTree();
jPanel7.setVisible(false);
} catch (ClipsException clipsException) {
MessageBox.showException(clipsException);
}
}//GEN-LAST:event_btDelEnterpriseActionPerformed
private void deleteEnterprise(DirectoryEnterpriseItem enterpriseItem) throws ClipsException {
ClipsException hasException = null;
SelectorEditableExceptional<DirectoryEnterpriseItem> iie = enterpriseItem.getItems();
for (int i = iie.size() - 1; i > -1; i--) {
DirectoryEnterpriseItem item = iie.get(i);
try {
deleteEnterprise(item);
} catch (ClipsException ex) {
hasException = ex;
}
}
DirectoryEnterpriseItem parent = enterpriseItem.getParent();
if (parent == null) {
iie = enterprises.getItems();
}
else {
iie = parent.getItems();
}
if (hasException == null) {
iie.remove(enterpriseItem);
} else {
throw hasException;
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btAddEnterprise;
private javax.swing.JButton btAddress;
private javax.swing.JButton btDelEnterprise;
private javax.swing.JButton btSetTitle;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
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 jPanel8;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private clips.accountant.PanelCollaboratorSelect panelSelColl;
private javax.swing.JTextArea taAddress;
private javax.swing.JTextField tfOkved;
private javax.swing.JTextField tfTitle;
private javax.swing.JTree treeEnterprises;
// End of variables declaration//GEN-END:variables
@Override
public String getPageTitle() {
return "Редактирование предприятий";
}
@Override
public DelegateLine2 getDelegate() {
return null;
}
@Override
public boolean readyForPrint() {
if (treeEnterprises.getLastSelectedPathComponent() instanceof DirectoryEnterpriseItem) {
DirectoryEnterpriseItem item = (DirectoryEnterpriseItem) treeEnterprises.getLastSelectedPathComponent();
return item.getID() != 0;
}
return false;
}
@Override
public void print() {
try {
if (readyForPrint()) {
HashMap<String, Object> data = new LinkedHashMap<String, Object>();
String title = "Сведения о предприятии: " + enterprise.getTitle();
String group = "";
DirectoryEnterpriseItem parent = enterprise;
ArrayList<String> titles = new ArrayList<String>();
while (parent != null) {
titles.add(parent.getTitle() + "\n");
parent = parent.getParent();
}
int k = titles.size() - 1;
for (int i = k; i > -1; i--) {
String iS = titles.get(i);
for (int j = i; j < k; j++) {
iS = " " + iS;
}
group += iS;
}
data.put("fullTitle", group);
data.put("address", "Адрес: " + enterprise.getAddress().toString());
String collab = enterprise.getCollaborator() != null ?
enterprise.getCollaborator().getTitle() : "";
data.put("collab", "Цеховой терапевт: " + collab);
PrintCreators.createGeneratedReport(title, null, data.entrySet(), null, null, getClass());
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}