KickJava   Java API By Example, From Geeks To Geeks.

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


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  * DocumentStructurePanel.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.ReportBandChangedEvent;
37 import it.businesslogic.ireport.gui.event.ReportListener;
38 import it.businesslogic.ireport.gui.event.SubDatasetObjectChangedListener;
39 import it.businesslogic.ireport.util.LanguageChangedListener;
40 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
41 import javax.swing.tree.DefaultTreeSelectionModel JavaDoc;
42 import it.businesslogic.ireport.gui.*;
43 import it.businesslogic.ireport.*;
44 import it.businesslogic.ireport.gui.event.*;
45 import java.util.Enumeration JavaDoc;
46 import java.util.Vector JavaDoc;
47 import javax.swing.tree.TreePath JavaDoc;
48 import it.businesslogic.ireport.chart.gui.*;
49 import it.businesslogic.ireport.gui.docking.GenericDragTargetListener;
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 import it.businesslogic.ireport.util.LanguageChangedEvent;
56 /**
57  *
58  * @author Administrator
59  */

60 public class DocumentStructurePanel extends javax.swing.JPanel JavaDoc implements ReportListener, SubDatasetObjectChangedListener, it.businesslogic.ireport.util.LanguageChangedListener {
61
62     private boolean dontHandleEvent = false;
63
64     private JReportFrame oldJReportFrame = null;
65
66     /** Creates new form DocumentStructurePanel */
67     public DocumentStructurePanel() {
68         initComponents();
69
70         DefaultTreeSelectionModel JavaDoc dtsm = (DefaultTreeSelectionModel JavaDoc)jTreeDocument.getSelectionModel();
71         dtsm.setSelectionMode( DefaultTreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION );
72
73         jTreeDocument.setDropTarget(new DropTarget JavaDoc(this, new GenericDragTargetListener()));
74
75         //Modified by Felix Firgau for I18n on Feb 9th 2006
76
DocumentStructureTreeNode rootDoc = new DocumentStructureTreeNode(it.businesslogic.ireport.util.I18n.getString("document","Document"));
77         //End
78

79         javax.swing.tree.DefaultTreeModel JavaDoc modelDoc = new javax.swing.tree.DefaultTreeModel JavaDoc(rootDoc);
80
81         jTreeDocument.setModel( modelDoc );
82         jTreeDocument.setCellRenderer( new DocumentStructureTreeCellRenderer());
83         
84         //I18n.addOnLanguageChangedListener( this );
85
}
86
87     /** This method is called from within the constructor to
88      * initialize the form.
89      * WARNING: Do NOT modify this code. The content of this method is
90      * always regenerated by the Form Editor.
91      */

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

242     }// </editor-fold>//GEN-END:initComponents
243

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

246         if (jTreeDocument.getSelectionCount() > 0) {
247             DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode)jTreeDocument.getSelectionPath().getLastPathComponent();
248             if (elementNode.getUserObject() instanceof ElementGroup) {
249                 String JavaDoc elementGroupName = ((ElementGroup)elementNode.getUserObject()).getName();
250                 while ( ((DocumentStructureTreeNode)elementNode.getParent()).getUserObject() instanceof ElementGroup)
251                 {
252                     elementNode = (DocumentStructureTreeNode)elementNode.getParent();
253                     elementGroupName = ((ElementGroup)elementNode.getUserObject()).getName() + "." + elementGroupName;
254                 }
255
256                 String JavaDoc newElementGroupName = elementGroupName;
257                 if (newElementGroupName.lastIndexOf(".")>=0)
258                 {
259                     newElementGroupName = newElementGroupName.substring(0,newElementGroupName.lastIndexOf("."));
260                 }
261                 else
262                 {
263                     newElementGroupName = "";
264                 }
265
266                 /************************/
267                 UnGroupEmentsOperation undoOp = new UnGroupEmentsOperation(MainFrame.getMainInstance().getActiveReportFrame());
268                 Vector JavaDoc elements = MainFrame.getMainInstance().getActiveReportFrame().getReport().getElements();
269                 for (int i=0; i<elements.size(); ++i)
270                 {
271                     ReportElement element = (ReportElement)elements.elementAt(i);
272                     String JavaDoc oldElementGroupName = element.getElementGroup();
273                     if (element.getElementGroup().startsWith(elementGroupName+".") || element.getElementGroup().equals(elementGroupName))
274                     {
275                          String JavaDoc tmpElementGroupName = element.getElementGroup().substring(elementGroupName.length());
276                          if (tmpElementGroupName.length() == 0) tmpElementGroupName = newElementGroupName;
277                          tmpElementGroupName = newElementGroupName + tmpElementGroupName;
278                          if (tmpElementGroupName.startsWith(".")) tmpElementGroupName = tmpElementGroupName.substring(1);
279
280                         element.setElementGroup(tmpElementGroupName);
281                         undoOp.addElement(element, i,i, oldElementGroupName, tmpElementGroupName);
282                     }
283                 }
284
285                 MainFrame.getMainInstance().getActiveReportFrame().addUndoOperation(undoOp);
286
287                 MainFrame.getMainInstance().getActiveReportFrame().fireReportListenerReportElementsChanged(new ReportElementChangedEvent(MainFrame.getMainInstance().getActiveReportFrame(), new Vector JavaDoc() , ReportElementChangedEvent.REMOVED ));
288             }
289         }
290         // Get group name...
291

292
293     }//GEN-LAST:event_jMenuItemUngroupActionPerformed
294

295     private void jTreeDocumentTreeCollapsed(javax.swing.event.TreeExpansionEvent JavaDoc evt) {//GEN-FIRST:event_jTreeDocumentTreeCollapsed
296
//System.out.println("collapsing..." + evt.getPath());
297
}//GEN-LAST:event_jTreeDocumentTreeCollapsed
298

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

302
303     }//GEN-LAST:event_jTreeDocumentTreeExpanded
304

305     private void jMenuItemDSB_propertiesActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDSB_propertiesActionPerformed
306
if (jTreeDocument.getSelectionCount() > 0) {
307             DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode)jTreeDocument.getSelectionPath().getLastPathComponent();
308             if (elementNode.getUserObject() instanceof Band) {
309                 String JavaDoc thisBand = ((Band)elementNode.getUserObject()).getName();
310                 MainFrame.getMainInstance().jMenuItemBandsActionPerformed(evt);
311                 MainFrame.getMainInstance().getBandsDialog().setSelectedBand( thisBand );
312             }
313         }
314     }//GEN-LAST:event_jMenuItemDSB_propertiesActionPerformed
315

316     private void jMenuItemDS_deleteActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_deleteActionPerformed
317
MainFrame.getMainInstance().getActiveReportFrame().deleteSelectedElements();
318     }//GEN-LAST:event_jMenuItemDS_deleteActionPerformed
319

320     private void jMenuItemDS_pasteActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_pasteActionPerformed
321
MainFrame.getMainInstance().getActiveReportFrame().paste();
322     }//GEN-LAST:event_jMenuItemDS_pasteActionPerformed
323

324     private void jMenuItemDS_copyActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_copyActionPerformed
325
MainFrame.getMainInstance().getActiveReportFrame().copy();
326     }//GEN-LAST:event_jMenuItemDS_copyActionPerformed
327

328     private void jMenuItemDS_cutActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_cutActionPerformed
329
MainFrame.getMainInstance().getActiveReportFrame().cut();
330     }//GEN-LAST:event_jMenuItemDS_cutActionPerformed
331

332     private void jMenuItemMoveDownActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemMoveDownActionPerformed
333
if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
334             JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
335             jrf.moveDown();
336
337             if (jrf.getSelectedElements().size()>0)
338                 updateDocumentStructureTree(jrf);
339
340         }
341     }//GEN-LAST:event_jMenuItemMoveDownActionPerformed
342

343     private void jMenuItemMoveUpActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemMoveUpActionPerformed
344
if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
345          JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
346          jrf.moveUp();
347          this.jTreeDocument.updateUI();
348          printSelectedPaths("Prima");
349          if (jrf.getSelectedElements().size()>0)
350              updateDocumentStructureTree(jrf);
351
352          printSelectedPaths("Dopo");
353
354         }
355
356     }//GEN-LAST:event_jMenuItemMoveUpActionPerformed
357

358     private void jMenuItemDS_chartPropertiesActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_chartPropertiesActionPerformed
359
if (MainFrame.getMainInstance().getActiveReportFrame() != null) {
360                 JReportFrame jrf = MainFrame.getMainInstance().getActiveReportFrame();
361                 if (jTreeDocument.getSelectionCount() > 0) {
362                     DocumentStructureTreeNode elementNode = (DocumentStructureTreeNode)jTreeDocument.getSelectionPath().getLastPathComponent();
363                     if (elementNode.getUserObject() instanceof ChartReportElement2 )
364                     {
365                         ChartPropertiesDialog cpd = new ChartPropertiesDialog(MainFrame.getMainInstance(),true);
366                         cpd.setChartElement( (ChartReportElement2)elementNode.getUserObject());
367                         cpd.setVisible(true);
368                     }
369
370                 }
371         }
372     }//GEN-LAST:event_jMenuItemDS_chartPropertiesActionPerformed
373

374     public void languageChanged(LanguageChangedEvent evt) {
375
376     this.applyI18n();
377 }
378
379
380     private void jMenuItemDS_propertiesActionPerformed(java.awt.event.ActionEvent JavaDoc evt) {//GEN-FIRST:event_jMenuItemDS_propertiesActionPerformed
381
MainFrame.getMainInstance().getElementPropertiesDialog().setVisible(true);
382       MainFrame.getMainInstance().getElementPropertiesDialog().updateSelection();
383     }//GEN-LAST:event_jMenuItemDS_propertiesActionPerformed
384

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

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

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

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

715             updateDocumentStructureTree( MainFrame.getMainInstance().getActiveReportFrame() );
716             jTreeDocument.updateUI();
717         }
718         else if (evt.getType() == ReportElementChangedEvent.ADDED) {
719
720             updateDocumentStructureTree( MainFrame.getMainInstance().getActiveReportFrame() );
721
722             /*
723             System.out.println("Elements in window: " + MainFrame.getMainInstance().getActiveReportFrame().getReport().getElements().size());
724             System.out.flush();
725             for (int ir=0; ir<evt.getElements().size(); ++ir)
726             {
727                 ReportElement re = (ReportElement)evt.getElements().elementAt(ir);
728                 System.out.println("Adding " + re);
729
730                 // add it to your band...
731                 //DocumentStructureTreeNode node = new DocumentStructureTreeNode(re);
732                 // Find the band node...
733                 DocumentStructureTreeNode root = (DocumentStructureTreeNode)jTreeDocument.getModel().getRoot();
734                 for (int i=0; i<root.getChildCount(); ++i) {
735                     DocumentStructureTreeNode bandNode = (DocumentStructureTreeNode)root.getChildAt(i);
736                     if (bandNode.getUserObject() == re.band) {
737
738                         String elementGroup = re.getElementGroup();
739                         try {
740                         if (re.getParentElement() != null)
741                             {
742                                 DocumentStructureTreeNode parentNode = findElementTreeNode(re.getParentElement(),true);
743                                 addElementToGroup(parentNode, elementGroup, re, null);
744                             }
745                             else
746                             {
747                                 System.out.println("Adding on " + bandNode + " " +re);
748                                 addElementToGroup(bandNode, elementGroup, re, null);
749                             }
750
751                         break;
752
753                         } catch (Exception ex)
754                         {
755                             ex.printStackTrace();
756                         }
757                     }
758                 }
759             }*/

760
761             jTreeDocument.updateUI();
762         }
763
764
765     }//end reportElementsChanged
766

767
768
769     public void reportElementsSelectionChanged(ReportElementsSelectionEvent evt) {
770
771         if (evt.getCrosstabReportElement() != null) return;
772         setDontHandleEvent(true);
773
774         TreePath JavaDoc[] path = jTreeDocument.getSelectionPaths();
775         if (path != null && evt.getSelectedElements() != null && evt.getSelectedElements().size() == 0 &&
776          ( ((DocumentStructureTreeNode) path[0].getLastPathComponent()).getUserObject() instanceof Band
777             || ((DocumentStructureTreeNode) path[0].getLastPathComponent()).getUserObject() instanceof ElementGroup)) {
778             setDontHandleEvent(false);
779             return;
780         }
781
782         jTreeDocument.getSelectionModel().clearSelection();
783
784
785         java.util.Vector JavaDoc v = evt.getSelectedElements();
786         Enumeration JavaDoc e = v.elements();
787         while (e.hasMoreElements()) {
788             ReportElement re = (ReportElement)e.nextElement();
789             // looking for it in the tree...
790
//1. Search the band...
791
DocumentStructureTreeNode enode = findElementTreeNode( re, true);
792             //TreePath treePath = findReportElement((DocumentStructureTreeNode)jTreeDocument.getModel().getRoot(), re);
793
if (enode != null)
794             {
795                 jTreeDocument.getSelectionModel().addSelectionPath(new TreePath JavaDoc(enode.getPath()));
796             }
797
798         }
799
800         setDontHandleEvent(false);
801
802     }
803
804     /*
805     private TreePath findReportElement(DocumentStructureTreeNode root, ReportElement re)
806     {
807             List nodes = new ArrayList();
808             nodes.add(root);
809             for (int i=0; i<root.getChildCount(); ++i) {
810                 DocumentStructureTreeNode bandNode = (DocumentStructureTreeNode)root.getChildAt(i);
811                 if (bandNode.getUserObject() == re.band) {
812                     String elementGroup = re.getElementGroup();
813                     nodes.add(bandNode);
814                     while ( elementGroup.length() > 0)
815                     {
816                         String localGroup = "";
817                         if (elementGroup.indexOf(".") > 0)
818                         {
819                             localGroup = elementGroup.substring(0, elementGroup.indexOf("."));
820                             elementGroup = elementGroup.substring(elementGroup.indexOf(".")+1);
821                         }
822                         else
823                         {
824                             localGroup = elementGroup;
825                             elementGroup = "";
826                         }
827
828                         boolean found = false;
829                         for (int k=0; k<bandNode.getChildCount(); ++k)
830                         {
831                             DocumentStructureTreeNode childNode = (DocumentStructureTreeNode)bandNode.getChildAt(k);
832                             if (childNode.getUserObject() instanceof ElementGroup &&
833                                 ((ElementGroup)childNode.getUserObject()).getName().equals(localGroup)) {
834
835                                 nodes.add( childNode );
836                                 bandNode = childNode;
837                                 found = true;
838                                 break;
839                             }
840                         }
841
842
843                     }
844
845                     for (int k=0; k<bandNode.getChildCount(); ++k) {
846                         DocumentStructureTreeNode child = (DocumentStructureTreeNode)bandNode.getChildAt(k);
847                         if (child.getUserObject() == re) {
848                             nodes.add(child);
849
850                             return new TreePath(nodes.toArray());
851                         }
852                     }
853                 }
854             }
855             return null;
856     }
857      */

858
859     /**
860      * If searchInYourBandOnly == true
861      * This method Look for the userObject re in elements contained in the same band
862      * of the element. This method MUST CHECK that the report IS in the band
863      * assigned. If not it must return null. The reason is that if the element
864      * is not found in your band, it has changed the band attribute and must be updated.
865      */

866     public DocumentStructureTreeNode findElementTreeNode( ReportElement re, boolean searchInYourBandOnly) {
867         // looking for it in the tree...
868
//1. Search the band...
869
DocumentStructureTreeNode root = (DocumentStructureTreeNode)jTreeDocument.getModel().getRoot();
870         for (int i=0; i<root.getChildCount(); ++i) {
871             DocumentStructureTreeNode bandNode = (DocumentStructureTreeNode)root.getChildAt(i);
872             if (!searchInYourBandOnly || bandNode.getUserObject() == re.band) {
873
874                 DocumentStructureTreeNode newchild = findElementInTreeNode(bandNode,re);
875                 if (newchild != null) return newchild;
876             }
877         }
878         return null;
879     }
880
881     public DocumentStructureTreeNode findElementInTreeNode(DocumentStructureTreeNode parentNode, ReportElement re)
882     {
883         for (int k=0; k<parentNode.getChildCount(); ++k) {
884             DocumentStructureTreeNode child = (DocumentStructureTreeNode)parentNode.getChildAt(k);
885             if (child.getUserObject() == re) {
886                 return child;
887             }
888             if (!re.getElementGroup().equals("") || re.getParentElement() != null)
889             {
890                 if (child.getUserObject() instanceof ElementGroup ||
891                     child.getUserObject() instanceof FrameReportElement)
892                 {
893                     DocumentStructureTreeNode newchild = findElementInTreeNode(child,re);
894                     if (newchild != null) return newchild;
895                 }
896             }
897         }
898         return null;
899     }
900
901     public boolean isDontHandleEvent() {
902         return dontHandleEvent;
903     }
904
905     public void setDontHandleEvent(boolean dontHandleEvent) {
906         this.dontHandleEvent = dontHandleEvent;
907     }
908
909     public void applyI18n(){
910                 // Start autogenerated code ----------------------
911
jMenuItemDS_chartProperties.setText(I18n.getString("documentStructurePanel.menuItemDS_chartProperties","Chart properties"));
912                 jMenuItemUngroup.setText(I18n.getString("documentStructurePanel.menuItemUngroup","Ungroup"));
913                 // End autogenerated code ----------------------
914
jMenuItemDS_cut.setText(it.businesslogic.ireport.util.I18n.getString("cut","Cut"));
915         jMenuItemDS_copy.setText(it.businesslogic.ireport.util.I18n.getString("copy","Copy"));
916         jMenuItemDS_paste.setText(it.businesslogic.ireport.util.I18n.getString("paste","Paste"));
917         jMenuItemDS_delete.setText(it.businesslogic.ireport.util.I18n.getString("delete","Delete"));
918         jMenuItemMoveUp.setText(it.businesslogic.ireport.util.I18n.getString("moveUp","Move up"));
919         jMenuItemMoveDown.setText(it.businesslogic.ireport.util.I18n.getString("moveDown","Move down"));
920         jMenuItemDSB_properties.setText(it.businesslogic.ireport.util.I18n.getString("bandProperties","Band properties"));
921         jMenuItemDS_properties.setText(it.businesslogic.ireport.util.I18n.getString("properties","Properties"));
922         jMenuItemDS_cut.setText(it.businesslogic.ireport.util.I18n.getString("cut","Cut"));
923         jMenuItemDS_copy.setText(it.businesslogic.ireport.util.I18n.getString("copy","Copy"));
924         jMenuItemDS_paste.setText(it.businesslogic.ireport.util.I18n.getString("paste","Paste"));
925         jMenuItemDS_delete.setText(it.businesslogic.ireport.util.I18n.getString("delete","Delete"));
926         jMenuItemMoveUp.setText(it.businesslogic.ireport.util.I18n.getString("moveUp","Move up"));
927         jMenuItemMoveDown.setText(it.businesslogic.ireport.util.I18n.getString("moveDown","Move down"));
928         jMenuItemDSB_properties.setText(it.businesslogic.ireport.util.I18n.getString("bandProperties","Band properties"));
929         jMenuItemDS_properties.setText(it.businesslogic.ireport.util.I18n.getString("properties","Properties"));
930
931         ((DefaultMutableTreeNode JavaDoc)this.jTreeDocument.getModel().getRoot()).setUserObject( it.businesslogic.ireport.util.I18n.getString("document","Document"));
932       
933     }
934
935     public void subDatasetObjectChanged(SubDatasetObjectChangedEvent evt) {
936
937     }
938
939     public void setCutCopyEnabled(boolean enabled) {
940         this.jMenuItemDS_cut.setEnabled(enabled);
941         this.jMenuItemDS_copy.setEnabled(enabled);
942         this.jMenuItemDS_delete.setEnabled(enabled);
943     }
944
945     public void setPasteEnebled(boolean enabled) {
946         this.jMenuItemDS_paste.setEnabled(enabled);
947     }
948 }
949
Popular Tags