KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > businesslogic > ireport > gui > documentstructure > CrosstabStructurePanel


1 /*
2  * Copyright (C) 2005 - 2006 JasperSoft Corporation. All rights reserved.
3  * http://www.jaspersoft.com.
4  *
5  * Unless you have purchased a commercial license agreement from JasperSoft,
6  * the following license terms apply:
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  *
12  * This program is distributed WITHOUT ANY WARRANTY; and without the
13  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
18  * or write to:
19  *
20  * Free Software Foundation, Inc.,
21  * 59 Temple Place - Suite 330,
22  * Boston, MA USA 02111-1307
23  *
24  *
25  *
26  *
27  * CrosstabStructurePanel.java
28  *
29  * Created on 25 novembre 2005, 11.34
30  *
31  */

32
33 package it.businesslogic.ireport.gui.documentstructure;
34 import it.businesslogic.ireport.ElementGroup;
35 import it.businesslogic.ireport.gui.MainFrame;
36 import it.businesslogic.ireport.gui.event.CrosstabLayoutChangedListener;
37 import it.businesslogic.ireport.gui.event.ReportListener;
38 import it.businesslogic.ireport.gui.event.SubDatasetObjectChangedListener;
39 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
40 import javax.swing.tree.DefaultTreeSelectionModel JavaDoc;
41 import it.businesslogic.ireport.gui.*;
42 import it.businesslogic.ireport.*;
43 import it.businesslogic.ireport.crosstab.CrosstabCell;
44 import it.businesslogic.ireport.crosstab.gui.CrosstabEditorPanel;
45 import it.businesslogic.ireport.gui.docking.GenericDragTargetListener;
46 import it.businesslogic.ireport.gui.event.*;
47 import java.util.Enumeration JavaDoc;
48 import java.util.Vector JavaDoc;
49 import javax.swing.tree.TreePath JavaDoc;
50 import it.businesslogic.ireport.undo.UnGroupEmentsOperation;
51 import java.awt.dnd.DropTarget JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.List JavaDoc;
54 import it.businesslogic.ireport.util.I18n;
55 /**
56  *
57  * @author Administrator
58  */

59 public class CrosstabStructurePanel extends javax.swing.JPanel JavaDoc implements ReportListener, SubDatasetObjectChangedListener, CrosstabLayoutChangedListener {
60     
61     private boolean dontHandleEvent = false;
62     
63     private JReportFrame oldJReportFrame = null;
64     private CrosstabReportElement crosstabReportElement = null;
65     private CrosstabEditorPanel oldCrosstabEditorPanel = null;
66             
67     /** Creates new form DocumentStructurePanel */
68     public CrosstabStructurePanel() {
69         initComponents();
70         
71         DefaultTreeSelectionModel JavaDoc dtsm = (DefaultTreeSelectionModel JavaDoc)jTreeDocument.getSelectionModel();
72         dtsm.setSelectionMode( DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION );
73         
74         DocumentStructureTreeNode rootDoc = new DocumentStructureTreeNode(it.businesslogic.ireport.util.I18n.getString("crossTab","Crosstab"));
75         javax.swing.tree.DefaultTreeModel JavaDoc modelDoc = new javax.swing.tree.DefaultTreeModel JavaDoc(rootDoc);
76         
77         jTreeDocument.setDropTarget(new DropTarget JavaDoc(this, new GenericDragTargetListener()));
78         
79         jTreeDocument.setModel( modelDoc );
80         jTreeDocument.setCellRenderer( new DocumentStructureTreeCellRenderer());
81         
82     }
83     
84     /** This method is called from within the constructor to
85      * initialize the form.
86      * WARNING: Do NOT modify this code. The content of this method is
87      * always regenerated by the Form Editor.
88      */

89     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
90
private void initComponents() {
91         jPopupMenuDocumentStructure = new javax.swing.JPopupMenu JavaDoc();
92         jMenuItemDS_properties = new javax.swing.JMenuItem JavaDoc();
93         jMenuItemDSB_crosstabProperties1 = new javax.swing.JMenuItem JavaDoc();
94         jSeparator22 = new javax.swing.JSeparator JavaDoc();
95         jMenuItemMoveUp = new javax.swing.JMenuItem JavaDoc();
96         jMenuItemMoveDown = new javax.swing.JMenuItem JavaDoc();
97         jSeparator23 = new javax.swing.JSeparator JavaDoc();
98         jMenuItemDS_cut = new javax.swing.JMenuItem JavaDoc();
99         jMenuItemDS_copy = new javax.swing.JMenuItem JavaDoc();
100         jMenuItemDS_paste = new javax.swing.JMenuItem JavaDoc();
101         jMenuItemDS_delete = new javax.swing.JMenuItem JavaDoc();
102         jPopupMenuDocumentStructureCell = new javax.swing.JPopupMenu JavaDoc();
103         jMenuItemDSB_crosstabProperties = new javax.swing.JMenuItem JavaDoc();
104         jMenuItemDSB_properties = new javax.swing.JMenuItem JavaDoc();
105         jPopupMenuDocumentStructureGroup = new javax.swing.JPopupMenu JavaDoc();
106         jMenuItemUngroup = new javax.swing.JMenuItem JavaDoc();
107         jScrollPane2 = new javax.swing.JScrollPane JavaDoc();
108         jTreeDocument = new javax.swing.JTree JavaDoc();
109
110         jMenuItemDS_properties.setText("Properties");
111         jMenuItemDS_properties.addActionListener(new java.awt.event.ActionListener JavaDoc() {
112             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
113                 jMenuItemDS_propertiesActionPerformed(evt);
114             }
115         });
116
117         jPopupMenuDocumentStructure.add(jMenuItemDS_properties);
118
119         jMenuItemDSB_crosstabProperties1.setText("Crosstab properties");
120         jMenuItemDSB_crosstabProperties1.addActionListener(new java.awt.event.ActionListener JavaDoc() {
121             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
122                 jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1(evt);
123             }
124         });
125
126         jPopupMenuDocumentStructure.add(jMenuItemDSB_crosstabProperties1);
127
128         jPopupMenuDocumentStructure.add(jSeparator22);
129
130         jMenuItemMoveUp.setText("Move up");
131         jMenuItemMoveUp.setEnabled(false);
132         jMenuItemMoveUp.addActionListener(new java.awt.event.ActionListener JavaDoc() {
133             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
134                 jMenuItemMoveUpActionPerformed(evt);
135             }
136         });
137
138         jPopupMenuDocumentStructure.add(jMenuItemMoveUp);
139
140         jMenuItemMoveDown.setText("Move down");
141         jMenuItemMoveDown.setEnabled(false);
142         jMenuItemMoveDown.addActionListener(new java.awt.event.ActionListener JavaDoc() {
143             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
144                 jMenuItemMoveDownActionPerformed(evt);
145             }
146         });
147
148         jPopupMenuDocumentStructure.add(jMenuItemMoveDown);
149
150         jPopupMenuDocumentStructure.add(jSeparator23);
151
152         jMenuItemDS_cut.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/menu/cut.png")));
153         jMenuItemDS_cut.setText("Cut");
154         jMenuItemDS_cut.setEnabled(false);
155         jMenuItemDS_cut.addActionListener(new java.awt.event.ActionListener JavaDoc() {
156             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
157                 jMenuItemDS_cutActionPerformed(evt);
158             }
159         });
160
161         jPopupMenuDocumentStructure.add(jMenuItemDS_cut);
162
163         jMenuItemDS_copy.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/menu/copy.png")));
164         jMenuItemDS_copy.setText("Copy");
165         jMenuItemDS_copy.setEnabled(false);
166         jMenuItemDS_copy.addActionListener(new java.awt.event.ActionListener JavaDoc() {
167             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
168                 jMenuItemDS_copyActionPerformed(evt);
169             }
170         });
171
172         jPopupMenuDocumentStructure.add(jMenuItemDS_copy);
173
174         jMenuItemDS_paste.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/menu/paste.png")));
175         jMenuItemDS_paste.setText("Paste");
176         jMenuItemDS_paste.setEnabled(false);
177         jMenuItemDS_paste.addActionListener(new java.awt.event.ActionListener JavaDoc() {
178             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
179                 jMenuItemDS_pasteActionPerformed(evt);
180             }
181         });
182
183         jPopupMenuDocumentStructure.add(jMenuItemDS_paste);
184
185         jMenuItemDS_delete.setIcon(new javax.swing.ImageIcon JavaDoc(getClass().getResource("/it/businesslogic/ireport/icons/menu/delete.png")));
186         jMenuItemDS_delete.setText("Delete");
187         jMenuItemDS_delete.setEnabled(false);
188         jMenuItemDS_delete.addActionListener(new java.awt.event.ActionListener JavaDoc() {
189             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
190                 jMenuItemDS_deleteActionPerformed(evt);
191             }
192         });
193
194         jPopupMenuDocumentStructure.add(jMenuItemDS_delete);
195
196         jMenuItemDSB_crosstabProperties.setText("Crosstab properties");
197         jMenuItemDSB_crosstabProperties.addActionListener(new java.awt.event.ActionListener JavaDoc() {
198             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
199                 jMenuItemDSB_propertiesActionPerformed1(evt);
200             }
201         });
202
203         jPopupMenuDocumentStructureCell.add(jMenuItemDSB_crosstabProperties);
204
205         jMenuItemDSB_properties.setText("Cell properties");
206         jMenuItemDSB_properties.addActionListener(new java.awt.event.ActionListener JavaDoc() {
207             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
208                 jMenuItemDSB_propertiesActionPerformed(evt);
209             }
210         });
211
212         jPopupMenuDocumentStructureCell.add(jMenuItemDSB_properties);
213
214         jMenuItemUngroup.setText("Ungroup");
215         jMenuItemUngroup.addActionListener(new java.awt.event.ActionListener JavaDoc() {
216             public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
217                 jMenuItemUngroupActionPerformed(evt);
218             }
219         });
220
221         jPopupMenuDocumentStructureGroup.add(jMenuItemUngroup);
222
223         setLayout(new java.awt.BorderLayout JavaDoc());
224
225         jTreeDocument.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener JavaDoc() {
226             public void treeCollapsed(javax.swing.event.TreeExpansionEvent JavaDoc evt) {
227                 jTreeDocumentTreeCollapsed(evt);
228             }
229             public void treeExpanded(javax.swing.event.TreeExpansionEvent JavaDoc evt) {
230                 jTreeDocumentTreeExpanded(evt);
231             }
232         });
233         jTreeDocument.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener JavaDoc() {
234             public void valueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt) {
235                 jTreeDocumentValueChanged(evt);
236             }
237         });
238         jTreeDocument.addMouseListener(new java.awt.event.MouseAdapter JavaDoc() {
239             public void mousePressed(java.awt.event.MouseEvent JavaDoc evt) {
240                 jTreeDocumentMousePressed(evt);
241             }
242         });
243
244         jScrollPane2.setViewportView(jTreeDocument);
245
246         add(jScrollPane2, java.awt.BorderLayout.CENTER);
247
248     }// </editor-fold>//GEN-END:initComponents
249

250     private void jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1
251
it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog cpd = new it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog(MainFrame.getMainInstance(),true);
252         cpd.setCurrentCrosstabReportElement( getCrosstabReportElement() );
253         cpd.setVisible(true);
254     }//GEN-LAST:event_jMenuItemDSB_crosstabPropertiesjMenuItemDSB_propertiesActionPerformed1
255

256     private void jMenuItemDSB_propertiesActionPerformed1(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDSB_propertiesActionPerformed1
257

258         it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog cpd = new it.businesslogic.ireport.crosstab.gui.CrosstabPropertiesDialog(MainFrame.getMainInstance(),true);
259         cpd.setCurrentCrosstabReportElement( getCrosstabReportElement() );
260         cpd.setVisible(true);
261
262     }//GEN-LAST:event_jMenuItemDSB_propertiesActionPerformed1
263

264     private void jMenuItemUngroupActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemUngroupActionPerformed
265

266         if (jTreeDocument.getSelectionCount() > 0) {
267             DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode)jTreeDocument.getSelectionPath().getLastPathComponent();
268             if (elementNode.getUserObject() instanceof ElementGroup) {
269                 String JavaDoc elementGroupName = ((ElementGroup)elementNode.getUserObject()).getName();
270                 while ( ((DocumentStructureTreeNode)elementNode.getParent()).getUserObject() instanceof ElementGroup)
271                 {
272                     elementNode = (DocumentStructureTreeNode)elementNode.getParent();
273                     elementGroupName = ((ElementGroup)elementNode.getUserObject()).getName() + "." + elementGroupName;
274                 }
275                 
276                 String JavaDoc newElementGroupName = elementGroupName;
277                 if (newElementGroupName.lastIndexOf(".")>=0)
278                 {
279                     newElementGroupName = newElementGroupName.substring(0,newElementGroupName.lastIndexOf("."));
280                 }
281                 else
282                 {
283                     newElementGroupName = "";
284                 }
285                 
286                 /************************/
287                 UnGroupEmentsOperation undoOp = new UnGroupEmentsOperation(MainFrame.getMainInstance().getActiveReportFrame());
288                 Vector JavaDoc elements = getCrosstabReportElement().getElements();
289                 for (int i=0; i<elements.size(); ++i)
290                 {
291                     ReportElement element = (ReportElement)elements.elementAt(i);
292                     String JavaDoc oldElementGroupName = element.getElementGroup();
293                     if (element.getElementGroup().startsWith(elementGroupName+".") || element.getElementGroup().equals(elementGroupName))
294                     {
295                          String JavaDoc tmpElementGroupName = element.getElementGroup().substring(elementGroupName.length());
296                          if (tmpElementGroupName.length() == 0) tmpElementGroupName = newElementGroupName;
297                          tmpElementGroupName = newElementGroupName + tmpElementGroupName;
298                          if (tmpElementGroupName.startsWith(".")) tmpElementGroupName = tmpElementGroupName.substring(1);
299                          
300                         element.setElementGroup(tmpElementGroupName);
301                         undoOp.addElement(element, i,i, oldElementGroupName, tmpElementGroupName);
302                     }
303                 }
304                         
305                 MainFrame.getMainInstance().getActiveReportFrame().addUndoOperation(undoOp);
306                 
307                 MainFrame.getMainInstance().getActiveReportFrame().fireReportListenerReportElementsChanged(new ReportElementChangedEvent(MainFrame.getMainInstance().getActiveReportFrame(), new Vector JavaDoc() , ReportElementChangedEvent.REMOVED ));
308             }
309         }
310         // Get group name...
311

312         
313     }//GEN-LAST:event_jMenuItemUngroupActionPerformed
314

315     private void jTreeDocumentTreeCollapsed(javax.swing.event.TreeExpansionEvent JavaDoc evt) {//GEN-FIRST:event_jTreeDocumentTreeCollapsed
316
//System.out.println("collapsing..." + evt.getPath());
317
}//GEN-LAST:event_jTreeDocumentTreeCollapsed
318

319     private void jTreeDocumentTreeExpanded(javax.swing.event.TreeExpansionEvent JavaDoc evt) {//GEN-FIRST:event_jTreeDocumentTreeExpanded
320
// System.out.println("expanding..." + evt.getPath());
321

322         
323     }//GEN-LAST:event_jTreeDocumentTreeExpanded
324

325     private void jMenuItemDSB_propertiesActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDSB_propertiesActionPerformed
326
if (jTreeDocument.getSelectionCount() > 0) {
327             DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode)jTreeDocument.getSelectionPath().getLastPathComponent();
328             if (elementNode.getUserObject() instanceof CrosstabCell) {
329                 CrosstabCell thisCell = ((CrosstabCell)elementNode.getUserObject());
330                 MainFrame.getMainInstance().getActiveReportFrame().getSelectedCrosstabEditorPanel().editCell(thisCell);
331             }
332         }
333     }//GEN-LAST:event_jMenuItemDSB_propertiesActionPerformed
334

335     private void jMenuItemDS_deleteActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_deleteActionPerformed
336
MainFrame.getMainInstance().getActiveReportFrame().getSelectedCrosstabEditorPanel().deleteSelectedElements();
337     }//GEN-LAST:event_jMenuItemDS_deleteActionPerformed
338

339     private void jMenuItemDS_pasteActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_pasteActionPerformed
340
MainFrame.getMainInstance().getActiveReportFrame().paste();
341     }//GEN-LAST:event_jMenuItemDS_pasteActionPerformed
342

343     private void jMenuItemDS_copyActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_copyActionPerformed
344
MainFrame.getMainInstance().getActiveReportFrame().copy();
345     }//GEN-LAST:event_jMenuItemDS_copyActionPerformed
346

347     private void jMenuItemDS_cutActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_cutActionPerformed
348
MainFrame.getMainInstance().getActiveReportFrame().cut();
349     }//GEN-LAST:event_jMenuItemDS_cutActionPerformed
350

351     private void jMenuItemMoveDownActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemMoveDownActionPerformed
352
if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
353             JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
354             jrf.moveDown();
355                        
356             if (jrf.getSelectedCrosstabEditorPanel().getSelectedElements().size()>0)
357                 updateDocumentStructureTree(jrf, getCrosstabReportElement());
358             
359         }
360     }//GEN-LAST:event_jMenuItemMoveDownActionPerformed
361

362     private void jMenuItemMoveUpActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemMoveUpActionPerformed
363
if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
364          JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
365          jrf.moveUp();
366          this.jTreeDocument.updateUI();
367
368          if (jrf.getSelectedCrosstabEditorPanel().getSelectedElements().size()>0)
369              updateDocumentStructureTree(jrf, getCrosstabReportElement());
370         }
371         
372     }//GEN-LAST:event_jMenuItemMoveUpActionPerformed
373

374     private void jMenuItemDS_propertiesActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_propertiesActionPerformed
375
MainFrame.getMainInstance().getElementPropertiesDialog().setVisible(true);
376       //MainFrame.getMainInstance().getElementPropertiesDialog().updateSelection();
377
}//GEN-LAST:event_jMenuItemDS_propertiesActionPerformed
378

379     private void jTreeDocumentValueChanged(javax.swing.event.TreeSelectionEvent JavaDoc evt) {//GEN-FIRST:event_jTreeDocumentValueChanged
380
if (isDontHandleEvent()) return;
381  
382                 // Get active jrf...
383
if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
384                     //dontHandleEvent = true;
385
try {
386                         JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
387                         Vector JavaDoc elementsToSelect = new Vector JavaDoc();
388
389                         TreePath JavaDoc[] path = jTreeDocument.getSelectionPaths();
390
391
392                         for (int i=0; i<path.length; ++i) {
393                             DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode)path[i].getLastPathComponent();
394                             if (elementNode.getUserObject() instanceof ReportElement) {
395                                 elementsToSelect.addElement( (ReportElement)elementNode.getUserObject());
396                             }
397                             else if (path.length == 1) {
398                                 jrf.getSelectedCrosstabEditorPanel().setSelectedElement(null);
399                                 return;
400                             }
401                             else if (elementNode.getUserObject() instanceof ElementGroup)
402                             {
403                                 
404                                 jrf.getSelectedCrosstabEditorPanel().setSelectedElement(null);
405                                 return;
406                             }
407                         }
408
409                         jrf.getSelectedCrosstabEditorPanel().setSelectedElement(null);
410                         Enumeration JavaDoc e = elementsToSelect.elements();
411                         while (e.hasMoreElements()) {
412                             jrf.getSelectedCrosstabEditorPanel().addSelectedElement((ReportElement)e.nextElement(), false);
413                         }
414                         jrf.getSelectedCrosstabEditorPanel().fireSelectionChangedEvent();
415                     } catch (Exception JavaDoc ex)
416                     {}
417                     //
418
}
419     }//GEN-LAST:event_jTreeDocumentValueChanged
420

421     private void jTreeDocumentMousePressed(java.awt.event.MouseEvent JavaDoc evt) {//GEN-FIRST:event_jTreeDocumentMousePressed
422
if (evt.getButton() == evt.BUTTON3) {
423             if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
424                 JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
425                 jTreeDocument.setSelectionPath( jTreeDocument.getPathForLocation(evt.getX(), evt.getY() ) );
426                 if (jTreeDocument.getSelectionCount() > 0) {
427                     DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode)jTreeDocument.getSelectionPath().getLastPathComponent();
428                     if (elementNode.getUserObject() instanceof ReportElement) {
429                         // (ReportElement)elementNode.getUserObject());
430
this.jPopupMenuDocumentStructure.show(jTreeDocument, evt.getPoint().x, evt.getPoint().y);
431                         
432                     } else if (elementNode.getUserObject() instanceof CrosstabCell) {
433                         // (ReportElement)elementNode.getUserObject());
434
this.jPopupMenuDocumentStructureCell.show(jTreeDocument, evt.getPoint().x, evt.getPoint().y);
435
436                     } else if (elementNode.getUserObject() instanceof ElementGroup) {
437                         // (ReportElement)elementNode.getUserObject());
438
this.jPopupMenuDocumentStructureGroup.show(jTreeDocument, evt.getPoint().x, evt.getPoint().y);
439                         
440                     }
441                 }
442             }
443             
444         }
445         else if (evt.getButton() == evt.BUTTON1 && evt.getClickCount() == 2) {
446           
447             if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
448                       
449                 JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
450                 if (jrf.getSelectedCrosstabEditorPanel().getSelectedElements().size() > 0) {
451                     MainFrame.getMainInstance().getElementPropertiesDialog().setVisible(true);
452                     MainFrame.getMainInstance().getElementPropertiesDialog().updateSelection();
453                 }
454             }
455         }
456     }//GEN-LAST:event_jTreeDocumentMousePressed
457

458     public void printSelectedPaths(String JavaDoc prefix)
459     {
460         try {
461                        Enumeration JavaDoc enum_extended_paths = jTreeDocument.getExpandedDescendants(new TreePath JavaDoc(new Object JavaDoc[]{ jTreeDocument.getModel().getRoot() }));
462                        if (enum_extended_paths != null)
463                        {
464                                   while (enum_extended_paths.hasMoreElements())
465                                   {
466                                    TreePath JavaDoc path = (TreePath JavaDoc)enum_extended_paths.nextElement();
467                                    //System.out.println(prefix+ " "+path);
468
}
469                        }
470                 } catch (Exception JavaDoc ex)
471                 {
472                     ex.printStackTrace();
473                 }
474     }
475     
476     // Variables declaration - do not modify//GEN-BEGIN:variables
477
private javax.swing.JMenuItem JavaDoc jMenuItemDSB_crosstabProperties;
478     private javax.swing.JMenuItem JavaDoc jMenuItemDSB_crosstabProperties1;
479     private javax.swing.JMenuItem JavaDoc jMenuItemDSB_properties;
480     private javax.swing.JMenuItem JavaDoc jMenuItemDS_copy;
481     private javax.swing.JMenuItem JavaDoc jMenuItemDS_cut;
482     private javax.swing.JMenuItem JavaDoc jMenuItemDS_delete;
483     private javax.swing.JMenuItem JavaDoc jMenuItemDS_paste;
484     private javax.swing.JMenuItem JavaDoc jMenuItemDS_properties;
485     private javax.swing.JMenuItem JavaDoc jMenuItemMoveDown;
486     private javax.swing.JMenuItem JavaDoc jMenuItemMoveUp;
487     private javax.swing.JMenuItem JavaDoc jMenuItemUngroup;
488     private javax.swing.JPopupMenu JavaDoc jPopupMenuDocumentStructure;
489     private javax.swing.JPopupMenu JavaDoc jPopupMenuDocumentStructureCell;
490     private javax.swing.JPopupMenu JavaDoc jPopupMenuDocumentStructureGroup;
491     private javax.swing.JScrollPane JavaDoc jScrollPane2;
492     private javax.swing.JSeparator JavaDoc jSeparator22;
493     private javax.swing.JSeparator JavaDoc jSeparator23;
494     private javax.swing.JTree JavaDoc jTreeDocument;
495     // End of variables declaration//GEN-END:variables
496

497     
498     
499     public void saveSelection()
500     {
501         List JavaDoc openedPaths = null;
502         try {
503             
504                 openedPaths = new ArrayList JavaDoc();
505                 Enumeration JavaDoc enum_extended_paths = jTreeDocument.getExpandedDescendants(new TreePath JavaDoc(new Object JavaDoc[]{ jTreeDocument.getModel().getRoot() }));
506                 if (enum_extended_paths!=null)
507                 {
508                     while (enum_extended_paths.hasMoreElements())
509                     {
510                         TreePath JavaDoc path = (TreePath JavaDoc)enum_extended_paths.nextElement();
511                         openedPaths.add( ((DocumentStructureTreeNode)path.getLastPathComponent()).getNodeId() );
512                     }
513                 }
514            } catch (Exception JavaDoc ex)
515            {
516                 ex.printStackTrace();
517            }
518
519         if (oldCrosstabEditorPanel != null) oldCrosstabEditorPanel.setOpenedNodesDocumentStructure( openedPaths );
520     }
521     
522     
523     public void updateDocumentStructureTree(JReportFrame jrf, CrosstabReportElement crosstabReportElement)
524     {
525         
526         
527         List JavaDoc openedPaths = null;
528         
529         try {
530             
531                 openedPaths = new ArrayList JavaDoc();
532                 Enumeration JavaDoc enum_extended_paths = jTreeDocument.getExpandedDescendants(new TreePath JavaDoc(new Object JavaDoc[]{ jTreeDocument.getModel().getRoot() }));
533                 if (enum_extended_paths!=null)
534                 {
535                     while (enum_extended_paths.hasMoreElements())
536                     {
537                         TreePath JavaDoc path = (TreePath JavaDoc)enum_extended_paths.nextElement();
538                         openedPaths.add( ((DocumentStructureTreeNode)path.getLastPathComponent()).getNodeId() );
539                     }
540                 }
541            } catch (Exception JavaDoc ex)
542            {
543                 ex.printStackTrace();
544            }
545         
546         if (jrf != null && jrf.getSelectedCrosstabEditorPanel() != oldCrosstabEditorPanel)
547         {
548             if (oldCrosstabEditorPanel != null) oldCrosstabEditorPanel.setOpenedNodesDocumentStructure( openedPaths );
549             if (jrf != null && jrf.getSelectedCrosstabEditorPanel() != null)
550             {
551                 openedPaths = jrf.getSelectedCrosstabEditorPanel().getOpenedNodesDocumentStructure();
552                 oldCrosstabEditorPanel = jrf.getSelectedCrosstabEditorPanel();
553             }
554             else
555             {
556                 oldCrosstabEditorPanel = null;
557             }
558              
559         }
560         
561         if (getCrosstabReportElement() != null && getCrosstabReportElement() != crosstabReportElement)
562         {
563             getCrosstabReportElement().removeCrosstabLayoutChangedListener(this);
564         }
565         
566         this.setCrosstabReportElement( crosstabReportElement );
567         
568         if (getCrosstabReportElement() != null)
569         {
570             this.getCrosstabReportElement().addCrosstabLayoutChangedListener( this );
571         }
572                 
573         setDontHandleEvent(true);
574         // Update the document tree structure...
575
((DocumentStructureTreeNode)this.jTreeDocument.getModel().getRoot()).removeAllChildren();
576         ((javax.swing.tree.DefaultTreeModel JavaDoc)(this.jTreeDocument.getModel())).reload();
577         
578         if (jrf != null && getCrosstabReportElement() != null)
579         {
580             
581             
582             Enumeration JavaDoc cells = getCrosstabReportElement().getCells().elements();
583             while (cells.hasMoreElements())
584             {
585                 CrosstabCell cell = (CrosstabCell)cells.nextElement();
586                 DocumentStructureTreeNode bandNode = new DocumentStructureTreeNode(cell);
587                 ((DocumentStructureTreeNode)this.jTreeDocument.getModel().getRoot()).add( bandNode);
588                 
589                 Enumeration JavaDoc elements = getCrosstabReportElement().getElements().elements();
590                 while (elements.hasMoreElements())
591                 {
592                     ReportElement element = (ReportElement)elements.nextElement();
593                     if (element.getCell() == cell)
594                     {
595                         String JavaDoc elementGroup = element.getElementGroup();
596                         try {
597                             if (element.getParentElement() != null)
598                             {
599                                 DocumentStructureTreeNode parentNode = findElementTreeNode(element.getParentElement(),true);
600                                 if (parentNode != null)
601                                 {
602                                     addElementToGroup(parentNode, elementGroup, element, openedPaths);
603                                     if (openedPaths != null && openedPaths.contains(parentNode.getNodeId()))
604                                     {
605                                         this.expandPath(parentNode);
606                                     }
607                                 }
608                             }
609                             else
610                             {
611                                 addElementToGroup(bandNode, elementGroup, element, openedPaths);
612                             }
613                             
614                         } catch (Exception JavaDoc ex)
615                         {
616                             ex.printStackTrace();
617                         }
618                     }
619                 }
620                 if (openedPaths != null && openedPaths.contains(bandNode.getNodeId()))
621                 {
622                     this.expandPath(bandNode);
623                 }
624             }
625             setDontHandleEvent(false);
626             if (jrf.getSelectedCrosstabEditorPanel() != null)
627             {
628                 this.reportElementsSelectionChanged(new ReportElementsSelectionEvent(jrf, getCrosstabReportElement(), jrf.getSelectedCrosstabEditorPanel().getSelectedElements()));
629             }
630         }
631
632         oldJReportFrame = jrf;
633         setCrosstabReportElement( crosstabReportElement);
634         
635         jTreeDocument.updateUI();
636     }
637     
638     public void addElementToGroup(DocumentStructureTreeNode parentNode, String JavaDoc subGroup, ReportElement element, List JavaDoc openedPaths)
639     {
640         // 1. We have to find the group node...
641
if (subGroup.equals(""))
642         {
643             DocumentStructureTreeNode newNode = new DocumentStructureTreeNode(element);
644             parentNode.add( newNode);
645             
646             if (openedPaths != null && openedPaths.contains(newNode.getNodeId()))
647             {
648                 expandPath( (DocumentStructureTreeNode)newNode);
649             }
650             
651             return;
652         }
653         String JavaDoc levelGroupName = "";
654         if (subGroup.indexOf(".")>=0)
655         {
656             levelGroupName = subGroup.substring(0, subGroup.indexOf("."));
657             subGroup = subGroup.substring(subGroup.indexOf(".")+1);
658         }
659         else
660         {
661             levelGroupName = subGroup;
662             subGroup = "";
663         }
664         
665         // 2. Look for the node named levelGroupName
666
for (int i =0; i<parentNode.getChildCount(); ++i)
667         {
668             DocumentStructureTreeNode dmtn = (DocumentStructureTreeNode)parentNode.getChildAt(i);
669             if (dmtn.getUserObject() != null && dmtn.getUserObject() instanceof ElementGroup)
670             {
671                 ElementGroup ge = (ElementGroup)dmtn.getUserObject();
672                 if (ge.getName().equals( levelGroupName ))
673                 {
674                     addElementToGroup(dmtn, subGroup, element, openedPaths);
675                     return;
676                 }
677             }
678         }
679         
680         // Node doesn't exists....
681
DocumentStructureTreeNode dmtn = new DocumentStructureTreeNode(new ElementGroup(levelGroupName));
682         parentNode.add( dmtn );
683         addElementToGroup(dmtn, subGroup, element, openedPaths);
684         
685         if (openedPaths != null && openedPaths.contains(dmtn.getNodeId()))
686         {
687             expandPath( (DocumentStructureTreeNode)dmtn);
688         }
689         
690         
691     }
692     
693     public void expandPath(DocumentStructureTreeNode node)
694     {
695        //if (node.getChildCount() == 0) return;
696
try {
697             if (node.getParent() != null)
698             {
699                 expandPath((DocumentStructureTreeNode)node.getParent());
700             }
701             jTreeDocument.expandPath(new TreePath JavaDoc(((DocumentStructureTreeNode)node).getPath() ));
702         } catch (Exception JavaDoc ex)
703         {
704             ex.printStackTrace();
705         }
706      }
707     
708     
709     public void crosstabLayoutChanged(CrosstabLayoutChangedEvent evt) {
710         
711         if (evt.getCrosstabReportElement() == getCrosstabReportElement())
712         {
713             
714             this.updateDocumentStructureTree( MainFrame.getMainInstance().getActiveReportFrame() , getCrosstabReportElement());
715         }
716     }
717     
718     public void reportElementsChanged(ReportElementChangedEvent evt) {
719         
720         if (evt.getType() == ReportElementChangedEvent.CHANGED) {
721             for (int ir=0; ir<evt.getElements().size(); ++ir)
722             {
723                 ReportElement re = (ReportElement)evt.getElements().elementAt(ir);
724                 
725                 DocumentStructureTreeNode node = findElementTreeNode(re, true);
726                 if (node == null) {
727                     // This node has changed your band....
728
// Find the node based on objects availables....
729
node = findElementTreeNode(re, false);
730                     if (node == null) return; // This should not happen....
731
// Remove from your parent....
732
node.removeFromParent();
733                     // Add to the new parent...
734
DocumentStructureTreeNode root = (DocumentStructureTreeNode)jTreeDocument.getModel().getRoot();
735                     for (int i=0; i<root.getChildCount(); ++i) {
736                         DocumentStructureTreeNode bandNode = (DocumentStructureTreeNode)root.getChildAt(i);
737                         if (bandNode.getUserObject() == re.band) {
738                             bandNode.add( node );
739                             setDontHandleEvent(true);
740                             if (evt.getJReportFrame().getSelectedCrosstabEditorPanel().getSelectedElements().contains( re )) {
741                                 jTreeDocument.getSelectionModel().addSelectionPath(new TreePath JavaDoc(new Object JavaDoc[]
742                                 {root, bandNode,node}));
743                             }
744                             setDontHandleEvent(false);
745                             break;
746                         }
747                     }
748                 }
749             }
750             jTreeDocument.updateUI();
751         }
752         else if (evt.getType() == ReportElementChangedEvent.REMOVED) {
753
754             updateDocumentStructureTree( MainFrame.getMainInstance().getActiveReportFrame(), getCrosstabReportElement());
755             jTreeDocument.updateUI();
756         }
757         else if (evt.getType() == ReportElementChangedEvent.ADDED) {
758             
759             updateDocumentStructureTree( MainFrame.getMainInstance().getActiveReportFrame(),getCrosstabReportElement() );
760             jTreeDocument.updateUI();
761         }
762          
763         
764     }//end reportElementsChanged
765

766     
767
768     public void reportElementsSelectionChanged(ReportElementsSelectionEvent evt) {
769         
770         if (evt.getCrosstabReportElement() == null) return;
771         setDontHandleEvent(true);
772         
773         TreePath JavaDoc[] path = jTreeDocument.getSelectionPaths();
774         if (path != null && evt.getSelectedElements() != null && evt.getSelectedElements().size() == 0 &&
775          ( ((DocumentStructureTreeNode) path[0].getLastPathComponent()).getUserObject() instanceof CrosstabCell
776             || ((DocumentStructureTreeNode) path[0].getLastPathComponent()).getUserObject() instanceof ElementGroup)) {
777             setDontHandleEvent(false);
778             return;
779         }
780         
781         jTreeDocument.getSelectionModel().clearSelection();
782         
783         
784         java.util.Vector JavaDoc v = evt.getSelectedElements();
785         Enumeration JavaDoc e = v.elements();
786         while (e.hasMoreElements()) {
787             ReportElement re = (ReportElement)e.nextElement();
788             // looking for it in the tree...
789
//1. Search the band...
790
DocumentStructureTreeNode enode = findElementTreeNode( re, true);
791             //TreePath treePath = findReportElement((DocumentStructureTreeNode)jTreeDocument.getModel().getRoot(), re);
792
if (enode != null)
793             {
794                 jTreeDocument.getSelectionModel().addSelectionPath(new TreePath JavaDoc(enode.getPath()));
795             }
796             
797         }
798         
799         setDontHandleEvent(false);
800         
801     }
802     
803     /**
804      * If searchInYourBandOnly == true
805      * This method Look for the userObject re in elements contained in the same band
806      * of the element. This method MUST CHECK that the report IS in the band
807      * assigned. If not it must return null. The reason is that if the element
808      * is not found in your band, it has changed the band attribute and must be updated.
809      */

810     public DocumentStructureTreeNode findElementTreeNode( ReportElement re, boolean searchInYourCellOnly) {
811         // looking for it in the tree...
812
//1. Search the band...
813
DocumentStructureTreeNode root = (DocumentStructureTreeNode)jTreeDocument.getModel().getRoot();
814         for (int i=0; i<root.getChildCount(); ++i) {
815             DocumentStructureTreeNode cellNode = (DocumentStructureTreeNode)root.getChildAt(i);
816             if (!searchInYourCellOnly || cellNode.getUserObject() == re.getCell()) {
817                 
818                 DocumentStructureTreeNode newchild = findElementInTreeNode(cellNode,re);
819                 if (newchild != null) return newchild;
820             }
821         }
822         return null;
823     }
824
825     public DocumentStructureTreeNode findElementInTreeNode(DocumentStructureTreeNode parentNode, ReportElement re)
826     {
827         for (int k=0; k<parentNode.getChildCount(); ++k) {
828             DocumentStructureTreeNode child = (DocumentStructureTreeNode)parentNode.getChildAt(k);
829             if (child.getUserObject() == re) {
830                 return child;
831             }
832             if (!re.getElementGroup().equals("") || re.getParentElement() != null)
833             {
834                 if (child.getUserObject() instanceof ElementGroup ||
835                     child.getUserObject() instanceof FrameReportElement)
836                 {
837                     DocumentStructureTreeNode newchild = findElementInTreeNode(child,re);
838                     if (newchild != null) return newchild;
839                 }
840             }
841         }
842         return null;
843     }
844     
845     public boolean isDontHandleEvent() {
846         return dontHandleEvent;
847     }
848
849     public void setDontHandleEvent(boolean dontHandleEvent) {
850         this.dontHandleEvent = dontHandleEvent;
851     }
852     
853     public void applyI18n(){
854                 // Start autogenerated code ----------------------
855
jMenuItemDSB_crosstabProperties.setText(I18n.getString("crosstabStructurePanel.menuItemDSB_crosstabProperties","Crosstab properties"));
856                 jMenuItemDSB_crosstabProperties1.setText(I18n.getString("crosstabStructurePanel.menuItemDSB_crosstabProperties1","Crosstab properties"));
857                 jMenuItemUngroup.setText(I18n.getString("crosstabStructurePanel.menuItemUngroup","Ungroup"));
858                 // End autogenerated code ----------------------
859
jMenuItemDS_cut.setText(it.businesslogic.ireport.util.I18n.getString("cut","Cut"));
860         jMenuItemDS_copy.setText(it.businesslogic.ireport.util.I18n.getString("copy","Copy"));
861         jMenuItemDS_paste.setText(it.businesslogic.ireport.util.I18n.getString("paste","Paste"));
862         jMenuItemDS_delete.setText(it.businesslogic.ireport.util.I18n.getString("delete","Delete"));
863         jMenuItemMoveUp.setText(it.businesslogic.ireport.util.I18n.getString("moveUp","Move up"));
864         jMenuItemMoveDown.setText(it.businesslogic.ireport.util.I18n.getString("moveDown","Move down"));
865         jMenuItemDSB_properties.setText(it.businesslogic.ireport.util.I18n.getString("cellProperties","Cell properties"));
866         jMenuItemDS_properties.setText(it.businesslogic.ireport.util.I18n.getString("properties","Properties"));
867         jMenuItemDS_cut.setText(it.businesslogic.ireport.util.I18n.getString("cut","Cut"));
868         jMenuItemDS_copy.setText(it.businesslogic.ireport.util.I18n.getString("copy","Copy"));
869         jMenuItemDS_paste.setText(it.businesslogic.ireport.util.I18n.getString("paste","Paste"));
870         jMenuItemDS_delete.setText(it.businesslogic.ireport.util.I18n.getString("delete","Delete"));
871         jMenuItemMoveUp.setText(it.businesslogic.ireport.util.I18n.getString("moveUp","Move up"));
872         jMenuItemMoveDown.setText(it.businesslogic.ireport.util.I18n.getString("moveDown","Move down"));
873         jMenuItemDSB_properties.setText(it.businesslogic.ireport.util.I18n.getString("cellProperties","Cell properties"));
874         jMenuItemDS_properties.setText(it.businesslogic.ireport.util.I18n.getString("properties","Properties"));
875         
876         ((DefaultMutableTreeNode JavaDoc)this.jTreeDocument.getModel().getRoot()).setUserObject( it.businesslogic.ireport.util.I18n.getString("crossTab","Crosstab"));
877       
878         
879     }
880
881     public void subDatasetObjectChanged(SubDatasetObjectChangedEvent evt) {
882         
883     }
884     
885     public void setCutCopyEnabled(boolean enabled) {
886         this.jMenuItemDS_cut.setEnabled(enabled);
887         this.jMenuItemDS_copy.setEnabled(enabled);
888         this.jMenuItemDS_delete.setEnabled(enabled);
889     }
890     
891     public void setPasteEnebled(boolean enabled) {
892         this.jMenuItemDS_paste.setEnabled(enabled);
893     }
894
895     public CrosstabReportElement getCrosstabReportElement() {
896         return crosstabReportElement;
897     }
898
899     public void setCrosstabReportElement(CrosstabReportElement crosstabReportElement) {
900         this.crosstabReportElement = crosstabReportElement;
901     }
902
903     public void reportBandChanged(ReportBandChangedEvent evt) {
904     }
905
906     public void crosstabMeasureChanged(CrosstabLayoutChangedEvent evt) {
907     }
908
909
910 }
911
Popular Tags