Source Code Cross Referenced for XMLFieldMappingEditor.java in  » Report » iReport-2.0.5 » it » businesslogic » ireport » data » xml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Report » iReport 2.0.5 » it.businesslogic.ireport.data.xml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * XMLFieldMappingEditor.java
003:         *
004:         * Created on May 15, 2007, 12:07 AM
005:         */
006:
007:        package it.businesslogic.ireport.data.xml;
008:
009:        import it.businesslogic.ireport.FieldsProviderEditor;
010:        import it.businesslogic.ireport.IReportConnection;
011:        import it.businesslogic.ireport.JRField;
012:        import it.businesslogic.ireport.connection.JRXMLDataSourceConnection;
013:        import it.businesslogic.ireport.gui.MainFrame;
014:        import it.businesslogic.ireport.gui.ReportQueryDialog;
015:        import it.businesslogic.ireport.util.I18n;
016:        import it.businesslogic.ireport.util.LanguageChangedEvent;
017:        import it.businesslogic.ireport.util.LanguageChangedListener;
018:        import it.businesslogic.ireport.util.Misc;
019:        import java.awt.Point;
020:        import java.util.Enumeration;
021:        import java.util.List;
022:        import javax.swing.JComponent;
023:        import javax.swing.JTree;
024:        import javax.swing.SwingUtilities;
025:        import javax.swing.table.DefaultTableModel;
026:        import javax.swing.tree.DefaultMutableTreeNode;
027:        import javax.swing.tree.DefaultTreeModel;
028:        import javax.swing.tree.TreeNode;
029:        import javax.swing.tree.TreePath;
030:        import javax.xml.transform.TransformerException;
031:        import net.sf.jasperreports.engine.util.JRXmlUtils;
032:        import org.apache.xpath.CachedXPathAPI;
033:        import org.w3c.dom.Document;
034:        import org.w3c.dom.NamedNodeMap;
035:        import org.w3c.dom.Node;
036:        import org.w3c.dom.NodeList;
037:
038:        /**
039:         *
040:         * @author  gtoffoli
041:         */
042:        public class XMLFieldMappingEditor extends javax.swing.JPanel implements 
043:                FieldsProviderEditor {
044:
045:            private String xpathExpression = null;
046:            private Document document = null;
047:            private CachedXPathAPI xpathAPI = new CachedXPathAPI();
048:            private ReportQueryDialog reportQueryDialog = null;
049:
050:            public ReportQueryDialog getReportQueryDialog() {
051:                return reportQueryDialog;
052:            }
053:
054:            public void setReportQueryDialog(ReportQueryDialog reportQueryDialog) {
055:                this .reportQueryDialog = reportQueryDialog;
056:            }
057:
058:            private List recordNodes = new java.util.ArrayList();
059:
060:            public List getRecordNodes() {
061:                return recordNodes;
062:            }
063:
064:            public void setRecordNodes(List recordNodes) {
065:                this .recordNodes = recordNodes;
066:            }
067:
068:            public CachedXPathAPI getXpathAPI() {
069:                return xpathAPI;
070:            }
071:
072:            public void setXpathAPI(CachedXPathAPI xpathAPI) {
073:                this .xpathAPI = xpathAPI;
074:            }
075:
076:            public Document getDocument() {
077:                return document;
078:            }
079:
080:            public void setDocument(Document document) {
081:                this .document = document;
082:            }
083:
084:            public String getXpathExpression() {
085:                return xpathExpression;
086:            }
087:
088:            public void setXpathExpression(String xpathExpression) {
089:                this .xpathExpression = xpathExpression;
090:            }
091:
092:            /** Creates new form XMLFieldMappingEditor 
093:             * @param rqd 
094:             */
095:            public XMLFieldMappingEditor(ReportQueryDialog rqd) {
096:                initComponents();
097:                this .reportQueryDialog = rqd;
098:                jTree1.setCellRenderer(new XMLDocumentTreeCellRenderer(this ));
099:                jTree1.setTransferHandler(new XMLTreeTransfertHandler(this ));
100:                setXpathExpression(rqd.getSQLExpressionArea().getText());
101:                updateView();
102:
103:                I18n
104:                        .addOnLanguageChangedListener(new LanguageChangedListener() {
105:                            public void languageChanged(LanguageChangedEvent evt) {
106:                                applyI18n();
107:                            }
108:                        });
109:
110:                applyI18n();
111:            }
112:
113:            /** This method is called from within the constructor to
114:             * initialize the form.
115:             * WARNING: Do NOT modify this code. The content of this method is
116:             * always regenerated by the Form Editor.
117:             */
118:            // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
119:            private void initComponents() {
120:                java.awt.GridBagConstraints gridBagConstraints;
121:
122:                jPopupMenuFields = new javax.swing.JPopupMenu();
123:                jMenuItemSetRecordNode = new javax.swing.JMenuItem();
124:                jMenuItemChangeRoot = new javax.swing.JMenuItem();
125:                jSeparator2 = new javax.swing.JSeparator();
126:                jMenuItemAddField = new javax.swing.JMenuItem();
127:                jMenuItemAddAbsoluteField = new javax.swing.JMenuItem();
128:                jSeparator1 = new javax.swing.JSeparator();
129:                jMenuItemExpandAll = new javax.swing.JMenuItem();
130:                jMenuItemCollapseAll = new javax.swing.JMenuItem();
131:                jMenuItemRefresh = new javax.swing.JMenuItem();
132:                jScrollPane1 = new javax.swing.JScrollPane();
133:                jTree1 = new javax.swing.JTree();
134:                jPanel1 = new javax.swing.JPanel();
135:                jLabelHelp = new javax.swing.JLabel();
136:
137:                jPopupMenuFields
138:                        .addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
139:                            public void popupMenuCanceled(
140:                                    javax.swing.event.PopupMenuEvent evt) {
141:                            }
142:
143:                            public void popupMenuWillBecomeInvisible(
144:                                    javax.swing.event.PopupMenuEvent evt) {
145:                            }
146:
147:                            public void popupMenuWillBecomeVisible(
148:                                    javax.swing.event.PopupMenuEvent evt) {
149:                                jPopupMenuFieldsPopupMenuWillBecomeVisible(evt);
150:                            }
151:                        });
152:
153:                jMenuItemSetRecordNode
154:                        .setText("Set record node (generate xPath)");
155:                jMenuItemSetRecordNode
156:                        .addActionListener(new java.awt.event.ActionListener() {
157:                            public void actionPerformed(
158:                                    java.awt.event.ActionEvent evt) {
159:                                jMenuItemSetRecordNodeActionPerformed(evt);
160:                            }
161:                        });
162:                jPopupMenuFields.add(jMenuItemSetRecordNode);
163:
164:                jMenuItemChangeRoot.setText("Set document root");
165:                jMenuItemChangeRoot
166:                        .addActionListener(new java.awt.event.ActionListener() {
167:                            public void actionPerformed(
168:                                    java.awt.event.ActionEvent evt) {
169:                                jMenuItemChangeRootActionPerformed(evt);
170:                            }
171:                        });
172:                jPopupMenuFields.add(jMenuItemChangeRoot);
173:                jPopupMenuFields.add(jSeparator2);
174:
175:                jMenuItemAddField.setText("Add node as field");
176:                jMenuItemAddField
177:                        .addActionListener(new java.awt.event.ActionListener() {
178:                            public void actionPerformed(
179:                                    java.awt.event.ActionEvent evt) {
180:                                jMenuItemAddFieldActionPerformed(evt);
181:                            }
182:                        });
183:                jPopupMenuFields.add(jMenuItemAddField);
184:
185:                jMenuItemAddAbsoluteField
186:                        .setText("Add node as field (using absolute path)");
187:                jMenuItemAddAbsoluteField
188:                        .addActionListener(new java.awt.event.ActionListener() {
189:                            public void actionPerformed(
190:                                    java.awt.event.ActionEvent evt) {
191:                                jMenuItemAddAbsoluteFieldActionPerformed(evt);
192:                            }
193:                        });
194:                jPopupMenuFields.add(jMenuItemAddAbsoluteField);
195:                jPopupMenuFields.add(jSeparator1);
196:
197:                jMenuItemExpandAll.setText("Expand all");
198:                jMenuItemExpandAll
199:                        .addActionListener(new java.awt.event.ActionListener() {
200:                            public void actionPerformed(
201:                                    java.awt.event.ActionEvent evt) {
202:                                jMenuItemExpandAllActionPerformed(evt);
203:                            }
204:                        });
205:                jPopupMenuFields.add(jMenuItemExpandAll);
206:
207:                jMenuItemCollapseAll.setText("Collapse all");
208:                jMenuItemCollapseAll
209:                        .addActionListener(new java.awt.event.ActionListener() {
210:                            public void actionPerformed(
211:                                    java.awt.event.ActionEvent evt) {
212:                                jMenuItemCollapseAllActionPerformed(evt);
213:                            }
214:                        });
215:                jPopupMenuFields.add(jMenuItemCollapseAll);
216:
217:                jMenuItemRefresh.setText("Reset / Refresh document");
218:                jMenuItemRefresh
219:                        .addActionListener(new java.awt.event.ActionListener() {
220:                            public void actionPerformed(
221:                                    java.awt.event.ActionEvent evt) {
222:                                jMenuItemRefreshActionPerformed(evt);
223:                            }
224:                        });
225:                jPopupMenuFields.add(jMenuItemRefresh);
226:
227:                setMinimumSize(new java.awt.Dimension(250, 23));
228:                setPreferredSize(new java.awt.Dimension(280, 322));
229:                setLayout(new java.awt.BorderLayout());
230:
231:                jScrollPane1
232:                        .addMouseListener(new java.awt.event.MouseAdapter() {
233:                            public void mouseClicked(
234:                                    java.awt.event.MouseEvent evt) {
235:                                jScrollPane1MouseClicked(evt);
236:                            }
237:                        });
238:
239:                jTree1.setDragEnabled(true);
240:                jTree1.setMinimumSize(new java.awt.Dimension(73, 64));
241:                jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
242:                    public void mouseClicked(java.awt.event.MouseEvent evt) {
243:                        jTree1MouseClicked(evt);
244:                    }
245:
246:                    public void mousePressed(java.awt.event.MouseEvent evt) {
247:                        jTree1MousePressed(evt);
248:                    }
249:                });
250:                jScrollPane1.setViewportView(jTree1);
251:
252:                add(jScrollPane1, java.awt.BorderLayout.CENTER);
253:
254:                jPanel1.setBackground(new java.awt.Color(255, 255, 204));
255:                jPanel1.setLayout(new java.awt.GridBagLayout());
256:
257:                jLabelHelp.setBackground(new java.awt.Color(255, 255, 204));
258:                jLabelHelp
259:                        .setIcon(new javax.swing.ImageIcon(
260:                                getClass()
261:                                        .getResource(
262:                                                "/it/businesslogic/ireport/icons/log/question.png"))); // NOI18N
263:                jLabelHelp
264:                        .setText("<html>Drag a node into the fields table to map a new field");
265:                jLabelHelp
266:                        .setVerticalTextPosition(javax.swing.SwingConstants.TOP);
267:                gridBagConstraints = new java.awt.GridBagConstraints();
268:                gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
269:                gridBagConstraints.weightx = 1.0;
270:                gridBagConstraints.weighty = 1.0;
271:                gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
272:                jPanel1.add(jLabelHelp, gridBagConstraints);
273:
274:                add(jPanel1, java.awt.BorderLayout.NORTH);
275:            }// </editor-fold>//GEN-END:initComponents
276:
277:            private void jMenuItemCollapseAllActionPerformed(
278:                    java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemCollapseAllActionPerformed
279:                expandAll(jTree1, false);
280:            }//GEN-LAST:event_jMenuItemCollapseAllActionPerformed
281:
282:            private void jMenuItemExpandAllActionPerformed(
283:                    java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemExpandAllActionPerformed
284:                expandAll(jTree1, true);
285:            }//GEN-LAST:event_jMenuItemExpandAllActionPerformed
286:
287:            public void expandAll(JTree tree, boolean expand) {
288:                TreeNode root = (TreeNode) tree.getModel().getRoot();
289:                // Traverse tree from root
290:                expandAll(tree, new TreePath(root), expand);
291:            }
292:
293:            private void expandAll(JTree tree, TreePath parent, boolean expand) {
294:                // Traverse children
295:                TreeNode node = (TreeNode) parent.getLastPathComponent();
296:                if (node.getChildCount() >= 0) {
297:                    for (Enumeration e = node.children(); e.hasMoreElements();) {
298:                        TreeNode n = (TreeNode) e.nextElement();
299:                        TreePath path = parent.pathByAddingChild(n);
300:                        expandAll(tree, path, expand);
301:                    }
302:                }
303:                // Expansion or collapse must be done bottom-up
304:                if (expand) {
305:                    tree.expandPath(parent);
306:                } else {
307:                    tree.collapsePath(parent);
308:                }
309:            }
310:
311:            private void jMenuItemAddAbsoluteFieldActionPerformed(
312:                    java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAddAbsoluteFieldActionPerformed
313:                TreePath tp = jTree1.getSelectionPath();
314:
315:                if (tp == null)
316:                    return;
317:
318:                JRField field = createField(tp, false);
319:                if (field != null) {
320:                    getReportQueryDialog().addField(field);
321:                }
322:            }//GEN-LAST:event_jMenuItemAddAbsoluteFieldActionPerformed
323:
324:            private void jMenuItemAddFieldActionPerformed(
325:                    java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemAddFieldActionPerformed
326:                TreePath tp = jTree1.getSelectionPath();
327:
328:                if (tp == null)
329:                    return;
330:
331:                JRField field = createField(tp, true);
332:                if (field != null) {
333:                    getReportQueryDialog().addField(field);
334:                }
335:            }//GEN-LAST:event_jMenuItemAddFieldActionPerformed
336:
337:            private void jMenuItemChangeRootActionPerformed(
338:                    java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemChangeRootActionPerformed
339:
340:                TreePath tp = jTree1.getSelectionPath();
341:
342:                if (tp == null)
343:                    return;
344:
345:                Node newRoot = (Node) ((DefaultMutableTreeNode) tp
346:                        .getLastPathComponent()).getUserObject();
347:
348:                jTree1.setModel(new DefaultTreeModel(this 
349:                        .addTreeDocument(newRoot)));
350:
351:            }//GEN-LAST:event_jMenuItemChangeRootActionPerformed
352:
353:            private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
354:                if (SwingUtilities.isRightMouseButton(evt)
355:                        && evt.getClickCount() == 1) {
356:                    Misc.ensurePathIsSelected(jTree1.getPathForLocation(evt
357:                            .getX(), evt.getY()), jTree1);
358:                    jPopupMenuFields.show(jTree1, evt.getX(), evt.getY());
359:
360:                }
361:            }//GEN-LAST:event_jTree1MouseClicked
362:
363:            private void jScrollPane1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jScrollPane1MouseClicked
364:                /*
365:                evt.setSource( jTree1 );
366:                Point p2 = SwingUtilities.convertPoint(jScrollPane1, evt.getPoint(), jTree1);
367:                evt.translatePoint(p2.x - evt.getX() , p2.y - evt.getY());
368:                jTree1MouseClicked(evt);
369:                 */
370:            }//GEN-LAST:event_jScrollPane1MouseClicked
371:
372:            private void jMenuItemSetRecordNodeActionPerformed(
373:                    java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemSetRecordNodeActionPerformed
374:
375:                TreePath tp = jTree1.getSelectionPath();
376:
377:                if (tp == null)
378:                    return;
379:
380:                String expression = generateXpathExpression(tp, false);
381:
382:                if (reportQueryDialog != null) {
383:                    reportQueryDialog.getSQLExpressionArea()
384:                            .setText(expression);
385:                }
386:
387:            }//GEN-LAST:event_jMenuItemSetRecordNodeActionPerformed
388:
389:            private void jTree1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MousePressed
390:
391:            }//GEN-LAST:event_jTree1MousePressed
392:
393:            private void jMenuItemRefreshActionPerformed(
394:                    java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItemRefreshActionPerformed
395:                updateView();
396:            }//GEN-LAST:event_jMenuItemRefreshActionPerformed
397:
398:            private void jPopupMenuFieldsPopupMenuWillBecomeVisible(
399:                    javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jPopupMenuFieldsPopupMenuWillBecomeVisible
400:
401:                TreePath tp = jTree1.getSelectionPath();
402:
403:                jMenuItemSetRecordNode.setEnabled(false);
404:                jMenuItemChangeRoot.setEnabled(false);
405:                jMenuItemAddField.setEnabled(false);
406:                jMenuItemAddAbsoluteField.setEnabled(false);
407:
408:                if (tp == null) {
409:                    return;
410:                }
411:
412:                if (((((DefaultMutableTreeNode) tp.getLastPathComponent())
413:                        .getUserObject()) instanceof  Node)) {
414:                    Node node = (Node) ((DefaultMutableTreeNode) tp
415:                            .getLastPathComponent()).getUserObject();
416:                    if (node.getNodeType() == Node.ELEMENT_NODE) {
417:                        jMenuItemSetRecordNode.setEnabled(true);
418:                        jMenuItemChangeRoot.setEnabled(true);
419:                    }
420:
421:                    jMenuItemAddField.setEnabled(true);
422:                    jMenuItemAddAbsoluteField.setEnabled(true);
423:                }
424:
425:            }//GEN-LAST:event_jPopupMenuFieldsPopupMenuWillBecomeVisible
426:
427:            // Variables declaration - do not modify//GEN-BEGIN:variables
428:            private javax.swing.JLabel jLabelHelp;
429:            private javax.swing.JMenuItem jMenuItemAddAbsoluteField;
430:            private javax.swing.JMenuItem jMenuItemAddField;
431:            private javax.swing.JMenuItem jMenuItemChangeRoot;
432:            private javax.swing.JMenuItem jMenuItemCollapseAll;
433:            private javax.swing.JMenuItem jMenuItemExpandAll;
434:            private javax.swing.JMenuItem jMenuItemRefresh;
435:            private javax.swing.JMenuItem jMenuItemSetRecordNode;
436:            private javax.swing.JPanel jPanel1;
437:            private javax.swing.JPopupMenu jPopupMenuFields;
438:            private javax.swing.JScrollPane jScrollPane1;
439:            private javax.swing.JSeparator jSeparator1;
440:            private javax.swing.JSeparator jSeparator2;
441:            private javax.swing.JTree jTree1;
442:
443:            // End of variables declaration//GEN-END:variables
444:
445:            public void updateView() {
446:                IReportConnection conn = (IReportConnection) MainFrame
447:                        .getMainInstance().getProperties().get(
448:                                "DefaultConnection");
449:
450:                String file = "";
451:                if (conn instanceof  JRXMLDataSourceConnection) {
452:                    file = ((JRXMLDataSourceConnection) conn).getFilename();
453:
454:                    try {
455:                        document = JRXmlUtils.parse(file);
456:                    } catch (Exception ex) {
457:                        ex.printStackTrace();
458:                    }
459:
460:                    DefaultMutableTreeNode root = addTreeDocument(document
461:                            .getDocumentElement());
462:                    jTree1.setModel(new DefaultTreeModel(root));
463:
464:                } else {
465:                    jTree1.setModel(new DefaultTreeModel(
466:                            new DefaultMutableTreeNode("No file found")));
467:                }
468:
469:                updateXpathView();
470:                jTree1.updateUI();
471:            }
472:
473:            public DefaultMutableTreeNode addTreeDocument(Node node) {
474:
475:                if (node.getNodeType() != Node.ELEMENT_NODE
476:                        && node.getNodeType() != Node.ATTRIBUTE_NODE)
477:                    return null;
478:                DefaultMutableTreeNode tnode = new DefaultMutableTreeNode(node);
479:
480:                NamedNodeMap nm = node.getAttributes();
481:                for (int i = 0; nm != null && i < nm.getLength(); ++i) {
482:                    Node childNode = nm.item(i);
483:                    DefaultMutableTreeNode cnode = addTreeDocument(childNode);
484:                    if (cnode != null) {
485:                        tnode.add(cnode);
486:                    }
487:                }
488:
489:                NodeList nl = node.getChildNodes();
490:
491:                for (int i = 0; nl != null && i < nl.getLength(); ++i) {
492:                    Node childNode = nl.item(i);
493:                    DefaultMutableTreeNode cnode = addTreeDocument(childNode);
494:                    if (cnode != null) {
495:                        tnode.add(cnode);
496:                    }
497:                }
498:
499:                return tnode;
500:            }
501:
502:            /**
503:             * Ad hoc queryChanged method for EJBQL queries....
504:             * @param newQuery 
505:             */
506:            public void queryChanged(String newQuery) {
507:
508:                setXpathExpression(newQuery);
509:
510:                try {
511:
512:                    Runnable r = new Runnable() {
513:                        public void run() {
514:                            updateXpathView();
515:                        }
516:                    };
517:
518:                    if (!SwingUtilities.isEventDispatchThread()) {
519:                        SwingUtilities.invokeAndWait(r);
520:                    } else {
521:                        r.run();
522:                    }
523:
524:                } catch (Exception e) {
525:                    // oh well we got interrupted.
526:                }
527:
528:            }
529:
530:            public void updateXpathView() {
531:                if (document == null || getXpathExpression() == null
532:                        || getXpathExpression().trim().length() == 0)
533:                    return;
534:                try {
535:
536:                    NodeList nodeList = xpathAPI.selectNodeList(document,
537:                            getXpathExpression());
538:
539:                    getRecordNodes().clear();
540:                    for (int i = 0; nodeList != null
541:                            && i < nodeList.getLength(); ++i) {
542:                        getRecordNodes().add(nodeList.item(i));
543:                    }
544:
545:                    if (reportQueryDialog != null) {
546:                        reportQueryDialog.getJLabelStatusSQL().setText(
547:                                "Selected nodes: " + nodeList.getLength());
548:                    }
549:
550:                    jTree1.updateUI();
551:
552:                } catch (Exception ex) {
553:                    if (reportQueryDialog != null) {
554:                        reportQueryDialog.getJLabelStatusSQL().setText(
555:                                "Invalid XPath expression: " + ex.getMessage()
556:                                        + " (" + getXpathExpression() + ")");
557:                        ex.printStackTrace();
558:                    }
559:                }
560:            }
561:
562:            /**
563:             *  Generates an xpath expression to arrive to that node from the root.
564:             *  The root considered is the tree root, that can be different from the XML Document root.
565:             * 
566:             *  @param tp The TreePath
567:             *  @param relative if the root is on the path, the xpath expression stops here....
568:             * @return the xpath representation of the path (like /root/node1/node2)
569:             */
570:            public String generateXpathExpression(TreePath tp, boolean relative) {
571:                String xpath = "";
572:                boolean rootFound = false;
573:                Object[] nodes = (Object[]) ((DefaultMutableTreeNode) tp
574:                        .getLastPathComponent()).getUserObjectPath();
575:
576:                if (relative) {
577:                    // Look for a record root on the path to the root...
578:                    for (int i = nodes.length - 1; i >= 0; --i) {
579:                        Node node = (Node) nodes[i];
580:                        if (getRecordNodes().contains(node)) {
581:                            rootFound = true;
582:                            break;
583:                        }
584:                    }
585:                    if (!rootFound) {
586:                        String postfix = "";
587:                        Node node = (Node) nodes[nodes.length - 1];
588:                        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
589:                            postfix = "/@" + node.getNodeName();
590:                            node = (Node) nodes[nodes.length - 2];
591:                        }
592:
593:                        // look if there is 
594:                        String exp = "ancestor::" + node.getNodeName();
595:                        for (int i = 0; i < getRecordNodes().size(); ++i) {
596:                            try {
597:                                org.w3c.dom.Node currentNode = (org.w3c.dom.Node) getRecordNodes()
598:                                        .get(i);
599:                                org.w3c.dom.Node foundNode = xpathAPI
600:                                        .selectSingleNode(currentNode, exp);
601:                                if (foundNode == node) {
602:                                    return exp + postfix;
603:                                }
604:                            } catch (TransformerException ex) {
605:                                //ex.printStackTrace();
606:                            }
607:                        }
608:                    }
609:                }
610:
611:                rootFound = false;
612:                for (int i = nodes.length - 1; i >= 0; --i) {
613:                    Node node = (Node) nodes[i];
614:                    if (relative) {
615:                        if (getRecordNodes().contains(node)) {
616:                            if (xpath.length() == 0) {
617:                                xpath = "child::text()";
618:                            }
619:                            rootFound = true;
620:                            break;
621:                        }
622:                    }
623:
624:                    if (xpath.length() > 0)
625:                        xpath = "/" + xpath;
626:
627:                    if (node.getNodeType() == Node.ELEMENT_NODE) {
628:                        xpath = node.getNodeName() + xpath;
629:                    } else {
630:                        xpath = "@" + node.getNodeName();
631:                    }
632:                }
633:
634:                if ((!rootFound || !relative) && !xpath.startsWith("/")) {
635:                    xpath = "/" + xpath;
636:                }
637:
638:                return xpath;
639:            }
640:
641:            public JRField createField(TreePath path, boolean mode) {
642:                JRField jrField = null;
643:                DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) path
644:                        .getLastPathComponent();
645:                if (dmtn.getUserObject() != null
646:                        && dmtn.getUserObject() instanceof  Node) {
647:                    Node node = (Node) dmtn.getUserObject();
648:                    jrField = new JRField(node.getNodeName(),
649:                            "java.lang.String");
650:                    jrField.setDescription(generateXpathExpression(path, mode));
651:                    boolean found = true;
652:                    String name = node.getNodeName();
653:
654:                    for (int i = 2; found; ++i) {
655:                        found = false;
656:                        DefaultTableModel dtm = (DefaultTableModel) getReportQueryDialog()
657:                                .getFieldsTable().getModel();
658:                        for (int k = 0; k < dtm.getRowCount(); ++k) {
659:                            JRField tmpField = (JRField) dtm.getValueAt(k, 0);
660:                            if (tmpField.getName().equals(name)) {
661:                                found = true;
662:                                break;
663:                            }
664:                        }
665:
666:                        if (found) {
667:                            name = node.getNodeName() + i;
668:                        }
669:                    }
670:
671:                    jrField.setName(name);
672:                }
673:
674:                return jrField;
675:            }
676:
677:            public void applyI18n() {
678:                jMenuItemSetRecordNode.setText(I18n.getString(
679:                        "XMLFieldMappingEditor.menuItemSetRecordNode",
680:                        "Set record node (generate xPath)"));
681:                jMenuItemChangeRoot.setText(I18n.getString(
682:                        "XMLFieldMappingEditor.menuItemChangeRoot",
683:                        "Set document root"));
684:                jMenuItemAddField.setText(I18n.getString(
685:                        "XMLFieldMappingEditor.menuItemAddField",
686:                        "Add node as field"));
687:                jMenuItemAddAbsoluteField.setText(I18n.getString(
688:                        "XMLFieldMappingEditor.menuItemAddAbsoluteField",
689:                        "Add node as field (using absolute path)"));
690:                jMenuItemRefresh.setText(I18n.getString(
691:                        "XMLFieldMappingEditor.menuItemRefresh",
692:                        "Reset / Refresh document"));
693:                jLabelHelp
694:                        .setText("<html>"
695:                                + I18n
696:                                        .getString(
697:                                                "XMLFieldMappingEditor.labelHelp",
698:                                                "Drag a node into the fields table to map a new field."));
699:
700:                jMenuItemExpandAll.setText(I18n
701:                        .getString("XMLFieldMappingEditor.menuItemExpandAll",
702:                                "Expand All"));
703:                jMenuItemCollapseAll.setText(I18n.getString(
704:                        "XMLFieldMappingEditor.menuItemCollapseAll",
705:                        "Collapse All"));
706:
707:            }
708:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.