/*
* DialogContentService.java
*
* Created on 13 Ноябрь 2008 г., 10:33
*/
package clips.directory.editors.serviceGroup;
import clips.delegate.directory.complex.DirectoryDoctorAction;
import clips.delegate.directory.complex.DirectoryDoctorActionItem;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import clips.delegate.directory.filtered.DirectoryServiceItem;
import cli_fmw.main.ClipsException;
import cli_fmw.main.audit.AuditManager;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.ModalDialog;
import cli_fmw.utils.SelectorEditableExceptional;
import cli_fmw.utils.table_config_states.StateSaver;
import java.awt.Frame;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.TreePath;
/**
*
* @author vip
*/
public class DialogContentService extends ModalDialog{
DirectoryServiceItem service;
DefaultListModel listModel;
TreeModelDirectoryActions treeModel;
ArrayList<DirectoryDoctorActionItem> actionList;
/** Creates new form DialogContentService */
public DialogContentService(Frame parent, DirectoryServiceItem service, AuditManager am) throws ClipsException {
super(parent, "Редактор услуг", am);
initComponents();
this.service = service;
SelectorEditableExceptional<DirectoryDoctorActionItem> doctorActions = service.getDoctorActions();
actionList = new ArrayList<DirectoryDoctorActionItem>();
for (int i = 0; i < doctorActions.size(); i++) {
DirectoryDoctorActionItem directoryDoctorActionItem = doctorActions.get(i);
actionList.add(directoryDoctorActionItem);
}
DirectoryDoctorAction directory = (DirectoryDoctorAction) DirectoryLocator.getDirectory(DirectoryDoctorAction.class, false);
treeModel = new TreeModelDirectoryActions(directory);
jTree1.setModel(treeModel);
jTree1.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
@Override
public void valueChanged(TreeSelectionEvent e) {
setEnabledComponents();
}
});
refreshList();
jList1.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
setEnabledComponents();
}
});
setTitle("Состав услуги: " + service.getTitle());
setEnabledComponents();
StateSaver.attachTo(this);
}
private void refreshList() {
if (listModel == null) {
listModel = new DefaultListModel();
jList1.setModel(listModel);
}
listModel.clear();
for (int i = 0; i < actionList.size(); i++) {
DirectoryDoctorActionItem action = actionList.get(i);
listModel.addElement(action);
}
}
private void setEnabledComponents() {
Object node = jTree1.getLastSelectedPathComponent();
btAddAction.setEnabled(node != null && treeModel.isLeaf(node));
Object value = jList1.getSelectedValue();
btDelAction.setEnabled(value != 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.
*/
// <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();
jList1 = new javax.swing.JList();
jPanel2 = new javax.swing.JPanel();
btAddAction = new javax.swing.JButton();
btDelAction = new javax.swing.JButton();
jScrollPane2 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jPanel3 = new javax.swing.JPanel();
btOK = new javax.swing.JButton();
btCancel = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
jSplitPane1.setDividerLocation(300);
jPanel1.setLayout(new java.awt.BorderLayout(10, 0));
jScrollPane1.setViewportView(jList1);
jPanel1.add(jScrollPane1, java.awt.BorderLayout.CENTER);
jPanel2.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));
btAddAction.setText("Добавить");
btAddAction.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btAddActionActionPerformed(evt);
}
});
jPanel2.add(btAddAction);
btDelAction.setText(" Удалить ");
btDelAction.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btDelActionActionPerformed(evt);
}
});
jPanel2.add(btDelAction);
jPanel1.add(jPanel2, java.awt.BorderLayout.NORTH);
jSplitPane1.setRightComponent(jPanel1);
jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTree1MouseClicked(evt);
}
});
jScrollPane2.setViewportView(jTree1);
jSplitPane1.setLeftComponent(jScrollPane2);
jPanel3.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));
btOK.setText("Записать");
btOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btOKActionPerformed(evt);
}
});
jPanel3.add(btOK);
btCancel.setText("Отменить");
btCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCancelActionPerformed(evt);
}
});
jPanel3.add(btCancel);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 861, Short.MAX_VALUE)
.addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 861, 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, 388, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
setVisible(false);
dispose();
}//GEN-LAST:event_closeDialog
private void btOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOKActionPerformed
try {
SelectorEditableExceptional<DirectoryDoctorActionItem> doctorActions = service.getDoctorActions();
//Убираем лишние
for (int i = doctorActions.size() - 1; i >=0 ; i--) {
DirectoryDoctorActionItem item = doctorActions.get(i);
if (!actionList.contains(item)) {
doctorActions.remove(i);
}
}
//Добавляем недостающие
for (int i = 0; i < actionList.size(); i++) {
DirectoryDoctorActionItem newItem = actionList.get(i);
boolean founded = false;
for (int j = 0; j < doctorActions.size(); j++) {
DirectoryDoctorActionItem item = doctorActions.get(j);
if (item.equals(newItem)) {
founded = true;
break;
}
}
if (!founded) {
doctorActions.append(newItem);
}
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
setVisible(false);
dispose();
}//GEN-LAST:event_btOKActionPerformed
private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
setVisible(false);
dispose();
}//GEN-LAST:event_btCancelActionPerformed
private void btAddActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionActionPerformed
TreePath[] paths = jTree1.getSelectionPaths();
ArrayList<DirectoryDoctorActionItem> acs = new ArrayList<DirectoryDoctorActionItem>();
for (int i = 0; i < paths.length; i++) {
acs.add((DirectoryDoctorActionItem)paths[i].getLastPathComponent());
}
for (int i = 0; i < acs.size(); i++) {
DirectoryDoctorActionItem item = acs.get(i);
if (!actionList.contains(item)) {
actionList.add(item);
}
}
refreshList();
}//GEN-LAST:event_btAddActionActionPerformed
private void btDelActionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btDelActionActionPerformed
ArrayList<DirectoryDoctorActionItem> acs = new ArrayList<DirectoryDoctorActionItem>();
for (int i = 0; i < jList1.getSelectedValues().length; i++) {
acs.add((DirectoryDoctorActionItem) jList1.getSelectedValues()[i]);
}
for (int i = 0; i < acs.size(); i++) {
actionList.remove(acs.get(i));
}
refreshList();
}//GEN-LAST:event_btDelActionActionPerformed
private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
if (evt.getClickCount() == 2) {
Object node = jTree1.getLastSelectedPathComponent();
if (node != null && treeModel.isLeaf(node)) {
boolean founded = false;
for (int i = 0; i < actionList.size(); i++) {
DirectoryDoctorActionItem oldItem = actionList.get(i);
if (node.equals(oldItem)) {
founded = true;
break;
}
}
if (!founded) {
actionList.add((DirectoryDoctorActionItem) node);
}
refreshList();
}
}
}//GEN-LAST:event_jTree1MouseClicked
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btAddAction;
private javax.swing.JButton btCancel;
private javax.swing.JButton btDelAction;
private javax.swing.JButton btOK;
private javax.swing.JList jList1;
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.JTree jTree1;
// End of variables declaration//GEN-END:variables
}