001/* 002 * Copyright (c) 2003-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: barseghian $' 006 * '$Date: 2011-02-05 02:51:00 +0000 (Sat, 05 Feb 2011) $' 007 * '$Revision: 27030 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.reporting.rio.test; 031 032import java.awt.event.WindowAdapter; 033import java.awt.event.WindowEvent; 034import java.util.ArrayList; 035import java.util.List; 036 037import javax.swing.JFrame; 038import javax.swing.JScrollPane; 039 040import org.apache.commons.logging.Log; 041import org.apache.commons.logging.LogFactory; 042import org.kepler.reporting.rio.DynamicReportItem; 043import org.kepler.reporting.rio.Item; 044import org.kepler.reporting.rio.ItemLabel; 045import org.kepler.reporting.rio.ItemValue; 046import org.kepler.reporting.rio.RIORenderer; 047import org.kepler.reporting.rio.ReportInstance; 048import org.kepler.reporting.rio.StaticReportItem; 049import org.kepler.reporting.rio.StaticReportItemHR; 050import org.kepler.reporting.rio.StaticReportItemImage; 051import org.kepler.reporting.rio.StaticReportItemText; 052import org.kepler.reporting.roml.ReportLayout; 053 054import ptolemy.data.ArrayToken; 055import ptolemy.data.RecordToken; 056import ptolemy.data.StringToken; 057import ptolemy.kernel.util.IllegalActionException; 058import ptolemy.kernel.util.NamedObj; 059 060public class ReportTest { 061 062 private static Log log = LogFactory.getLog(ReportTest.class); 063 064 public static ReportInstance makeReport() { 065 ReportInstance report = new ReportInstance(); 066 report.setTitle("My report"); 067 068 //first item 069 ItemLabel label = new ItemLabel("My label:"); 070 label.setPosition(ItemLabel.NORTH); 071 072 Item item = null; 073 //this is a dynamic item, let's say 074 try { 075 item = new DynamicReportItem(new NamedObj("testing").getFullName()); 076 item.setType("dynamic"); 077 //we would then use provenance to look up the values 078 } catch (IllegalActionException e) { 079 //oops! 080 e.printStackTrace(); 081 } 082 List values = new ArrayList(); 083 ItemValue value = new ItemValue(); 084 item.addLabel(label); 085 try { 086 value.setValue(new RecordToken("{Column_A={1,2,3}, Column_B={4,5,6}, Column_C={7,8,9}}")); 087 } catch (IllegalActionException e) { 088 // TODO Auto-generated catch block 089 e.printStackTrace(); 090 } 091 ItemValue value2 = null; 092 try { 093 value2 = new ItemValue(new ArrayToken("{1,2,3,4,5,6,7}")); 094 } catch (IllegalActionException e) { 095 // TODO Auto-generated catch block 096 e.printStackTrace(); 097 } 098 values.add(value); 099 values.add(value2); 100 values.add(new ItemValue(new StringToken("What are you doing?"))); 101 values.add(new ItemValue(new StringToken("Wow, really?"))); 102 item.setValues(values); 103 104 //another item (image) 105 Item imageItem = new StaticReportItem(); 106 imageItem.setType("image"); 107 108 //imageItem.setType(StaticReportItem.IMAGE); 109 List imageValues = new ArrayList(); 110 ItemValue imageValue = new ItemValue(); 111 imageValue.setValue(new StringToken(ReportTest.class.getResource("R_linear_regression1.png").getFile())); 112 imageValues.add(imageValue); 113 imageItem.addLabel(new ItemLabel("Linear regression")); 114 imageItem.setValues(imageValues); 115 116 //another item (pdf) 117 Item pdfItem = new StaticReportItem(); 118 pdfItem.setType("pdf"); 119 //pdfItem.setType(StaticReportItem.PDF); 120 List pdfValues = new ArrayList(); 121 ItemValue pdfValue = new ItemValue(); 122 pdfValue.setValue(new StringToken(ReportTest.class.getResource("RExpression1.pdf").getFile())); 123 pdfValues.add(pdfValue); 124 pdfItem.addLabel(new ItemLabel("Pairs (PDF)")); 125 pdfItem.setValues(pdfValues); 126 127 //HR item 128 Item hrItem = new StaticReportItem(); 129 hrItem.setType("hr"); 130 131 //text item 132 Item textItem = new StaticReportItem(); 133 textItem.setType("text"); 134 textItem.addLabel(new ItemLabel("Static text:")); 135 textItem.getValues().add(new ItemValue(new StringToken("This is user-entered text"))); 136 137 138 139 report.getSections().get(0).getColumns().get(0).getItems().add(item); 140 report.getSections().get(0).getColumns().get(0).getItems().add(imageItem); 141 report.getSections().get(0).getColumns().get(0).getItems().add(pdfItem); 142 report.getSections().get(0).getColumns().get(0).getItems().add(hrItem); 143 report.getSections().get(0).getColumns().get(0).getItems().add(textItem); 144 145 return report; 146 } 147 148 public static ReportLayout makeReportLayout() { 149 ReportLayout report = new ReportLayout(); 150 report.setTitle("My new report"); 151 report.setHeaderGraphic("../reporting/lib/xslt/sanparks.jpg"); 152 153 //first item 154 ItemLabel northlabel = new ItemLabel("My North label"); 155 northlabel.setPosition(ItemLabel.NORTH); 156 157 ItemLabel southlabel = new ItemLabel("My South label"); 158 southlabel.setPosition(ItemLabel.SOUTH); 159 160 ItemLabel westlabel = new ItemLabel("My West label"); 161 westlabel.setPosition(ItemLabel.WEST); 162 163 ItemLabel eastlabel = new ItemLabel("My East label"); 164 eastlabel.setPosition(ItemLabel.EAST); 165 166 Item item = null; 167 //this is a dynamic item, let's say 168 try { 169 item = new DynamicReportItem(new NamedObj("testing").getFullName()); 170 //we would then use provenance to look up the values 171 } catch (IllegalActionException e) { 172 //oops! 173 e.printStackTrace(); 174 } 175 176 item.addLabel(northlabel); 177 item.addLabel(southlabel); 178 item.addLabel(westlabel); 179 item.addLabel(eastlabel); 180 181 //another item (image) 182 Item imageItem = new StaticReportItemImage(); 183 imageItem.addLabel(new ItemLabel("Linear regression")); 184 185 186 //another item (pdf) 187 Item pdfItem = new StaticReportItem(); 188 pdfItem.addLabel(new ItemLabel("Pairs (PDF)")); 189 pdfItem.setType("pdf"); 190 191 //HR item 192 Item hrItem = new StaticReportItemHR(); 193 194 //text item 195 Item textItem = new StaticReportItemText(); 196 textItem.addLabel(new ItemLabel("Static text:")); 197 textItem.getValues().add(new ItemValue(new StringToken("This is user-entered text"))); 198 199 report.getSections().get(0).getColumns().get(0).getItems().add(item); 200 report.getSections().get(0).getColumns().get(0).getItems().add(imageItem); 201 // report.getSections().get(0).getColumns().get(0).getItems().add(pdfItem); 202 203 report.getSections().get(0).getColumns().get(0).getItems().add(hrItem); 204 report.getSections().get(0).getColumns().get(0).getItems().add(textItem); 205 206 return report; 207 } 208 209 public static void main(String[] args) { 210 ReportInstance report = makeReport(); 211 log.debug(report.toString()); 212 213 //show it 214 JFrame frame = new JFrame("Report"); 215 frame.addWindowListener(new WindowAdapter(){ 216 public void windowClosing(WindowEvent e) { 217 System.exit(0); 218 } 219 }); 220 221 JScrollPane scrollPane = new JScrollPane(RIORenderer.jpanelRIO(report)); 222 frame.add(scrollPane); 223 frame.pack(); 224 frame.setVisible(true); 225 226 } 227}