0001: /*
0002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
0003: * http://www.jaspersoft.com.
0004: *
0005: * Unless you have purchased a commercial license agreement from JasperSoft,
0006: * the following license terms apply:
0007: *
0008: * This program is free software; you can redistribute it and/or modify
0009: * it under the terms of the GNU General Public License version 2 as published by
0010: * the Free Software Foundation.
0011: *
0012: * This program is distributed WITHOUT ANY WARRANTY; and without the
0013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0014: * See the GNU General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU General Public License
0017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
0018: * or write to:
0019: *
0020: * Free Software Foundation, Inc.,
0021: * 59 Temple Place - Suite 330,
0022: * Boston, MA USA 02111-1307
0023: *
0024: *
0025: *
0026: *
0027: * CrosstabStructurePanel.java
0028: *
0029: * Created on 25 novembre 2005, 11.34
0030: *
0031: */
0032:
0033: package it.businesslogic.ireport.gui.documentstructure;
0034:
0035: import it.businesslogic.ireport.ElementGroup;
0036: import it.businesslogic.ireport.gui.MainFrame;
0037: import it.businesslogic.ireport.gui.event.CrosstabLayoutChangedListener;
0038: import it.businesslogic.ireport.gui.event.ReportListener;
0039: import it.businesslogic.ireport.gui.event.SubDatasetObjectChangedListener;
0040: import it.businesslogic.ireport.util.Misc;
0041: import javax.swing.tree.DefaultMutableTreeNode;
0042: import javax.swing.tree.DefaultTreeSelectionModel;
0043: import it.businesslogic.ireport.gui.*;
0044: import it.businesslogic.ireport.*;
0045: import it.businesslogic.ireport.crosstab.CrosstabCell;
0046: import it.businesslogic.ireport.crosstab.gui.CrosstabEditorPanel;
0047: import it.businesslogic.ireport.gui.docking.GenericDragTargetListener;
0048: import it.businesslogic.ireport.gui.event.*;
0049: import java.util.Enumeration;
0050: import java.util.Vector;
0051: import javax.swing.tree.TreePath;
0052: import it.businesslogic.ireport.undo.UnGroupEmentsOperation;
0053: import java.awt.dnd.DropTarget;
0054: import java.util.ArrayList;
0055: import java.util.List;
0056: import it.businesslogic.ireport.util.I18n;
0057:
0058: /**
0059: *
0060: * @author Administrator
0061: */
0062: public class CrosstabStructurePanel extends javax.swing.JPanel
0063: implements ReportListener, SubDatasetObjectChangedListener,
0064: CrosstabLayoutChangedListener {
0065:
0066: private boolean dontHandleEvent = false;
0067:
0068: private JReportFrame oldJReportFrame = null;
0069: private CrosstabReportElement crosstabReportElement = null;
0070: private CrosstabEditorPanel oldCrosstabEditorPanel = null;
0071:
0072: /** Creates new form DocumentStructurePanel */
0073: public CrosstabStructurePanel() {
0074: initComponents();
0075:
0076: DefaultTreeSelectionModel dtsm = (DefaultTreeSelectionModel) jTreeDocument
0077: .getSelectionModel();
0078: dtsm
0079: .setSelectionMode(DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION);
0080:
0081: DocumentStructureTreeNode rootDoc = new DocumentStructureTreeNode(
0082: it.businesslogic.ireport.util.I18n.getString(
0083: "crossTab", "Crosstab"));
0084: javax.swing.tree.DefaultTreeModel modelDoc = new javax.swing.tree.DefaultTreeModel(
0085: rootDoc);
0086:
0087: jTreeDocument.setDropTarget(new DropTarget(this ,
0088: new GenericDragTargetListener()));
0089:
0090: jTreeDocument.setModel(modelDoc);
0091: jTreeDocument
0092: .setCellRenderer(new DocumentStructureTreeCellRenderer());
0093:
0094: }
0095:
0096: /** This method is called from within the constructor to
0097: * initialize the form.
0098: * WARNING: Do NOT modify this code. The content of this method is
0099: * always regenerated by the Form Editor.
0100: */
0101: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
0102: private void initComponents() {
0103: jPopupMenuDocumentStructure = new javax.swing.JPopupMenu();
0104: jMenuItemDS_properties = new javax.swing.JMenuItem();
0105: jMenuItemDSB_crosstabProperties1 = new javax.swing.JMenuItem();
0106: jSeparator22 = new javax.swing.JSeparator();
0107: jMenuItemMoveUp = new javax.swing.JMenuItem();
0108: jMenuItemMoveDown = new javax.swing.JMenuItem();
0109: jSeparator23 = new javax.swing.JSeparator();
0110: jMenuItemDS_cut = new javax.swing.JMenuItem();
0111: jMenuItemDS_copy = new javax.swing.JMenuItem();
0112: jMenuItemDS_paste = new javax.swing.JMenuItem();
0113: jMenuItemDS_delete = new javax.swing.JMenuItem();
0114: jPopupMenuDocumentStructureCell = new javax.swing.JPopupMenu();
0115: jMenuItemDSB_crosstabProperties = new javax.swing.JMenuItem();
0116: jMenuItemDSB_properties = new javax.swing.JMenuItem();
0117: jPopupMenuDocumentStructureGroup = new javax.swing.JPopupMenu();
0118: jMenuItemUngroup = new javax.swing.JMenuItem();
0119: jScrollPane2 = new javax.swing.JScrollPane();
0120: jTreeDocument = new javax.swing.JTree();
0121:
0122: jMenuItemDS_properties.setText("Properties");
0123: jMenuItemDS_properties
0124: .addActionListener(new java.awt.event.ActionListener() {
0125: public void actionPerformed(
0126: java.awt.event.ActionEvent evt) {
0127: jMenuItemDS_propertiesActionPerformed(evt);
0128: }
0129: });
0130:
0131: jPopupMenuDocumentStructure.add(jMenuItemDS_properties);
0132:
0133: jMenuItemDSB_crosstabProperties1.setText("Crosstab properties");
0134: jMenuItemDSB_crosstabProperties1
0135: .addActionListener(new java.awt.event.ActionListener() {
0136: public void actionPerformed(
0137: java.awt.event.ActionEvent evt) {
0138: jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1(evt);
0139: }
0140: });
0141:
0142: jPopupMenuDocumentStructure
0143: .add(jMenuItemDSB_crosstabProperties1);
0144:
0145: jPopupMenuDocumentStructure.add(jSeparator22);
0146:
0147: jMenuItemMoveUp.setText("Move up");
0148: jMenuItemMoveUp.setEnabled(false);
0149: jMenuItemMoveUp
0150: .addActionListener(new java.awt.event.ActionListener() {
0151: public void actionPerformed(
0152: java.awt.event.ActionEvent evt) {
0153: jMenuItemMoveUpActionPerformed(evt);
0154: }
0155: });
0156:
0157: jPopupMenuDocumentStructure.add(jMenuItemMoveUp);
0158:
0159: jMenuItemMoveDown.setText("Move down");
0160: jMenuItemMoveDown.setEnabled(false);
0161: jMenuItemMoveDown
0162: .addActionListener(new java.awt.event.ActionListener() {
0163: public void actionPerformed(
0164: java.awt.event.ActionEvent evt) {
0165: jMenuItemMoveDownActionPerformed(evt);
0166: }
0167: });
0168:
0169: jPopupMenuDocumentStructure.add(jMenuItemMoveDown);
0170:
0171: jPopupMenuDocumentStructure.add(jSeparator23);
0172:
0173: jMenuItemDS_cut
0174: .setIcon(new javax.swing.ImageIcon(
0175: getClass()
0176: .getResource(
0177: "/it/businesslogic/ireport/icons/menu/cut.png")));
0178: jMenuItemDS_cut.setText("Cut");
0179: jMenuItemDS_cut.setEnabled(false);
0180: jMenuItemDS_cut
0181: .addActionListener(new java.awt.event.ActionListener() {
0182: public void actionPerformed(
0183: java.awt.event.ActionEvent evt) {
0184: jMenuItemDS_cutActionPerformed(evt);
0185: }
0186: });
0187:
0188: jPopupMenuDocumentStructure.add(jMenuItemDS_cut);
0189:
0190: jMenuItemDS_copy
0191: .setIcon(new javax.swing.ImageIcon(
0192: getClass()
0193: .getResource(
0194: "/it/businesslogic/ireport/icons/menu/copy.png")));
0195: jMenuItemDS_copy.setText("Copy");
0196: jMenuItemDS_copy.setEnabled(false);
0197: jMenuItemDS_copy
0198: .addActionListener(new java.awt.event.ActionListener() {
0199: public void actionPerformed(
0200: java.awt.event.ActionEvent evt) {
0201: jMenuItemDS_copyActionPerformed(evt);
0202: }
0203: });
0204:
0205: jPopupMenuDocumentStructure.add(jMenuItemDS_copy);
0206:
0207: jMenuItemDS_paste
0208: .setIcon(new javax.swing.ImageIcon(
0209: getClass()
0210: .getResource(
0211: "/it/businesslogic/ireport/icons/menu/paste.png")));
0212: jMenuItemDS_paste.setText("Paste");
0213: jMenuItemDS_paste.setEnabled(false);
0214: jMenuItemDS_paste
0215: .addActionListener(new java.awt.event.ActionListener() {
0216: public void actionPerformed(
0217: java.awt.event.ActionEvent evt) {
0218: jMenuItemDS_pasteActionPerformed(evt);
0219: }
0220: });
0221:
0222: jPopupMenuDocumentStructure.add(jMenuItemDS_paste);
0223:
0224: jMenuItemDS_delete
0225: .setIcon(new javax.swing.ImageIcon(
0226: getClass()
0227: .getResource(
0228: "/it/businesslogic/ireport/icons/menu/delete.png")));
0229: jMenuItemDS_delete.setText("Delete");
0230: jMenuItemDS_delete.setEnabled(false);
0231: jMenuItemDS_delete
0232: .addActionListener(new java.awt.event.ActionListener() {
0233: public void actionPerformed(
0234: java.awt.event.ActionEvent evt) {
0235: jMenuItemDS_deleteActionPerformed(evt);
0236: }
0237: });
0238:
0239: jPopupMenuDocumentStructure.add(jMenuItemDS_delete);
0240:
0241: jMenuItemDSB_crosstabProperties.setText("Crosstab properties");
0242: jMenuItemDSB_crosstabProperties
0243: .addActionListener(new java.awt.event.ActionListener() {
0244: public void actionPerformed(
0245: java.awt.event.ActionEvent evt) {
0246: jMenuItemDSB_propertiesActionPerformed1(evt);
0247: }
0248: });
0249:
0250: jPopupMenuDocumentStructureCell
0251: .add(jMenuItemDSB_crosstabProperties);
0252:
0253: jMenuItemDSB_properties.setText("Cell properties");
0254: jMenuItemDSB_properties
0255: .addActionListener(new java.awt.event.ActionListener() {
0256: public void actionPerformed(
0257: java.awt.event.ActionEvent evt) {
0258: jMenuItemDSB_propertiesActionPerformed(evt);
0259: }
0260: });
0261:
0262: jPopupMenuDocumentStructureCell.add(jMenuItemDSB_properties);
0263:
0264: jMenuItemUngroup.setText("Ungroup");
0265: jMenuItemUngroup
0266: .addActionListener(new java.awt.event.ActionListener() {
0267: public void actionPerformed(
0268: java.awt.event.ActionEvent evt) {
0269: jMenuItemUngroupActionPerformed(evt);
0270: }
0271: });
0272:
0273: jPopupMenuDocumentStructureGroup.add(jMenuItemUngroup);
0274:
0275: setLayout(new java.awt.BorderLayout());
0276:
0277: jTreeDocument
0278: .addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
0279: public void treeCollapsed(
0280: javax.swing.event.TreeExpansionEvent evt) {
0281: jTreeDocumentTreeCollapsed(evt);
0282: }
0283:
0284: public void treeExpanded(
0285: javax.swing.event.TreeExpansionEvent evt) {
0286: jTreeDocumentTreeExpanded(evt);
0287: }
0288: });
0289: jTreeDocument
0290: .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
0291: public void valueChanged(
0292: javax.swing.event.TreeSelectionEvent evt) {
0293: jTreeDocumentValueChanged(evt);
0294: }
0295: });
0296: jTreeDocument
0297: .addMouseListener(new java.awt.event.MouseAdapter() {
0298: public void mousePressed(
0299: java.awt.event.MouseEvent evt) {
0300: jTreeDocumentMousePressed(evt);
0301: }
0302: });
0303:
0304: jScrollPane2.setViewportView(jTreeDocument);
0305:
0306: add(jScrollPane2, java.awt.BorderLayout.CENTER);
0307:
0308: }// </editor-fold>//GEN-END:initComponents
0309:
0310: private void jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1(
0311: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1
0312: it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog cpd = new it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog(
0313: MainFrame.getMainInstance(), true);
0314: cpd.setCurrentCrosstabReportElement(getCrosstabReportElement());
0315: cpd.setVisible(true);
0316: }//GEN-LAST:event_jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1
0317:
0318: private void jMenuItemDSB_propertiesActionPerformed1(
0319: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDSB_propertiesActionPerformed1
0320:
0321: it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog cpd = new it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog(
0322: MainFrame.getMainInstance(), true);
0323: cpd.setCurrentCrosstabReportElement(getCrosstabReportElement());
0324: cpd.setVisible(true);
0325:
0326: }//GEN-LAST:event_jMenuItemDSB_propertiesActionPerformed1
0327:
0328: private void jMenuItemUngroupActionPerformed(
0329: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemUngroupActionPerformed
0330:
0331: if (jTreeDocument.getSelectionCount() > 0) {
0332: DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode) jTreeDocument
0333: .getSelectionPath().getLastPathComponent();
0334: if (elementNode.getUserObject() instanceof ElementGroup) {
0335: String elementGroupName = ((ElementGroup) elementNode
0336: .getUserObject()).getName();
0337: while (((DocumentStructureTreeNode) elementNode
0338: .getParent()).getUserObject() instanceof ElementGroup) {
0339: elementNode = (DocumentStructureTreeNode) elementNode
0340: .getParent();
0341: elementGroupName = ((ElementGroup) elementNode
0342: .getUserObject()).getName()
0343: + "." + elementGroupName;
0344: }
0345:
0346: String newElementGroupName = elementGroupName;
0347: if (newElementGroupName.lastIndexOf(".") >= 0) {
0348: newElementGroupName = newElementGroupName
0349: .substring(0, newElementGroupName
0350: .lastIndexOf("."));
0351: } else {
0352: newElementGroupName = "";
0353: }
0354:
0355: /************************/
0356: UnGroupEmentsOperation undoOp = new UnGroupEmentsOperation(
0357: MainFrame.getMainInstance()
0358: .getActiveReportFrame());
0359: Vector elements = getCrosstabReportElement()
0360: .getElements();
0361: for (int i = 0; i < elements.size(); ++i) {
0362: ReportElement element = (ReportElement) elements
0363: .elementAt(i);
0364: String oldElementGroupName = element
0365: .getElementGroup();
0366: if (element.getElementGroup().startsWith(
0367: elementGroupName + ".")
0368: || element.getElementGroup().equals(
0369: elementGroupName)) {
0370: String tmpElementGroupName = element
0371: .getElementGroup().substring(
0372: elementGroupName.length());
0373: if (tmpElementGroupName.length() == 0)
0374: tmpElementGroupName = newElementGroupName;
0375: tmpElementGroupName = newElementGroupName
0376: + tmpElementGroupName;
0377: if (tmpElementGroupName.startsWith("."))
0378: tmpElementGroupName = tmpElementGroupName
0379: .substring(1);
0380:
0381: element.setElementGroup(tmpElementGroupName);
0382: undoOp.addElement(element, i, i,
0383: oldElementGroupName,
0384: tmpElementGroupName);
0385: }
0386: }
0387:
0388: MainFrame.getMainInstance().getActiveReportFrame()
0389: .addUndoOperation(undoOp);
0390:
0391: MainFrame
0392: .getMainInstance()
0393: .getActiveReportFrame()
0394: .fireReportListenerReportElementsChanged(
0395: new ReportElementChangedEvent(
0396: MainFrame.getMainInstance()
0397: .getActiveReportFrame(),
0398: new Vector(),
0399: ReportElementChangedEvent.REMOVED));
0400: }
0401: }
0402: // Get group name...
0403:
0404: }//GEN-LAST:event_jMenuItemUngroupActionPerformed
0405:
0406: private void jTreeDocumentTreeCollapsed(
0407: javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_jTreeDocumentTreeCollapsed
0408: //System.out.println("collapsing..." + evt.getPath());
0409: }//GEN-LAST:event_jTreeDocumentTreeCollapsed
0410:
0411: private void jTreeDocumentTreeExpanded(
0412: javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_jTreeDocumentTreeExpanded
0413: // System.out.println("expanding..." + evt.getPath());
0414:
0415: }//GEN-LAST:event_jTreeDocumentTreeExpanded
0416:
0417: private void jMenuItemDSB_propertiesActionPerformed(
0418: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDSB_propertiesActionPerformed
0419: if (jTreeDocument.getSelectionCount() > 0) {
0420: DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode) jTreeDocument
0421: .getSelectionPath().getLastPathComponent();
0422: if (elementNode.getUserObject() instanceof CrosstabCell) {
0423: CrosstabCell this Cell = ((CrosstabCell) elementNode
0424: .getUserObject());
0425: MainFrame.getMainInstance().getActiveReportFrame()
0426: .getSelectedCrosstabEditorPanel().editCell(
0427: this Cell);
0428: }
0429: }
0430: }//GEN-LAST:event_jMenuItemDSB_propertiesActionPerformed
0431:
0432: private void jMenuItemDS_deleteActionPerformed(
0433: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDS_deleteActionPerformed
0434: MainFrame.getMainInstance().getActiveReportFrame()
0435: .getSelectedCrosstabEditorPanel()
0436: .deleteSelectedElements();
0437: }//GEN-LAST:event_jMenuItemDS_deleteActionPerformed
0438:
0439: private void jMenuItemDS_pasteActionPerformed(
0440: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDS_pasteActionPerformed
0441: MainFrame.getMainInstance().getActiveReportFrame().paste();
0442: }//GEN-LAST:event_jMenuItemDS_pasteActionPerformed
0443:
0444: private void jMenuItemDS_copyActionPerformed(
0445: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDS_copyActionPerformed
0446: MainFrame.getMainInstance().getActiveReportFrame().copy();
0447: }//GEN-LAST:event_jMenuItemDS_copyActionPerformed
0448:
0449: private void jMenuItemDS_cutActionPerformed(
0450: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDS_cutActionPerformed
0451: MainFrame.getMainInstance().getActiveReportFrame().cut();
0452: }//GEN-LAST:event_jMenuItemDS_cutActionPerformed
0453:
0454: private void jMenuItemMoveDownActionPerformed(
0455: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemMoveDownActionPerformed
0456: if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
0457: JReportFrame jrf = MainFrame.getMainInstance()
0458: .getActiveReportFrame();
0459: jrf.moveDown();
0460:
0461: if (jrf.getSelectedCrosstabEditorPanel()
0462: .getSelectedElements().size() > 0)
0463: updateDocumentStructureTree(jrf,
0464: getCrosstabReportElement());
0465:
0466: }
0467: }//GEN-LAST:event_jMenuItemMoveDownActionPerformed
0468:
0469: private void jMenuItemMoveUpActionPerformed(
0470: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemMoveUpActionPerformed
0471: if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
0472: JReportFrame jrf = MainFrame.getMainInstance()
0473: .getActiveReportFrame();
0474: jrf.moveUp();
0475: this .jTreeDocument.updateUI();
0476:
0477: if (jrf.getSelectedCrosstabEditorPanel()
0478: .getSelectedElements().size() > 0)
0479: updateDocumentStructureTree(jrf,
0480: getCrosstabReportElement());
0481: }
0482:
0483: }//GEN-LAST:event_jMenuItemMoveUpActionPerformed
0484:
0485: private void jMenuItemDS_propertiesActionPerformed(
0486: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemDS_propertiesActionPerformed
0487: MainFrame.getMainInstance().getElementPropertiesDialog()
0488: .setVisible(true);
0489: //MainFrame.getMainInstance().getElementPropertiesDialog().updateSelection();
0490: }//GEN-LAST:event_jMenuItemDS_propertiesActionPerformed
0491:
0492: private void jTreeDocumentValueChanged(
0493: javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTreeDocumentValueChanged
0494: if (isDontHandleEvent())
0495: return;
0496:
0497: // Get active jrf...
0498: if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
0499: //dontHandleEvent = true;
0500: try {
0501: JReportFrame jrf = MainFrame.getMainInstance()
0502: .getActiveReportFrame();
0503: Vector elementsToSelect = new Vector();
0504:
0505: TreePath[] path = jTreeDocument.getSelectionPaths();
0506:
0507: for (int i = 0; i < path.length; ++i) {
0508: DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode) path[i]
0509: .getLastPathComponent();
0510: if (elementNode.getUserObject() instanceof ReportElement) {
0511: elementsToSelect
0512: .addElement((ReportElement) elementNode
0513: .getUserObject());
0514: } else if (path.length == 1) {
0515: jrf.getSelectedCrosstabEditorPanel()
0516: .setSelectedElement(null);
0517: return;
0518: } else if (elementNode.getUserObject() instanceof ElementGroup) {
0519:
0520: jrf.getSelectedCrosstabEditorPanel()
0521: .setSelectedElement(null);
0522: return;
0523: }
0524: }
0525:
0526: jrf.getSelectedCrosstabEditorPanel()
0527: .setSelectedElement(null);
0528: Enumeration e = elementsToSelect.elements();
0529: while (e.hasMoreElements()) {
0530: jrf.getSelectedCrosstabEditorPanel()
0531: .addSelectedElement(
0532: (ReportElement) e.nextElement(),
0533: false);
0534: }
0535: jrf.getSelectedCrosstabEditorPanel()
0536: .fireSelectionChangedEvent();
0537: } catch (Exception ex) {
0538: }
0539: //
0540: }
0541: }//GEN-LAST:event_jTreeDocumentValueChanged
0542:
0543: private void jTreeDocumentMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTreeDocumentMousePressed
0544: if (evt.getButton() == evt.BUTTON3) {
0545: if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
0546: JReportFrame jrf = MainFrame.getMainInstance()
0547: .getActiveReportFrame();
0548:
0549: Misc.ensurePathIsSelected(jTreeDocument
0550: .getPathForLocation(evt.getX(), evt.getY()),
0551: jTreeDocument);
0552: //jTreeDocument.setSelectionPath( jTreeDocument.getPathForLocation(evt.getX(), evt.getY() ) );
0553: if (jTreeDocument.getSelectionCount() > 0) {
0554: DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode) jTreeDocument
0555: .getSelectionPath().getLastPathComponent();
0556: if (elementNode.getUserObject() instanceof ReportElement) {
0557: // (ReportElement)elementNode.getUserObject());
0558: this .jPopupMenuDocumentStructure.show(
0559: jTreeDocument, evt.getPoint().x, evt
0560: .getPoint().y);
0561:
0562: } else if (elementNode.getUserObject() instanceof CrosstabCell) {
0563: // (ReportElement)elementNode.getUserObject());
0564: this .jPopupMenuDocumentStructureCell.show(
0565: jTreeDocument, evt.getPoint().x, evt
0566: .getPoint().y);
0567:
0568: } else if (elementNode.getUserObject() instanceof ElementGroup) {
0569: // (ReportElement)elementNode.getUserObject());
0570: this .jPopupMenuDocumentStructureGroup.show(
0571: jTreeDocument, evt.getPoint().x, evt
0572: .getPoint().y);
0573:
0574: }
0575: }
0576: }
0577:
0578: } else if (evt.getButton() == evt.BUTTON1
0579: && evt.getClickCount() == 2) {
0580:
0581: if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
0582:
0583: JReportFrame jrf = MainFrame.getMainInstance()
0584: .getActiveReportFrame();
0585: if (jrf.getSelectedCrosstabEditorPanel()
0586: .getSelectedElements().size() > 0) {
0587: MainFrame.getMainInstance()
0588: .getElementPropertiesDialog().setVisible(
0589: true);
0590: MainFrame.getMainInstance()
0591: .getElementPropertiesDialog()
0592: .updateSelection();
0593: }
0594: }
0595: }
0596: }//GEN-LAST:event_jTreeDocumentMousePressed
0597:
0598: public void printSelectedPaths(String prefix) {
0599: try {
0600: Enumeration enum_extended_paths = jTreeDocument
0601: .getExpandedDescendants(new TreePath(
0602: new Object[] { jTreeDocument.getModel()
0603: .getRoot() }));
0604: if (enum_extended_paths != null) {
0605: while (enum_extended_paths.hasMoreElements()) {
0606: TreePath path = (TreePath) enum_extended_paths
0607: .nextElement();
0608: //System.out.println(prefix+ " "+path);
0609: }
0610: }
0611: } catch (Exception ex) {
0612: ex.printStackTrace();
0613: }
0614: }
0615:
0616: // Variables declaration - do not modify//GEN-BEGIN:variables
0617: private javax.swing.JMenuItem jMenuItemDSB_crosstabProperties;
0618: private javax.swing.JMenuItem jMenuItemDSB_crosstabProperties1;
0619: private javax.swing.JMenuItem jMenuItemDSB_properties;
0620: private javax.swing.JMenuItem jMenuItemDS_copy;
0621: private javax.swing.JMenuItem jMenuItemDS_cut;
0622: private javax.swing.JMenuItem jMenuItemDS_delete;
0623: private javax.swing.JMenuItem jMenuItemDS_paste;
0624: private javax.swing.JMenuItem jMenuItemDS_properties;
0625: private javax.swing.JMenuItem jMenuItemMoveDown;
0626: private javax.swing.JMenuItem jMenuItemMoveUp;
0627: private javax.swing.JMenuItem jMenuItemUngroup;
0628: private javax.swing.JPopupMenu jPopupMenuDocumentStructure;
0629: private javax.swing.JPopupMenu jPopupMenuDocumentStructureCell;
0630: private javax.swing.JPopupMenu jPopupMenuDocumentStructureGroup;
0631: private javax.swing.JScrollPane jScrollPane2;
0632: private javax.swing.JSeparator jSeparator22;
0633: private javax.swing.JSeparator jSeparator23;
0634: private javax.swing.JTree jTreeDocument;
0635:
0636: // End of variables declaration//GEN-END:variables
0637:
0638: public void saveSelection() {
0639: List openedPaths = null;
0640: try {
0641:
0642: openedPaths = new ArrayList();
0643: Enumeration enum_extended_paths = jTreeDocument
0644: .getExpandedDescendants(new TreePath(
0645: new Object[] { jTreeDocument.getModel()
0646: .getRoot() }));
0647: if (enum_extended_paths != null) {
0648: while (enum_extended_paths.hasMoreElements()) {
0649: TreePath path = (TreePath) enum_extended_paths
0650: .nextElement();
0651: openedPaths.add(((DocumentStructureTreeNode) path
0652: .getLastPathComponent()).getNodeId());
0653: }
0654: }
0655: } catch (Exception ex) {
0656: ex.printStackTrace();
0657: }
0658:
0659: if (oldCrosstabEditorPanel != null)
0660: oldCrosstabEditorPanel
0661: .setOpenedNodesDocumentStructure(openedPaths);
0662: }
0663:
0664: public void updateDocumentStructureTree(JReportFrame jrf,
0665: CrosstabReportElement crosstabReportElement) {
0666:
0667: List openedPaths = null;
0668:
0669: try {
0670:
0671: openedPaths = new ArrayList();
0672: Enumeration enum_extended_paths = jTreeDocument
0673: .getExpandedDescendants(new TreePath(
0674: new Object[] { jTreeDocument.getModel()
0675: .getRoot() }));
0676: if (enum_extended_paths != null) {
0677: while (enum_extended_paths.hasMoreElements()) {
0678: TreePath path = (TreePath) enum_extended_paths
0679: .nextElement();
0680: openedPaths.add(((DocumentStructureTreeNode) path
0681: .getLastPathComponent()).getNodeId());
0682: }
0683: }
0684: } catch (Exception ex) {
0685: ex.printStackTrace();
0686: }
0687:
0688: if (jrf != null
0689: && jrf.getSelectedCrosstabEditorPanel() != oldCrosstabEditorPanel) {
0690: if (oldCrosstabEditorPanel != null)
0691: oldCrosstabEditorPanel
0692: .setOpenedNodesDocumentStructure(openedPaths);
0693: if (jrf != null
0694: && jrf.getSelectedCrosstabEditorPanel() != null) {
0695: openedPaths = jrf.getSelectedCrosstabEditorPanel()
0696: .getOpenedNodesDocumentStructure();
0697: oldCrosstabEditorPanel = jrf
0698: .getSelectedCrosstabEditorPanel();
0699: } else {
0700: oldCrosstabEditorPanel = null;
0701: }
0702:
0703: }
0704:
0705: if (getCrosstabReportElement() != null
0706: && getCrosstabReportElement() != crosstabReportElement) {
0707: getCrosstabReportElement()
0708: .removeCrosstabLayoutChangedListener(this );
0709: }
0710:
0711: this .setCrosstabReportElement(crosstabReportElement);
0712:
0713: if (getCrosstabReportElement() != null) {
0714: this .getCrosstabReportElement()
0715: .addCrosstabLayoutChangedListener(this );
0716: }
0717:
0718: setDontHandleEvent(true);
0719: // Update the document tree structure...
0720: ((DocumentStructureTreeNode) this .jTreeDocument.getModel()
0721: .getRoot()).removeAllChildren();
0722: ((javax.swing.tree.DefaultTreeModel) (this .jTreeDocument
0723: .getModel())).reload();
0724:
0725: if (jrf != null && getCrosstabReportElement() != null) {
0726:
0727: Enumeration cells = getCrosstabReportElement().getCells()
0728: .elements();
0729: while (cells.hasMoreElements()) {
0730: CrosstabCell cell = (CrosstabCell) cells.nextElement();
0731: DocumentStructureTreeNode bandNode = new DocumentStructureTreeNode(
0732: cell);
0733: ((DocumentStructureTreeNode) this .jTreeDocument
0734: .getModel().getRoot()).add(bandNode);
0735:
0736: Enumeration elements = getCrosstabReportElement()
0737: .getElements().elements();
0738: while (elements.hasMoreElements()) {
0739: ReportElement element = (ReportElement) elements
0740: .nextElement();
0741: if (element.getCell() == cell) {
0742: String elementGroup = element.getElementGroup();
0743: try {
0744: if (element.getParentElement() != null) {
0745: DocumentStructureTreeNode parentNode = findElementTreeNode(
0746: element.getParentElement(),
0747: true);
0748: if (parentNode != null) {
0749: addElementToGroup(parentNode,
0750: elementGroup, element,
0751: openedPaths);
0752: if (openedPaths != null
0753: && openedPaths
0754: .contains(parentNode
0755: .getNodeId())) {
0756: this .expandPath(parentNode);
0757: }
0758: }
0759: } else {
0760: addElementToGroup(bandNode,
0761: elementGroup, element,
0762: openedPaths);
0763: }
0764:
0765: } catch (Exception ex) {
0766: ex.printStackTrace();
0767: }
0768: }
0769: }
0770: if (openedPaths != null
0771: && openedPaths.contains(bandNode.getNodeId())) {
0772: this .expandPath(bandNode);
0773: }
0774: }
0775: setDontHandleEvent(false);
0776: if (jrf.getSelectedCrosstabEditorPanel() != null) {
0777: this
0778: .reportElementsSelectionChanged(new ReportElementsSelectionEvent(
0779: jrf, getCrosstabReportElement(),
0780: jrf.getSelectedCrosstabEditorPanel()
0781: .getSelectedElements()));
0782: }
0783: }
0784:
0785: oldJReportFrame = jrf;
0786: setCrosstabReportElement(crosstabReportElement);
0787:
0788: jTreeDocument.updateUI();
0789: }
0790:
0791: public void addElementToGroup(DocumentStructureTreeNode parentNode,
0792: String subGroup, ReportElement element, List openedPaths) {
0793: // 1. We have to find the group node...
0794: if (subGroup.equals("")) {
0795: DocumentStructureTreeNode newNode = new DocumentStructureTreeNode(
0796: element);
0797: parentNode.add(newNode);
0798:
0799: if (openedPaths != null
0800: && openedPaths.contains(newNode.getNodeId())) {
0801: expandPath((DocumentStructureTreeNode) newNode);
0802: }
0803:
0804: return;
0805: }
0806: String levelGroupName = "";
0807: if (subGroup.indexOf(".") >= 0) {
0808: levelGroupName = subGroup.substring(0, subGroup
0809: .indexOf("."));
0810: subGroup = subGroup.substring(subGroup.indexOf(".") + 1);
0811: } else {
0812: levelGroupName = subGroup;
0813: subGroup = "";
0814: }
0815:
0816: // 2. Look for the node named levelGroupName
0817: for (int i = 0; i < parentNode.getChildCount(); ++i) {
0818: DocumentStructureTreeNode dmtn = (DocumentStructureTreeNode) parentNode
0819: .getChildAt(i);
0820: if (dmtn.getUserObject() != null
0821: && dmtn.getUserObject() instanceof ElementGroup) {
0822: ElementGroup ge = (ElementGroup) dmtn.getUserObject();
0823: if (ge.getName().equals(levelGroupName)) {
0824: addElementToGroup(dmtn, subGroup, element,
0825: openedPaths);
0826: return;
0827: }
0828: }
0829: }
0830:
0831: // Node doesn't exists....
0832: DocumentStructureTreeNode dmtn = new DocumentStructureTreeNode(
0833: new ElementGroup(levelGroupName));
0834: parentNode.add(dmtn);
0835: addElementToGroup(dmtn, subGroup, element, openedPaths);
0836:
0837: if (openedPaths != null
0838: && openedPaths.contains(dmtn.getNodeId())) {
0839: expandPath((DocumentStructureTreeNode) dmtn);
0840: }
0841:
0842: }
0843:
0844: public void expandPath(DocumentStructureTreeNode node) {
0845: //if (node.getChildCount() == 0) return;
0846: try {
0847: if (node.getParent() != null) {
0848: expandPath((DocumentStructureTreeNode) node.getParent());
0849: }
0850: jTreeDocument.expandPath(new TreePath(
0851: ((DocumentStructureTreeNode) node).getPath()));
0852: } catch (Exception ex) {
0853: ex.printStackTrace();
0854: }
0855: }
0856:
0857: public void crosstabLayoutChanged(CrosstabLayoutChangedEvent evt) {
0858:
0859: if (evt.getCrosstabReportElement() == getCrosstabReportElement()) {
0860:
0861: this .updateDocumentStructureTree(MainFrame
0862: .getMainInstance().getActiveReportFrame(),
0863: getCrosstabReportElement());
0864: }
0865: }
0866:
0867: public void reportElementsChanged(ReportElementChangedEvent evt) {
0868:
0869: if (evt.getType() == ReportElementChangedEvent.CHANGED) {
0870: for (int ir = 0; ir < evt.getElements().size(); ++ir) {
0871: ReportElement re = (ReportElement) evt.getElements()
0872: .elementAt(ir);
0873:
0874: DocumentStructureTreeNode node = findElementTreeNode(
0875: re, true);
0876: if (node == null) {
0877: // This node has changed your band....
0878: // Find the node based on objects availables....
0879: node = findElementTreeNode(re, false);
0880: if (node == null)
0881: return; // This should not happen....
0882: // Remove from your parent....
0883: node.removeFromParent();
0884: // Add to the new parent...
0885: DocumentStructureTreeNode root = (DocumentStructureTreeNode) jTreeDocument
0886: .getModel().getRoot();
0887: for (int i = 0; i < root.getChildCount(); ++i) {
0888: DocumentStructureTreeNode bandNode = (DocumentStructureTreeNode) root
0889: .getChildAt(i);
0890: if (bandNode.getUserObject() == re.band) {
0891: bandNode.add(node);
0892: setDontHandleEvent(true);
0893: if (evt.getJReportFrame()
0894: .getSelectedCrosstabEditorPanel()
0895: .getSelectedElements().contains(re)) {
0896: jTreeDocument
0897: .getSelectionModel()
0898: .addSelectionPath(
0899: new TreePath(
0900: new Object[] {
0901: root,
0902: bandNode,
0903: node }));
0904: }
0905: setDontHandleEvent(false);
0906: break;
0907: }
0908: }
0909: }
0910: }
0911: jTreeDocument.updateUI();
0912: } else if (evt.getType() == ReportElementChangedEvent.REMOVED) {
0913:
0914: updateDocumentStructureTree(MainFrame.getMainInstance()
0915: .getActiveReportFrame(), getCrosstabReportElement());
0916: jTreeDocument.updateUI();
0917: } else if (evt.getType() == ReportElementChangedEvent.ADDED) {
0918:
0919: updateDocumentStructureTree(MainFrame.getMainInstance()
0920: .getActiveReportFrame(), getCrosstabReportElement());
0921: jTreeDocument.updateUI();
0922: }
0923:
0924: }//end reportElementsChanged
0925:
0926: public void reportElementsSelectionChanged(
0927: ReportElementsSelectionEvent evt) {
0928:
0929: if (evt.getCrosstabReportElement() == null)
0930: return;
0931: setDontHandleEvent(true);
0932:
0933: TreePath[] path = jTreeDocument.getSelectionPaths();
0934: if (path != null
0935: && evt.getSelectedElements() != null
0936: && evt.getSelectedElements().size() == 0
0937: && (((DocumentStructureTreeNode) path[0]
0938: .getLastPathComponent()).getUserObject() instanceof CrosstabCell || ((DocumentStructureTreeNode) path[0]
0939: .getLastPathComponent()).getUserObject() instanceof ElementGroup)) {
0940: setDontHandleEvent(false);
0941: return;
0942: }
0943:
0944: jTreeDocument.getSelectionModel().clearSelection();
0945:
0946: java.util.Vector v = evt.getSelectedElements();
0947: Enumeration e = v.elements();
0948: while (e.hasMoreElements()) {
0949: ReportElement re = (ReportElement) e.nextElement();
0950: // looking for it in the tree...
0951: //1. Search the band...
0952: DocumentStructureTreeNode enode = findElementTreeNode(re,
0953: true);
0954: //TreePath treePath = findReportElement((DocumentStructureTreeNode)jTreeDocument.getModel().getRoot(), re);
0955: if (enode != null) {
0956: jTreeDocument.getSelectionModel().addSelectionPath(
0957: new TreePath(enode.getPath()));
0958: }
0959:
0960: }
0961:
0962: setDontHandleEvent(false);
0963:
0964: }
0965:
0966: /**
0967: * If searchInYourBandOnly == true
0968: * This method Look for the userObject re in elements contained in the same band
0969: * of the element. This method MUST CHECK that the report IS in the band
0970: * assigned. If not it must return null. The reason is that if the element
0971: * is not found in your band, it has changed the band attribute and must be updated.
0972: */
0973: public DocumentStructureTreeNode findElementTreeNode(
0974: ReportElement re, boolean searchInYourCellOnly) {
0975: // looking for it in the tree...
0976: //1. Search the band...
0977: DocumentStructureTreeNode root = (DocumentStructureTreeNode) jTreeDocument
0978: .getModel().getRoot();
0979: for (int i = 0; i < root.getChildCount(); ++i) {
0980: DocumentStructureTreeNode cellNode = (DocumentStructureTreeNode) root
0981: .getChildAt(i);
0982: if (!searchInYourCellOnly
0983: || cellNode.getUserObject() == re.getCell()) {
0984:
0985: DocumentStructureTreeNode newchild = findElementInTreeNode(
0986: cellNode, re);
0987: if (newchild != null)
0988: return newchild;
0989: }
0990: }
0991: return null;
0992: }
0993:
0994: public DocumentStructureTreeNode findElementInTreeNode(
0995: DocumentStructureTreeNode parentNode, ReportElement re) {
0996: for (int k = 0; k < parentNode.getChildCount(); ++k) {
0997: DocumentStructureTreeNode child = (DocumentStructureTreeNode) parentNode
0998: .getChildAt(k);
0999: if (child.getUserObject() == re) {
1000: return child;
1001: }
1002: if (!re.getElementGroup().equals("")
1003: || re.getParentElement() != null) {
1004: if (child.getUserObject() instanceof ElementGroup
1005: || child.getUserObject() instanceof FrameReportElement) {
1006: DocumentStructureTreeNode newchild = findElementInTreeNode(
1007: child, re);
1008: if (newchild != null)
1009: return newchild;
1010: }
1011: }
1012: }
1013: return null;
1014: }
1015:
1016: public boolean isDontHandleEvent() {
1017: return dontHandleEvent;
1018: }
1019:
1020: public void setDontHandleEvent(boolean dontHandleEvent) {
1021: this .dontHandleEvent = dontHandleEvent;
1022: }
1023:
1024: public void applyI18n() {
1025: // Start autogenerated code ----------------------
1026: jMenuItemDSB_crosstabProperties
1027: .setText(I18n
1028: .getString(
1029: "crosstabStructurePanel.menuItemDSB_crosstabProperties",
1030: "Crosstab properties"));
1031: jMenuItemDSB_crosstabProperties1
1032: .setText(I18n
1033: .getString(
1034: "crosstabStructurePanel.menuItemDSB_crosstabProperties1",
1035: "Crosstab properties"));
1036: jMenuItemUngroup.setText(I18n.getString(
1037: "crosstabStructurePanel.menuItemUngroup", "Ungroup"));
1038: // End autogenerated code ----------------------
1039: jMenuItemDS_cut.setText(it.businesslogic.ireport.util.I18n
1040: .getString("cut", "Cut"));
1041: jMenuItemDS_copy.setText(it.businesslogic.ireport.util.I18n
1042: .getString("copy", "Copy"));
1043: jMenuItemDS_paste.setText(it.businesslogic.ireport.util.I18n
1044: .getString("paste", "Paste"));
1045: jMenuItemDS_delete.setText(it.businesslogic.ireport.util.I18n
1046: .getString("delete", "Delete"));
1047: jMenuItemMoveUp.setText(it.businesslogic.ireport.util.I18n
1048: .getString("moveUp", "Move up"));
1049: jMenuItemMoveDown.setText(it.businesslogic.ireport.util.I18n
1050: .getString("moveDown", "Move down"));
1051: jMenuItemDSB_properties
1052: .setText(it.businesslogic.ireport.util.I18n.getString(
1053: "cellProperties", "Cell properties"));
1054: jMenuItemDS_properties
1055: .setText(it.businesslogic.ireport.util.I18n.getString(
1056: "properties", "Properties"));
1057: jMenuItemDS_cut.setText(it.businesslogic.ireport.util.I18n
1058: .getString("cut", "Cut"));
1059: jMenuItemDS_copy.setText(it.businesslogic.ireport.util.I18n
1060: .getString("copy", "Copy"));
1061: jMenuItemDS_paste.setText(it.businesslogic.ireport.util.I18n
1062: .getString("paste", "Paste"));
1063: jMenuItemDS_delete.setText(it.businesslogic.ireport.util.I18n
1064: .getString("delete", "Delete"));
1065: jMenuItemMoveUp.setText(it.businesslogic.ireport.util.I18n
1066: .getString("moveUp", "Move up"));
1067: jMenuItemMoveDown.setText(it.businesslogic.ireport.util.I18n
1068: .getString("moveDown", "Move down"));
1069: jMenuItemDSB_properties
1070: .setText(it.businesslogic.ireport.util.I18n.getString(
1071: "cellProperties", "Cell properties"));
1072: jMenuItemDS_properties
1073: .setText(it.businesslogic.ireport.util.I18n.getString(
1074: "properties", "Properties"));
1075:
1076: ((DefaultMutableTreeNode) this .jTreeDocument.getModel()
1077: .getRoot())
1078: .setUserObject(it.businesslogic.ireport.util.I18n
1079: .getString("crossTab", "Crosstab"));
1080:
1081: }
1082:
1083: public void subDatasetObjectChanged(SubDatasetObjectChangedEvent evt) {
1084:
1085: }
1086:
1087: public void setCutCopyEnabled(boolean enabled) {
1088: this .jMenuItemDS_cut.setEnabled(enabled);
1089: this .jMenuItemDS_copy.setEnabled(enabled);
1090: this .jMenuItemDS_delete.setEnabled(enabled);
1091: }
1092:
1093: public void setPasteEnebled(boolean enabled) {
1094: this .jMenuItemDS_paste.setEnabled(enabled);
1095: }
1096:
1097: public CrosstabReportElement getCrosstabReportElement() {
1098: return crosstabReportElement;
1099: }
1100:
1101: public void setCrosstabReportElement(
1102: CrosstabReportElement crosstabReportElement) {
1103: this .crosstabReportElement = crosstabReportElement;
1104: }
1105:
1106: public void reportBandChanged(ReportBandChangedEvent evt) {
1107: }
1108:
1109: public void crosstabMeasureChanged(CrosstabLayoutChangedEvent evt) {
1110: }
1111:
1112: public void reportBandsSelectionChanged(
1113: ReportBandsSelectionEvent evt) {
1114: }
1115:
1116: public void reportObjectsSelectionChanged(
1117: ReportObjectsSelectionEvent evt) {
1118: }
1119:
1120: }
|