/*
* PanelExpedentureDefaultEdit.java
*
* Created on 13 Март 2008 г., 18:31
*/
package clips.administrator.expenditure;
import cli_fmw.delegate.AuditInfo;
import cli_fmw.delegate.directory.DirectoryItemRO;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import clips.delegate.directory.filtered.DirectoryServiceItem;
import clips.delegate.directory.complex.DirectoryServicesGroup;
import clips.delegate.directory.complex.DirectoryServicesGroupItem;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.main.Lockable;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageException;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.main.Printable;
import cli_fmw.utils.ErrorModeListener;
import cli_fmw.utils.MessageBox;
import cli_fmw.main.ClipsException;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.PrintCreators;
import cli_fmw.utils.table_config_states.StateSaver;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import javax.swing.JPanel;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
/**
* Панель редактирования дефолтного расхода материалов
* @author petr
*/
public class PanelExpenditureDefaultEdit extends PageGeneric implements PageContainer, Persistentable, Printable {
PanelExpenTable innerPanel;//панель с таблицей
PageGeneric page;
private TreeModelServices treeModel;// модель дерева
private ExpTreeSelectionListener listener = new ExpTreeSelectionListener();//листенер для дерева
private TreePath lastSel;//старое выделение
private DirectoryServiceItem service;
/** Creates new form PanelExpedentureDefaultEdit
* @param container
* @throws ClipsException
*/
public PanelExpenditureDefaultEdit(PageContainer container) throws ClipsException {
super(container);
initComponents();
DirectoryServicesGroup directory = DirectoryLocator.getDirectory(DirectoryServicesGroup.class, false);
DirectoryItemRO[] array = directory.toArray(false, false);
treeModel = new TreeModelServices(array, "Услуги");
trServices.setModel(treeModel);
DefaultTreeCellRenderer render = new DefaultTreeCellRenderer();
render.setLeafIcon(null);
// render.setOpenIcon(null);
// render.setClosedIcon(null);
trServices.setCellRenderer(render);
trServices.addTreeSelectionListener(listener);
StateSaver.attachTo(this);
}
/**
* Класс реализующий интерфейс слушателя для дерева
*/
class ExpTreeSelectionListener implements TreeSelectionListener {
@Override
public void valueChanged(TreeSelectionEvent arg0) {
if (isDirty()) {// если редактирование не закончено - откат
MessageBox.showWarning(MessageBox.W_NOT_CLOSE_EDITING);
trServices.removeTreeSelectionListener(listener);
trServices.setSelectionPath(lastSel);
trServices.addTreeSelectionListener(listener);
return;
}
Object selObject = trServices.getLastSelectedPathComponent();
if (selObject != null && selObject instanceof DirectoryServiceItem) {
try {
service = (DirectoryServiceItem) selObject;
resetPanel(service);
lastSel = trServices.getSelectionPath();
} catch (PageException ex) {
MessageBox.showException(ex);
} catch (ClipsException ex) {
setErrorMode();
MessageBox.showException(ex);
}
} else {
try {
removePage(page, false);
page = null;
} catch (PageException ex) {
MessageBox.showException(ex);
}
}
fireContainerEventStateChanged();
}
}
private void resetPanel(DirectoryServiceItem service) throws ClipsException, PageException {
if (page != null) {
removePage(innerPanel, false); // Пробуем удалить страницу
}
if (page == null &&
trServices.getModel().isLeaf(trServices.getLastSelectedPathComponent())) {
// Сработает если страница удалена (или не существовала)
innerPanel = new PanelExpenTable(service, this);
addNewPage((PageGeneric) innerPanel, 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();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
trServices = new javax.swing.JTree();
jPanel4 = new javax.swing.JPanel();
btCollapseAll = new javax.swing.JButton();
btExpandAll = new javax.swing.JButton();
jPanel3 = new javax.swing.JPanel();
pnlForExpTable = new javax.swing.JPanel();
jSplitPane1.setBorder(null);
trServices.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseMoved(java.awt.event.MouseEvent evt) {
trServicesMouseMoved(evt);
}
});
jScrollPane1.setViewportView(trServices);
jPanel4.setLayout(new java.awt.GridLayout(1, 2, 5, 0));
btCollapseAll.setText("<html><Center>Свернуть<br>все</Center></html>");
btCollapseAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCollapseAllActionPerformed(evt);
}
});
jPanel4.add(btCollapseAll);
btExpandAll.setText("<html><Center>Раскрыть<br>все</Center></html>");
btExpandAll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btExpandAllActionPerformed(evt);
}
});
jPanel4.add(btExpandAll);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jPanel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE))
.addGap(0, 0, 0))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 223, Short.MAX_VALUE)
.addGap(8, 8, 8)
.addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jSplitPane1.setLeftComponent(jPanel2);
jPanel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 5, 10, 5));
jPanel3.setLayout(new java.awt.BorderLayout());
pnlForExpTable.setBorder(javax.swing.BorderFactory.createTitledBorder("Расход материалов"));
pnlForExpTable.setLayout(new java.awt.BorderLayout());
jPanel3.add(pnlForExpTable, java.awt.BorderLayout.CENTER);
jSplitPane1.setRightComponent(jPanel3);
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, 577, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jSplitPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 289, Short.MAX_VALUE))
);
}// </editor-fold>//GEN-END:initComponents
/**
* нажатие на кнопку "свернуть всё"
* @param evt
*/
private void btCollapseAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCollapseAllActionPerformed
if (isDirty()) {
MessageBox.showWarning(MessageBox.W_NOT_CLOSE_EDITING);
return;
}
for (int i = trServices.getRowCount(); i > 0; i--) {
trServices.collapseRow(i);
}
trServices.setSelectionPath(null);
try {
removePage(page, false);
} catch (PageException ex) {
MessageBox.showException(ex);
}
fireContainerEventStateChanged();
}//GEN-LAST:event_btCollapseAllActionPerformed
/**
* нажатие на кнопку "раскрыть всё"
* @param evt
*/
private void btExpandAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btExpandAllActionPerformed
if (isDirty()) {
MessageBox.showWarning(MessageBox.W_NOT_CLOSE_EDITING);
return;
}
for (int i = 1; i <= trServices.getRowCount(); i++) {
trServices.expandRow(i);
}
trServices.setSelectionPath(null);
fireContainerEventStateChanged();
}//GEN-LAST:event_btExpandAllActionPerformed
private void trServicesMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_trServicesMouseMoved
if (trServices.getPathForLocation(evt.getX(), evt.getY()) == null) {
return;
}
Object obj = trServices.getPathForLocation(evt.getX(), evt.getY()).getLastPathComponent();
if (obj instanceof DirectoryItemRO) {
DirectoryItemRO item = (DirectoryItemRO) obj;
trServices.setToolTipText(item.getTitle());
}
else {
trServices.setToolTipText(null);
}
}//GEN-LAST:event_trServicesMouseMoved
/**
* Клик на кнопке типы матераилов, запуск редактора... тока хз пока как его делать
* @param evt
*/
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btCollapseAll;
private javax.swing.JButton btExpandAll;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JPanel jPanel4;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JPanel pnlForExpTable;
private javax.swing.JTree trServices;
// End of variables declaration//GEN-END:variables
private void setErrorMode(){
trServices.setEnabled(false);
btCollapseAll.setEnabled(false);
btExpandAll.setEnabled(false);
if (innerPanel != null && innerPanel instanceof Lockable) {
innerPanel.lock();
}
ErrorModeListener eml = ErrorModeListener.errorModeListener;
trServices.addMouseListener(eml);
btCollapseAll.addMouseListener(eml);
btExpandAll.addMouseListener(eml);
}
//PageGeneric
@Override
public String getPageTitle() {
return "Расход материалов";
}
//Persistentable
@Override
public boolean isDirty() {
if (innerPanel != null) {
return innerPanel.isDirty();
}
return false;
}
@Override
public void save() throws ClipsException {
innerPanel.save();
}
@Override
public void restore() {
innerPanel.restore();
}
@Override
public PageGeneric addNewPage(PageGeneric pageToAdd, PageGeneric afterPage) throws PageException {
if (page == null) {
page = pageToAdd;
pnlForExpTable.add((JPanel) pageToAdd, java.awt.BorderLayout.CENTER);
pnlForExpTable.revalidate();
pnlForExpTable.repaint();
return pageToAdd;
} else {
throw new PageException("Страница не может быть добавлена");
}
}
@Override
public void removePage(PageGeneric page, boolean forced) throws PageException {
if (page != null) {
if (!((Persistentable)page).isDirty()) {
pnlForExpTable.removeAll();
}
this.page = null;
}
pnlForExpTable.revalidate();
pnlForExpTable.repaint();
}
@Override
public void activatePage(PageGeneric page) throws PageException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public PageGeneric getActivePage() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int getPageCount() {
return (page == null) ? 0:1;
}
@Override
public PageGeneric getPage(int index) {
return page;
}
@Override
public void contentStateChanged() {
super.fireContainerEventStateChanged();
}
@Override
public DelegateLine2 getDelegate() {
return null;
}
@Override
public boolean readyForPrint() {
Object selObject = trServices.getLastSelectedPathComponent();
return (!isDirty() && selObject instanceof DirectoryServiceItem);
}
@Override
public void print() {
try {
if (readyForPrint()) {
String title = "Расход материалов для услуги: ";
String group = "";
DirectoryServicesGroupItem parent = service.getGroup();
ArrayList<String> titles = new ArrayList<String>();
while (parent != null) {
titles.add(parent.getTitle()+ "\n");
parent = parent.getParent();
}
int k = titles.size() - 1;
String iS;
for (int i = k; i > -1; i--) {
iS = titles.get(i);
for (int j = i; j < k; j++) {
iS = " " + iS;
}
group += iS;
}
iS = service.getTitle();
for (int i = 0; i < k + 1; i++) {
iS = " " + iS;
}
group += iS;
HashMap<String, Object> data = new LinkedHashMap<String, Object>();
data.put("groupFullTitle", group);
PrintCreators.createGeneratedReport(title, null, data.entrySet(), null, innerPanel.getTableModel(), getClass());
}
} catch (ClipsException ex) {
MessageBox.showException(ex);
}
}
}