Advertisement
Guest User

Final Eclipse Birt Engine and Chart Example

a guest
Mar 4th, 2010
1,459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 22.77 KB | None | 0 0
  1. package your.package.here;
  2.  
  3. /*
  4.  * THIS EXAMPLE IS ONLY USING THE TWO FIRST COLUMNS SELECTED FROM THE SQL-DB AND
  5.  * THE FIRST COLUMN MUST BE IN STRING-FORMAT AND THE SECOND IN NUMERIC-FORMAT (INT/FLOAT...)
  6.  * GL
  7. */
  8.  
  9. import org.eclipse.birt.chart.model.attribute.Position;
  10. import org.eclipse.birt.chart.model.type.BarSeries;
  11. import org.eclipse.birt.chart.model.type.LineSeries;
  12. import org.eclipse.birt.chart.model.type.PieSeries;
  13. import org.eclipse.birt.chart.model.type.impl.*;
  14. import org.eclipse.birt.chart.model.*;
  15. import org.eclipse.birt.chart.model.attribute.Angle3D;
  16. import org.eclipse.birt.chart.model.attribute.AxisType;
  17. import org.eclipse.birt.chart.model.attribute.ChartDimension;
  18. import org.eclipse.birt.chart.model.attribute.IntersectionType;
  19. import org.eclipse.birt.chart.model.attribute.LegendItemType;
  20. import org.eclipse.birt.chart.model.attribute.Orientation;
  21. import org.eclipse.birt.chart.model.attribute.TickStyle;
  22. import org.eclipse.birt.chart.model.attribute.impl.Angle3DImpl;
  23. import org.eclipse.birt.chart.model.attribute.impl.Rotation3DImpl;
  24. import org.eclipse.birt.chart.model.component.Axis;
  25. import org.eclipse.birt.chart.model.component.Series;
  26. import org.eclipse.birt.chart.model.component.impl.AxisImpl;
  27. import org.eclipse.birt.chart.model.component.impl.SeriesImpl;
  28. import org.eclipse.birt.chart.model.data.Query;
  29. import org.eclipse.birt.chart.model.data.SeriesDefinition;
  30. import org.eclipse.birt.chart.model.data.impl.QueryImpl;
  31. import org.eclipse.birt.chart.model.data.impl.SeriesDefinitionImpl;
  32. import org.eclipse.birt.chart.model.impl.*;
  33. import org.eclipse.birt.chart.model.layout.Legend;
  34. import java.util.*;
  35. import java.util.List;
  36. import java.awt.event.*;
  37. import javax.swing.*;
  38.  
  39. import java.awt.*;
  40. import java.util.logging.Level;
  41.  
  42. import java.io.File;
  43.  
  44.  
  45. import org.eclipse.birt.report.engine.api.*;
  46. import org.eclipse.birt.report.model.api.*;
  47. import org.eclipse.birt.report.model.api.elements.structures.CachedMetaData;
  48. import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
  49. import org.eclipse.birt.report.model.api.elements.structures.OdaResultSetColumn;
  50. import org.eclipse.birt.report.model.api.util.CompatibilityUtil;
  51. import org.eclipse.birt.core.framework.Platform;
  52. import com.ibm.icu.util.ULocale;
  53.  
  54. public class ReportWindow extends JFrame implements ActionListener {
  55.  
  56.     /**
  57.      *
  58.      */
  59.     private static final long serialVersionUID = -3957044182265192709L;
  60.  
  61.     private JTabbedPane tabbedpane = new JTabbedPane();
  62.    
  63.     private ImageIcon iconSettings = null;
  64.     private ImageIcon iconReport = null;
  65.    
  66.     private JTextField birtHome = new JTextField("C:/dev/BIRT/birt-runtime-2_5_1");
  67.     private JTextField birtEngine = new JTextField("C:/dev/BIRT/birt-runtime-2_5_1/ReportEngine");
  68.     private JTextField birtLog = new JTextField("C:/dev/BIRT/ReportWindow/output/log");
  69.     private JTextField datasourceName = new JTextField("Sample Data Source");
  70.     private JTextField datasourceDriver = new JTextField("org.eclipse.birt.report.data.oda.sampledb.Driver");
  71.     private JTextField datasourceUrl = new JTextField("jdbc:classicmodels:sampledb");
  72.     private JTextField datasourceUser = new JTextField("ClassicModels");
  73.     private JPasswordField datasourcePassword = new JPasswordField("");
  74.     private JTextField datasetSelect = new JTextField("CUSTOMERNAME, CREDITLIMIT");
  75.     private JTextField datasetFrom = new JTextField("CUSTOMERS");
  76.     private JTextField datasetWhere = new JTextField("");
  77.     private JTextField reportDesign = new JTextField("C:/dev/BIRT/ReportWindow/output/design/dynamictable_design_" + getDate() + ".rptdesign");
  78.     private JTextField reportReport = new JTextField("C:/dev/BIRT/ReportWindow/output/report/dynamictable_report_" + getDate() + ".rptdocument");
  79.     private JTextField reportHtml = new JTextField("C:/dev/BIRT/ReportWindow/output/html/test_" + getDate() + ".html");
  80.     private JTextField reportImages = new JTextField("C:/dev/BIRT/ReportWindow/output/html/images/");
  81.     private JCheckBox checkCake = new JCheckBox("Do you want the data with a tasteful pie", true);
  82.     private JCheckBox checkLine = new JCheckBox("and line", true);
  83.     private JCheckBox checkBar = new JCheckBox("'em up in the bar?", true);
  84.    
  85.     private JButton buttonExecute = new JButton("Execute");
  86.    
  87.     private String getDate() {
  88.         return new Date(System.currentTimeMillis()).toString().replace(':', '-');
  89.     }
  90.    
  91.     ReportWindow() {
  92.         super("Report Window");
  93.         setIconImage(new ImageIcon("./resources/icon.png").getImage());
  94.  
  95.         getContentPane().setLayout(new BorderLayout());
  96.         getContentPane().add(tabbedpane, BorderLayout.CENTER);
  97.        
  98.         JPanel settings = new JPanel();
  99.  
  100.         iconSettings = new ImageIcon("./resources/settings.png");
  101.         iconReport = new ImageIcon("./resources/report.png");
  102.        
  103.         JPanel birt = new JPanel(new BorderLayout());
  104.         JPanel birtLabels = new JPanel(new GridLayout(3, 1));
  105.         birtLabels.add(new JLabel("Home Directory:"));
  106.         birtLabels.add(new JLabel("Report Engine Directory:"));
  107.         birtLabels.add(new JLabel("Log Directory:"));
  108.         JPanel birtInput = new JPanel(new GridLayout(3, 1));
  109.         birtInput.add(birtHome);
  110.         birtInput.add(birtEngine);
  111.         birtInput.add(birtLog);
  112.         birt.setBorder(BorderFactory.createTitledBorder("Specify Birt"));
  113.         birt.add(birtLabels, BorderLayout.WEST);
  114.         birt.add(birtInput, BorderLayout.CENTER);
  115.        
  116.         JPanel datasource = new JPanel(new BorderLayout());
  117.         JPanel datasourceLabels = new JPanel(new GridLayout(6, 1));
  118.         datasourceLabels.add(new JLabel("Name:"));
  119.         datasourceLabels.add(new JLabel("Type:"));
  120.         datasourceLabels.add(new JLabel("Driver Class:"));
  121.         datasourceLabels.add(new JLabel("URL:"));
  122.         datasourceLabels.add(new JLabel("Username:"));
  123.         datasourceLabels.add(new JLabel("Password:"));
  124.         JPanel datasourceInput = new JPanel(new GridLayout(6, 1));
  125.         datasourceInput.add(datasourceName);
  126.         datasourceInput.add(new JComboBox(new Object[] {"JDBC"}));
  127.         datasourceInput.add(datasourceDriver);
  128.         datasourceInput.add(datasourceUrl);
  129.         datasourceInput.add(datasourceUser);
  130.         datasourceInput.add(datasourcePassword);
  131.         datasource.add(datasourceLabels, BorderLayout.WEST);
  132.         datasource.add(datasourceInput, BorderLayout.CENTER);
  133.         datasource.setBorder(BorderFactory.createTitledBorder("Specify Data Source"));
  134.        
  135.         JPanel dataset = new JPanel(new BorderLayout());
  136.         JPanel datasetLabels = new JPanel(new GridLayout(4, 1));
  137.         datasetLabels.add(new JLabel("Type:"));
  138.         datasetLabels.add(new JLabel("Select (comma-separated columns):"));
  139.         datasetLabels.add(new JLabel("From:"));
  140.         datasetLabels.add(new JLabel("Where (optional):"));
  141.         JPanel datasetInput = new JPanel(new GridLayout(4, 1));
  142.         datasetInput.add(new JComboBox(new Object[] {"JDBC"}), BorderLayout.NORTH);
  143.         datasetInput.add(datasetSelect);
  144.         datasetInput.add(datasetFrom);
  145.         datasetInput.add(datasetWhere);
  146.         dataset.add(datasetLabels, BorderLayout.WEST);
  147.         dataset.add(datasetInput, BorderLayout.CENTER);
  148.         dataset.setBorder(BorderFactory.createTitledBorder("Specify Data Set"));
  149.        
  150.         JPanel report = new JPanel(new BorderLayout());
  151.         JPanel reportLabels = new JPanel(new GridLayout(4, 1));
  152.         reportLabels.add(new JLabel("Design-file:"));
  153.         reportLabels.add(new JLabel("Report-file:"));
  154.         reportLabels.add(new JLabel("HTML-file:"));
  155.         reportLabels.add(new JLabel("Images directory:"));
  156.         JPanel reportInput = new JPanel(new GridLayout(4, 1));
  157.         reportInput.add(reportDesign);
  158.         reportInput.add(reportReport);
  159.         reportInput.add(reportHtml);
  160.         reportInput.add(reportImages);
  161.         report.add(reportLabels, BorderLayout.WEST);
  162.         report.add(reportInput, BorderLayout.CENTER);
  163.         report.setBorder(BorderFactory.createTitledBorder("Specify Report Output"));
  164.        
  165.         JPanel execute = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  166.         buttonExecute.addActionListener(this);
  167.         checkCake.setHorizontalTextPosition(JCheckBox.LEADING);
  168.         checkLine.setHorizontalTextPosition(JCheckBox.LEADING);
  169.         checkBar.setHorizontalTextPosition(JCheckBox.LEADING);
  170.         execute.add(checkCake);
  171.         execute.add(checkLine);
  172.         execute.add(checkBar);
  173.         execute.add(buttonExecute);
  174.  
  175.         SpringLayout layout = new SpringLayout();
  176.         settings.setLayout(layout);
  177.         settings.add(birt);
  178.         settings.add(datasource);
  179.         settings.add(dataset);
  180.         settings.add(report);
  181.         settings.add(execute);
  182.         layout.putConstraint(SpringLayout.WEST, birt, 0, SpringLayout.WEST, settings);
  183.         layout.putConstraint(SpringLayout.NORTH, datasource, 5, SpringLayout.SOUTH, birt);
  184.         layout.putConstraint(SpringLayout.NORTH, dataset, 5, SpringLayout.SOUTH, datasource);
  185.         layout.putConstraint(SpringLayout.NORTH, report, 5, SpringLayout.SOUTH, dataset);
  186.         layout.putConstraint(SpringLayout.NORTH, execute, 5, SpringLayout.SOUTH, report);
  187.         layout.putConstraint(SpringLayout.EAST, execute, 0, SpringLayout.EAST, report);
  188.        
  189.         tabbedpane.addTab("Setup", iconSettings, settings, "Setup Birt, data source, data set and the report here!");
  190.        
  191.         setLocation(100, 100);
  192.         setSize(800, 650);
  193.         setVisible(true);
  194.     }
  195.        
  196.     public void actionPerformed(ActionEvent ae) {
  197.         if (ae.getSource() == buttonExecute) {
  198.             makeNewTab(
  199.                     birtHome.getText(), birtEngine.getText(), birtLog.getText(),
  200.                     datasourceName.getText(), datasourceDriver.getText(),
  201.                     datasourceUrl.getText(), datasourceUser.getText(),
  202.                     datasourcePassword.getPassword(), datasetSelect.getText(), datasetFrom.getText(),
  203.                     reportDesign.getText(), reportReport.getText(), reportHtml.getText(),
  204.                     checkCake.isSelected());
  205.         }
  206.     }
  207.    
  208.  
  209.     public Chart createPie(ReportDesignHandle designHandle, ElementFactory designFactory, String[] cols) throws Exception
  210.     {
  211.         System.out.println("starting buildPieChartReport method");
  212.        
  213.         ChartWithoutAxes cwoaPie = ChartWithoutAxesImpl.create( );
  214.         cwoaPie.setDimension(ChartDimension.TWO_DIMENSIONAL_WITH_DEPTH_LITERAL );
  215.         cwoaPie.setType( "Pie Chart" );    
  216.         cwoaPie.setSubType( "Standard Pie Chart" );
  217.         cwoaPie.getBlock().getBounds().setWidth(800.0); // REQUIRED!
  218.         cwoaPie.getBlock().getBounds().setHeight(400.0); // REQUIRED!
  219.        
  220.         // Plot
  221.         cwoaPie.setSeriesThickness( 1 );
  222.  
  223.         // Legend
  224.         Legend lg = cwoaPie.getLegend( );
  225.         lg.getOutline( ).setVisible( true );
  226.  
  227.         // Title
  228.         cwoaPie.getTitle( ).getLabel( ).getCaption( ).setValue( "Pie Chart" );
  229.  
  230.         // Base Series
  231.         SeriesDefinition sd = SeriesDefinitionImpl.create( );
  232.         sd.getSeriesPalette( ).shift( 0 );
  233.  
  234.         Series seCategory = SeriesImpl.create( );
  235.         Query query = QueryImpl.create( "row[\"" + cols[0] + "\"]" ); // MUST BE STRING-FORMAT
  236.         seCategory.getDataDefinition().add(query);
  237.         sd.getSeries( ).add( seCategory );
  238.        
  239.         cwoaPie.getSeriesDefinitions( ).add( sd );
  240.  
  241.         // Orthogonal Series
  242.         PieSeries sePie = (PieSeries) PieSeriesImpl.create( );
  243.         Query query2 = QueryImpl.create( "row[\"" + cols[1] + "\"]" ); // MUST BE NUMERIC FORMAT
  244.         sePie.getDataDefinition().add(query2);
  245.        
  246.         sePie.setSeriesIdentifier( cols[0] ); // NAME
  247.         sePie.setExplosion( 5 );
  248.        
  249.         SeriesDefinition sdCity = SeriesDefinitionImpl.create( );
  250.         sd.getSeriesDefinitions( ).add( sdCity );
  251.         sdCity.getSeries( ).add( sePie );
  252.        
  253.         return cwoaPie;
  254.     }
  255.    
  256.     private Chart createBar(ReportDesignHandle design, ElementFactory factory, String[] cols) {
  257.         ChartWithAxes bar = ChartWithAxesImpl.create();
  258.         bar.setDimension(ChartDimension.THREE_DIMENSIONAL_LITERAL);
  259.         bar.setType("Bar Chart");
  260.         bar.setSubType("Side-by-side");
  261.         bar.getBlock().getBounds().setWidth(600.0); // REQUIRED
  262.         bar.getBlock().getBounds().setHeight(600.0); // REQUIRED
  263.        
  264.         bar.getBlock().getOutline().setVisible(true);
  265.        
  266.         bar.getTitle().getLabel().getCaption().setValue("Baren");
  267.        
  268.         bar.getLegend().setItemType(LegendItemType.CATEGORIES_LITERAL);
  269.        
  270.         // X
  271.         Axis x = bar.getPrimaryBaseAxes()[0];
  272.         x.setType(AxisType.TEXT_LITERAL);
  273.         x.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
  274.         x.getOrigin().setType(IntersectionType.MIN_LITERAL);
  275.        
  276.         // Y
  277.         Axis y = bar.getPrimaryOrthogonalAxis(x);
  278.         y.setType(AxisType.LINEAR_LITERAL);
  279.         y.getMajorGrid().setTickStyle(TickStyle.LEFT_LITERAL);
  280.         y.getLabel().getCaption().getFont().setRotation(90.0);
  281.        
  282.         // Z
  283.         Axis z = AxisImpl.create(Axis.ANCILLARY_BASE);
  284.         z.setType(AxisType.TEXT_LITERAL);
  285.         z.setLabelPosition(Position.BELOW_LITERAL);
  286.         z.setTitlePosition(Position.BELOW_LITERAL);
  287.         z.getMajorGrid().setTickStyle(TickStyle.BELOW_LITERAL);
  288.         z.setOrientation(Orientation.HORIZONTAL_LITERAL);
  289.         x.getAncillaryAxes().add(z);
  290.        
  291.         // X series
  292.         Series xCategory = SeriesImpl.create();
  293.         xCategory.getDataDefinition().add(QueryImpl.create("row[\"" + cols[0] + "\"]")); // MUST BE STRING-FORMAT
  294.        
  295.         SeriesDefinition xDefinition = SeriesDefinitionImpl.create();
  296.         xDefinition.getSeriesPalette().shift(0);
  297.         xDefinition.getSeries().add(xCategory);
  298.         x.getSeriesDefinitions().add(xDefinition);
  299.        
  300.         // Y series 1
  301.         BarSeries yCategory = (BarSeries) BarSeriesImpl.create();
  302.         yCategory.getDataDefinition().add(QueryImpl.create("row[\"" + cols[1] + "\"]")); // MUST BE NUMERIC
  303.        
  304.         yCategory.getLabel().setVisible(true);
  305.         yCategory.setLabelPosition(Position.OUTSIDE_LITERAL);
  306.        
  307.         // Y series 2
  308.         BarSeries yCategory2 = (BarSeries) BarSeriesImpl.create();
  309.         yCategory2.getDataDefinition().add(QueryImpl.create("row[\"" + cols[1] + "\"]")); // MUST BE NUMERIC
  310.        
  311.         yCategory2.getLabel().setVisible(true);
  312.         yCategory2.setLabelPosition(Position.OUTSIDE_LITERAL);
  313.        
  314.         SeriesDefinition yDefinition = SeriesDefinitionImpl.create();
  315.         yDefinition.getSeriesPalette().shift(-1);
  316.         yDefinition.getSeries().add(yCategory);
  317.         yDefinition.getSeries().add(yCategory2);
  318.         y.getSeriesDefinitions().add(yDefinition);
  319.        
  320.         // Z series
  321.         SeriesDefinition zDefinition = SeriesDefinitionImpl.create();
  322.         z.getSeriesDefinitions().add(zDefinition);
  323.        
  324.         // Rotate
  325.         bar.setRotation(Rotation3DImpl.create(new Angle3D[] {Angle3DImpl.create(-10, 25, 0)}));
  326.        
  327.         return bar;
  328.     }
  329.    
  330.     private Chart createLine(ReportDesignHandle design, ElementFactory factory, String[] cols) {
  331.         ChartWithAxes bar = ChartWithAxesImpl.create();
  332.         bar.setType("Line Chart");
  333.         bar.setSubType("Overlay");
  334.         bar.getBlock().getBounds().setWidth(800.0); // REQUIRED
  335.         bar.getBlock().getBounds().setHeight(400.0); // REQUIRED
  336.  
  337.         bar.getTitle().getLabel().getCaption().setValue("Baren"); // funny name :P
  338.         bar.getTitle().setVisible(true);
  339.        
  340.         bar.getLegend().setVisible(true);
  341.        
  342.         // X-axis
  343.         Axis x = bar.getPrimaryBaseAxes()[0];
  344.         x.setType(AxisType.TEXT_LITERAL);
  345.        
  346.         // Y-axis
  347.         Axis y = bar.getPrimaryOrthogonalAxis(x);
  348.         y.setType(AxisType.LINEAR_LITERAL);
  349.        
  350.         // X series
  351.         SeriesDefinition xDefinition = SeriesDefinitionImpl.create();
  352.         xDefinition.getSeriesPalette().shift(0);
  353.         Series xCategory = SeriesImpl.create();
  354.         xCategory.getDataDefinition().add(QueryImpl.create("row[\"" + cols[0] + "\"]")); // MUST BE STRING-FORMAT
  355.  
  356.         xDefinition.getSeries().add(xCategory);
  357.         x.getSeriesDefinitions().add(xDefinition);
  358.        
  359.         // Y series
  360.         LineSeries yCategory = (LineSeries) LineSeriesImpl.create();
  361.         yCategory.getDataDefinition().add(QueryImpl.create("row[\"" + cols[1] + "\"]")); // MUST BE NUMERIC
  362.         yCategory.getLabel().setVisible(true);
  363.        
  364.         SeriesDefinition yDefinition = SeriesDefinitionImpl.create();
  365.         yDefinition.getGrouping().setEnabled(false);
  366.         yDefinition.getSeries().add(yCategory);
  367.         y.getSeriesDefinitions().add(yDefinition);
  368.        
  369.         return bar;
  370.     }
  371.    
  372.     private void makeNewTab(String birtHome, String birtEngine, String birtLog,
  373.             String name, String driver, String url, String user, char[] password,
  374.             String select, String from,
  375.             String designFilename, String reportFilename, String htmlFilename,
  376.             boolean cake) {
  377.        
  378.         JScrollPane scrollis = new JScrollPane();
  379.        
  380.         System.out.println("Starting ...");
  381.         try {
  382.            
  383.             // Startup
  384.             EngineConfig config = new EngineConfig();
  385.             config.setEngineHome(birtEngine);
  386.             config.setLogConfig(birtLog, Level.WARNING);
  387.            
  388.             Platform.startup(config);
  389.             IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
  390.             IReportEngine engine = factory.createReportEngine(config);
  391.            
  392.             System.out.println("Engine is set up ...");
  393.  
  394.             // Create new design
  395.             DesignConfig config2 = new DesignConfig();
  396.             config2.setProperty("BIRT_HOME", birtHome);
  397.             IDesignEngine engine2 = null;
  398.             try {
  399.                 Platform.startup(config2);
  400.                 IDesignEngineFactory factory2 = (IDesignEngineFactory) Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
  401.                 engine2 = factory2.createDesignEngine(config2);
  402.             } catch (Exception e) {
  403.                 e.printStackTrace();
  404.             }
  405.             SessionHandle session = engine2.newSessionHandle(ULocale.ENGLISH);
  406.             ReportDesignHandle design = session.createDesign();
  407.             ElementFactory factory3 = design.getElementFactory();
  408.            
  409.             // Required
  410.             DesignElementHandle element = factory3.newSimpleMasterPage("Page Master");
  411.             design.getMasterPages().add(element);
  412.  
  413.             // Data source
  414.             OdaDataSourceHandle dsHandle = factory3.newOdaDataSource(
  415.                     name, "org.eclipse.birt.report.data.oda.jdbc" );
  416.             dsHandle.setProperty( "odaDriverClass", driver );
  417.             dsHandle.setProperty( "odaURL", url );
  418.             dsHandle.setProperty( "odaUser", user );
  419.             dsHandle.setProperty( "odaPassword", String.valueOf(password) );
  420.  
  421.             design.getDataSources( ).add( dsHandle );  
  422.            
  423.             // Data set
  424.             OdaDataSetHandle dsetHandle = factory3.newOdaDataSet( "My Data Set",
  425.                 "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
  426.             dsetHandle.setDataSource( name );
  427.            
  428.             String[] cols = select.split(",");
  429.             for (int i=0; i<cols.length; i++) {
  430.                 cols[i] = cols[i].trim();
  431.             }
  432.             ExtendedItemHandle chartItemPie = factory3.newExtendedItem(null, "Chart"); // "Chart" with capital C!!
  433.             chartItemPie.setProperty(ExtendedItemHandle.DATA_SET_PROP, "My Data Set");
  434.             chartItemPie.setProperty("outputFormat", "PNG");
  435.            
  436.             ExtendedItemHandle chartItemBar = factory3.newExtendedItem(null, "Chart");
  437.             chartItemBar.setProperty(ExtendedItemHandle.DATA_SET_PROP, "My Data Set");
  438.             chartItemBar.setProperty("outputFormat", "PNG");
  439.            
  440.             ExtendedItemHandle chartItemLine = factory3.newExtendedItem(null, "Chart");
  441.             chartItemLine.setProperty(ExtendedItemHandle.DATA_SET_PROP, "My Data Set");
  442.             chartItemLine.setProperty("outputFormat", "PNG");
  443.  
  444.             // Cached things
  445.             List<OdaResultSetColumn> columns = new LinkedList<OdaResultSetColumn>();
  446.             // Cached
  447.             dsetHandle.setCachedMetaData(new CachedMetaData());
  448.  
  449.            
  450.             StringBuilder query = new StringBuilder();
  451.             query.append("SELECT ");
  452.             for (int i=0; i<cols.length; i++) {
  453.                 query.append(cols[i]);
  454.                
  455.                 // Something
  456.                 ComputedColumn cc = StructureFactory.createComputedColumn();
  457.                 cc.setName(cols[i]);
  458.                 if (i == 0)
  459.                     cc.setDataType("string");
  460.                 else
  461.                     cc.setDataType("decimal");
  462.                 cc.setExpression("dataSetRow[\"" + cols[i] + "\"]");
  463.                
  464.                 chartItemPie.addColumnBinding(cc, true);
  465.                 chartItemBar.addColumnBinding(cc, true);
  466.                 chartItemLine.addColumnBinding(cc, true);
  467.                 // Cached things
  468.                 OdaResultSetColumn column = StructureFactory.createOdaResultSetColumn();
  469.                 column.setColumnName(cols[i]);
  470.                 column.setPosition(new Integer(i + 1));
  471.                 columns.add(column);
  472.                 /*
  473.                 */
  474.                 if (i < cols.length - 1)
  475.                     query.append(", ");
  476.             }
  477.             query.append(" FROM ");
  478.             query.append(from);
  479.             String where = datasetWhere.getText();
  480.             if (where.length() > 0) {
  481.                 query.append(" ");
  482.                 query.append(where);
  483.             }
  484.             dsetHandle.setQueryText( query.toString() );
  485.             System.out.println("query:" + query);
  486.  
  487.             CompatibilityUtil.addResultSetColumn(dsetHandle, columns);
  488.            
  489.             design.getDataSets( ).add( dsetHandle );
  490.  
  491.             if (checkLine.isSelected()) {
  492.                 chartItemLine.getReportItem().setProperty("chart.instance", createLine(design,factory3, cols));
  493.                 design.getBody().add(chartItemLine);
  494.             }
  495.            
  496.             if (cake) {
  497.                 chartItemPie.getReportItem().setProperty("chart.instance", createPie(design, factory3, cols));
  498.                 design.getBody().add(chartItemPie);
  499.             }
  500.            
  501.             if (checkBar.isSelected()) {
  502.                 chartItemBar.getReportItem().setProperty("chart.instance", createBar(design, factory3, cols));
  503.                 design.getBody().add(chartItemBar);
  504.             }
  505.            
  506.             // Build table
  507.             TableHandle table = factory3.newTableItem( "table", cols.length );
  508.             table.setWidth( "100%" );
  509.             table.setDataSet( design.findDataSet( "My Data Set" ) );
  510.  
  511.             PropertyHandle computedSet = table.getColumnBindings( );
  512.             ComputedColumn  cs1 = null;
  513.  
  514.             for( int i=0; i < cols.length; i++){
  515.                 cs1 = StructureFactory.createComputedColumn();
  516.                 cs1.setName(cols[i]);
  517.                 cs1.setExpression("dataSetRow[\"" + cols[i] + "\"]");
  518.                 computedSet.addItem(cs1);
  519.             }
  520.            
  521.             // table header
  522.             RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
  523.            
  524.             for( int i=0; i < cols.length; i++){
  525.                 LabelHandle label1 = factory3.newLabel( cols[i] ); 
  526.                 label1.setText(cols[i]);
  527.                 CellHandle cell = (CellHandle) tableheader.getCells( ).get( i );
  528.                 cell.getContent( ).add( label1 );
  529.             }                          
  530.    
  531.             // table detail
  532.             RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
  533.             for( int i=0; i < cols.length; i++){
  534.                 CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i );
  535.                 DataItemHandle data = factory3.newDataItem( "data_"+cols[i] );
  536.                 data.setResultSetColumn( cols[i]);
  537.                 cell.getContent( ).add( data );
  538.             }
  539.  
  540.             design.getBody( ).add( table );
  541.  
  542.             // Save the design and close it.
  543.             design.saveAs( designFilename ); //$NON-NLS-1$
  544.             design.close( );
  545.             System.out.println("Finished");
  546.  
  547.  
  548.             // Html
  549.             IRunAndRenderTask supertask = engine.createRunAndRenderTask(engine.openReportDesign(designFilename));
  550.             supertask.setMaxRowsPerQuery(10);
  551.             HTMLRenderOption htmlOptions = new HTMLRenderOption();
  552.             htmlOptions.setImageDirectory(reportImages.getText());
  553.             htmlOptions.setHtmlPagination(false);
  554.             htmlOptions.setHtmlRtLFlag(false);
  555.             htmlOptions.setEmbeddable(false);
  556.             htmlOptions.setSupportedImageFormats("JPG;PNG;BMP;SVG");
  557.             htmlOptions.setOutputFormat("html");
  558.             htmlOptions.setOutputFileName(htmlFilename);
  559.             supertask.setRenderOption(htmlOptions);
  560.             supertask.run();
  561.             supertask.close();
  562.  
  563.             // Show html-page
  564.             File file = new File(htmlFilename);
  565.             JEditorPane editor = new JEditorPane();
  566.             System.out.println("Opening file:" + file.toURI().toURL().toString());
  567.             editor.setPage(file.toURI().toURL());
  568.             scrollis.getViewport().add(editor);
  569.             tabbedpane.addTab(htmlFilename, iconReport, scrollis, "Check out the new file!!");
  570.            
  571.            
  572.             // Close
  573.             engine.destroy();
  574.             Platform.shutdown();
  575.         } catch (Exception e) {
  576.             e.printStackTrace();
  577.             JOptionPane.showMessageDialog(this, e.getMessage(), "Error!", JOptionPane.ERROR_MESSAGE);
  578.         }
  579.         System.out.println("Done!");
  580.     }
  581.    
  582.     public void processWindowEvent(WindowEvent we) {
  583.         if (we.getID() == we.WINDOW_CLOSING) {
  584.             close();
  585.         }
  586.     }
  587.    
  588.     void close() {
  589.         System.exit(0);
  590.     }
  591.  
  592.     public static void main(String[] args) {
  593.         new ReportWindow();
  594.     }
  595.  
  596. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement