0001: /*
0002: * Copyright (C) 2005 - 2008 JasperSoft Corporation. All rights reserved.
0003: * http://www.jaspersoft.com.
0004: *
0005: * Unless you have purchased a commercial license agreement from JasperSoft,
0006: * the following license terms apply:
0007: *
0008: * This program is free software; you can redistribute it and/or modify
0009: * it under the terms of the GNU General Public License version 2 as published by
0010: * the Free Software Foundation.
0011: *
0012: * This program is distributed WITHOUT ANY WARRANTY; and without the
0013: * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0014: * See the GNU General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU General Public License
0017: * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
0018: * or write to:
0019: *
0020: * Free Software Foundation, Inc.,
0021: * 59 Temple Place - Suite 330,
0022: * Boston, MA USA 02111-1307
0023: *
0024: *
0025: *
0026: *
0027: * ExpressionEditor.java
0028: *
0029: * Created on 17 novembre 2003, 22.03
0030: *
0031: */
0032:
0033: package it.businesslogic.ireport.gui;
0034:
0035: import bsh.EvalError;
0036: import bsh.ParseException;
0037: import bsh.TargetError;
0038: import groovy.lang.Binding;
0039: import groovy.lang.GroovyShell;
0040: import it.businesslogic.ireport.CrosstabReportElement;
0041: import it.businesslogic.ireport.ExpressionContext;
0042: import it.businesslogic.ireport.IRParsingException;
0043: import it.businesslogic.ireport.JRField;
0044: import it.businesslogic.ireport.JRParameter;
0045: import it.businesslogic.ireport.JRVariable;
0046: import it.businesslogic.ireport.SubDataset;
0047: import it.businesslogic.ireport.crosstab.CrosstabGroup;
0048: import it.businesslogic.ireport.crosstab.CrosstabParameter;
0049: import it.businesslogic.ireport.crosstab.Measure;
0050: import it.businesslogic.ireport.gui.expbuilder.ArithmeticOperationHelper;
0051: import it.businesslogic.ireport.gui.expbuilder.ExpObject;
0052: import it.businesslogic.ireport.gui.expbuilder.ExpObjectCellRenderer;
0053: import it.businesslogic.ireport.util.I18n;
0054: import it.businesslogic.ireport.util.JRFakeObject;
0055: import it.businesslogic.ireport.util.Misc;
0056: import java.awt.Color;
0057: import java.io.StringReader;
0058: import java.text.MessageFormat;
0059: import javax.swing.event.CaretEvent;
0060: import javax.swing.event.CaretListener;
0061: import javax.swing.event.ChangeEvent;
0062: import javax.swing.event.ChangeListener;
0063: import javax.swing.text.Style;
0064: import javax.swing.text.StyleConstants;
0065: import javax.swing.tree.*;
0066: import javax.swing.*;
0067: import java.util.*;
0068: import net.sf.jasperreports.engine.JRExpression;
0069: import net.sf.jasperreports.engine.JRExpressionChunk;
0070: import net.sf.jasperreports.engine.design.JRDesignExpression;
0071: import net.sf.jasperreports.engine.util.JRStringUtil;
0072: import org.codehaus.groovy.control.CompilationFailedException;
0073: import javax.swing.text.DefaultStyledDocument;
0074:
0075: /**
0076: *
0077: * @author Administrator
0078: */
0079: public class ExpressionEditor extends javax.swing.JDialog implements
0080: CaretListener {
0081:
0082: //public final static int CONTEXT_REPORT_DATASET = 0;
0083: //public final static int CONTEXT_BUCKET_MEASURE = 1;
0084: //public final static int CONTEXT_DATASET_RUN = 2;
0085: //public final static int CONTEXT_STYLE = 3;
0086:
0087: public static final int VARIABLE = 1;
0088: public static final int PARAMETER = 2;
0089: public static final int FIELD = 3;
0090:
0091: private SubDataset subDataset = null;
0092: private Vector crosstabElements = new Vector();
0093:
0094: public static Vector defaultExpressions = null;
0095: public boolean init = false;
0096:
0097: public static Vector recentExpressions = new Vector();
0098:
0099: Style errorStyle = null;
0100: Style okStyle = null;
0101: DefaultStyledDocument doc = null;
0102:
0103: static {
0104: defaultExpressions = new Vector();
0105: defaultExpressions.add("( <condition> ? exp1 : exp2 )");
0106: defaultExpressions.add("msg(<pattern>, <arg0>)");
0107: defaultExpressions.add("msg(<pattern>, <arg0>, <arg1>)");
0108: defaultExpressions
0109: .add("msg(<pattern>, <arg0>, <arg1>, <arg2>)");
0110: defaultExpressions.add("str(<key>)");
0111: defaultExpressions
0112: .add("((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).subDataSource(<select expression>)");
0113: defaultExpressions
0114: .add("((net.sf.jasperreports.engine.data.JRXmlDataSource)$P{REPORT_DATA_SOURCE}).dataSource(<select expression>)");
0115: }
0116:
0117: /** Creates new form ExpressionEditor */
0118: public ExpressionEditor() {
0119: super (MainFrame.getMainInstance(), true);
0120: initComponents();
0121: this .setSize(750, 450);
0122:
0123: this .setModal(true);
0124: //this.setModal(true);
0125: org.syntax.jedit.SyntaxDocument sd = new org.syntax.jedit.SyntaxDocument();
0126: sd
0127: .setTokenMarker(new org.syntax.jedit.tokenmarker.JavaTokenMarker());
0128:
0129: this .jEditTextArea1.setDocument(sd);
0130: this .jEditTextArea1.getPainter().setEOLMarkersPainted(false);
0131: this .jEditTextArea1.getPainter().setInvalidLinesPainted(false);
0132: this .jEditTextArea1.getPainter().setLineHighlightEnabled(false);
0133:
0134: DefaultMutableTreeNode dmtn = new DefaultMutableTreeNode("root");
0135:
0136: this .jTree1
0137: .setCellRenderer(new DocumentExpressionEditorTreeCellRenderer());
0138: this .jTree1
0139: .setModel(new javax.swing.tree.DefaultTreeModel(dmtn));
0140:
0141: it.businesslogic.ireport.util.Misc.centerFrame(this );
0142:
0143: jList1.setModel(new DefaultListModel());
0144: jList2.setModel(new DefaultListModel());
0145:
0146: jEditTextArea1.requestFocusInWindow();
0147:
0148: jEditTextArea1.getDocument().addDocumentListener(
0149: new javax.swing.event.DocumentListener() {
0150: public void changedUpdate(
0151: javax.swing.event.DocumentEvent evt) {
0152: jRTextExpressionAreaTextChanged();
0153: }
0154:
0155: public void insertUpdate(
0156: javax.swing.event.DocumentEvent evt) {
0157: jRTextExpressionAreaTextChanged();
0158: }
0159:
0160: public void removeUpdate(
0161: javax.swing.event.DocumentEvent evt) {
0162: jRTextExpressionAreaTextChanged();
0163: }
0164: });
0165:
0166: jEditTextArea1.addCaretListener(this );
0167:
0168: javax.swing.KeyStroke escape = javax.swing.KeyStroke
0169: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
0170: false);
0171: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
0172: public void actionPerformed(java.awt.event.ActionEvent e) {
0173: setVisible(false);
0174: }
0175: };
0176:
0177: getRootPane().getInputMap(
0178: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
0179: escape, "ESCAPE");
0180: getRootPane().getActionMap().put("ESCAPE", escapeAction);
0181:
0182: jList1.getSelectionModel().setSelectionMode(
0183: jList1.getSelectionModel().SINGLE_SELECTION);
0184: jList1.setCellRenderer(new ExpObjectCellRenderer(jList1));
0185: jList2.setCellRenderer(new TextAreaCellRenderer(jList2));
0186:
0187: //to make the default button ...
0188: //this.getRootPane().setDefaultButton(this.jButtonOK);
0189: jSplitPane3.updateUI();
0190: applyI18n();
0191: caretUpdate(null);
0192:
0193: jTabbedPane1.addChangeListener(new ChangeListener() {
0194: public void stateChanged(ChangeEvent e) {
0195: if (jTabbedPane1.getSelectedIndex() == 1) {
0196: SwingUtilities.invokeLater(new Runnable() {
0197:
0198: public void run() {
0199: try {
0200: validateExpression();
0201: } catch (Exception ex) {
0202: setErrorText(
0203: "Exception occurred during the expression validation",
0204: true);
0205: }
0206: }
0207: });
0208: }
0209: }
0210: });
0211:
0212: doc = new DefaultStyledDocument();
0213:
0214: jTextPaneErrors.setDocument(doc);
0215: errorStyle = doc.addStyle("errorStyle", null);
0216: StyleConstants.setForeground(errorStyle, Color.red);
0217: okStyle = doc.addStyle("okStyle", null);
0218: StyleConstants.setForeground(okStyle, Color.blue);
0219:
0220: this .pack();
0221: }
0222:
0223: /** This method is called from within the constructor to
0224: * initialize the form.
0225: * WARNING: Do NOT modify this code. The content of this method is
0226: * always regenerated by the Form Editor.
0227: */
0228: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
0229: private void initComponents() {
0230: java.awt.GridBagConstraints gridBagConstraints;
0231:
0232: jPanel1 = new javax.swing.JPanel();
0233: jSplitPane1 = new javax.swing.JSplitPane();
0234: jPanel6 = new javax.swing.JPanel();
0235: jEditTextArea1 = new org.syntax.jedit.JEditTextArea();
0236: jLabel1 = new javax.swing.JLabel();
0237: jLabelCaretPosition = new javax.swing.JLabel();
0238: jPanel5 = new javax.swing.JPanel();
0239: jTabbedPane1 = new javax.swing.JTabbedPane();
0240: jSplitPaneOAE = new javax.swing.JSplitPane();
0241: jPanel4 = new javax.swing.JPanel();
0242: jSplitPane3 = new javax.swing.JSplitPane();
0243: jScrollPane1 = new javax.swing.JScrollPane();
0244: jList1 = new javax.swing.JList();
0245: jScrollPane3 = new javax.swing.JScrollPane();
0246: jList2 = new javax.swing.JList();
0247: jPanel3 = new javax.swing.JPanel();
0248: jScrollPane2 = new javax.swing.JScrollPane();
0249: jTree1 = new javax.swing.JTree();
0250: jScrollPane4 = new javax.swing.JScrollPane();
0251: jTextPaneErrors = new javax.swing.JTextPane();
0252: jPanel2 = new javax.swing.JPanel();
0253: jButtonLoadExpression = new javax.swing.JButton();
0254: jButtonSaveExpression = new javax.swing.JButton();
0255: jButtonCheckExpression = new javax.swing.JButton();
0256: jButtonApply = new javax.swing.JButton();
0257: jButtonCancel = new javax.swing.JButton();
0258:
0259: setTitle("Expression editor...");
0260: setModal(true);
0261: addComponentListener(new java.awt.event.ComponentAdapter() {
0262: public void componentResized(
0263: java.awt.event.ComponentEvent evt) {
0264: formComponentResized(evt);
0265: }
0266: });
0267: addWindowListener(new java.awt.event.WindowAdapter() {
0268: public void windowClosing(java.awt.event.WindowEvent evt) {
0269: exitForm(evt);
0270: }
0271: });
0272:
0273: jPanel1.setLayout(new java.awt.BorderLayout());
0274:
0275: jPanel1.setPreferredSize(new java.awt.Dimension(752, 416));
0276: jSplitPane1.setDividerSize(4);
0277: jSplitPane1
0278: .setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
0279: jSplitPane1.setResizeWeight(0.5);
0280: jPanel6.setLayout(new java.awt.GridBagLayout());
0281:
0282: jEditTextArea1
0283: .setBorder(javax.swing.BorderFactory
0284: .createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
0285: jEditTextArea1.setFocusCycleRoot(true);
0286: jEditTextArea1.setFont(new java.awt.Font("Courier New", 0, 11));
0287: jEditTextArea1.setMinimumSize(new java.awt.Dimension(20, 20));
0288: jEditTextArea1
0289: .setPreferredSize(new java.awt.Dimension(750, 150));
0290: jEditTextArea1
0291: .addComponentListener(new java.awt.event.ComponentAdapter() {
0292: public void componentResized(
0293: java.awt.event.ComponentEvent evt) {
0294: jEditTextArea1ComponentResized(evt);
0295: }
0296: });
0297: jEditTextArea1
0298: .addInputMethodListener(new java.awt.event.InputMethodListener() {
0299: public void caretPositionChanged(
0300: java.awt.event.InputMethodEvent evt) {
0301: }
0302:
0303: public void inputMethodTextChanged(
0304: java.awt.event.InputMethodEvent evt) {
0305: jEditTextArea1InputMethodTextChanged(evt);
0306: }
0307: });
0308:
0309: gridBagConstraints = new java.awt.GridBagConstraints();
0310: gridBagConstraints.gridwidth = 2;
0311: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
0312: gridBagConstraints.weightx = 1.0;
0313: gridBagConstraints.weighty = 1.0;
0314: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
0315: jPanel6.add(jEditTextArea1, gridBagConstraints);
0316:
0317: jLabel1.setForeground(new java.awt.Color(255, 0, 51));
0318: jLabel1.setText(" ");
0319: gridBagConstraints = new java.awt.GridBagConstraints();
0320: gridBagConstraints.gridx = 0;
0321: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
0322: gridBagConstraints.weightx = 1.0;
0323: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
0324: jPanel6.add(jLabel1, gridBagConstraints);
0325:
0326: jLabelCaretPosition
0327: .setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
0328: jLabelCaretPosition.setText("Line: 1 Column: 1");
0329: gridBagConstraints = new java.awt.GridBagConstraints();
0330: gridBagConstraints.gridx = 1;
0331: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
0332: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
0333: jPanel6.add(jLabelCaretPosition, gridBagConstraints);
0334:
0335: jSplitPane1.setTopComponent(jPanel6);
0336:
0337: jPanel5.setLayout(new java.awt.GridBagLayout());
0338:
0339: jSplitPaneOAE.setBorder(null);
0340: jSplitPaneOAE.setDividerSize(4);
0341: jSplitPaneOAE.setResizeWeight(0.1);
0342: jSplitPaneOAE.setAutoscrolls(true);
0343: jSplitPaneOAE
0344: .setPreferredSize(new java.awt.Dimension(361, 160));
0345: jPanel4.setLayout(new java.awt.BorderLayout());
0346:
0347: jPanel4.setPreferredSize(new java.awt.Dimension(100, 100));
0348: jSplitPane3.setBorder(null);
0349: jSplitPane3.setDividerSize(4);
0350: jSplitPane3.setResizeWeight(0.8);
0351: jList1
0352: .addListSelectionListener(new javax.swing.event.ListSelectionListener() {
0353: public void valueChanged(
0354: javax.swing.event.ListSelectionEvent evt) {
0355: jList1ValueChanged(evt);
0356: }
0357: });
0358: jList1.addMouseListener(new java.awt.event.MouseAdapter() {
0359: public void mouseClicked(java.awt.event.MouseEvent evt) {
0360: jList1MouseClicked(evt);
0361: }
0362: });
0363:
0364: jScrollPane1.setViewportView(jList1);
0365:
0366: jSplitPane3.setLeftComponent(jScrollPane1);
0367:
0368: jList2.addMouseListener(new java.awt.event.MouseAdapter() {
0369: public void mouseClicked(java.awt.event.MouseEvent evt) {
0370: jList2MouseClicked(evt);
0371: }
0372: });
0373:
0374: jScrollPane3.setViewportView(jList2);
0375:
0376: jSplitPane3.setRightComponent(jScrollPane3);
0377:
0378: jPanel4.add(jSplitPane3, java.awt.BorderLayout.CENTER);
0379:
0380: jSplitPaneOAE.setRightComponent(jPanel4);
0381: jPanel4.getAccessibleContext().setAccessibleParent(jPanel1);
0382:
0383: jPanel3.setLayout(new java.awt.BorderLayout());
0384:
0385: jScrollPane2.setPreferredSize(new java.awt.Dimension(100, 100));
0386: jTree1.setRootVisible(false);
0387: jTree1
0388: .addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
0389: public void valueChanged(
0390: javax.swing.event.TreeSelectionEvent evt) {
0391: jTree1ValueChanged(evt);
0392: }
0393: });
0394:
0395: jScrollPane2.setViewportView(jTree1);
0396:
0397: jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);
0398:
0399: jSplitPaneOAE.setLeftComponent(jPanel3);
0400: jPanel3.getAccessibleContext().setAccessibleParent(jPanel1);
0401:
0402: jTabbedPane1.addTab("Objects and expressions", jSplitPaneOAE);
0403:
0404: jTextPaneErrors.setEditable(false);
0405: jScrollPane4.setViewportView(jTextPaneErrors);
0406:
0407: jTabbedPane1.addTab("Validation errors", jScrollPane4);
0408:
0409: gridBagConstraints = new java.awt.GridBagConstraints();
0410: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
0411: gridBagConstraints.weightx = 1.0;
0412: gridBagConstraints.weighty = 1.0;
0413: jPanel5.add(jTabbedPane1, gridBagConstraints);
0414:
0415: jSplitPane1.setRightComponent(jPanel5);
0416:
0417: jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER);
0418:
0419: getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
0420:
0421: jPanel2.setLayout(new java.awt.GridBagLayout());
0422:
0423: jPanel2.setMinimumSize(new java.awt.Dimension(10, 30));
0424: jPanel2.setPreferredSize(new java.awt.Dimension(10, 30));
0425: jButtonLoadExpression.setLabel("Import...");
0426: jButtonLoadExpression
0427: .addActionListener(new java.awt.event.ActionListener() {
0428: public void actionPerformed(
0429: java.awt.event.ActionEvent evt) {
0430: jButtonLoadExpressionActionPerformed1(evt);
0431: }
0432: });
0433:
0434: gridBagConstraints = new java.awt.GridBagConstraints();
0435: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
0436: jPanel2.add(jButtonLoadExpression, gridBagConstraints);
0437:
0438: jButtonSaveExpression.setText("Export...");
0439: jButtonSaveExpression
0440: .addActionListener(new java.awt.event.ActionListener() {
0441: public void actionPerformed(
0442: java.awt.event.ActionEvent evt) {
0443: jButtonSaveExpressionActionPerformed1(evt);
0444: }
0445: });
0446:
0447: gridBagConstraints = new java.awt.GridBagConstraints();
0448: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
0449: jPanel2.add(jButtonSaveExpression, gridBagConstraints);
0450:
0451: jButtonCheckExpression.setText("Check expression");
0452: jButtonCheckExpression
0453: .addActionListener(new java.awt.event.ActionListener() {
0454: public void actionPerformed(
0455: java.awt.event.ActionEvent evt) {
0456: jButtonCheckExpressionActionPerformed(evt);
0457: }
0458: });
0459:
0460: gridBagConstraints = new java.awt.GridBagConstraints();
0461: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
0462: jPanel2.add(jButtonCheckExpression, gridBagConstraints);
0463:
0464: jButtonApply.setMnemonic('a');
0465: jButtonApply.setText("Apply");
0466: jButtonApply
0467: .addActionListener(new java.awt.event.ActionListener() {
0468: public void actionPerformed(
0469: java.awt.event.ActionEvent evt) {
0470: jButtonApplyActionPerformed(evt);
0471: }
0472: });
0473:
0474: gridBagConstraints = new java.awt.GridBagConstraints();
0475: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
0476: gridBagConstraints.weightx = 1.0;
0477: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
0478: jPanel2.add(jButtonApply, gridBagConstraints);
0479:
0480: jButtonCancel.setMnemonic('c');
0481: jButtonCancel.setText("Cancel");
0482: jButtonCancel
0483: .addActionListener(new java.awt.event.ActionListener() {
0484: public void actionPerformed(
0485: java.awt.event.ActionEvent evt) {
0486: jButtonCancelActionPerformed(evt);
0487: }
0488: });
0489:
0490: gridBagConstraints = new java.awt.GridBagConstraints();
0491: gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
0492: jPanel2.add(jButtonCancel, gridBagConstraints);
0493:
0494: getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);
0495:
0496: pack();
0497: }// </editor-fold>//GEN-END:initComponents
0498:
0499: private void jButtonCheckExpressionActionPerformed(
0500: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCheckExpressionActionPerformed
0501:
0502: SwingUtilities.invokeLater(new Runnable() {
0503:
0504: public void run() {
0505: try {
0506: validateExpression();
0507: } catch (Exception ex) {
0508: setErrorText(
0509: "Exception occurred during the expression validation",
0510: true);
0511: }
0512: }
0513: });
0514: }//GEN-LAST:event_jButtonCheckExpressionActionPerformed
0515:
0516: private void jButtonSaveExpressionActionPerformed1(
0517: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveExpressionActionPerformed1
0518: Misc.saveExpression(jEditTextArea1.getText(), this );
0519: }//GEN-LAST:event_jButtonSaveExpressionActionPerformed1
0520:
0521: private void jButtonLoadExpressionActionPerformed1(
0522: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadExpressionActionPerformed1
0523: String expression = Misc.loadExpression(this );
0524:
0525: if (expression != null) {
0526: jEditTextArea1.setText(expression);
0527: }
0528: }//GEN-LAST:event_jButtonLoadExpressionActionPerformed1
0529:
0530: static int xxx = 0;
0531:
0532: public void jRTextExpressionAreaTextChanged() {
0533: if (init)
0534: return;
0535: jLabel1.setText(" ");
0536: SwingUtilities.invokeLater(new Runnable() {
0537: public void run() {
0538: try {
0539: bsh.Parser parser = new bsh.Parser(
0540: new StringReader(jEditTextArea1.getText()
0541: + ";"));
0542: while (!parser.Line()) {
0543: //SimpleNode node = parser.popNode();
0544: // use the node, etc. (See bsh.BSH* classes)
0545: }
0546:
0547: } catch (Throwable ex) {
0548: jLabel1.setText("" + ex.getMessage());
0549: }
0550: }
0551: });
0552:
0553: }
0554:
0555: private void jEditTextArea1InputMethodTextChanged(
0556: java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_jEditTextArea1InputMethodTextChanged
0557:
0558: }//GEN-LAST:event_jEditTextArea1InputMethodTextChanged
0559:
0560: private void jList2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList2MouseClicked
0561:
0562: if (evt.getButton() == evt.BUTTON1 && evt.getClickCount() == 2) {
0563: if (jList1.getSelectedValue() != null
0564: && jList2.getSelectedValue() != null) {
0565: try {
0566:
0567: String objName = "";
0568: if (jList1.getSelectedValue() instanceof ExpObject) {
0569: objName = ((ExpObject) jList1
0570: .getSelectedValue()).getExpression();
0571: } else {
0572: objName = "" + jList1.getSelectedValue();
0573: }
0574:
0575: String method = (jList2.getSelectedValue() + "");
0576: method = method.substring(0, method
0577: .lastIndexOf(")") + 1);
0578: jEditTextArea1.getDocument().replace(
0579: jEditTextArea1.getSelectionStart(),
0580: jEditTextArea1.getSelectionEnd()
0581: - jEditTextArea1
0582: .getSelectionStart(),
0583: objName + "." + method, null);
0584: } catch (Exception ex) {
0585: }
0586: }
0587: }
0588: }//GEN-LAST:event_jList2MouseClicked
0589:
0590: private void jList1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jList1MouseClicked
0591:
0592: if (evt.getButton() == evt.BUTTON1 && evt.getClickCount() == 2) {
0593: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) jTree1
0594: .getSelectionPath().getLastPathComponent();
0595: if (("" + dmtn.getUserObject()).equalsIgnoreCase(I18n
0596: .getString("Wizards", "Wizards"))) {
0597: ArithmeticOperationHelper aoe = new ArithmeticOperationHelper();
0598:
0599: aoe.setLanguage(MainFrame.getMainInstance()
0600: .getActiveReportFrame().getReport()
0601: .getLanguage());
0602:
0603: if ((jList1.getSelectedValue() + "").equals(I18n
0604: .getString("Addition", "Addition (+)"))) {
0605: aoe.setOperation("+");
0606: }
0607: if ((jList1.getSelectedValue() + "").equals(I18n
0608: .getString("Subtraction", "Subtraction (-)"))) {
0609: aoe.setOperation("-");
0610: }
0611: if ((jList1.getSelectedValue() + "").equals(I18n
0612: .getString("Division", "Division (/)"))) {
0613: aoe.setOperation("/");
0614: }
0615: if ((jList1.getSelectedValue() + "").equals(I18n
0616: .getString("Multiplication",
0617: "Multiplication (*)"))) {
0618: aoe.setOperation("*");
0619: }
0620:
0621: // Collect all objects...
0622: aoe.setValues(getAllObjects());
0623:
0624: if (aoe.showDialog(this ) == JOptionPane.OK_OPTION) {
0625:
0626: String exp = aoe.getExpression();
0627: try {
0628: jEditTextArea1.getDocument().replace(
0629: jEditTextArea1.getSelectionStart(),
0630: jEditTextArea1.getSelectionEnd()
0631: - jEditTextArea1
0632: .getSelectionStart(),
0633: exp, null);
0634: } catch (Exception ex) {
0635: }
0636: }
0637: } else {
0638: try {
0639: String objName = "";
0640: if (jList1.getSelectedValue() instanceof ExpObject) {
0641: objName = ((ExpObject) jList1
0642: .getSelectedValue()).getExpression();
0643: } else {
0644: objName = "" + jList1.getSelectedValue();
0645: }
0646:
0647: jEditTextArea1.getDocument().replace(
0648: jEditTextArea1.getSelectionStart(),
0649: jEditTextArea1.getSelectionEnd()
0650: - jEditTextArea1
0651: .getSelectionStart(),
0652: objName + "", null);
0653: } catch (Exception ex) {
0654: }
0655: }
0656: }
0657: }//GEN-LAST:event_jList1MouseClicked
0658:
0659: private void jList1ValueChanged(
0660: javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
0661:
0662: DefaultListModel dlm = (DefaultListModel) jList2.getModel();
0663: dlm.removeAllElements();
0664:
0665: Class clazz = null; //getSelectedObjectClass();
0666:
0667: if (jList1.getSelectedValue() instanceof ExpObject) {
0668: try {
0669: clazz = this .getClass().getClassLoader().loadClass(
0670: ((ExpObject) jList1.getSelectedValue())
0671: .getClassType());
0672:
0673: } catch (Throwable ex) {
0674:
0675: }
0676: }
0677:
0678: if (clazz != null) {
0679: java.lang.reflect.Method[] methods = clazz.getMethods();
0680: for (int i = 0; i < methods.length; ++i) {
0681: if ((methods[i].getModifiers() & java.lang.reflect.Modifier.PUBLIC) != 0) {
0682: String method_firm = methods[i].getName() + "(";
0683: Class[] params = methods[i].getParameterTypes();
0684: int j = 0;
0685: for (j = 0; j < params.length; ++j) {
0686:
0687: if (j > 0)
0688: method_firm += ", ";
0689: else
0690: method_firm += " ";
0691: method_firm += getPrintableTypeName(params[j]
0692: .getName());
0693: }
0694: if (j > 0)
0695: method_firm += " ";
0696: method_firm += ") ";
0697:
0698: String rname = methods[i].getReturnType().getName();
0699: if (rname.equals("void"))
0700: continue; // we have to return something always!
0701: method_firm += getPrintableTypeName(rname);
0702: dlm.addElement(method_firm);
0703: }
0704: }
0705: }
0706:
0707: }//GEN-LAST:event_jList1ValueChanged
0708:
0709: public String getPrintableTypeName(String type) {
0710: if (type == null)
0711: return "void";
0712:
0713: if (type.endsWith(";"))
0714: type = type.substring(0, type.length() - 1);
0715:
0716: while (type.startsWith("[")) {
0717: type = type.substring(1) + "[]";
0718: if (type.startsWith("["))
0719: continue;
0720: if (type.startsWith("L"))
0721: type = type.substring(1);
0722: if (type.startsWith("Z"))
0723: type = "boolean" + type.substring(1);
0724: if (type.startsWith("B"))
0725: type = "byte" + type.substring(1);
0726: if (type.startsWith("C"))
0727: type = "char" + type.substring(1);
0728: if (type.startsWith("D"))
0729: type = "double" + type.substring(1);
0730: if (type.startsWith("F"))
0731: type = "float" + type.substring(1);
0732: if (type.startsWith("I"))
0733: type = "int" + type.substring(1);
0734: if (type.startsWith("J"))
0735: type = "long" + type.substring(1);
0736: if (type.startsWith("S"))
0737: type = "short" + type.substring(1);
0738: }
0739:
0740: if (type.startsWith("java.lang.")) {
0741: type = type.substring("java.lang.".length());
0742: if (type.indexOf(".") > 0) {
0743: type = "java.lang." + type;
0744: }
0745: }
0746: return type;
0747: }
0748:
0749: private void jTree1ValueChanged(
0750: javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
0751:
0752: ((DefaultListModel) jList1.getModel()).removeAllElements();
0753:
0754: //if (getSubDataset() == null) return;
0755: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) jTree1
0756: .getSelectionPath().getLastPathComponent();
0757: if (dmtn.getUserObject() instanceof CrosstabReportElementWrapper) {
0758: CrosstabReportElement cr = ((CrosstabReportElementWrapper) dmtn
0759: .getUserObject()).getCrosstabReportElement();
0760: Enumeration e = cr.getMeasures().elements();
0761: while (e.hasMoreElements()) {
0762: Measure measure = (Measure) e.nextElement();
0763: //( (DefaultListModel)jList1.getModel()).addElement( "$V{"+measure+"}" );
0764: ((DefaultListModel) jList1.getModel())
0765: .addElement(new ExpObject(measure.getName(),
0766: ExpObject.TYPE_VARIABLE, measure
0767: .getClassType()));
0768:
0769: for (int j = 0; j < cr.getRowGroups().size(); ++j) {
0770: CrosstabGroup group = (CrosstabGroup) cr
0771: .getRowGroups().elementAt(j);
0772: //((DefaultListModel)jList1.getModel()).addElement( "$V{"+measure+"_"+group.getName()+"_"+"ALL}");
0773: ((DefaultListModel) jList1.getModel())
0774: .addElement(new ExpObject(measure.getName()
0775: + "_" + group.getName() + "_"
0776: + "ALL", ExpObject.TYPE_VARIABLE,
0777: group.getBucketExpressionClass()));
0778: }
0779: for (int j = 0; j < cr.getColumnGroups().size(); ++j) {
0780: CrosstabGroup group = (CrosstabGroup) cr
0781: .getColumnGroups().elementAt(j);
0782: //((DefaultListModel)jList1.getModel()).addElement( "$V{"+measure+"_"+group.getName()+"_"+"ALL}");
0783: ((DefaultListModel) jList1.getModel())
0784: .addElement(new ExpObject(measure.getName()
0785: + "_" + group.getName() + "_"
0786: + "ALL", ExpObject.TYPE_VARIABLE,
0787: group.getBucketExpressionClass()));
0788: }
0789: }
0790:
0791: for (int i = 0; i < cr.getRowGroups().size(); ++i) {
0792: CrosstabGroup group = (CrosstabGroup) cr.getRowGroups()
0793: .elementAt(i);
0794: //( (DefaultListModel)jList1.getModel()).addElement( "$V{"+group +"}" );
0795: ((DefaultListModel) jList1.getModel())
0796: .addElement(new ExpObject(group.getName(),
0797: ExpObject.TYPE_VARIABLE, group
0798: .getBucketExpressionClass()));
0799: }
0800:
0801: for (int i = 0; i < cr.getColumnGroups().size(); ++i) {
0802: CrosstabGroup group = (CrosstabGroup) cr
0803: .getColumnGroups().elementAt(i);
0804: //( (DefaultListModel)jList1.getModel()).addElement( "$V{"+group +"}" );
0805: ((DefaultListModel) jList1.getModel())
0806: .addElement(new ExpObject(group.getName(),
0807: ExpObject.TYPE_VARIABLE, group
0808: .getBucketExpressionClass()));
0809: }
0810:
0811: for (int i = 0; i < cr.getCrosstabParameters().size(); ++i) {
0812: CrosstabParameter parameter = (CrosstabParameter) cr
0813: .getCrosstabParameters().elementAt(i);
0814: //( (DefaultListModel)jList1.getModel()).addElement( "$P{"+parameter +"}" );
0815: ((DefaultListModel) jList1.getModel())
0816: .addElement(new ExpObject(parameter.getName(),
0817: ExpObject.TYPE_PARAM, parameter
0818: .getClassType()));
0819: }
0820:
0821: } else if (("" + dmtn.getUserObject())
0822: .equalsIgnoreCase("Fields")
0823: && getSubDataset() != null) {
0824: Enumeration e = getSubDataset().getFields().elements();
0825: while (e.hasMoreElements()) {
0826: JRField field = (JRField) e.nextElement();
0827: //( (DefaultListModel)jList1.getModel()).addElement( "$F{"+e.nextElement() +"}" );
0828: ((DefaultListModel) jList1.getModel())
0829: .addElement(new ExpObject(field.getName(),
0830: ExpObject.TYPE_FIELD, field
0831: .getClassType()));
0832: }
0833: } else if (("" + dmtn.getUserObject())
0834: .equalsIgnoreCase("Parameters")
0835: && getSubDataset() != null) {
0836: Enumeration e = getSubDataset().getParameters().elements();
0837: while (e.hasMoreElements()) {
0838: JRParameter parameter = (JRParameter) e.nextElement();
0839: //( (DefaultListModel)jList1.getModel()).addElement( "$P{"+ e.nextElement()+"}" );
0840: ((DefaultListModel) jList1.getModel())
0841: .addElement(new ExpObject(parameter.getName(),
0842: ExpObject.TYPE_PARAM, parameter
0843: .getClassType()));
0844:
0845: }
0846: } else if (("" + dmtn.getUserObject())
0847: .equalsIgnoreCase("Variables")
0848: && getSubDataset() != null) {
0849: Enumeration e = getSubDataset().getVariables().elements();
0850: while (e.hasMoreElements()) {
0851: JRVariable variable = (JRVariable) e.nextElement();
0852: //( (DefaultListModel)jList1.getModel()).addElement( "$V{"+e.nextElement() +"}" );
0853: ((DefaultListModel) jList1.getModel())
0854: .addElement(new ExpObject(variable.getName(),
0855: ExpObject.TYPE_VARIABLE, variable
0856: .getClassType()));
0857: }
0858: } else if (("" + dmtn.getUserObject()).equalsIgnoreCase(I18n
0859: .getString("Formulas", "Formulas"))) {
0860: for (int i = 0; i < defaultExpressions.size(); ++i) {
0861: ((DefaultListModel) jList1.getModel())
0862: .addElement(defaultExpressions.elementAt(i));
0863: }
0864: } else if (("" + dmtn.getUserObject()).equalsIgnoreCase(I18n
0865: .getString("RecentExpressions", "Recent expressions"))) {
0866: for (int i = 0; i < recentExpressions.size(); ++i) {
0867: ((DefaultListModel) jList1.getModel())
0868: .addElement(recentExpressions.elementAt(i));
0869: }
0870: } else if (("" + dmtn.getUserObject()).equalsIgnoreCase(I18n
0871: .getString("Wizards", "Wizards"))) {
0872: // Adding wizards...
0873: ((DefaultListModel) jList1.getModel()).addElement(I18n
0874: .getString("Addition", "Addition (+)"));
0875: ((DefaultListModel) jList1.getModel()).addElement(I18n
0876: .getString("Subtraction", "Subtraction (-)"));
0877: ((DefaultListModel) jList1.getModel()).addElement(I18n
0878: .getString("Division", "Division (/)"));
0879: ((DefaultListModel) jList1.getModel()).addElement(I18n
0880: .getString("Multiplication", "Multiplication (*)"));
0881: }
0882: }//GEN-LAST:event_jTree1ValueChanged
0883:
0884: private void jButtonCancelActionPerformed(
0885: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
0886: //this.setExpression( this.jEditTextArea1.getText() );
0887: this .setDialogResult(javax.swing.JOptionPane.NO_OPTION);
0888: this .dispose();
0889: }//GEN-LAST:event_jButtonCancelActionPerformed
0890:
0891: private void jButtonApplyActionPerformed(
0892: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonApplyActionPerformed
0893: this .setExpression(this .jEditTextArea1.getText());
0894:
0895: recentExpressions.add(this .jEditTextArea1.getText());
0896:
0897: this .setDialogResult(javax.swing.JOptionPane.OK_OPTION);
0898: this .dispose();
0899: }//GEN-LAST:event_jButtonApplyActionPerformed
0900:
0901: private void jEditTextArea1ComponentResized(
0902: java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jEditTextArea1ComponentResized
0903: // Add your handling code here:
0904: }//GEN-LAST:event_jEditTextArea1ComponentResized
0905:
0906: private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
0907: //this.jEditTextArea1.recalculateVisibleLines();
0908: this .jEditTextArea1.updateScrollBars();
0909: }//GEN-LAST:event_formComponentResized
0910:
0911: /** Exit the Application */
0912: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
0913:
0914: }//GEN-LAST:event_exitForm
0915:
0916: /**
0917: * @param args the command line arguments
0918: */
0919: public static void main(String args[]) {
0920: new ExpressionEditor().setVisible(true);
0921: }
0922:
0923: /** Getter for property Expression.
0924: * @return Value of property Expression.
0925: *
0926: */
0927: public java.lang.String getExpression() {
0928: return Expression;
0929: }
0930:
0931: /** Setter for property Expression.
0932: * @param Expression New value of property Expression.
0933: *
0934: */
0935: public void setExpression(java.lang.String Expression) {
0936: init = true;
0937: this .jEditTextArea1.setText(Expression);
0938: this .Expression = Expression;
0939: init = false;
0940: }
0941:
0942: /** Getter for property dialogResult.
0943: * @return Value of property dialogResult.
0944: *
0945: */
0946: public int getDialogResult() {
0947: return dialogResult;
0948: }
0949:
0950: /** Setter for property dialogResult.
0951: * @param dialogResult New value of property dialogResult.
0952: *
0953: */
0954: public void setDialogResult(int dialogResult) {
0955: this .dialogResult = dialogResult;
0956: }
0957:
0958: // Variables declaration - do not modify//GEN-BEGIN:variables
0959: private javax.swing.JButton jButtonApply;
0960: private javax.swing.JButton jButtonCancel;
0961: private javax.swing.JButton jButtonCheckExpression;
0962: private javax.swing.JButton jButtonLoadExpression;
0963: private javax.swing.JButton jButtonSaveExpression;
0964: private org.syntax.jedit.JEditTextArea jEditTextArea1;
0965: private javax.swing.JLabel jLabel1;
0966: private javax.swing.JLabel jLabelCaretPosition;
0967: private javax.swing.JList jList1;
0968: private javax.swing.JList jList2;
0969: private javax.swing.JPanel jPanel1;
0970: private javax.swing.JPanel jPanel2;
0971: private javax.swing.JPanel jPanel3;
0972: private javax.swing.JPanel jPanel4;
0973: private javax.swing.JPanel jPanel5;
0974: private javax.swing.JPanel jPanel6;
0975: private javax.swing.JScrollPane jScrollPane1;
0976: private javax.swing.JScrollPane jScrollPane2;
0977: private javax.swing.JScrollPane jScrollPane3;
0978: private javax.swing.JScrollPane jScrollPane4;
0979: private javax.swing.JSplitPane jSplitPane1;
0980: private javax.swing.JSplitPane jSplitPane3;
0981: private javax.swing.JSplitPane jSplitPaneOAE;
0982: private javax.swing.JTabbedPane jTabbedPane1;
0983: private javax.swing.JTextPane jTextPaneErrors;
0984: private javax.swing.JTree jTree1;
0985: // End of variables declaration//GEN-END:variables
0986:
0987: private String Expression;
0988:
0989: private int dialogResult;
0990:
0991: public SubDataset getSubDataset() {
0992: return subDataset;
0993: }
0994:
0995: public void setSubDataset(SubDataset subDataset) {
0996: this .subDataset = subDataset;
0997:
0998: // Selezioniamo il campo fields....
0999: if (subDataset != null) {
1000: jEditTextArea1.getTokenMarker().setKeywordLookup(
1001: subDataset.getKeywordLookup());
1002: }
1003: updateTreeEntries();
1004:
1005: }
1006:
1007: public void updateTreeEntries() {
1008:
1009: DefaultMutableTreeNode root = (DefaultMutableTreeNode) jTree1
1010: .getModel().getRoot();
1011:
1012: root.removeAllChildren();
1013:
1014: if (getSubDataset() != null) {
1015: root.add(new DefaultMutableTreeNode("Fields"));
1016: root.add(new DefaultMutableTreeNode("Variables"));
1017: root.add(new DefaultMutableTreeNode("Parameters"));
1018: }
1019:
1020: for (int i = 0; i < getCrosstabElements().size(); ++i) {
1021: root
1022: .add(new DefaultMutableTreeNode(
1023: new CrosstabReportElementWrapper(
1024: (CrosstabReportElement) getCrosstabElements()
1025: .get(i))));
1026: }
1027: root.add(new DefaultMutableTreeNode(I18n.getString("Formulas",
1028: "Formulas")));
1029:
1030: root.add(new DefaultMutableTreeNode(I18n.getString(
1031: "RecentExpressions", "Recent expressions")));
1032:
1033: root.add(new DefaultMutableTreeNode(new IconedString(I18n
1034: .getString("Wizards", "Wizards"),
1035: "/it/businesslogic/ireport/icons/wand.png")));
1036:
1037: jTree1.expandPath(new TreePath(root));
1038: jTree1.updateUI();
1039: jTree1.setSelectionRow(0);
1040: }
1041:
1042: public Vector getCrosstabElements() {
1043: return crosstabElements;
1044: }
1045:
1046: public void setCrosstabElements(Vector crosstabElements) {
1047: this .crosstabElements = crosstabElements;
1048: }
1049:
1050: public void addCrosstabReportElement(CrosstabReportElement re) {
1051: if (!this .getCrosstabElements().contains(re)) {
1052: getCrosstabElements().add(re);
1053: updateTreeEntries();
1054: }
1055: }
1056:
1057: /**
1058: * Set an expression contex. If the passes expression context is null,
1059: * nothing is done...
1060: * The context replace all previos settings
1061: */
1062: public void setExpressionContext(ExpressionContext ec) {
1063: if (ec == null)
1064: return;
1065: setCrosstabElements(ec.getCrosstabReportElements());
1066:
1067: setSubDataset(ec.getSubDataset());
1068: }
1069:
1070: /*
1071: private Class getSelectedObjectClass()
1072: {
1073: try {
1074: if (jList1.getSelectedValue() == null) return null;
1075: String s = (String)jList1.getSelectedValue();
1076:
1077: if (s == null) return null;
1078:
1079: DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode)jTree1.getSelectionPath().getLastPathComponent();
1080: if (dmtn.getUserObject() instanceof CrosstabReportElementWrapper)
1081: {
1082: // look for the object here....
1083: CrosstabReportElement cr = ((CrosstabReportElementWrapper)dmtn.getUserObject()).getCrosstabReportElement();
1084: Enumeration e = cr.getMeasures().elements();
1085: while (e.hasMoreElements())
1086: {
1087: Measure measure = (Measure)e.nextElement();
1088: if (s.equals( "$V{"+measure+"}" ))
1089: {
1090: return this.getClass().getClassLoader().loadClass( measure.getClassType() );
1091: }
1092:
1093: for (int j=0; j<cr.getRowGroups().size(); ++j)
1094: {
1095: CrosstabGroup group = (CrosstabGroup)cr.getRowGroups().elementAt(j);
1096: if (s.equals( "$V{"+measure+"_"+group.getName()+"_"+"ALL}"))
1097: {
1098: // should be always a number?
1099: return this.getClass().getClassLoader().loadClass( measure.getClassType() );
1100: }
1101: }
1102: for (int j=0; j<cr.getColumnGroups().size(); ++j)
1103: {
1104: CrosstabGroup group = (CrosstabGroup)cr.getColumnGroups().elementAt(j);
1105: if (s.equals( "$V{"+group+"_"+group.getName()+"_"+"ALL}"))
1106: {
1107: // should be always a number?
1108: return this.getClass().getClassLoader().loadClass( group.getBucketExpressionClass());
1109: }
1110: }
1111: }
1112:
1113: for (int i=0; i<cr.getRowGroups().size(); ++i)
1114: {
1115: CrosstabGroup group = (CrosstabGroup)cr.getRowGroups().elementAt(i);
1116: if (s.equals( "$V{"+group+"}"))
1117: {
1118: // should be always a number?
1119: return this.getClass().getClassLoader().loadClass( group.getBucketExpressionClass());
1120: }
1121: }
1122:
1123: for (int i=0; i<cr.getColumnGroups().size(); ++i)
1124: {
1125: CrosstabGroup group = (CrosstabGroup)cr.getColumnGroups().elementAt(i);
1126: if (s.equals( "$V{"+group+"}"))
1127: {
1128: // should be always a number?
1129: return this.getClass().getClassLoader().loadClass( group.getBucketExpressionClass());
1130: }
1131: }
1132:
1133: for (int i=0; i<cr.getCrosstabParameters().size(); ++i)
1134: {
1135: CrosstabParameter parameter = (CrosstabParameter)cr.getCrosstabParameters().elementAt(i);
1136: if (s.equals( "$P{"+parameter +"}"))
1137: {
1138: // should be always a number?
1139: return this.getClass().getClassLoader().loadClass( parameter.getClassType());
1140: }
1141: }
1142: }
1143: else if ((""+dmtn.getUserObject()).equalsIgnoreCase("Fields") && getSubDataset() != null)
1144: {
1145: Enumeration e = getSubDataset().getFields().elements();
1146: while (e.hasMoreElements())
1147: {
1148: JRField f = (JRField)e.nextElement();
1149: if (s.equals( "$F{"+ f +"}"))
1150: {
1151: // should be always a number?
1152: return this.getClass().getClassLoader().loadClass( f.getClassType());
1153: }
1154: }
1155: }
1156: else if ((""+dmtn.getUserObject()).equalsIgnoreCase("Parameters") && getSubDataset() != null)
1157: {
1158: Enumeration e = getSubDataset().getParameters().elements();
1159: while (e.hasMoreElements())
1160: {
1161: JRParameter p = (JRParameter)e.nextElement();
1162: if (s.equals( "$P{"+ p +"}"))
1163: {
1164: // should be always a number?
1165: return this.getClass().getClassLoader().loadClass( p.getClassType());
1166: }
1167: }
1168: }
1169: else if ((""+dmtn.getUserObject()).equalsIgnoreCase("Variables") && getSubDataset() != null)
1170: {
1171: Enumeration e = getSubDataset().getVariables().elements();
1172: while (e.hasMoreElements())
1173: {
1174: JRVariable v = (JRVariable)e.nextElement();
1175: if (s.equals( "$V{"+ v +"}"))
1176: {
1177: // should be always a number?
1178: return this.getClass().getClassLoader().loadClass(v.getClassType());
1179: }
1180: }
1181: }
1182:
1183: } catch (Throwable ex)
1184: {
1185: ex.printStackTrace();
1186: return null;
1187: }
1188:
1189: return null;
1190: }
1191: */
1192: public void caretUpdate(CaretEvent e) {
1193:
1194: MessageFormat formatter = new MessageFormat("");
1195: formatter.applyPattern(I18n.getString("LineColumn",
1196: "Line {0,number,integer}, Column {1,number,integer}"));
1197:
1198: if (jEditTextArea1.getCaretLine() < 0) {
1199: jLabelCaretPosition.setText(formatter.format(new Object[] {
1200: new Integer(0), new Integer(0) }));
1201: }
1202: String s = jEditTextArea1.getText();
1203:
1204: int pos = jEditTextArea1.getCaretPosition();
1205: int newLineStart = s.substring(0,
1206: jEditTextArea1.getCaretPosition()).lastIndexOf('\n') + 1;
1207: pos = pos - newLineStart;
1208: pos++;
1209:
1210: jLabelCaretPosition.setText(formatter.format(new Object[] {
1211: new Integer((jEditTextArea1.getCaretLine() + 1)),
1212: new Integer(pos) }));
1213: }
1214:
1215: public void applyI18n() {
1216: // Start autogenerated code ----------------------
1217: jLabelCaretPosition.setText(I18n.getString(
1218: "expressionEditor.labelCaretPosition",
1219: "Line: 1 Column: 1"));
1220: // End autogenerated code ----------------------
1221: jButtonLoadExpression
1222: .setText(it.businesslogic.ireport.util.I18n.getString(
1223: "import", "Import..."));
1224: jButtonSaveExpression
1225: .setText(it.businesslogic.ireport.util.I18n.getString(
1226: "export", "Export..."));
1227: jButtonCheckExpression
1228: .setText(it.businesslogic.ireport.util.I18n.getString(
1229: "checkExpression", "Check expression"));
1230: jButtonApply.setText(it.businesslogic.ireport.util.I18n
1231: .getString("gui.OptionsDialog.ButtonApply", "Apply"));
1232: jButtonCancel.setText(it.businesslogic.ireport.util.I18n
1233: .getString("gui.OptionsDialog.ButtonCancel", "Cancel"));
1234: jTabbedPane1.setTitleAt(0, it.businesslogic.ireport.util.I18n
1235: .getString("ObjectsAndExpressions",
1236: "Objects and expressions"));
1237: jTabbedPane1.setTitleAt(1, it.businesslogic.ireport.util.I18n
1238: .getString("ValidationErrors", "Validation errors"));
1239:
1240: jTabbedPane1.setTitleAt(0, it.businesslogic.ireport.util.I18n
1241: .getString(
1242: "expressionEditor.tab.ObjectsAndExpressions",
1243: "Objects and expressions"));
1244: jTabbedPane1.setTitleAt(1, it.businesslogic.ireport.util.I18n
1245: .getString("expressionEditor.tab.ValidationErrors",
1246: "Validation errors"));
1247: this .setTitle(I18n.getString("expressionEditor.title",
1248: "Expression editor..."));
1249: jButtonCancel
1250: .setMnemonic(I18n.getString(
1251: "expressionEditor.buttonCancelMnemonic", "c")
1252: .charAt(0));
1253: jButtonApply.setMnemonic(I18n.getString(
1254: "expressionEditor.buttonApplyMnemonic", "a").charAt(0));
1255: }
1256:
1257: public void validateExpression() {
1258: String expression_to_validate = jEditTextArea1.getText();
1259:
1260: // 1. replace all $Parameters, $Variables, $Fields and $R expressions
1261: JRDesignExpression jrExpression = new JRDesignExpression();
1262: jrExpression.setText(expression_to_validate);
1263: jTextPaneErrors.setText("");
1264:
1265: try {
1266:
1267: java.util.List paramsObjects = new java.util.ArrayList();
1268: java.util.List varsObjects = new java.util.ArrayList();
1269: java.util.List fieldsObjects = new java.util.ArrayList();
1270:
1271: String final_espression = generateExpression(jrExpression,
1272: paramsObjects, fieldsObjects, varsObjects);
1273:
1274: if (MainFrame.getMainInstance().getActiveReportFrame()
1275: .getReport().getLanguage().equals("groovy")) {
1276: Binding binding = new Binding();
1277: for (int i = 0; i < paramsObjects.size(); ++i) {
1278: String name = "" + paramsObjects.get(i);
1279: binding.setVariable(name, new JRFakeObject());
1280: }
1281: for (int i = 0; i < varsObjects.size(); ++i) {
1282: String name = "" + varsObjects.get(i);
1283: binding.setVariable(name, new JRFakeObject());
1284: }
1285: for (int i = 0; i < fieldsObjects.size(); ++i) {
1286: String name = "" + fieldsObjects.get(i);
1287: binding.setVariable(name, new JRFakeObject());
1288: }
1289: GroovyShell shell = new GroovyShell(binding);
1290:
1291: shell.parse(final_espression);
1292:
1293: } else if (MainFrame.getMainInstance()
1294: .getActiveReportFrame().getReport().getLanguage()
1295: .equals("java")) {
1296: bsh.Interpreter interpreter = new bsh.Interpreter();
1297:
1298: StringTokenizer st = new StringTokenizer(MainFrame
1299: .getMainInstance().getProperties().getProperty(
1300: "classpath", ""), "\n");
1301: interpreter.eval("String tmp;");
1302: while (st.hasMoreTokens()) {
1303: String token = st.nextToken();
1304: if (token != null && token.trim().length() > 0) {
1305: interpreter.set("tmp", token.trim());
1306: interpreter.eval("addClassPath(tmp);");
1307: }
1308: }
1309:
1310: // Add report import directives to the bsh interpreter
1311: Enumeration imps = MainFrame.getMainInstance()
1312: .getActiveReportFrame().getReport()
1313: .getImports().elements();
1314: while (imps.hasMoreElements()) {
1315: String var = (String) imps.nextElement();
1316: interpreter.eval("import " + var + ";");
1317: }
1318:
1319: interpreter.eval("import java.util.*;");
1320: interpreter.eval("import java.math.*;");
1321: interpreter.eval("import java.text.*;");
1322: interpreter.eval("import java.io.*;");
1323: interpreter.eval("import java.net.*;");
1324: interpreter.setStrictJava(true);
1325:
1326: for (int i = 0; i < paramsObjects.size(); ++i) {
1327: String name = "" + paramsObjects.get(i);
1328: interpreter.set(name, new JRFakeObject());
1329: }
1330: for (int i = 0; i < varsObjects.size(); ++i) {
1331: String name = "" + varsObjects.get(i);
1332: interpreter.set(name, new JRFakeObject());
1333: }
1334: for (int i = 0; i < fieldsObjects.size(); ++i) {
1335: String name = "" + fieldsObjects.get(i);
1336: interpreter.set(name, new JRFakeObject());
1337: }
1338:
1339: interpreter.eval(final_espression);
1340: }
1341: setErrorText(it.businesslogic.ireport.util.I18n.getString(
1342: "ValidationOK",
1343: "Expression successfully validated."), false);
1344:
1345: } catch (CompilationFailedException ex) {
1346: setErrorText(ex.getMessage(), true);
1347: } catch (ParseException ex) {
1348: setErrorText(ex.getMessage() + "\n" + ex.getErrorText()
1349: + "\nLine: " + ex.getErrorLineNumber(), true);
1350:
1351: } catch (TargetError ex) {
1352: // What to say... we tried!
1353: setErrorText(it.businesslogic.ireport.util.I18n.getString(
1354: "ValidationOK",
1355: "Expression successfully validated."), true);
1356:
1357: } catch (EvalError ex) {
1358: setErrorText(ex.getMessage() + "\n" + ex.getErrorText()
1359: + "\nLine: " + ex.getErrorLineNumber(), true);
1360: } catch (IRParsingException ex) {
1361: setErrorText(ex.getMessage(), true);
1362: if (ex.getChunk() != null) {
1363: int pos = expression_to_validate.indexOf(ex.getChunk());
1364: if (pos >= 0) {
1365: jEditTextArea1.setSelectionStart(pos);
1366: jEditTextArea1.setSelectionEnd(pos);
1367: jEditTextArea1.setSelectionEnd(pos
1368: + ex.getChunk().length());
1369: }
1370: }
1371: } catch (Exception ex) {
1372: setErrorText(ex.getMessage(), true);
1373: ex.printStackTrace();
1374: }
1375:
1376: jTabbedPane1.setSelectedIndex(1);
1377: }
1378:
1379: private String generateExpression(JRExpression expression,
1380: java.util.List paramsObjects, java.util.List fieldsObjects,
1381: java.util.List varsObjects) throws IRParsingException {
1382: net.sf.jasperreports.engine.JRParameter jrParameter = null;
1383: net.sf.jasperreports.engine.JRField jrField = null;
1384: net.sf.jasperreports.engine.JRVariable jrVariable = null;
1385:
1386: StringBuffer sb = new StringBuffer();
1387:
1388: JRExpressionChunk[] chunks = expression.getChunks();
1389: JRExpressionChunk chunk = null;
1390: String chunkText = null;
1391: if (chunks != null && chunks.length > 0) {
1392: for (int i = 0; i < chunks.length; i++) {
1393: chunk = chunks[i];
1394:
1395: chunkText = chunk.getText();
1396: if (chunkText == null) {
1397: chunkText = "";
1398: }
1399:
1400: switch (chunk.getType()) {
1401: case JRExpressionChunk.TYPE_TEXT: {
1402: sb.append(chunkText);
1403: break;
1404: }
1405: case JRExpressionChunk.TYPE_PARAMETER: {
1406: // Look for the given parameter...
1407: JRParameter param = (JRParameter) getExpressionObject(
1408: chunkText, PARAMETER);
1409:
1410: sb.append("((");
1411: sb.append(param.getClassType());
1412: sb.append(")");
1413: String vname = "parameter_"
1414: + JRStringUtil.getLiteral(chunkText);
1415: sb.append(vname);
1416: paramsObjects.add(vname);
1417: sb.append(".getValue())");
1418:
1419: break;
1420: }
1421: case JRExpressionChunk.TYPE_FIELD: {
1422: JRField field = (JRField) getExpressionObject(
1423: chunkText, FIELD);
1424:
1425: sb.append("((");
1426: sb.append(field.getClassType());
1427: sb.append(")");
1428: String vname = "field_"
1429: + JRStringUtil.getLiteral(chunkText);
1430: sb.append(vname);
1431: fieldsObjects.add(vname);
1432: sb.append(".get");
1433: //sb.append((String)fieldPrefixMap.get(new Byte(evaluationType)));
1434: sb.append("Value())");
1435:
1436: break;
1437: }
1438: case JRExpressionChunk.TYPE_VARIABLE: {
1439: JRVariable variable = (JRVariable) getExpressionObject(
1440: chunkText, VARIABLE);
1441:
1442: sb.append("((");
1443: sb.append(variable.getClassType());
1444: sb.append(")");
1445: String vname = "variable_"
1446: + JRStringUtil.getLiteral(chunkText);
1447: sb.append(vname);
1448: varsObjects.add(vname);
1449: sb.append(".get");
1450: //sb.append((String)variablePrefixMap.get(new Byte(evaluationType)));
1451: sb.append("Value())");
1452:
1453: break;
1454: }
1455: case JRExpressionChunk.TYPE_RESOURCE: {
1456: sb.append("str(\"");
1457: sb.append(chunkText);
1458: sb.append("\")");
1459:
1460: break;
1461: }
1462: }
1463: }
1464: }
1465:
1466: if (sb.length() == 0) {
1467: sb.append("null");
1468: }
1469:
1470: return sb.toString();
1471: }
1472:
1473: private Object getExpressionObject(String name, int type)
1474: throws IRParsingException {
1475: if (name == null)
1476: throw new IRParsingException("Object not found!");
1477:
1478: SubDataset subDataset = getSubDataset();
1479: if (getSubDataset() == null
1480: && getCrosstabElements().size() == 0) {
1481: subDataset = MainFrame.getMainInstance()
1482: .getActiveReportFrame().getReport();
1483: }
1484:
1485: if (type == PARAMETER) {
1486: if (subDataset != null) {
1487: Enumeration e = subDataset.getParameters().elements();
1488: while (e.hasMoreElements()) {
1489: JRParameter p = (JRParameter) e.nextElement();
1490: if (p.getName().equals(name))
1491: return p;
1492: }
1493: }
1494:
1495: // Try to look in some crosstab...
1496: for (int k = 0; k < crosstabElements.size(); ++k) {
1497: //----------------------------------------------
1498: CrosstabReportElement cr = (CrosstabReportElement) crosstabElements
1499: .get(k);
1500:
1501: for (int i = 0; i < cr.getCrosstabParameters().size(); ++i) {
1502: CrosstabParameter parameter = (CrosstabParameter) cr
1503: .getCrosstabParameters().elementAt(i);
1504: if (name.equals("" + parameter)) {
1505: // should be always a number?
1506: return new JRParameter(parameter.getName(),
1507: parameter.getClassType());
1508: }
1509: }
1510: //-----------------------------------------------------
1511:
1512: }
1513: // Parameter not found!!!
1514:
1515: throw new IRParsingException("Parameter " + name
1516: + " not found!", "$P{" + name + "}");
1517: } else if (type == FIELD) {
1518: if (subDataset != null) {
1519: Enumeration e = subDataset.getFields().elements();
1520: while (e.hasMoreElements()) {
1521: JRField p = (JRField) e.nextElement();
1522: if (p.getName().equals(name))
1523: return p;
1524: }
1525: }
1526: // Parameter not found!!!
1527: throw new IRParsingException("Field " + name
1528: + " not found!", "$F{" + name + "}");
1529: } else if (type == VARIABLE) {
1530: if (subDataset != null) {
1531: Enumeration e = subDataset.getVariables().elements();
1532: while (e.hasMoreElements()) {
1533: JRVariable p = (JRVariable) e.nextElement();
1534: if (p.getName().equals(name))
1535: return p;
1536: }
1537: }
1538:
1539: // Try to look in some crosstab...
1540: for (int k = 0; k < crosstabElements.size(); ++k) {
1541: //----------------------------------------------
1542: CrosstabReportElement cr = (CrosstabReportElement) crosstabElements
1543: .get(k);
1544: Enumeration e = cr.getMeasures().elements();
1545: while (e.hasMoreElements()) {
1546: Measure measure = (Measure) e.nextElement();
1547: if (name.equals(measure.getName())) {
1548: return new JRVariable(measure.getName(),
1549: measure.getClassType(), false);
1550: }
1551:
1552: for (int j = 0; j < cr.getRowGroups().size(); ++j) {
1553: CrosstabGroup group = (CrosstabGroup) cr
1554: .getRowGroups().elementAt(j);
1555: if (name.equals("" + measure + "_"
1556: + group.getName() + "_" + "ALL")) {
1557: // should be always a number?
1558: return new JRVariable(measure + "_"
1559: + group.getName(), group
1560: .getBucketExpressionClass(), false);
1561: }
1562: }
1563: for (int j = 0; j < cr.getColumnGroups().size(); ++j) {
1564: CrosstabGroup group = (CrosstabGroup) cr
1565: .getColumnGroups().elementAt(j);
1566: if (name.equals("" + group + "_"
1567: + group.getName() + "_" + "ALL")) {
1568: // should be always a number?
1569: return new JRVariable(measure + "_"
1570: + group.getName(), group
1571: .getBucketExpressionClass(), false);
1572: }
1573: }
1574: }
1575:
1576: for (int i = 0; i < cr.getRowGroups().size(); ++i) {
1577: CrosstabGroup group = (CrosstabGroup) cr
1578: .getRowGroups().elementAt(i);
1579: if (name.equals("" + group)) {
1580: // should be always a number?
1581: return new JRVariable("" + group, group
1582: .getBucketExpressionClass(), false);
1583: }
1584: }
1585:
1586: for (int i = 0; i < cr.getColumnGroups().size(); ++i) {
1587: CrosstabGroup group = (CrosstabGroup) cr
1588: .getColumnGroups().elementAt(i);
1589: if (name.equals("" + group)) {
1590: // should be always a number?
1591: return new JRVariable("" + group, group
1592: .getBucketExpressionClass(), false);
1593: }
1594: }
1595:
1596: //-----------------------------------------------------
1597:
1598: }
1599: // Parameter not found!!!
1600: throw new IRParsingException("Variable " + name
1601: + " not found!", "$F{" + name + "}");
1602: }
1603:
1604: throw new IRParsingException("Object " + name + " not found!",
1605: name);
1606: }
1607:
1608: public void setErrorText(String msg, boolean isError) {
1609: jTextPaneErrors.setText("");
1610: try {
1611: doc.insertString(doc.getLength(), msg,
1612: (isError) ? errorStyle : okStyle);
1613: } catch (Exception ex) {
1614: }
1615: }
1616:
1617: public java.util.List getAllObjects() {
1618: java.util.List list = new java.util.ArrayList();
1619: if (getSubDataset() != null) {
1620: Enumeration e = getSubDataset().getFields().elements();
1621: while (e.hasMoreElements()) {
1622: JRField f = (JRField) e.nextElement();
1623: list.add(new ExpObject(f.getName(),
1624: ExpObject.TYPE_FIELD, f.getClassType()));
1625: }
1626:
1627: e = getSubDataset().getVariables().elements();
1628: while (e.hasMoreElements()) {
1629: JRVariable f = (JRVariable) e.nextElement();
1630: list.add(new ExpObject(f.getName(),
1631: ExpObject.TYPE_VARIABLE, f.getClassType()));
1632: }
1633:
1634: e = getSubDataset().getParameters().elements();
1635: while (e.hasMoreElements()) {
1636: JRParameter f = (JRParameter) e.nextElement();
1637: list.add(new ExpObject(f.getName(),
1638: ExpObject.TYPE_PARAM, f.getClassType()));
1639: }
1640: }
1641: for (int ct = 0; ct < getCrosstabElements().size(); ++ct) {
1642: CrosstabReportElement cr = (CrosstabReportElement) getCrosstabElements()
1643: .get(ct);
1644: Enumeration e = cr.getMeasures().elements();
1645: while (e.hasMoreElements()) {
1646: Measure measure = (Measure) e.nextElement();
1647: list
1648: .add(new ExpObject(measure.getName(),
1649: ExpObject.TYPE_VARIABLE, measure
1650: .getClassType()));
1651:
1652: for (int j = 0; j < cr.getRowGroups().size(); ++j) {
1653: CrosstabGroup group = (CrosstabGroup) cr
1654: .getRowGroups().elementAt(j);
1655: list.add(new ExpObject(measure.getName() + "_"
1656: + group.getName() + "_" + "ALL",
1657: ExpObject.TYPE_VARIABLE, measure
1658: .getClassType()));
1659: }
1660: for (int j = 0; j < cr.getColumnGroups().size(); ++j) {
1661: CrosstabGroup group = (CrosstabGroup) cr
1662: .getColumnGroups().elementAt(j);
1663: list.add(new ExpObject(measure.getName() + "_"
1664: + group.getName() + "_" + "ALL",
1665: ExpObject.TYPE_VARIABLE, measure
1666: .getClassType()));
1667: }
1668: }
1669:
1670: for (int i = 0; i < cr.getRowGroups().size(); ++i) {
1671: CrosstabGroup group = (CrosstabGroup) cr.getRowGroups()
1672: .elementAt(i);
1673: list.add(new ExpObject(group.getName(),
1674: ExpObject.TYPE_VARIABLE, group
1675: .getBucketExpressionClass()));
1676: }
1677:
1678: for (int i = 0; i < cr.getColumnGroups().size(); ++i) {
1679: CrosstabGroup group = (CrosstabGroup) cr
1680: .getColumnGroups().elementAt(i);
1681: list.add(new ExpObject(group.getName(),
1682: ExpObject.TYPE_VARIABLE, group
1683: .getBucketExpressionClass()));
1684: }
1685:
1686: for (int i = 0; i < cr.getCrosstabParameters().size(); ++i) {
1687: CrosstabParameter parameter = (CrosstabParameter) cr
1688: .getCrosstabParameters().elementAt(i);
1689: list
1690: .add(new ExpObject(parameter.getName(),
1691: ExpObject.TYPE_PARAM, parameter
1692: .getClassType()));
1693: }
1694: }
1695:
1696: return list;
1697: }
1698:
1699: }
|