reportquerydialog.java

来自「优秀的打印控件全源代码,类似水晶表的设计器!」· Java 代码 · 共 1,003 行 · 第 1/3 页

JAVA
1,003
字号
                        if ( returnType.equalsIgnoreCase(standard_types[cc]))                        {                                                   it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(fieldName, returnType);                            field.setDescription(path + "" + fieldName);                            Vector row = new Vector();                            row.addElement(field);                            row.addElement(field.getClassType());                            row.addElement(field.getDescription());                            dtm.addRow(row);                            found = true;                            break;                        }                  }                  if (!found)                  {                        it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(fieldName, "java.lang.Object");                        field.setDescription(path + "" + fieldName);                        Vector row = new Vector();                        row.addElement(field);                        row.addElement(field.getClassType());                        row.addElement(field.getDescription());                        dtm.addRow(row);                        Class subClazz = Class.forName(returnType);                        getFieldsFromClass( subClazz , path + fieldName + ".");                  }                }            }                        }        private void automaticlyReadFieldsCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_automaticlyReadFieldsCheckBoxActionPerformed        if( automaticlyReadFieldsCheckBox.isSelected() ) {            // Automagically get quiery fields.            // User has just enabled this so get field list.            readFieldsButton.setEnabled(false);            processQueryChanged( jRSQLExpressionArea1.getText().trim() );        } else {            // Turn off automagic field reading. User will have to press the             // Read Fields button            okButton.setEnabled(false);            readFieldsButton.setEnabled(true);            setColumnsError( "Enter your query above. Then use the Read " +                    "Fields button to retrieve the list of fields." );        }            }//GEN-LAST:event_automaticlyReadFieldsCheckBoxActionPerformed    private void readFieldsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_readFieldsButtonActionPerformed        processQueryChanged( jRSQLExpressionArea1.getText().trim() );    }//GEN-LAST:event_readFieldsButtonActionPerformed    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed        // No changes.        this.setVisible(false);    }//GEN-LAST:event_cancelButtonActionPerformed    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed                // save the query to the report.        this.getJReportFrame().getReport().setQuery( jRSQLExpressionArea1.getText());                // Clear all the existing fields.        this.getJReportFrame().getReport().getFields().clear();                // Add the new fields.        int[] selectedRows = jTableFields.getSelectedRows();        for (int i=0; i<selectedRows.length; ++i) {            it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField)this.jTableFields.getValueAt( i, 0);            Enumeration e = this.getJReportFrame().getReport().getFields().elements();            boolean found = false;            while (e.hasMoreElements()) {                it.businesslogic.ireport.JRField f = (it.businesslogic.ireport.JRField)e.nextElement();                if (f.getName().equalsIgnoreCase(field.getName())) {                    found = true;                    break;                }            }            if (!found) {                this.getJReportFrame().getReport().addField(field);            }        }        this.getJReportFrame().getMainFrame().getValuesDialog().updateFields();        this.setVisible(false);            }//GEN-LAST:event_okButtonActionPerformed    private void jTableFieldsKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTableFieldsKeyReleased                //javax.swing.JOptionPane.showMessageDialog(null,"Key: "+evt.getKeyCode() + " (" + java.awt.event.KeyEvent.VK_DELETE + ")");        if (evt.getKeyCode() == java.awt.event.KeyEvent.VK_DELETE)        {             javax.swing.table.DefaultTableModel dtm =  (javax.swing.table.DefaultTableModel)jTableFields.getModel();             int[] selectedRows = jTableFields.getSelectedRows();             for (int i= selectedRows.length-1; i>=0; --i)              {                 it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField)this.jTableFields.getValueAt( i, 0);                 this.getJReportFrame().getReport().removeField(field);                 this.jTableFields.removeRowSelectionInterval(i,i);             }        }    }//GEN-LAST:event_jTableFieldsKeyReleased    private void jButtonReadBeanAttributesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonReadBeanAttributesActionPerformed        String classname = jTextFieldBeanClass.getText().trim();        if (classname.equals("")){                     javax.swing.JOptionPane.showMessageDialog(this, "No class specified!","Error",javax.swing.JOptionPane.ERROR_MESSAGE);            return;        }        try {                        java.lang.reflect.Method[] methods = Class.forName(classname).getMethods();            java.lang.reflect.Field[] fields = Class.forName(classname).getFields();            // for any method, looking for get<FieldName> ....                        DefaultTableModel dtm = (DefaultTableModel)jTableFields.getModel();            dtm.setRowCount(0);                        for (int i=0; i<methods.length; ++i)            {                               if ( java.lang.reflect.Modifier.isPublic( methods[i].getModifiers() ) &&                     methods[i].getDeclaringClass().getName().equals(classname) &&                     !java.lang.reflect.Modifier.isNative( methods[i].getModifiers() )                          && methods[i].getName().startsWith("get")                        && !methods[i].getReturnType().isPrimitive()                         && !methods[i].getReturnType().isArray())                {                   String fieldName = methods[i].getName().substring(3);                   // Looking for the field...                   for (int f=0; f<fields.length; ++f)                   {                       if (fields[f].getName().equalsIgnoreCase( fieldName ))                       {                                                      fieldName = fields[f].getName();                           break;                       }                   }                                      String returnType =  methods[i].getReturnType().getName();                   it.businesslogic.ireport.JRField field = new it.businesslogic.ireport.JRField(fieldName, returnType);                   field.setDescription("Field returned by " +methods[i].getName() + " method of " + classname);                   Vector row = new Vector();                   row.addElement(field);                   row.addElement(field.getClassType());                   dtm.addRow(row);                }            }                    } catch (ClassNotFoundException cnf)        {            javax.swing.JOptionPane.showMessageDialog(this, "Class not found!\nCheck your classpath and retry.","Error",javax.swing.JOptionPane.ERROR_MESSAGE);            return;        } catch (Exception ex)        {            javax.swing.JOptionPane.showMessageDialog(this, ex.getMessage() ,"Error",javax.swing.JOptionPane.ERROR_MESSAGE);            return;        }    }//GEN-LAST:event_jButtonReadBeanAttributesActionPerformed        /** Closes the dialog */    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog        setVisible(false);        dispose();    }//GEN-LAST:event_closeDialog        /**     * @param args the command line arguments     */    public static void main(String args[]) {        new ReportQueryDialog(new javax.swing.JFrame(), true).show();    }        /** Getter for property jReportFrame.     * @return Value of property jReportFrame.     *     */    public it.businesslogic.ireport.gui.JReportFrame getJReportFrame() {        return jReportFrame;    }            /** Setter for property jReportFrame.     * @param jReportFrame New value of property jReportFrame.     *     */    public void setJReportFrame(it.businesslogic.ireport.gui.JReportFrame jReportFrame) {        this.jReportFrame = jReportFrame;        DefaultTableModel dtm = (DefaultTableModel)jTableFields.getModel();        dtm.setRowCount(0);         // Load query...        if (jReportFrame == null)            this.jRSQLExpressionArea1.setText("");        else        {            // Use query, and use existing field list. ie Dont load from DB            isSettingSQLExpression = true;            this.jRSQLExpressionArea1.setText( this.jReportFrame.getReport().getQuery() );            isSettingSQLExpression = false;                        List columns = new ArrayList();            Iterator i = jReportFrame.getReport().getFields().iterator();            while( i.hasNext() ) {                it.businesslogic.ireport.JRField field = (it.businesslogic.ireport.JRField)i.next();                columns.add( new Object[]{field, field.getClassType()} );            }            setColumns( columns );        }    }            Map parameterNameToExpressionID = null;        /**     * Create an expression evaluator for report parameters.     *     */    private Interpreter prepareExpressionEvaluator() throws dori.jasper.engine.JRException, bsh.EvalError {        JasperDesign jd = new JasperDesign();        parameterNameToExpressionID = new HashMap();                Enumeration enumParams = getJReportFrame().getReport().getParameters().elements();        while( enumParams.hasMoreElements() ) {            it.businesslogic.ireport.JRParameter parameter = (it.businesslogic.ireport.JRParameter)enumParams.nextElement();            if( parameter.getDefaultValueExpression().length()==0 ) {                // no default value for this param                continue;            }                        Class c = classStringToClass( parameter.getClassType() );                        JRDesignParameter p = new JRDesignParameter();            //JRDesignExpression exp = parameter.getDefaultValueExpression();            JRDesignExpression exp = new JRDesignExpression();            if (c==null) c = java.lang.Object.class;            exp.setValueClass( c );            exp.setName("parameterDefaultValue_" + parameter.getName() );            exp.setText( parameter.getDefaultValueExpression() );            p.setName( parameter.getName() );            p.setValueClass( c );            p.setForPrompting( false );            p.setDefaultValueExpression( exp );            jd.addParameter( p );            parameterNameToExpressionID.put( parameter.getName(), new Integer(exp.getId()) );        }        String bshScript = JRBshGenerator.generateScript( jd );        Interpreter interpreter = new Interpreter();        interpreter.setClassLoader(interpreter.getClass().getClassLoader());        interpreter.eval(new StringReader(bshScript));        interpreter.eval("bshCalculator = createBshCalculator()");                return interpreter;    }    /**     * Convert a class name string into its class object.     * There must be a function in JasperReports that does this somewhere.     *     *     */    private Class classStringToClass(String classType) {        Class c = null;                        if ( classType.equals("java.lang.String") ) {            c = java.lang.String.class;        } else if ( classType.equals("java.lang.Integer") ) {            c = java.lang.Integer.class;        } else if ( classType.equals("java.lang.Boolean") ) {            c = java.lang.Boolean.class;        } else if ( classType.equals("java.lang.Byte") ) {            c = java.lang.Byte.class;        } else if ( classType.equals("java.util.Date") ) {            c = java.util.Date.class;        } else if ( classType.equals("java.sql.Timestamp") ) {            c = java.sql.Timestamp.class;        } else if ( classType.equals("java.sql.Time") ) {            c = java.sql.Time.class;        } else if ( classType.equals("java.lang.Double") ) {            c = java.lang.Double.class;        } else if ( classType.equals("java.lang.Float") ) {            c = java.lang.Float.class;        } else if ( classType.equals("java.lang.Long") ) {            c = java.lang.Long.class;        } else if ( classType.equals("java.lang.Short") ) {            c = java.lang.Short.class;        } else if ( classType.equals("java.math.BigDecimal") ) {            c = java.math.BigDecimal.class;        }                return c;    }            // Variables declaration - do not modify//GEN-BEGIN:variables    private javax.swing.JCheckBox automaticlyReadFieldsCheckBox;    private javax.swing.JButton cancelButton;    private javax.swing.JLabel columnsErrorMsgLabel;    private javax.swing.JScrollPane columnsErrorScrollPane;    private javax.swing.JScrollPane columnsScrollPane;    private javax.swing.JButton jButtonReadBeanAttributes;    private javax.swing.JButton jButtonReadBeanAttributes1;    private javax.swing.JLabel jLabel2;    private javax.swing.JPanel jPanel1;    private javax.swing.JPanel jPanel2;    private javax.swing.JPanel jPanel3;    private javax.swing.JPanel jPanel4;    private javax.swing.JPanel jPanel5;    private javax.swing.JPanel jPanel6;    private javax.swing.JPanel jPanel7;    private javax.swing.JPanel jPanel9;    private javax.swing.JPanel jPanelSQL;    private it.businesslogic.ireport.gui.JRSQLExpressionArea jRSQLExpressionArea1;    private javax.swing.JSplitPane jSplitPane1;    private javax.swing.JTabbedPane jTabbedPane1;    private javax.swing.JTable jTableFields;    private javax.swing.JTextField jTextFieldBeanClass;    private javax.swing.JButton okButton;    private javax.swing.JButton readFieldsButton;    // End of variables declaration//GEN-END:variables    private JReportFrame jReportFrame;            private boolean isSettingSQLExpression = false;    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?