/*
* PanelContractService.java
*
* Created on 28 Февраль 2008 г., 12:53
*/
package clips.contract.info.contractService;
import cli_fmw.delegate.directory.Directory;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.main.ClipsException;
import clips.delegate.contract.ContractLocal;
import clips.delegate.directory.complex.DirectoryServicesGroup;
import clips.delegate.directory.complex.DirectoryServicesGroupItem;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.table_config_states.StateSaver;
import cli_fmw.utils.table_config_states.TableState;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.ListSelectionModel;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.tree.TreePath;
/**
*
* @author vip
*/
public class PanelContractService extends PageGeneric{
private static ImageIcon ICON_SEL_ALL = new ImageIcon(PanelContractService.class.getResource("/resources/icons/selected_all.png"));
private ContractLocal contractLocal;
private String index;
/** Creates new form PanelContractService */
public PanelContractService(PageContainer container, ContractLocal contractLocal) throws ClipsException {
super(container);
initComponents();
this.contractLocal = contractLocal;
index = contractLocal.getIndex();
Directory directoryServicesGroup = DirectoryLocator.getDirectory(DirectoryServicesGroup.class, false);
TreeModelServiceGroup model = new TreeModelServiceGroup(directoryServicesGroup);
jTree1.setRootVisible(true);
jTree1.setModel(model);
jTree1.setCellRenderer(new TreeCellRendererServiceGroup(directoryServicesGroup,contractLocal));
//jTree1.setEditable(true);
reloadTable();
resizeColumn();
StateSaver.attachTo(this);
}
private void reloadTable() throws ClipsException {
DirectoryServicesGroupItem serviceGroup;
if (jTree1.getLastSelectedPathComponent() instanceof DirectoryServicesGroupItem) {
serviceGroup = (DirectoryServicesGroupItem) jTree1.getLastSelectedPathComponent();
} else {
serviceGroup = null;
}
jTable1.setModel(new TableModelServicePrice(serviceGroup,contractLocal));
jTable1.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
jTree1.repaint();
}
});
jTable1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
jTable1.setDefaultRenderer(Object.class, new TableCellRendererContractService(contractLocal) );
}
/**
* Нужно вызвать чтобы обновить дерево и таблицу при изменении contractServiceMap
*/
public void contractServiceListModified() throws ClipsException {
jTree1.repaint();
reloadTable();
}
private void resizeColumn() {
TableState state = new TableState();
state.setPreferredWidth(TableModelServicePrice.COL_INCONTRACT, 4);
state.setPreferredWidth(TableModelServicePrice.COL_RECEPTION_TYPE, 4);
state.setPreferredWidth(TableModelServicePrice.COL_SERVICE, 16);
state.setPreferredWidth(TableModelServicePrice.COL_PRICE, 4);
state.setPreferredWidth(TableModelServicePrice.COL_MONEY, 4);
StateSaver.setDefaultState(this, jTable1, state);
}
/** 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();
jScrollPane1 = new javax.swing.JScrollPane();
jTree1 = new javax.swing.JTree();
jScrollPane2 = new javax.swing.JScrollPane();
jTable1 = new cli_fmw.utils.sorted_table.SortedTable();
jSplitPane1.setDividerLocation(280);
jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(java.awt.event.MouseEvent evt) {
jTree1MousePressed(evt);
}
});
jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
jTree1ValueChanged(evt);
}
});
jScrollPane1.setViewportView(jTree1);
jSplitPane1.setLeftComponent(jScrollPane1);
jScrollPane2.addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
jScrollPane2ComponentResized(evt);
}
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane2.setViewportView(jTable1);
jSplitPane1.setRightComponent(jScrollPane2);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 220, Short.MAX_VALUE)
.addContainerGap())
);
}// </editor-fold>//GEN-END:initComponents
private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
try {
reloadTable();
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}//GEN-LAST:event_jTree1ValueChanged
private void jTree1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MousePressed
TreePath path = jTree1.getPathForLocation(evt.getX(), evt.getY());
if (path != null && path.getLastPathComponent() instanceof DirectoryServicesGroupItem) {
DirectoryServicesGroupItem item = (DirectoryServicesGroupItem) path.getLastPathComponent();
Rectangle rect = jTree1.getPathBounds(path);
if (rect != null) {
int dx = evt.getX() - rect.x;
int dy = evt.getY() - rect.y;
if (dx <= ICON_SEL_ALL.getIconWidth()) {
//Попали мышкой по иконке - пытаемся либо добавить либо удалить дочерние услуги из договора
try {
int selectedStatus = contractLocal.getSelectedStatus(item);
if (selectedStatus == ContractLocal.SEL_STATUS_ALL) {
// Пытаемся удалить все услуги
if (MessageBox.showConfirmYesNo(MessageBox.C_DEL_SERVS_FROM_CONT) == MessageBox.ANSWER_OK) {
contractLocal.setSelectedStatus(item, ContractLocal.SEL_STATUS_NOTHING);
reloadTable();
jTree1.repaint();
}
} else if (selectedStatus == ContractLocal.SEL_STATUS_NOTHING ||
selectedStatus == ContractLocal.SEL_STATUS_SOME) {
// Пытаемся добавить все услуги
if (MessageBox.showConfirmYesNo(MessageBox.C_ADD_SERVS_TO_CONT) == MessageBox.ANSWER_OK) {
contractLocal.setSelectedStatus(item, ContractLocal.SEL_STATUS_ALL);
reloadTable();
jTree1.repaint();
}
} else if (selectedStatus == ContractLocal.SEL_STATUS_EMPTY) {
// услуг нету
MessageBox.showWarning(MessageBox.W_GROUP_HASNT_SERVICES);
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}
}
if (path != null && !(path.getLastPathComponent() instanceof DirectoryServicesGroupItem)) {
try {
reloadTable();
} catch (ClipsException ex) {
ex.printStackTrace();
}
}
}//GEN-LAST:event_jTree1MousePressed
private void jScrollPane2ComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jScrollPane2ComponentResized
}//GEN-LAST:event_jScrollPane2ComponentResized
/**
* Добавление группы услуг
* @param evt
*/
@Override
public String getPageTitle() {
return "Цены на услуги по договору " + index;
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JSplitPane jSplitPane1;
private cli_fmw.utils.sorted_table.SortedTable jTable1;
private javax.swing.JTree jTree1;
// End of variables declaration//GEN-END:variables
@Override
public DelegateLine2 getDelegate() {
return contractLocal;
}
}