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: * ReportQueryDialog.java
0028: *
0029: * Created on 27 maggio 2003, 19.47
0030: *
0031: */
0032:
0033: package it.businesslogic.ireport.gui;
0034:
0035: import bsh.EvalError;
0036: import it.businesslogic.ireport.*;
0037: import it.businesslogic.ireport.JRParameter;
0038: import it.businesslogic.ireport.data.BeanInspectorPanel;
0039: import it.businesslogic.ireport.data.CincomMDXFieldsProvider;
0040: import it.businesslogic.ireport.data.EJBQLFieldsProvider;
0041: import it.businesslogic.ireport.data.HQLFieldsProvider;
0042: import it.businesslogic.ireport.data.MDXFieldsProvider;
0043: import it.businesslogic.ireport.data.SQLFieldsProvider;
0044: import it.businesslogic.ireport.gui.queryexecuters.QueryExecuterDef;
0045: import it.businesslogic.ireport.gui.subdataset.FilterExpressionDialog;
0046: import it.businesslogic.ireport.gui.subdataset.SortFieldsDialog;
0047: import it.businesslogic.ireport.util.*;
0048: import it.businesslogic.ireport.util.LanguageChangedEvent;
0049: import it.businesslogic.ireport.util.LanguageChangedListener;
0050: import java.awt.BorderLayout;
0051: import java.awt.Component;
0052: import javax.swing.table.*;
0053: import javax.swing.*;
0054: import java.util.*;
0055: import java.sql.*;
0056: import bsh.Interpreter;
0057: import it.businesslogic.ireport.data.XMLFieldsProvider;
0058: import it.businesslogic.ireport.gui.dnd.FieldsContainer;
0059: import it.businesslogic.ireport.gui.dnd.FieldsContainerTransferHandler;
0060: import it.businesslogic.ireport.gui.sheet.Tag;
0061: import it.businesslogic.ireport.gui.table.CustomColumnControlButton;
0062:
0063: import javax.swing.event.*;
0064: import javax.swing.tree.*;
0065: import java.awt.datatransfer.*;
0066: import net.sf.jasperreports.engine.design.JRDesignExpression;
0067: import net.sf.jasperreports.engine.design.JRDesignQuery;
0068: import org.jdesktop.swingx.JXBusyLabel;
0069: import org.jdesktop.swingx.icon.ColumnControlIcon;
0070: import org.jdesktop.swingx.table.ColumnControlButton;
0071:
0072: /**
0073: * A dialog which allows the user to enter a SQL query and then choose the
0074: * fields to use in the report.
0075: *
0076: * @author <a href="mailto:gt78@users.sourceforge.net">Giulio Toffoli</a>
0077: * @author <a href="mailto:phenderson@users.sourceforge.net">Peter Henderson</a>
0078: */
0079: public class ReportQueryDialog extends javax.swing.JDialog implements
0080: ClipboardOwner, FieldsContainer {
0081:
0082: private BeanInspectorPanel bip1 = null;
0083:
0084: private FieldsProvider fieldsProvider = null;
0085: private boolean init = false;
0086:
0087: protected static String standard_types[] = new String[] {
0088: "java.lang.String", "java.lang.Object",
0089: "java.lang.Boolean", "java.lang.Byte", "java.util.Date",
0090: "java.sql.Timestamp", "java.sql.Time", "java.lang.Double",
0091: "java.lang.Float", "java.lang.Integer",
0092: "java.io.InputStream", "java.lang.Long", "java.lang.Short",
0093: "java.math.BigDecimal" };
0094:
0095: public FieldReader readerThread = null;
0096: public static int num = 1;
0097:
0098: public JLabel getJLabelStatusSQL() {
0099: return this .jLabelStatusSQL;
0100: }
0101:
0102: public void updateQueryLanguages() {
0103: boolean oldInit = setInit(true);
0104:
0105: Object oldItem = jComboBoxQueryType.getSelectedItem();
0106:
0107: jComboBoxQueryType.removeAllItems();
0108: jComboBoxQueryType.addItem(new Tag("sql", "SQL"));
0109: jComboBoxQueryType.addItem(new Tag("hql",
0110: "Hibernate Query Language (HQL)"));
0111: jComboBoxQueryType.addItem(new Tag("xPath", "XPath"));
0112: jComboBoxQueryType.addItem(new Tag("ejbql", "EJBQL"));
0113: jComboBoxQueryType.addItem(new Tag("mdx", "MDX"));
0114: jComboBoxQueryType.addItem(new Tag("xmla-mdx", "XMLA-MDX"));
0115:
0116: Enumeration e = MainFrame.getMainInstance().getQueryExecuters()
0117: .elements();
0118: while (e.hasMoreElements()) {
0119: QueryExecuterDef qe = (QueryExecuterDef) e.nextElement();
0120: jComboBoxQueryType.addItem(new Tag(qe, qe.getLanguage()));
0121: }
0122: if (oldItem != null) {
0123: jComboBoxQueryType.setSelectedItem(oldItem);
0124: }
0125: setInit(oldInit);
0126: }
0127:
0128: /** Creates new form ReportQueryFrame */
0129: public ReportQueryDialog(java.awt.Frame parent, boolean modal) {
0130:
0131: super (parent, modal);
0132: initComponents();
0133: this .setSize(800, 550);
0134: Misc.centerFrame(this );
0135:
0136: stoppedChanging.setRepeats(false);
0137:
0138: jRSQLExpressionArea1.getDocument().addDocumentListener(
0139: new DocumentListener() {
0140: public void changedUpdate(DocumentEvent e) {
0141: if (isSettingSQLExpression)
0142: return;
0143: //okButton.setEnabled(false);
0144: stoppedChanging.restart();
0145: }
0146:
0147: public void insertUpdate(DocumentEvent e) {
0148: if (isSettingSQLExpression)
0149: return;
0150: //okButton.setEnabled(false);
0151: stoppedChanging.restart();
0152: }
0153:
0154: public void removeUpdate(DocumentEvent e) {
0155: if (isSettingSQLExpression)
0156: return;
0157: //okButton.setEnabled(false);
0158: stoppedChanging.restart();
0159: }
0160: });
0161:
0162: setColumnsError("Please open a report.");
0163: if (MainFrame.getMainInstance().getProperties().getProperty(
0164: "beanClass") != null) {
0165: jTextFieldBeanClass1.setText(MainFrame.getMainInstance()
0166: .getProperties().getProperty("beanClass")
0167: + "");
0168: }
0169:
0170: updateQueryLanguages();
0171:
0172: okButton.setEnabled(false);
0173:
0174: bip1 = new BeanInspectorPanel();
0175: bip1.setComboVisible(false);
0176: bip1.setJTableFields(jTableFields);
0177: bip1.setPathOnDescription(true);
0178: jPanel11.add(bip1, BorderLayout.CENTER);
0179:
0180: javax.swing.KeyStroke escape = javax.swing.KeyStroke
0181: .getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0,
0182: false);
0183: javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
0184: public void actionPerformed(java.awt.event.ActionEvent e) {
0185: cancelButtonActionPerformed(e);
0186: }
0187: };
0188:
0189: getRootPane().getInputMap(
0190: javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(
0191: escape, "ESCAPE");
0192: getRootPane().getActionMap().put("ESCAPE", escapeAction);
0193:
0194: applyI18n();
0195:
0196: I18n
0197: .addOnLanguageChangedListener(new LanguageChangedListener() {
0198: public void languageChanged(LanguageChangedEvent evt) {
0199: applyI18n();
0200: }
0201: });
0202:
0203: FieldsContainerTransferHandler fcth = new FieldsContainerTransferHandler(
0204: this );
0205: jTableFields.setTransferHandler(fcth);
0206: columnsErrorMsgLabel.setTransferHandler(fcth);
0207: columnsScrollPane.setTransferHandler(fcth);
0208: columnsErrorScrollPane.setTransferHandler(fcth);
0209: //to make the default button ...
0210: //this.getRootPane().setDefaultButton(this.jButtonOK);
0211:
0212: jTableFields.setColumnControl(new CustomColumnControlButton(
0213: jTableFields, new ColumnControlIcon()));
0214:
0215: }
0216:
0217: /**
0218: * A timer to detect when the SQL expression area has not been changed, for
0219: * a short moment. This is to prevent the database being hit with every
0220: * with every key press.
0221: */
0222: javax.swing.Timer stoppedChanging = new javax.swing.Timer(500,
0223: new java.awt.event.ActionListener() {
0224: public void actionPerformed(
0225: java.awt.event.ActionEvent evt) {
0226: if (automaticlyReadFieldsCheckBox.isSelected()) {
0227: processQueryChanged(jRSQLExpressionArea1
0228: .getText().trim());
0229: }
0230: }
0231: });
0232:
0233: /**
0234: * Given a database query string, extract the database columns, then display
0235: * them. If there was a problem loading the list of columns, show an error
0236: * panel which contains the reason why.
0237: *
0238: * @param query The SQL query string, which can contain JasperReports parameters.
0239: */
0240: private void processQueryChanged(String query) {
0241:
0242: if (isSettingSQLExpression)
0243: return;
0244:
0245: //System.out.println("processQueryChanged");
0246: //Thread.currentThread().dumpStack();
0247:
0248: if (subDataset == null) {
0249: setColumnsError("Please open a report.");
0250: return;
0251: }
0252:
0253: if (query.length() == 0) {
0254: setColumnsError("You must insert a valid query first");
0255: return;
0256: }
0257:
0258: IReportConnection conn = (IReportConnection) MainFrame
0259: .getMainInstance().getProperties().get(
0260: "DefaultConnection");
0261:
0262: Object obj = jComboBoxQueryType.getSelectedItem();
0263: String queryLanguage = "sql";
0264: if (obj != null && obj instanceof Tag) {
0265: queryLanguage = "" + ((Tag) obj).getValue();
0266: } else {
0267: queryLanguage = "" + obj;
0268: }
0269:
0270: try {
0271: // Run the query in the backgroud as it is not quick.
0272: if (readerThread != null && readerThread.isAlive()) {
0273: readerThread.interrupt();
0274: }
0275: } catch (Throwable ex) {
0276: ex.printStackTrace();
0277:
0278: }
0279:
0280: readerThread = new FieldReader(query, conn, queryLanguage);
0281: readerThread.start();
0282: }
0283:
0284: public static int elaborationSequence = 0;
0285:
0286: public boolean isInit() {
0287: return init;
0288: }
0289:
0290: public boolean setInit(boolean init) {
0291: boolean oldValue = this .init;
0292: this .init = init;
0293: return oldValue;
0294: }
0295:
0296: /**
0297: * A Thread class to extract field names from a SQL query.
0298: *
0299: */
0300: class FieldReader extends Thread {
0301: String src_query;
0302: String src_query_language = "sql";
0303: IReportConnection conn;
0304:
0305: int elaborationID = 0;
0306:
0307: /**
0308: * ctor.
0309: * @param query The query to read the field from
0310: * @param conn The IRport DB connection to use.
0311: */
0312: public FieldReader(String query, IReportConnection conn) {
0313: this (query, conn, "sql");
0314: }
0315:
0316: /**
0317: * ctor.
0318: * @param query The query to read the field from
0319: * @param conn The IRport DB connection to use.
0320: */
0321: public FieldReader(String query, IReportConnection conn,
0322: String query_language) {
0323: src_query = query;
0324: this .conn = conn;
0325: this .src_query_language = query_language;
0326: ReportQueryDialog.elaborationSequence++;
0327: elaborationID = ReportQueryDialog.elaborationSequence;
0328: }
0329:
0330: /**
0331: * Set the fields table data to the supplied data.
0332: * This is called from a none swing thread, hence all the invoke and
0333: * wait magic. If the current thread is the AWT Event Dispacher, no
0334: * invoke and wait is call.
0335: * The columns are only set if the query string matches the one the
0336: * results are for.
0337: *
0338: * @param columns The list of columns to set.
0339: */
0340: public void setColumnsFromWorker(final List columns) {
0341: try {
0342:
0343: Runnable r = new Runnable() {
0344: public void run() {
0345: String str = jRSQLExpressionArea1.getText()
0346: .trim();
0347: if (str.compareTo(src_query) == 0) {
0348: setColumns(columns);
0349: }
0350: }
0351: };
0352:
0353: if (!SwingUtilities.isEventDispatchThread()) {
0354: SwingUtilities.invokeAndWait(r);
0355: } else {
0356: r.run();
0357: }
0358:
0359: } catch (Exception e) {
0360: // oh well we got interrupted.
0361: }
0362: }
0363:
0364: /**
0365: * Set the columns error message.
0366: * This is called from a none swing thread, hence all the invoke and
0367: * wait magic.
0368: * The message is only set if the query string matches the one the
0369: * error message is for.
0370: *
0371: * @param columns The list of columns to set.
0372: */
0373: public void setColumnErrorFromWork(final String error_msg) {
0374: try {
0375:
0376: Runnable r = new Runnable() {
0377: public void run() {
0378: String str = jRSQLExpressionArea1.getText()
0379: .trim();
0380: if (str.compareTo(src_query) == 0) {
0381: setColumnsError(error_msg);
0382: jRSQLExpressionArea1.requestFocusInWindow();
0383: }
0384:
0385: }
0386: };
0387:
0388: if (SwingUtilities.isEventDispatchThread()) {
0389: r.run();
0390: } else {
0391: SwingUtilities.invokeAndWait(r);
0392: }
0393: } catch (Exception e) {
0394: // oh well we got interrupted.
0395: }
0396: }
0397:
0398: public String interpretQuery() {
0399:
0400: String query = this .src_query;
0401: try {
0402: Interpreter interpreter = prepareExpressionEvaluator();
0403:
0404: // look for parameters in the query and replace them with default values.
0405: // parameters look something like
0406: // $P{QuoteGroupID}
0407: // or
0408: // $P!{OrderByClause}
0409: java.util.List queryParams = new ArrayList();
0410: Enumeration enumParams = subDataset.getParameters()
0411: .elements();
0412: while (enumParams.hasMoreElements()) {
0413: it.businesslogic.ireport.JRParameter parameter = (it.businesslogic.ireport.JRParameter) enumParams
0414: .nextElement();
0415:
0416: String p1 = "$P{" + parameter.getName() + "}";
0417: String p2 = "$P!{" + parameter.getName() + "}";
0418:
0419: // evaluate the Default expression value
0420:
0421: // Integer expID = (Integer)parameterNameToExpressionID.get(parameter.getName());
0422:
0423: Object defValue;
0424: if (parameter.getDefaultValueExpression() != null
0425: && !parameter.getDefaultValueExpression()
0426: .equals("")) {
0427: defValue = recursiveInterpreter(interpreter,
0428: parameter.getDefaultValueExpression(),
0429: subDataset.getParameters(), 0,
0430: parameter.getName());
0431: // interpreter.eval("bshCalculator.evaluate(" + expID.intValue() + ")");
0432: } else {
0433: // this param does not have a default value.
0434: defValue = null;
0435: }
0436:
0437: int ip1 = query.indexOf(p1);
0438: while (ip1 != -1) {
0439: // String replacement, Altering the SQL statement.
0440:
0441: //if( defValue==null ) {
0442: // throw new IllegalArgumentException("Please set a " +
0443: // "default value for the parameter '"
0444: // + parameter.getName() + "'" );
0445: //}
0446:
0447: String before = query.substring(0, ip1);
0448: String after = query.substring(ip1
0449: + p1.length());
0450: if (defValue != null
0451: && parameter.getClassType().equals(
0452: "java.lang.String")) {
0453: String stt = defValue.toString();
0454: stt = it.businesslogic.ireport.util.Misc
0455: .string_replace("''", "'", stt);
0456: query = before + "'" + stt + "'" + after;
0457: } else
0458: query = before + "" + defValue.toString()
0459: + "" + after;
0460:
0461: ip1 = query.indexOf(p1);
0462: }
0463:
0464: int ip2 = query.indexOf(p2);
0465: while (ip2 != -1) {
0466: // String replacement, Altering the SQL statement.
0467: if (defValue == null) {
0468: //throw new IllegalArgumentException("Please set a " +
0469: // "default value for the parameter '"
0470: // + parameter.getName() + "'" );
0471: defValue = "NULL";
0472:
0473: }
0474:
0475: String before = query.substring(0, ip2);
0476: String after = query.substring(ip2
0477: + p2.length());
0478: query = before + "" + defValue.toString() + ""
0479: + after;
0480: ip2 = query.indexOf(p2);
0481: }
0482: }
0483:
0484: return query;
0485: } catch (Exception ex) {
0486: javax.swing.JOptionPane.showMessageDialog(null, ex
0487: .getMessage());
0488: return "";
0489: }
0490: }
0491:
0492: public void run() {
0493:
0494: ((JXBusyLabel) ReportQueryDialog.this .getJLabelStatusSQL())
0495: .setBusy(true);
0496:
0497: try {
0498: String error_msg = "";
0499: num++;
0500: int in = num;
0501:
0502: FieldsProvider fp = getFieldsProvider();
0503: if (fp == null)
0504: return;
0505:
0506: if (fp.hasEditorComponent()) {
0507: FieldsProviderEditor editor = (FieldsProviderEditor) fp
0508: .getEditorComponent(ReportQueryDialog.this );
0509: editor.queryChanged(this .src_query);
0510: }
0511:
0512: if (in < num)
0513: return; //Abort, new execution requested
0514:
0515: if (fp.supportsGetFieldsOperation()) {
0516: try {
0517:
0518: // Create the JasperReport object
0519: Report rep = MainFrame.getMainInstance()
0520: .getActiveReportFrame().getReport();
0521:
0522: net.sf.jasperreports.engine.design.JasperDesign report = new net.sf.jasperreports.engine.design.JasperDesign();
0523: JRDesignQuery queryDes = new JRDesignQuery();
0524: queryDes.setText(this .src_query);
0525: queryDes.setLanguage(this .src_query_language);
0526: report.setQuery(queryDes);
0527:
0528: for (int i = 0; i < rep.getJRproperties()
0529: .size(); ++i) {
0530: JRProperty property = (JRProperty) rep
0531: .getJRproperties().elementAt(i);
0532: report.setProperty(property.getName(),
0533: property.getValue());
0534: }
0535:
0536: Interpreter interpreter = prepareExpressionEvaluator();
0537: HashMap parametersValues = new HashMap();
0538:
0539: for (int i = 0; i < rep.getParameters().size(); ++i) {
0540: JRParameter ireportParam = (JRParameter) rep
0541: .getParameters().elementAt(i);
0542: if (ireportParam.isBuiltin())
0543: continue;
0544: // The parameter could be already there...
0545: // check for duplicated parameters....
0546: if (report.getParametersMap().containsKey(
0547: ireportParam.getName()))
0548: continue;
0549:
0550: net.sf.jasperreports.engine.design.JRDesignParameter param = new net.sf.jasperreports.engine.design.JRDesignParameter();
0551: param.setName(ireportParam.getName());
0552: JRDesignExpression de = new JRDesignExpression();
0553: de.setText(ireportParam
0554: .getDefaultValueExpression());
0555: de.setValueClassName(ireportParam
0556: .getClassType());
0557: param.setDefaultValueExpression(de);
0558: param.setForPrompting(ireportParam
0559: .isIsForPrompting());
0560: param.setValueClassName(ireportParam
0561: .getClassType());
0562: report.addParameter(param);
0563:
0564: Object defValue;
0565: if (ireportParam
0566: .getDefaultValueExpression() != null
0567: && !ireportParam
0568: .getDefaultValueExpression()
0569: .equals("")) {
0570:
0571: defValue = recursiveInterpreter(
0572: interpreter,
0573: ireportParam
0574: .getDefaultValueExpression(),
0575: subDataset.getParameters(), 0,
0576: ireportParam.getName());
0577: //defValue = interpreter.eval( parameter.getDefaultValueExpression() );
0578: // interpreter.eval("bshCalculator.evaluate(" + expID.intValue() + ")");
0579: } else {
0580: // this param does not have a default value.
0581: defValue = null;
0582: }
0583:
0584: parametersValues.put(
0585: ireportParam.getName(), defValue);
0586: }
0587:
0588: try {
0589: report.setName(rep.getName());
0590: } catch (Exception ex) {
0591: }
0592:
0593: if (in < num)
0594: return; //Abort, new execution requested
0595:
0596: // Create a temporary JasperReports object...
0597: net.sf.jasperreports.engine.JRField[] jrFields = fp
0598: .getFields(this .conn, report
0599: .getMainDataset(),
0600: parametersValues);
0601:
0602: if (in < num)
0603: return; //Abort, new execution requested
0604:
0605: List columns = new ArrayList();
0606: for (int i = 0; i < jrFields.length; ++i) {
0607: JRField field = new JRField(jrFields[i]
0608: .getName(), jrFields[i]
0609: .getValueClassName());
0610: field.setDescription(jrFields[i]
0611: .getDescription());
0612: columns.add(new Object[] { field,
0613: field.getClassType(),
0614: field.getDescription() });
0615: }
0616:
0617: if (in < num)
0618: return; //Abort, new execution requested
0619:
0620: setColumnsFromWorker(columns);
0621:
0622: } catch (Exception ex) {
0623: ex.printStackTrace();
0624: setColumnErrorFromWork("Error: "
0625: + ex.getMessage());
0626: }
0627: getJLabelStatusSQL().setText("Ready");
0628: }
0629:
0630: /////////////////////
0631: } finally {
0632: if (elaborationID == ReportQueryDialog.elaborationSequence) {
0633: ((JXBusyLabel) ReportQueryDialog.this
0634: .getJLabelStatusSQL()).setBusy(false);
0635: }
0636: }
0637: }
0638: } // Class end
0639:
0640: /**
0641: * Shows the list of columns.
0642: * If the column error message label is visible remove it first.
0643: *
0644: * @param cols A List Object[], for the fields.
0645: */
0646: public void setColumns(final List cols) {
0647:
0648: columnsErrorMsgLabel.setText("");
0649: jPanel2.remove(columnsErrorScrollPane);
0650: jPanel2.add(columnsScrollPane, java.awt.BorderLayout.CENTER);
0651: jPanel2.revalidate();
0652:
0653: javax.swing.table.DefaultTableModel dtm = (javax.swing.table.DefaultTableModel) jTableFields
0654: .getModel();
0655: dtm.getDataVector().clear();
0656: for (int i = 0; i < cols.size(); i++) {
0657: Object[] row = (Object[]) cols.get(i);
0658: dtm.addRow(row);
0659: }
0660:
0661: // Select all the fields so the new user does not get confused, when
0662: // they press OK. As only the selected fields are actually saved to the
0663: // report
0664: jTableFields.selectAll();
0665:
0666: okButton.setEnabled(true);
0667: }
0668:
0669: /**
0670: * Replace the columns list with a label that contains the reason why
0671: * columns cannot be loaded.
0672: *
0673: * @param msg The error message to display, can be in HTML.
0674: */
0675: public void setColumnsError(final String msg) {
0676: columnsErrorMsgLabel.setText(msg);
0677: jPanel2.remove(columnsScrollPane);
0678: jPanel2.add(columnsErrorScrollPane,
0679: java.awt.BorderLayout.CENTER);
0680: jPanel2.revalidate();
0681: columnsErrorMsgLabel.repaint();
0682:
0683: //okButton.setEnabled(false);
0684: }
0685:
0686: private FieldsProvider getFieldsProvider() {
0687: return fieldsProvider;
0688: }
0689:
0690: private void setFieldsProvider(FieldsProvider fieldsProvider) {
0691: this .fieldsProvider = fieldsProvider;
0692: }
0693:
0694: /** This method is called from within the constructor to
0695: * initialize the form.
0696: * WARNING: Do NOT modify this code. The content of this method is
0697: * always regenerated by the Form Editor.
0698: */
0699: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
0700: private void initComponents() {
0701: java.awt.GridBagConstraints gridBagConstraints;
0702:
0703: jSplitPane1 = new javax.swing.JSplitPane();
0704: jPanel1 = new javax.swing.JPanel();
0705: jTabbedPane1 = new javax.swing.JTabbedPane();
0706: jPanelSQL = new javax.swing.JPanel();
0707: jPanel14 = new javax.swing.JPanel();
0708: jLabel1 = new javax.swing.JLabel();
0709: jComboBoxQueryType = new javax.swing.JComboBox();
0710: jPanel16 = new javax.swing.JPanel();
0711: jButtonLoadQuery = new javax.swing.JButton();
0712: jButtonSaveQuery = new javax.swing.JButton();
0713: jPanelQueryArea = new javax.swing.JPanel();
0714: jSplitPane2 = new javax.swing.JSplitPane();
0715: jPanel7 = new javax.swing.JPanel();
0716: jRSQLExpressionArea1 = new it.businesslogic.ireport.gui.JRSQLExpressionArea();
0717: jLabelStatusSQL = new org.jdesktop.swingx.JXBusyLabel();
0718: automaticlyReadFieldsCheckBox = new javax.swing.JCheckBox();
0719: readFieldsButton = new javax.swing.JButton();
0720: jButtonOpenDesigner = new javax.swing.JButton();
0721: exportQueryButton = new javax.swing.JButton();
0722: jPanel8 = new javax.swing.JPanel();
0723: jTextFieldBeanClass1 = new javax.swing.JTextField();
0724: jLabel3 = new javax.swing.JLabel();
0725: jButtonReadBeanAttributes3 = new javax.swing.JButton();
0726: jPanel11 = new javax.swing.JPanel();
0727: jSeparator1 = new javax.swing.JSeparator();
0728: jPanel6 = new javax.swing.JPanel();
0729: jButton2 = new javax.swing.JButton();
0730: jPanel15 = new javax.swing.JPanel();
0731: jButton3 = new javax.swing.JButton();
0732: jPanel2 = new javax.swing.JPanel();
0733: jPanel3 = new javax.swing.JPanel();
0734: jPanel4 = new javax.swing.JPanel();
0735: okButton = new javax.swing.JButton();
0736: cancelButton = new javax.swing.JButton();
0737: jPanel9 = new javax.swing.JPanel();
0738: jButton1 = new javax.swing.JButton();
0739: jButton4 = new javax.swing.JButton();
0740: columnsErrorScrollPane = new javax.swing.JScrollPane();
0741: columnsErrorMsgLabel = new javax.swing.JLabel();
0742: columnsScrollPane = new javax.swing.JScrollPane();
0743: jTableFields = new org.jdesktop.swingx.JXTable();
0744:
0745: setTitle("Report query");
0746: addWindowListener(new java.awt.event.WindowAdapter() {
0747: public void windowClosing(java.awt.event.WindowEvent evt) {
0748: closeDialog(evt);
0749: }
0750:
0751: public void windowOpened(java.awt.event.WindowEvent evt) {
0752: formWindowOpened(evt);
0753: }
0754: });
0755:
0756: jSplitPane1
0757: .setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
0758: jSplitPane1.setResizeWeight(0.5);
0759:
0760: jPanel1.setMinimumSize(new java.awt.Dimension(10, 100));
0761: jPanel1.setPreferredSize(new java.awt.Dimension(10, 350));
0762: jPanel1.setLayout(new java.awt.BorderLayout());
0763:
0764: jTabbedPane1.setMinimumSize(new java.awt.Dimension(154, 350));
0765: jTabbedPane1.setPreferredSize(new java.awt.Dimension(154, 350));
0766:
0767: jPanelSQL.setMinimumSize(new java.awt.Dimension(1, 100));
0768: jPanelSQL.setPreferredSize(new java.awt.Dimension(1, 350));
0769: jPanelSQL.setLayout(new java.awt.GridBagLayout());
0770:
0771: jPanel14.setLayout(new java.awt.GridBagLayout());
0772:
0773: jLabel1.setText("Query language");
0774: gridBagConstraints = new java.awt.GridBagConstraints();
0775: gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
0776: jPanel14.add(jLabel1, gridBagConstraints);
0777:
0778: jComboBoxQueryType.setEditable(true);
0779: jComboBoxQueryType.setMinimumSize(new java.awt.Dimension(200,
0780: 18));
0781: jComboBoxQueryType.setPreferredSize(new java.awt.Dimension(200,
0782: 22));
0783: jComboBoxQueryType
0784: .addActionListener(new java.awt.event.ActionListener() {
0785: public void actionPerformed(
0786: java.awt.event.ActionEvent evt) {
0787: jComboBoxQueryTypeActionPerformed(evt);
0788: }
0789: });
0790: gridBagConstraints = new java.awt.GridBagConstraints();
0791: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
0792: gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
0793: jPanel14.add(jComboBoxQueryType, gridBagConstraints);
0794:
0795: jPanel16.setLayout(new java.awt.GridBagLayout());
0796:
0797: jButtonLoadQuery
0798: .setIcon(new javax.swing.ImageIcon(
0799: getClass()
0800: .getResource(
0801: "/it/businesslogic/ireport/icons/menu/folder_database.png"))); // NOI18N
0802: jButtonLoadQuery.setText("Load query");
0803: jButtonLoadQuery.setMargin(new java.awt.Insets(2, 4, 2, 4));
0804: jButtonLoadQuery
0805: .addActionListener(new java.awt.event.ActionListener() {
0806: public void actionPerformed(
0807: java.awt.event.ActionEvent evt) {
0808: jButtonLoadQueryActionPerformed(evt);
0809: }
0810: });
0811: gridBagConstraints = new java.awt.GridBagConstraints();
0812: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
0813: gridBagConstraints.weightx = 1.0;
0814: jPanel16.add(jButtonLoadQuery, gridBagConstraints);
0815:
0816: jButtonSaveQuery
0817: .setIcon(new javax.swing.ImageIcon(
0818: getClass()
0819: .getResource(
0820: "/it/businesslogic/ireport/icons/menu/database_save.png"))); // NOI18N
0821: jButtonSaveQuery.setText("Save query");
0822: jButtonSaveQuery.setMargin(new java.awt.Insets(2, 4, 2, 4));
0823: jButtonSaveQuery
0824: .addActionListener(new java.awt.event.ActionListener() {
0825: public void actionPerformed(
0826: java.awt.event.ActionEvent evt) {
0827: jButtonSaveQueryActionPerformed(evt);
0828: }
0829: });
0830: gridBagConstraints = new java.awt.GridBagConstraints();
0831: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 4);
0832: jPanel16.add(jButtonSaveQuery, gridBagConstraints);
0833:
0834: gridBagConstraints = new java.awt.GridBagConstraints();
0835: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
0836: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
0837: gridBagConstraints.weightx = 1.0;
0838: jPanel14.add(jPanel16, gridBagConstraints);
0839:
0840: gridBagConstraints = new java.awt.GridBagConstraints();
0841: gridBagConstraints.gridx = 0;
0842: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
0843: gridBagConstraints.weightx = 1.0;
0844: jPanelSQL.add(jPanel14, gridBagConstraints);
0845:
0846: jPanelQueryArea.setLayout(new java.awt.BorderLayout());
0847:
0848: jSplitPane2.setBorder(null);
0849: jSplitPane2.setDividerSize(6);
0850: jSplitPane2.setResizeWeight(0.5);
0851: jSplitPane2.setOneTouchExpandable(true);
0852:
0853: jPanel7.setLayout(new java.awt.GridBagLayout());
0854:
0855: jRSQLExpressionArea1.setBorder(javax.swing.BorderFactory
0856: .createEtchedBorder());
0857: jRSQLExpressionArea1.setMinimumSize(new java.awt.Dimension(50,
0858: 200));
0859: jRSQLExpressionArea1.setPreferredSize(new java.awt.Dimension(
0860: 661, 340));
0861: gridBagConstraints = new java.awt.GridBagConstraints();
0862: gridBagConstraints.gridx = 0;
0863: gridBagConstraints.gridwidth = 4;
0864: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
0865: gridBagConstraints.weightx = 1.0;
0866: gridBagConstraints.weighty = 1.0;
0867: jPanel7.add(jRSQLExpressionArea1, gridBagConstraints);
0868:
0869: jLabelStatusSQL.setBorder(javax.swing.BorderFactory
0870: .createEtchedBorder());
0871: gridBagConstraints = new java.awt.GridBagConstraints();
0872: gridBagConstraints.gridx = 0;
0873: gridBagConstraints.gridwidth = 4;
0874: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
0875: jPanel7.add(jLabelStatusSQL, gridBagConstraints);
0876:
0877: automaticlyReadFieldsCheckBox.setSelected(true);
0878: automaticlyReadFieldsCheckBox
0879: .setText("Automatically Retrieve Fields");
0880: automaticlyReadFieldsCheckBox
0881: .addActionListener(new java.awt.event.ActionListener() {
0882: public void actionPerformed(
0883: java.awt.event.ActionEvent evt) {
0884: automaticlyReadFieldsCheckBoxActionPerformed(evt);
0885: }
0886: });
0887: gridBagConstraints = new java.awt.GridBagConstraints();
0888: gridBagConstraints.gridy = 2;
0889: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
0890: gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
0891: jPanel7.add(automaticlyReadFieldsCheckBox, gridBagConstraints);
0892:
0893: readFieldsButton.setText("Read Fields");
0894: readFieldsButton.setEnabled(false);
0895: readFieldsButton
0896: .addActionListener(new java.awt.event.ActionListener() {
0897: public void actionPerformed(
0898: java.awt.event.ActionEvent evt) {
0899: readFieldsButtonActionPerformed(evt);
0900: }
0901: });
0902: gridBagConstraints = new java.awt.GridBagConstraints();
0903: gridBagConstraints.gridy = 2;
0904: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
0905: gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 0);
0906: jPanel7.add(readFieldsButton, gridBagConstraints);
0907:
0908: jButtonOpenDesigner.setText("Query designer");
0909: jButtonOpenDesigner
0910: .addActionListener(new java.awt.event.ActionListener() {
0911: public void actionPerformed(
0912: java.awt.event.ActionEvent evt) {
0913: jButtonOpenDesignerActionPerformed(evt);
0914: }
0915: });
0916: gridBagConstraints = new java.awt.GridBagConstraints();
0917: gridBagConstraints.gridy = 2;
0918: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
0919: gridBagConstraints.weightx = 1.0;
0920: gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 0);
0921: jPanel7.add(jButtonOpenDesigner, gridBagConstraints);
0922:
0923: exportQueryButton.setText("Send to clipboard");
0924: exportQueryButton
0925: .addActionListener(new java.awt.event.ActionListener() {
0926: public void actionPerformed(
0927: java.awt.event.ActionEvent evt) {
0928: exportQueryButtonActionPerformed(evt);
0929: }
0930: });
0931: gridBagConstraints = new java.awt.GridBagConstraints();
0932: gridBagConstraints.gridy = 2;
0933: gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
0934: gridBagConstraints.weightx = 1.0;
0935: gridBagConstraints.insets = new java.awt.Insets(2, 10, 2, 0);
0936: jPanel7.add(exportQueryButton, gridBagConstraints);
0937:
0938: jSplitPane2.setLeftComponent(jPanel7);
0939:
0940: jPanelQueryArea.add(jSplitPane2, java.awt.BorderLayout.CENTER);
0941:
0942: gridBagConstraints = new java.awt.GridBagConstraints();
0943: gridBagConstraints.gridx = 0;
0944: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
0945: gridBagConstraints.weightx = 1.0;
0946: gridBagConstraints.weighty = 1.0;
0947: gridBagConstraints.insets = new java.awt.Insets(0, 4, 2, 4);
0948: jPanelSQL.add(jPanelQueryArea, gridBagConstraints);
0949:
0950: jTabbedPane1.addTab("Report query ", jPanelSQL);
0951:
0952: jPanel8.setMinimumSize(new java.awt.Dimension(235, 30));
0953: jPanel8.setPreferredSize(new java.awt.Dimension(215, 30));
0954: jPanel8.setLayout(new java.awt.GridBagLayout());
0955: gridBagConstraints = new java.awt.GridBagConstraints();
0956: gridBagConstraints.gridx = 0;
0957: gridBagConstraints.gridy = 1;
0958: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
0959: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
0960: gridBagConstraints.weightx = 1.0;
0961: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
0962: jPanel8.add(jTextFieldBeanClass1, gridBagConstraints);
0963:
0964: jLabel3.setText("Class name");
0965: jLabel3.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
0966: gridBagConstraints = new java.awt.GridBagConstraints();
0967: gridBagConstraints.gridx = 0;
0968: gridBagConstraints.gridy = 0;
0969: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
0970: gridBagConstraints.insets = new java.awt.Insets(3, 3, 5, 3);
0971: jPanel8.add(jLabel3, gridBagConstraints);
0972:
0973: jButtonReadBeanAttributes3.setText("Read attributes");
0974: jButtonReadBeanAttributes3
0975: .addActionListener(new java.awt.event.ActionListener() {
0976: public void actionPerformed(
0977: java.awt.event.ActionEvent evt) {
0978: jButtonReadBeanAttributes3ActionPerformed(evt);
0979: }
0980: });
0981: gridBagConstraints = new java.awt.GridBagConstraints();
0982: gridBagConstraints.gridx = 1;
0983: gridBagConstraints.gridy = 1;
0984: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
0985: gridBagConstraints.insets = new java.awt.Insets(0, 3, 5, 3);
0986: jPanel8.add(jButtonReadBeanAttributes3, gridBagConstraints);
0987:
0988: jPanel11.setLayout(new java.awt.BorderLayout());
0989: gridBagConstraints = new java.awt.GridBagConstraints();
0990: gridBagConstraints.gridx = 0;
0991: gridBagConstraints.gridy = 3;
0992: gridBagConstraints.gridwidth = 2;
0993: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
0994: gridBagConstraints.weightx = 1.0;
0995: gridBagConstraints.weighty = 1.0;
0996: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 4);
0997: jPanel8.add(jPanel11, gridBagConstraints);
0998:
0999: jSeparator1.setMinimumSize(new java.awt.Dimension(0, 4));
1000: jSeparator1.setPreferredSize(new java.awt.Dimension(0, 4));
1001: gridBagConstraints = new java.awt.GridBagConstraints();
1002: gridBagConstraints.gridx = 0;
1003: gridBagConstraints.gridy = 2;
1004: gridBagConstraints.gridwidth = 2;
1005: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
1006: gridBagConstraints.weightx = 1.0;
1007: gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
1008: jPanel8.add(jSeparator1, gridBagConstraints);
1009:
1010: jTabbedPane1.addTab("JavaBean Datasource", jPanel8);
1011:
1012: jButton2.setText("Get fields from datasource");
1013: jButton2.addActionListener(new java.awt.event.ActionListener() {
1014: public void actionPerformed(java.awt.event.ActionEvent evt) {
1015: jButton2ActionPerformed(evt);
1016: }
1017: });
1018: jPanel6.add(jButton2);
1019:
1020: jTabbedPane1.addTab("DataSource Provider", jPanel6);
1021:
1022: jButton3.setText("Get fields from datasource");
1023: jButton3.addActionListener(new java.awt.event.ActionListener() {
1024: public void actionPerformed(java.awt.event.ActionEvent evt) {
1025: jButton2ActionPerformed1(evt);
1026: }
1027: });
1028: jPanel15.add(jButton3);
1029:
1030: jTabbedPane1.addTab("CSV Datasource", jPanel15);
1031:
1032: jPanel1.add(jTabbedPane1, java.awt.BorderLayout.CENTER);
1033:
1034: jSplitPane1.setTopComponent(jPanel1);
1035:
1036: jPanel2.setPreferredSize(new java.awt.Dimension(453, 150));
1037: jPanel2.setLayout(new java.awt.BorderLayout());
1038:
1039: jPanel3.setMinimumSize(new java.awt.Dimension(10, 34));
1040: jPanel3.setPreferredSize(new java.awt.Dimension(10, 34));
1041: jPanel3.setLayout(new java.awt.BorderLayout());
1042:
1043: jPanel4.setPreferredSize(new java.awt.Dimension(150, 10));
1044: jPanel4.setLayout(new java.awt.GridBagLayout());
1045:
1046: okButton.setText("OK");
1047: okButton.setEnabled(false);
1048: okButton.setMaximumSize(new java.awt.Dimension(67, 25));
1049: okButton.setMinimumSize(new java.awt.Dimension(67, 25));
1050: okButton.setPreferredSize(new java.awt.Dimension(67, 25));
1051: okButton.addActionListener(new java.awt.event.ActionListener() {
1052: public void actionPerformed(java.awt.event.ActionEvent evt) {
1053: okButtonActionPerformed(evt);
1054: }
1055: });
1056: gridBagConstraints = new java.awt.GridBagConstraints();
1057: gridBagConstraints.gridx = 0;
1058: gridBagConstraints.gridy = 0;
1059: jPanel4.add(okButton, gridBagConstraints);
1060:
1061: cancelButton.setText("Cancel");
1062: cancelButton
1063: .addActionListener(new java.awt.event.ActionListener() {
1064: public void actionPerformed(
1065: java.awt.event.ActionEvent evt) {
1066: cancelButtonActionPerformed(evt);
1067: }
1068: });
1069: gridBagConstraints = new java.awt.GridBagConstraints();
1070: gridBagConstraints.gridx = 1;
1071: gridBagConstraints.gridy = 0;
1072: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
1073: jPanel4.add(cancelButton, gridBagConstraints);
1074:
1075: jPanel3.add(jPanel4, java.awt.BorderLayout.EAST);
1076:
1077: jPanel9.setMinimumSize(new java.awt.Dimension(100, 20));
1078: jPanel9.setLayout(new java.awt.GridBagLayout());
1079:
1080: jButton1.setText("Filter expression...");
1081: jButton1.addActionListener(new java.awt.event.ActionListener() {
1082: public void actionPerformed(java.awt.event.ActionEvent evt) {
1083: jButton1ActionPerformed(evt);
1084: }
1085: });
1086: gridBagConstraints = new java.awt.GridBagConstraints();
1087: gridBagConstraints.gridx = 0;
1088: gridBagConstraints.gridy = 0;
1089: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
1090: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
1091: jPanel9.add(jButton1, gridBagConstraints);
1092:
1093: jButton4.setText("Sort options...");
1094: jButton4.addActionListener(new java.awt.event.ActionListener() {
1095: public void actionPerformed(java.awt.event.ActionEvent evt) {
1096: jButton1ActionPerformed1(evt);
1097: }
1098: });
1099: gridBagConstraints = new java.awt.GridBagConstraints();
1100: gridBagConstraints.gridx = 1;
1101: gridBagConstraints.gridy = 0;
1102: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
1103: gridBagConstraints.weightx = 1.0;
1104: gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
1105: jPanel9.add(jButton4, gridBagConstraints);
1106:
1107: jPanel3.add(jPanel9, java.awt.BorderLayout.CENTER);
1108:
1109: jPanel2.add(jPanel3, java.awt.BorderLayout.SOUTH);
1110:
1111: columnsErrorMsgLabel.setText("jLabel1");
1112: columnsErrorMsgLabel
1113: .setVerticalAlignment(javax.swing.SwingConstants.TOP);
1114: columnsErrorMsgLabel
1115: .setVerticalTextPosition(javax.swing.SwingConstants.TOP);
1116: columnsErrorScrollPane.setViewportView(columnsErrorMsgLabel);
1117:
1118: jPanel2.add(columnsErrorScrollPane,
1119: java.awt.BorderLayout.CENTER);
1120:
1121: jTableFields.setModel(new javax.swing.table.DefaultTableModel(
1122: new Object[][] {
1123:
1124: }, new String[] { "Field name", "Field type",
1125: "Description" }) {
1126: boolean[] canEdit = new boolean[] { false, false, false };
1127:
1128: public boolean isCellEditable(int rowIndex, int columnIndex) {
1129: return canEdit[columnIndex];
1130: }
1131: });
1132: jTableFields.setColumnControlVisible(true);
1133: jTableFields
1134: .addMouseListener(new java.awt.event.MouseAdapter() {
1135: public void mouseClicked(
1136: java.awt.event.MouseEvent evt) {
1137: jTableFieldsMouseClicked(evt);
1138: }
1139: });
1140: jTableFields.addKeyListener(new java.awt.event.KeyAdapter() {
1141: public void keyReleased(java.awt.event.KeyEvent evt) {
1142: jTableFieldsKeyReleased(evt);
1143: }
1144: });
1145: columnsScrollPane.setViewportView(jTableFields);
1146:
1147: jPanel2
1148: .add(columnsScrollPane,
1149: java.awt.BorderLayout.LINE_START);
1150:
1151: jSplitPane1.setBottomComponent(jPanel2);
1152:
1153: getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);
1154:
1155: pack();
1156: }// </editor-fold>//GEN-END:initComponents
1157:
1158: private void jTableFieldsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableFieldsMouseClicked
1159: if (SwingUtilities.isLeftMouseButton(evt)
1160: && evt.getClickCount() == 2) {
1161: if (this .jTableFields.getSelectedRow() >= 0) {
1162: JRField field = (JRField) this .jTableFields.getValueAt(
1163: this .jTableFields.getSelectedRow(), 0);
1164:
1165: JRFieldDialog jrpd = new JRFieldDialog(MainFrame
1166: .getMainInstance(), true);
1167: jrpd.setSubDataset(this .getSubDataset());
1168: jrpd.setField(field);
1169: jrpd.setVisible(true);
1170:
1171: if (jrpd.getDialogResult() == JOptionPane.OK_OPTION) {
1172: field = jrpd.getField();
1173: DefaultTableModel dtm = (DefaultTableModel) jTableFields
1174: .getModel();
1175: int index = jTableFields
1176: .convertRowIndexToModel(this .jTableFields
1177: .getSelectedRow());
1178: dtm.setValueAt(field, index, 0);
1179: dtm.setValueAt(field.getClassType(), index, 1);
1180: dtm.setValueAt(field.getDescription(), index, 2);
1181: this .jTableFields.updateUI();
1182: }
1183: }
1184: }
1185: }//GEN-LAST:event_jTableFieldsMouseClicked
1186:
1187: private void jTableFieldsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableFieldsKeyReleased
1188: //javax.swing.JOptionPane.showMessageDialog(null,"Key: "+evt.getKeyCode() + " (" + java.awt.event.KeyEvent.VK_DELETE + ")");
1189: if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_DELETE) {
1190: javax.swing.table.DefaultTableModel dtm = (javax.swing.table.DefaultTableModel) jTableFields
1191: .getModel();
1192: //int[] selectedRows = jTableFields.getSelectedRows();
1193: //for (int i= selectedRows.length-1; i>=0; --i)
1194: //{
1195: // it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField)this.jTableFields.getValueAt( i, 0);
1196: //this.subDataset.removeField(field);
1197: //this.jTableFields.removeRowSelectionInterval(i,i);
1198: //}
1199: while (jTableFields.getSelectedRow() >= 0) {
1200: dtm.removeRow(jTableFields.getSelectedRow());
1201: }
1202: }
1203: }//GEN-LAST:event_jTableFieldsKeyReleased
1204:
1205: private void jButtonOpenDesignerActionPerformed(
1206: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOpenDesignerActionPerformed
1207:
1208: if (getFieldsProvider() != null
1209: && getFieldsProvider().hasQueryDesigner()) {
1210: try {
1211: String newQuery = getFieldsProvider().designQuery(
1212: (IReportConnection) MainFrame.getMainInstance()
1213: .getProperties().get(
1214: "DefaultConnection"),
1215: jRSQLExpressionArea1.getText(), this );
1216: if (newQuery != null
1217: && !jRSQLExpressionArea1.getText().equals(
1218: newQuery)) {
1219: jRSQLExpressionArea1.setText(newQuery);
1220: }
1221: } catch (Exception ex) {
1222: ex.printStackTrace();
1223: javax.swing.JOptionPane.showMessageDialog(null, ex
1224: .getMessage());
1225: }
1226: }
1227:
1228: }//GEN-LAST:event_jButtonOpenDesignerActionPerformed
1229:
1230: private void jButton1ActionPerformed1(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed1
1231:
1232: SortFieldsDialog sfd = new SortFieldsDialog(this , true);
1233: sfd.setSubDataset(this .getSubDataset());
1234: sfd.setVisible(true);
1235:
1236: }//GEN-LAST:event_jButton1ActionPerformed1
1237:
1238: private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
1239:
1240: openFilterExpressionDialog(false);
1241: }//GEN-LAST:event_jButton1ActionPerformed
1242:
1243: private void jButtonSaveQueryActionPerformed(
1244: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveQueryActionPerformed
1245:
1246: Misc.saveSQLQuery(jRSQLExpressionArea1.getText(), this );
1247:
1248: }//GEN-LAST:event_jButtonSaveQueryActionPerformed
1249:
1250: private void jButtonLoadQueryActionPerformed(
1251: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLoadQueryActionPerformed
1252:
1253: String query = Misc.loadSQLQuery(this );
1254:
1255: if (query != null) {
1256: jRSQLExpressionArea1.setText(query);
1257: }
1258:
1259: }//GEN-LAST:event_jButtonLoadQueryActionPerformed
1260:
1261: private void jComboBoxQueryTypeActionPerformed(
1262: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxQueryTypeActionPerformed
1263: // TODO add your handling code here:
1264: boolean autoReadFields = automaticlyReadFieldsCheckBox
1265: .isSelected();
1266:
1267: readFieldsButton.setEnabled(false);
1268: automaticlyReadFieldsCheckBox.setSelected(false);
1269: readFieldsButton.setEnabled(false);
1270:
1271: String language = "sql";
1272:
1273: if (jComboBoxQueryType.getSelectedItem() != null
1274: && jComboBoxQueryType.getSelectedItem() instanceof Tag) {
1275: language = ""
1276: + ((Tag) jComboBoxQueryType.getSelectedItem())
1277: .getValue();
1278: } else if (jComboBoxQueryType.getSelectedItem() != null) {
1279: language = "" + jComboBoxQueryType.getSelectedItem();
1280: }
1281:
1282: // 1. Look for a special FieldsProvider....
1283: getJLabelStatusSQL().setText(
1284: "Looking for a valid Fields provider for " + language
1285: + " queries....");
1286: /////////////////////////////
1287: setFieldsProvider(null);
1288:
1289: Enumeration enum_qe = MainFrame.getMainInstance()
1290: .getQueryExecuters().elements();
1291: while (enum_qe.hasMoreElements()) {
1292: QueryExecuterDef qed = (QueryExecuterDef) enum_qe
1293: .nextElement();
1294: if (qed.getLanguage().equals(language)
1295: && qed.getFieldsProvider() != null
1296: && qed.getFieldsProvider().length() > 0) {
1297: try {
1298:
1299: // We have to set the query executer class...
1300: net.sf.jasperreports.engine.util.JRProperties
1301: .setProperty(
1302: "net.sf.jasperreports.query.executer.factory."
1303: + language, qed
1304: .getClassName());
1305: setFieldsProvider((FieldsProvider) Class.forName(
1306: qed.getFieldsProvider(),
1307: true,
1308: MainFrame.getMainInstance()
1309: .getReportClassLoader())
1310: .newInstance());
1311: } catch (Throwable t) {
1312: getJLabelStatusSQL().setText(
1313: "Error creating the fields provider "
1314: + t.getMessage());
1315: }
1316: }
1317: }
1318:
1319: exportQueryButton.setEnabled(language.equals("sql"));
1320:
1321: if (getFieldsProvider() == null && language.equals("sql")) {
1322: setFieldsProvider(new SQLFieldsProvider());
1323: } else if (getFieldsProvider() == null
1324: && (language.toLowerCase().equals("xpath") || language
1325: .toLowerCase().equals("xpath2"))) {
1326: setFieldsProvider(new XMLFieldsProvider());
1327: } else if (getFieldsProvider() == null
1328: && language.equals("hql")) {
1329: setFieldsProvider(new HQLFieldsProvider());
1330: } else if (getFieldsProvider() == null
1331: && language.equals("ejbql")) {
1332: setFieldsProvider(new EJBQLFieldsProvider());
1333: } else if (getFieldsProvider() == null
1334: && language.equals("mdx")) {
1335: setFieldsProvider(new MDXFieldsProvider());
1336: } else if (getFieldsProvider() == null
1337: && language.equals("xmla-mdx")) {
1338: setFieldsProvider(new CincomMDXFieldsProvider());
1339: }
1340:
1341: boolean isSettingSQLExpressionOldValue = isSettingSQLExpression;
1342: isSettingSQLExpression = true;
1343:
1344: if (getFieldsProvider() == null) {
1345: setSpecialLanguageComponent(null);
1346: readFieldsButton.setEnabled(false);
1347: jButtonOpenDesigner.setEnabled(false);
1348: automaticlyReadFieldsCheckBox.setEnabled(false);
1349: automaticlyReadFieldsCheckBox.setSelected(autoReadFields);
1350: getJLabelStatusSQL().setText(
1351: "No Fields Provider available for " + language
1352: + " queries!");
1353: } else {
1354: readFieldsButton.setEnabled(getFieldsProvider()
1355: .supportsGetFieldsOperation());
1356: automaticlyReadFieldsCheckBox
1357: .setEnabled(getFieldsProvider()
1358: .supportsAutomaticQueryExecution());
1359: automaticlyReadFieldsCheckBox.setSelected(autoReadFields);
1360: jButtonOpenDesigner.setEnabled(getFieldsProvider()
1361: .hasQueryDesigner());
1362: if (getFieldsProvider().hasEditorComponent()) {
1363: FieldsProviderEditor fpe = getFieldsProvider()
1364: .getEditorComponent(this );
1365: if (fpe instanceof Component) {
1366: setSpecialLanguageComponent((Component) fpe);
1367: }
1368: } else {
1369: setSpecialLanguageComponent(null);
1370: }
1371: getJLabelStatusSQL().setText(
1372: "Fields provider for " + language
1373: + " queries ready.");
1374: }
1375: isSettingSQLExpression = isSettingSQLExpressionOldValue;
1376:
1377: }//GEN-LAST:event_jComboBoxQueryTypeActionPerformed
1378:
1379: public void setSpecialLanguageComponent(Component c) {
1380: if (c == null) {
1381: if (jPanelQueryArea.getComponent(0) != jPanel7) {
1382: jPanelQueryArea.removeAll();
1383: jPanelQueryArea.add(jPanel7, BorderLayout.CENTER);
1384: }
1385: } else {
1386:
1387: if (jPanelQueryArea.getComponent(0) != jSplitPane2
1388: || jSplitPane2.getRightComponent() != c) {
1389: jPanelQueryArea.removeAll();
1390: jSplitPane2.setLeftComponent(jPanel7);
1391: jSplitPane2.setRightComponent(c);
1392: jPanelQueryArea.add(jSplitPane2, BorderLayout.CENTER);
1393: }
1394: }
1395: jPanelQueryArea.updateUI();
1396: jRSQLExpressionArea1.requestFocusInWindow();
1397: jRSQLExpressionArea1.requestFocus();
1398: }
1399:
1400: private void jButton2ActionPerformed1(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed1
1401: IReportConnection conn = (IReportConnection) MainFrame
1402: .getMainInstance().getProperties().get(
1403: "DefaultConnection");
1404: if (conn == null
1405: || !(conn instanceof it.businesslogic.ireport.connection.JRCSVDataSourceConnection)) {
1406: setColumnsError("The active connection is not a JasperReports CSV DataSource.");
1407: return;
1408: } else {
1409: it.businesslogic.ireport.connection.JRCSVDataSourceConnection ic = (it.businesslogic.ireport.connection.JRCSVDataSourceConnection) conn;
1410: try {
1411: Vector names = ic.getColumnNames();
1412: DefaultTableModel dtm = (DefaultTableModel) jTableFields
1413: .getModel();
1414: dtm.setRowCount(0);
1415:
1416: for (int nd = 0; nd < names.size(); ++nd) {
1417: String fieldName = "" + names.elementAt(nd);
1418: it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(
1419: fieldName, "java.lang.String");
1420: field.setDescription(""); //Field returned by " +methods[i].getName() + " (real type: "+ returnType +")");
1421:
1422: Vector row = new Vector();
1423: row.addElement(field);
1424: row.addElement(field.getClassType());
1425: row.addElement(field.getDescription());
1426: dtm.addRow(row);
1427: }
1428: jTableFields.setRowSelectionInterval(0,
1429: names.size() - 1);
1430: } catch (Exception ex) {
1431: setColumnsError("" + ex.getMessage());
1432:
1433: }
1434: }
1435: }//GEN-LAST:event_jButton2ActionPerformed1
1436:
1437: private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
1438:
1439: }//GEN-LAST:event_formWindowOpened
1440:
1441: private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
1442:
1443: IReportConnection conn = (IReportConnection) MainFrame
1444: .getMainInstance().getProperties().get(
1445: "DefaultConnection");
1446: if (conn == null
1447: || !(conn instanceof it.businesslogic.ireport.connection.JRDataSourceProviderConnection)) {
1448: setColumnsError("The active connection is not a JasperReports DataSource provider.");
1449: return;
1450: } else {
1451: it.businesslogic.ireport.connection.JRDataSourceProviderConnection ic = (it.businesslogic.ireport.connection.JRDataSourceProviderConnection) conn;
1452: try {
1453: Report rep = MainFrame.getMainInstance()
1454: .getActiveReportFrame().getReport();
1455:
1456: net.sf.jasperreports.engine.design.JasperDesign report = new net.sf.jasperreports.engine.design.JasperDesign();
1457: JRDesignQuery queryDes = new JRDesignQuery();
1458: queryDes.setText(jRSQLExpressionArea1.getText());
1459: String queryLanguage = "sql";
1460: Object obj = jComboBoxQueryType.getSelectedItem();
1461: if (obj != null && obj instanceof Tag) {
1462: queryLanguage = "" + ((Tag) obj).getValue();
1463: } else {
1464: queryLanguage = "" + obj;
1465: }
1466: queryDes.setLanguage(queryLanguage);
1467: report.setQuery(queryDes);
1468:
1469: for (int i = 0; i < rep.getJRproperties().size(); ++i) {
1470: JRProperty property = (JRProperty) rep
1471: .getJRproperties().elementAt(i);
1472: report.setProperty(property.getName(), property
1473: .getValue());
1474: }
1475:
1476: for (int i = 0; i < rep.getParameters().size(); ++i) {
1477: JRParameter ireportParam = (JRParameter) rep
1478: .getParameters().elementAt(i);
1479: if (ireportParam.isBuiltin())
1480: continue;
1481: net.sf.jasperreports.engine.design.JRDesignParameter param = new net.sf.jasperreports.engine.design.JRDesignParameter();
1482: param.setName(ireportParam.getName());
1483: JRDesignExpression de = new JRDesignExpression();
1484: de
1485: .setText(ireportParam
1486: .getDefaultValueExpression());
1487: de.setValueClassName(ireportParam.getClassType());
1488: param.setDefaultValueExpression(de);
1489: param.setForPrompting(ireportParam
1490: .isIsForPrompting());
1491: param
1492: .setValueClassName(ireportParam
1493: .getClassType());
1494: report.addParameter(param);
1495: }
1496:
1497: try {
1498:
1499: report.setName(rep.getName());
1500: } catch (Exception ex) {
1501:
1502: }
1503:
1504: // Create a temporary JasperReports object...
1505: net.sf.jasperreports.engine.JasperReport jr = new net.sf.jasperreports.engine.JasperReport(
1506: report, "", null, null, "");
1507:
1508: net.sf.jasperreports.engine.JRField[] jrfields = ic
1509: .getDataSourceProvider().getFields(jr);
1510: DefaultTableModel dtm = (DefaultTableModel) jTableFields
1511: .getModel();
1512: dtm.setRowCount(0);
1513: for (int i = 0; i < jrfields.length; ++i) {
1514: it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(
1515: jrfields[i].getName(), jrfields[i]
1516: .getValueClassName());
1517: field
1518: .setDescription(it.businesslogic.ireport.util.Misc
1519: .nvl(jrfields[i].getDescription(),
1520: ""));
1521: Vector row = new Vector();
1522: row.addElement(field);
1523: row.addElement(field.getClassType());
1524: row.addElement(field.getDescription());
1525: dtm.addRow(row);
1526: }
1527: } catch (Exception ex) {
1528: setColumnsError("" + ex.getMessage());
1529:
1530: }
1531: }
1532:
1533: }//GEN-LAST:event_jButton2ActionPerformed
1534:
1535: private void exportQueryButtonActionPerformed(
1536: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exportQueryButtonActionPerformed
1537:
1538: FieldReader fr = new FieldReader(
1539: jRSQLExpressionArea1.getText(), null);
1540: String query = fr.interpretQuery();
1541:
1542: java.awt.datatransfer.Clipboard clipboard = java.awt.Toolkit
1543: .getDefaultToolkit().getSystemClipboard();
1544: StringSelection fieldContent = new StringSelection(query);
1545:
1546: clipboard.setContents(fieldContent, this );
1547:
1548: // TODO add your handling code here:
1549: }//GEN-LAST:event_exportQueryButtonActionPerformed
1550:
1551: private void jButtonReadBeanAttributes3ActionPerformed(
1552: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonReadBeanAttributes3ActionPerformed
1553:
1554: bip1.setClassName(jTextFieldBeanClass1.getText().trim());
1555: }//GEN-LAST:event_jButtonReadBeanAttributes3ActionPerformed
1556:
1557: protected void getFieldsFromClass(Class clazz, String path)
1558: throws Exception {
1559: DefaultTableModel dtm = (DefaultTableModel) jTableFields
1560: .getModel();
1561:
1562: java.lang.reflect.Method[] methods = clazz.getMethods();
1563: java.lang.reflect.Field[] fields = clazz.getFields();
1564: // for any method, looking for get<FieldName> ....
1565:
1566: for (int i = 0; i < methods.length; ++i) {
1567:
1568: if (java.lang.reflect.Modifier.isPublic(methods[i]
1569: .getModifiers())
1570: && methods[i].getDeclaringClass().getName().equals(
1571: clazz.getName())
1572: && !java.lang.reflect.Modifier.isNative(methods[i]
1573: .getModifiers())
1574: && methods[i].getName().startsWith("get")
1575: && !methods[i].getReturnType().isPrimitive()
1576: && !methods[i].getReturnType().isArray()) {
1577: String fieldName = methods[i].getName().substring(3);
1578: // Looking for the field...
1579: for (int f = 0; f < fields.length; ++f) {
1580: if (fields[f].getName().equalsIgnoreCase(fieldName)) {
1581:
1582: fieldName = fields[f].getName();
1583: break;
1584: }
1585: }
1586:
1587: String returnType = methods[i].getReturnType()
1588: .getName();
1589: boolean found = false;
1590: for (int cc = 0; cc < standard_types.length; ++cc) {
1591: if (returnType.equalsIgnoreCase(standard_types[cc])) {
1592: it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(
1593: fieldName, returnType);
1594: field.setDescription(path + "" + fieldName);
1595: Vector row = new Vector();
1596: row.addElement(field);
1597: row.addElement(field.getClassType());
1598: row.addElement(field.getDescription());
1599: dtm.addRow(row);
1600: found = true;
1601: break;
1602: }
1603: }
1604: if (!found) {
1605: it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(
1606: fieldName, "java.lang.Object");
1607: field.setDescription(path + "" + fieldName);
1608: Vector row = new Vector();
1609: row.addElement(field);
1610: row.addElement(field.getClassType());
1611: row.addElement(field.getDescription());
1612: dtm.addRow(row);
1613: Class subClazz = Class.forName(returnType);
1614: getFieldsFromClass(subClazz, path + fieldName + ".");
1615: }
1616: }
1617: }
1618: }
1619:
1620: private void automaticlyReadFieldsCheckBoxActionPerformed(
1621: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_automaticlyReadFieldsCheckBoxActionPerformed
1622:
1623: if (automaticlyReadFieldsCheckBox.isSelected()) {
1624: // Automagically get quiery fields.
1625: // User has just enabled this so get field list.
1626: readFieldsButton.setEnabled(false);
1627: if (!isSettingSQLExpression) {
1628: processQueryChanged(jRSQLExpressionArea1.getText()
1629: .trim());
1630: }
1631: } else {
1632: // Turn off automagic field reading. User will have to press the
1633: // Read Fields button
1634: //okButton.setEnabled(false);
1635: readFieldsButton.setEnabled(true);
1636: //setColumnsError( "Enter your query above. Then use the Read " +
1637: // "Fields button to retrieve the list of fields." );
1638: this .jLabelStatusSQL
1639: .setText("Enter your query above. Then use the Read "
1640: + "Fields button to retrieve the list of fields.");
1641: }
1642:
1643: MainFrame.getMainInstance().getProperties().setProperty(
1644: "UseAutoRegiesterFields",
1645: "" + automaticlyReadFieldsCheckBox.isSelected());
1646:
1647: }//GEN-LAST:event_automaticlyReadFieldsCheckBoxActionPerformed
1648:
1649: private void readFieldsButtonActionPerformed(
1650: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readFieldsButtonActionPerformed
1651: processQueryChanged(jRSQLExpressionArea1.getText().trim());
1652: }//GEN-LAST:event_readFieldsButtonActionPerformed
1653:
1654: private void cancelButtonActionPerformed(
1655: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
1656: // No changes.
1657: num++;
1658: this .setVisible(false);
1659: }//GEN-LAST:event_cancelButtonActionPerformed
1660:
1661: private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
1662:
1663: try {
1664: if (stoppedChanging != null)
1665: stoppedChanging.stop();
1666:
1667: if (this .getSubDataset() != null) {
1668: num++; // avoid syncronization problems....
1669:
1670: Object obj = jComboBoxQueryType.getSelectedItem();
1671: if (obj != null && obj instanceof Tag) {
1672: this .subDataset.setQueryLanguage(""
1673: + ((Tag) obj).getValue());
1674: } else {
1675: this .subDataset.setQueryLanguage("" + obj);
1676: }
1677: // save the query to the report.
1678: this .subDataset
1679: .setQuery(jRSQLExpressionArea1.getText());
1680:
1681: if (jTableFields.getRowCount() > 0) {
1682: // Clear all the existing fields.
1683: this .subDataset.getFields().clear();
1684:
1685: // Add the new fields.
1686:
1687: //int[] selectedRows = jTableFields.getSelectedRows();
1688: //for (int i=0; i<selectedRows.length ; ++i) {
1689: // if (selectedRows[i] > jTableFields.getRowCount()) continue;
1690:
1691: // it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField)this.jTableFields.getValueAt(selectedRows[i], 0);
1692: for (int i = 0; i < jTableFields.getRowCount(); ++i) {
1693: it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField) this .jTableFields
1694: .getValueAt(i, 0);
1695: Enumeration e = this .subDataset.getFields()
1696: .elements();
1697: boolean found = false;
1698: while (e.hasMoreElements()) {
1699: it.businesslogic.ireport.JRField f = (it.businesslogic.ireport.JRField) e
1700: .nextElement();
1701: if (f.getName().equalsIgnoreCase(
1702: field.getName())) {
1703: found = true;
1704: break;
1705: }
1706: }
1707: if (!found) {
1708: this .subDataset.addField(field);
1709: }
1710: }
1711: if (subDataset instanceof Report) {
1712: MainFrame.getMainInstance().getValuesDialog()
1713: .getValuesPanel().updateFields();
1714: }
1715: }
1716: }
1717:
1718: } catch (Throwable ex) {
1719: ex.printStackTrace();
1720: }
1721:
1722: this .setVisible(false);
1723:
1724: }//GEN-LAST:event_okButtonActionPerformed
1725:
1726: /** Closes the dialog */
1727: private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
1728: setVisible(false);
1729: dispose();
1730: }//GEN-LAST:event_closeDialog
1731:
1732: /**
1733: * @param args the command line arguments
1734: */
1735: public static void main(String args[]) {
1736: new ReportQueryDialog(new javax.swing.JFrame(), true)
1737: .setVisible(true);
1738: }
1739:
1740: Map parameterNameToExpressionID = null;
1741:
1742: /**
1743: * Create an expression evaluator for report parameters.
1744: *
1745: */
1746: private Interpreter prepareExpressionEvaluator()
1747: throws bsh.EvalError {
1748:
1749: Interpreter interpreter = new Interpreter();
1750: interpreter.setClassLoader(interpreter.getClass()
1751: .getClassLoader());
1752:
1753: // Staring patch from rp4
1754: StringTokenizer st = new StringTokenizer(MainFrame
1755: .getMainInstance().getProperties().getProperty(
1756: "classpath", ""), "\n");
1757: interpreter.eval("String tmp;");
1758: while (st.hasMoreTokens()) {
1759: String token = st.nextToken();
1760: if (token != null && token.trim().length() > 0) {
1761: interpreter.set("tmp", token.trim());
1762: interpreter.eval("addClassPath(tmp);");
1763: }
1764: }
1765:
1766: // Add report import directives to the bsh interpreter
1767: interpreter.eval("import net.sf.jasperreports.engine.*;");
1768: interpreter.eval("import net.sf.jasperreports.engine.fill.*;");
1769: interpreter.eval("import java.util.*;");
1770: interpreter.eval("import java.math.*;");
1771: interpreter.eval("import java.text.*;");
1772: interpreter.eval("import java.io.*;");
1773: interpreter.eval("import java.net.*;");
1774: interpreter.eval("import java.util.*;");
1775: interpreter.eval("import net.sf.jasperreports.engine.*;");
1776: interpreter.eval("import net.sf.jasperreports.engine.data.*;");
1777:
1778: Enumeration imps = MainFrame.getMainInstance()
1779: .getActiveReportFrame().getReport().getImports()
1780: .elements();
1781: while (imps.hasMoreElements()) {
1782: String var = (String) imps.nextElement();
1783: interpreter.eval("import " + var + ";");
1784: }
1785: // End patch from rp4
1786: /*
1787: interpreter.eval(new StringReader(bshScript));
1788:
1789: interpreter.eval("bshCalculator = createBshCalculator()");
1790: */
1791: return interpreter;
1792:
1793: // return null;
1794: }
1795:
1796: /**
1797: * Convert a class name string into its class object.
1798: * There must be a function in JasperReports that does this somewhere.
1799: *
1800: *
1801: */
1802: private Class classStringToClass(String classType) {
1803: Class c = null;
1804:
1805: if (classType.equals("java.lang.String")) {
1806: c = java.lang.String.class;
1807: } else if (classType.equals("java.lang.Integer")) {
1808: c = java.lang.Integer.class;
1809: } else if (classType.equals("java.lang.Boolean")) {
1810: c = java.lang.Boolean.class;
1811: } else if (classType.equals("java.lang.Byte")) {
1812: c = java.lang.Byte.class;
1813: } else if (classType.equals("java.util.Date")) {
1814: c = java.util.Date.class;
1815: } else if (classType.equals("java.sql.Timestamp")) {
1816: c = java.sql.Timestamp.class;
1817: } else if (classType.equals("java.sql.Time")) {
1818: c = java.sql.Time.class;
1819: } else if (classType.equals("java.lang.Double")) {
1820: c = java.lang.Double.class;
1821: } else if (classType.equals("java.lang.Float")) {
1822: c = java.lang.Float.class;
1823: } else if (classType.equals("java.lang.Long")) {
1824: c = java.lang.Long.class;
1825: } else if (classType.equals("java.lang.Short")) {
1826: c = java.lang.Short.class;
1827: } else if (classType.equals("java.math.BigDecimal")) {
1828: c = java.math.BigDecimal.class;
1829: }
1830:
1831: return c;
1832: }
1833:
1834: // Variables declaration - do not modify//GEN-BEGIN:variables
1835: private javax.swing.JCheckBox automaticlyReadFieldsCheckBox;
1836: private javax.swing.JButton cancelButton;
1837: private javax.swing.JLabel columnsErrorMsgLabel;
1838: private javax.swing.JScrollPane columnsErrorScrollPane;
1839: private javax.swing.JScrollPane columnsScrollPane;
1840: private javax.swing.JButton exportQueryButton;
1841: private javax.swing.JButton jButton1;
1842: private javax.swing.JButton jButton2;
1843: private javax.swing.JButton jButton3;
1844: private javax.swing.JButton jButton4;
1845: private javax.swing.JButton jButtonLoadQuery;
1846: private javax.swing.JButton jButtonOpenDesigner;
1847: private javax.swing.JButton jButtonReadBeanAttributes3;
1848: private javax.swing.JButton jButtonSaveQuery;
1849: private javax.swing.JComboBox jComboBoxQueryType;
1850: private javax.swing.JLabel jLabel1;
1851: private javax.swing.JLabel jLabel3;
1852: private org.jdesktop.swingx.JXBusyLabel jLabelStatusSQL;
1853: private javax.swing.JPanel jPanel1;
1854: private javax.swing.JPanel jPanel11;
1855: private javax.swing.JPanel jPanel14;
1856: private javax.swing.JPanel jPanel15;
1857: private javax.swing.JPanel jPanel16;
1858: private javax.swing.JPanel jPanel2;
1859: private javax.swing.JPanel jPanel3;
1860: private javax.swing.JPanel jPanel4;
1861: private javax.swing.JPanel jPanel6;
1862: private javax.swing.JPanel jPanel7;
1863: private javax.swing.JPanel jPanel8;
1864: private javax.swing.JPanel jPanel9;
1865: private javax.swing.JPanel jPanelQueryArea;
1866: private javax.swing.JPanel jPanelSQL;
1867: private it.businesslogic.ireport.gui.JRSQLExpressionArea jRSQLExpressionArea1;
1868: private javax.swing.JSeparator jSeparator1;
1869: private javax.swing.JSplitPane jSplitPane1;
1870: private javax.swing.JSplitPane jSplitPane2;
1871: private javax.swing.JTabbedPane jTabbedPane1;
1872: private org.jdesktop.swingx.JXTable jTableFields;
1873: private javax.swing.JTextField jTextFieldBeanClass1;
1874: private javax.swing.JButton okButton;
1875: private javax.swing.JButton readFieldsButton;
1876: // End of variables declaration//GEN-END:variables
1877:
1878: private boolean isSettingSQLExpression = false;
1879:
1880: public void lostOwnership(Clipboard parClipboard,
1881: Transferable parTransferable) {
1882: }
1883:
1884: public static Object recursiveInterpreter(Interpreter interpreter,
1885: String expression, Vector parameters) throws EvalError {
1886: return recursiveInterpreter(interpreter, expression,
1887: parameters, 0);
1888: }
1889:
1890: public static Object recursiveInterpreter(Interpreter interpreter,
1891: String expression, Vector parameters, int recursion_level)
1892: throws EvalError {
1893: return recursiveInterpreter(interpreter, expression,
1894: parameters, 0, null);
1895: }
1896:
1897: public static Object recursiveInterpreter(Interpreter interpreter,
1898: String expression, Vector parameters, int recursion_level,
1899: String this _param_name) throws EvalError {
1900: ++recursion_level;
1901:
1902: if (expression == null || expression.length() == 0)
1903: return null;
1904:
1905: //System.out.println("Valuto ["+ recursion_level +"]: " + expression);
1906: if (recursion_level > 100)
1907: return null;
1908: if (expression != null && expression.trim().length() > 0) {
1909: // for each parameter, we have to calc the real value...
1910: while (expression.indexOf("$P{") >= 0) {
1911: int start_index = expression.indexOf("$P{") + 3;
1912: String param_name = expression.substring(start_index,
1913: expression.indexOf("}", start_index));
1914: String param_expression = "";
1915: for (int i = 0; i < parameters.size(); ++i) {
1916: JRParameter p = (JRParameter) parameters
1917: .elementAt(i);
1918: if (p.getName().equals(param_name)) {
1919: param_expression = p
1920: .getDefaultValueExpression();
1921: break;
1922: }
1923: }
1924:
1925: String param_name_literal = "param_"
1926: + net.sf.jasperreports.engine.util.JRStringUtil
1927: .getLiteral(param_name);
1928:
1929: expression = Misc.string_replace(param_name_literal,
1930: "$P{" + param_name + "}", expression);
1931: //interpreter.set( param_name_literal, recursiveInterpreter(interpreter, param_expression, parameters, recursion_level));
1932:
1933: // If the parameter was never evaluated before, that can happen is some cases,
1934: // evaluate it now!
1935: if (interpreter.get(param_name_literal) == null) {
1936: Object paramValue = recursiveInterpreter(
1937: interpreter, param_expression, parameters,
1938: recursion_level, this _param_name);
1939: interpreter.set(param_name_literal, paramValue);
1940: }
1941: }
1942:
1943: String this _param_name_literal = "param_unknow";
1944:
1945: if (this _param_name != null) {
1946: this _param_name_literal = "param_"
1947: + net.sf.jasperreports.engine.util.JRStringUtil
1948: .getLiteral(this _param_name);
1949: }
1950: //System.out.println("interpreto ["+ recursion_level +"]: " + expression);
1951: //System.out.flush();
1952: Object res = interpreter.eval(expression);
1953: interpreter.set(this _param_name_literal, res);
1954: //System.out.println("Result: " + res);
1955: //System.out.flush();
1956: return res;
1957: }
1958: return null;
1959: }
1960:
1961: private SubDataset subDataset;
1962:
1963: public SubDataset getSubDataset() {
1964: return subDataset;
1965: }
1966:
1967: public void setSubDataset(SubDataset subDataset) {
1968:
1969: isSettingSQLExpression = true;
1970: try { // Used only to perform a finally op
1971: this .subDataset = subDataset;
1972:
1973: DefaultTableModel dtm = (DefaultTableModel) jTableFields
1974: .getModel();
1975: dtm.setRowCount(0);
1976:
1977: num++;
1978: jLabelStatusSQL.setText("");
1979:
1980: jRSQLExpressionArea1.setText("");
1981:
1982: try {
1983: automaticlyReadFieldsCheckBox
1984: .setSelected(Boolean
1985: .valueOf(
1986: MainFrame
1987: .getMainInstance()
1988: .getProperties()
1989: .getProperty(
1990: "UseAutoRegiesterFields",
1991: "true"))
1992: .booleanValue());
1993: automaticlyReadFieldsCheckBoxActionPerformed(null);
1994: } catch (Exception ex) {
1995:
1996: }
1997: // Load query...
1998: if (subDataset == null)
1999: this .jRSQLExpressionArea1.setText("");
2000: else {
2001: // Use query, and use existing field list. ie Dont load from DB
2002: this .jRSQLExpressionArea1.setText(this .subDataset
2003: .getQuery());
2004:
2005: List columns = new ArrayList();
2006: Iterator i = subDataset.getFields().iterator();
2007: while (i.hasNext()) {
2008: it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField) i
2009: .next();
2010: columns.add(new Object[] { field,
2011: field.getClassType(),
2012: field.getDescription() });
2013: }
2014: setColumns(columns);
2015:
2016: for (int ix = 0; ix < jComboBoxQueryType.getItemCount(); ++ix) {
2017: if (!(jComboBoxQueryType.getItemAt(ix) instanceof Tag)) {
2018: jComboBoxQueryType.removeItemAt(ix);
2019: ix--;
2020: }
2021: }
2022:
2023: boolean found = false;
2024: for (int ix = 0; ix < jComboBoxQueryType.getItemCount(); ++ix) {
2025: Tag t = (Tag) jComboBoxQueryType.getItemAt(ix);
2026: if (t.getValue().equals(
2027: subDataset.getQueryLanguage())) {
2028: found = true;
2029: jComboBoxQueryType.setSelectedIndex(ix);
2030: break;
2031: }
2032: }
2033: if (!found) // Default is sql...
2034: {
2035: jComboBoxQueryType.setEditable(true);
2036: jComboBoxQueryType.setSelectedItem(subDataset
2037: .getQueryLanguage());
2038: }
2039:
2040: jLabelStatusSQL.setText("");
2041:
2042: }
2043:
2044: } finally {
2045:
2046: isSettingSQLExpression = false;
2047: }
2048: }
2049:
2050: public void applyI18n() {
2051: // Start autogenerated code ----------------------
2052: automaticlyReadFieldsCheckBox.setText(I18n.getString(
2053: "reportQueryDialog.utomaticlyReadFieldsCheckBox",
2054: "Automatically Retrieve Fields"));
2055: // End autogenerated code ----------------------
2056: // Start autogenerated code ----------------------
2057: cancelButton.setText(I18n.getString(
2058: "reportQueryDialog.ancelButton", "Cancel"));
2059: exportQueryButton.setText(I18n.getString(
2060: "reportQueryDialog.xportQueryButton",
2061: "Send to clipboard"));
2062: jButton1.setText(I18n.getString("reportQueryDialog.button1",
2063: "Filter expression..."));
2064: jButton2.setText(I18n.getString("reportQueryDialog.button2",
2065: "Get fields from datasource"));
2066: jButton3.setText(I18n.getString("reportQueryDialog.button3",
2067: "Get fields from datasource"));
2068: jButton4.setText(I18n.getString("reportQueryDialog.button4",
2069: "Sort options..."));
2070: jButtonLoadQuery.setText(I18n.getString(
2071: "reportQueryDialog.buttonLoadQuery", "Load query"));
2072: jButtonReadBeanAttributes3.setText(I18n.getString(
2073: "reportQueryDialog.buttonReadBeanAttributes3",
2074: "Read attributes"));
2075: jButtonSaveQuery.setText(I18n.getString(
2076: "reportQueryDialog.buttonSaveQuery", "Save query"));
2077: jLabel1.setText(I18n.getString("reportQueryDialog.label1",
2078: "Query language"));
2079: jLabel3.setText(I18n.getString("reportQueryDialog.label3",
2080: "Class name"));
2081: okButton.setText(I18n.getString("reportQueryDialog.kButton",
2082: "OK"));
2083: readFieldsButton.setText(I18n.getString(
2084: "reportQueryDialog.eadFieldsButton", "Read Fields"));
2085: // End autogenerated code ----------------------
2086:
2087: jTableFields.getColumnModel().getColumn(0).setHeaderValue(
2088: I18n.getString(
2089: "reportQueryDialog.tablecolumn.fieldName",
2090: "Field name"));
2091: jTableFields.getColumnModel().getColumn(1).setHeaderValue(
2092: I18n.getString(
2093: "reportQueryDialog.tablecolumn.fieldType",
2094: "Field type"));
2095: jTableFields.getColumnModel().getColumn(2).setHeaderValue(
2096: I18n.getString(
2097: "reportQueryDialog.tablecolumn.description",
2098: "Description"));
2099:
2100: jTabbedPane1.setTitleAt(0, it.businesslogic.ireport.util.I18n
2101: .getString("reportQueryDialog.tab.ReportQuery",
2102: "Report query"));
2103: jTabbedPane1.setTitleAt(1, it.businesslogic.ireport.util.I18n
2104: .getString("reportQueryDialog.tab.JavaBeanDatasource",
2105: "JavaBean Datasource"));
2106: jTabbedPane1.setTitleAt(2, it.businesslogic.ireport.util.I18n
2107: .getString("reportQueryDialog.tab.DataSourceProvider",
2108: "DataSource Provider"));
2109: jTabbedPane1.setTitleAt(3, it.businesslogic.ireport.util.I18n
2110: .getString("reportQueryDialog.tab.CSVDatasource",
2111: "CSV Datasource"));
2112:
2113: this .setTitle(I18n.getString("reportQueryDialog.title",
2114: "Report query"));
2115: cancelButton.setMnemonic(I18n.getString(
2116: "reportQueryDialog.cancelButtonMnemonic", "c")
2117: .charAt(0));
2118: okButton.setMnemonic(I18n.getString(
2119: "reportQueryDialog.okButtonMnemonic", "o").charAt(0));
2120: }
2121:
2122: /**
2123: * Thie method can be useful for a CustomQueryEditor
2124: */
2125: public JRSQLExpressionArea getSQLExpressionArea() {
2126: return jRSQLExpressionArea1;
2127: }
2128:
2129: /**
2130: * Thie method can be useful for a CustomQueryEditor
2131: * Return the table containing all the fields.
2132: */
2133: public JTable getFieldsTable() {
2134: return jTableFields;
2135: }
2136:
2137: /**
2138: * This method is used to open the filter expression in case of errors...
2139: */
2140: public void openFilterExpressionDialog(boolean showAsError) {
2141: if (getSubDataset() == null)
2142: return;
2143: FilterExpressionDialog fed = new FilterExpressionDialog(this ,
2144: true);
2145: fed.setFilterExpression(this .getSubDataset()
2146: .getFilterExpression(), getSubDataset());
2147: if (showAsError) {
2148: fed
2149: .setFocusedExpression(FilterExpressionDialog.COMPONENT_EXPRESSION);
2150: }
2151:
2152: fed.setVisible(true);
2153: if (fed.getDialogResult() == JOptionPane.OK_OPTION) {
2154: getSubDataset().setFilterExpression(
2155: fed.getFilterExpression());
2156: System.out.println("Set filter expression to "
2157: + getSubDataset() + " ("
2158: + getSubDataset().getFilterExpression() + ")");
2159: }
2160: }
2161:
2162: public void addField(JRField field) {
2163:
2164: // Add the field if there is not already a fiels with the same name...
2165: if (field == null)
2166: return;
2167:
2168: if (columnsErrorMsgLabel.isVisible()) {
2169: columnsErrorMsgLabel.setText("");
2170: jPanel2.remove(columnsErrorScrollPane);
2171: jPanel2
2172: .add(columnsScrollPane,
2173: java.awt.BorderLayout.CENTER);
2174: jPanel2.revalidate();
2175: }
2176:
2177: DefaultTableModel dtm = (DefaultTableModel) jTableFields
2178: .getModel();
2179: for (int i = 0; i < dtm.getRowCount(); ++i) {
2180: JRField tmpField = (JRField) dtm.getValueAt(i, 0);
2181: if (tmpField.getName().equals(field.getName()))
2182: return;
2183: }
2184: Vector row = new Vector();
2185: row.addElement(field);
2186: row.addElement(field.getClassType());
2187: row.addElement(field.getDescription());
2188: dtm.addRow(row);
2189:
2190: jTableFields.addRowSelectionInterval(
2191: jTableFields.getRowCount() - 1, jTableFields
2192: .getRowCount() - 1);
2193:
2194: jTableFields.updateUI();
2195:
2196: }
2197:
2198: }
|