1 28 29 34 package net.sf.jasperreports.view; 35 36 import java.awt.BorderLayout ; 37 import java.awt.Toolkit ; 38 import java.io.InputStream ; 39 import java.util.Locale ; 40 41 import net.sf.jasperreports.engine.JRException; 42 import net.sf.jasperreports.engine.JasperPrint; 43 44 45 49 public class JasperViewer extends javax.swing.JFrame 50 { 51 52 53 56 private JRViewer viewer = null; 57 58 61 private boolean isExitOnClose = true; 62 63 64 65 public JasperViewer( 66 String sourceFile, 67 boolean isXMLFile 68 ) throws JRException 69 { 70 this(sourceFile, isXMLFile, true); 71 } 72 73 74 75 public JasperViewer( 76 InputStream is, 77 boolean isXMLFile 78 ) throws JRException 79 { 80 this(is, isXMLFile, true); 81 } 82 83 84 85 public JasperViewer( 86 JasperPrint jasperPrint 87 ) 88 { 89 this(jasperPrint, true); 90 } 91 92 93 94 public JasperViewer( 95 String sourceFile, 96 boolean isXMLFile, 97 boolean isExitOnClose 98 ) throws JRException 99 { 100 this(sourceFile, isXMLFile, isExitOnClose, null); 101 } 102 103 104 105 public JasperViewer( 106 InputStream is, 107 boolean isXMLFile, 108 boolean isExitOnClose 109 ) throws JRException 110 { 111 this(is, isXMLFile, isExitOnClose, null); 112 } 113 114 115 116 public JasperViewer( 117 JasperPrint jasperPrint, 118 boolean isExitOnClose 119 ) 120 { 121 this(jasperPrint, isExitOnClose, null); 122 } 123 124 125 126 public JasperViewer( 127 String sourceFile, 128 boolean isXMLFile, 129 boolean isExitOnClose, 130 Locale locale 131 ) throws JRException 132 { 133 if (locale != null) 134 setLocale(locale); 135 136 this.isExitOnClose = isExitOnClose; 137 138 initComponents(); 139 140 this.viewer = new JRViewer(sourceFile, isXMLFile, locale); 141 this.pnlMain.add(this.viewer, BorderLayout.CENTER); 142 } 143 144 145 146 public JasperViewer( 147 InputStream is, 148 boolean isXMLFile, 149 boolean isExitOnClose, 150 Locale locale 151 ) throws JRException 152 { 153 if (locale != null) 154 setLocale(locale); 155 156 this.isExitOnClose = isExitOnClose; 157 158 initComponents(); 159 160 this.viewer = new JRViewer(is, isXMLFile, locale); 161 this.pnlMain.add(this.viewer, BorderLayout.CENTER); 162 } 163 164 165 166 public JasperViewer( 167 JasperPrint jasperPrint, 168 boolean isExitOnClose, 169 Locale locale 170 ) 171 { 172 if (locale != null) 173 setLocale(locale); 174 175 this.isExitOnClose = isExitOnClose; 176 177 initComponents(); 178 179 this.viewer = new JRViewer(jasperPrint, locale); 180 this.pnlMain.add(this.viewer, BorderLayout.CENTER); 181 } 182 183 184 189 private void initComponents() { pnlMain = new javax.swing.JPanel (); 191 192 setTitle("JasperViewer"); 193 setIconImage(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/jricon.GIF")).getImage()); 194 addWindowListener(new java.awt.event.WindowAdapter () { 195 public void windowClosing(java.awt.event.WindowEvent evt) { 196 exitForm(); 197 } 198 }); 199 200 pnlMain.setLayout(new java.awt.BorderLayout ()); 201 202 getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER); 203 204 pack(); 205 206 Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit(); 207 java.awt.Dimension screenSize = toolkit.getScreenSize(); 208 int screenResolution = toolkit.getScreenResolution(); 209 float zoom = ((float) screenResolution) / JRViewer.REPORT_RESOLUTION; 210 211 int height = (int) (550 * zoom); 212 if (height > screenSize.getHeight()) 213 { 214 height = (int) screenSize.getHeight(); 215 } 216 int width = (int) (750 * zoom); 217 if (width > screenSize.getWidth()) 218 { 219 width = (int) screenSize.getWidth(); 220 } 221 222 java.awt.Dimension dimension = new java.awt.Dimension (width, height); 223 setSize(dimension); 224 setLocation((screenSize.width-width)/2,(screenSize.height-height)/2); 225 } 227 228 void exitForm() { 230 if (this.isExitOnClose) 231 { 232 System.exit(0); 233 } 234 else 235 { 236 this.setVisible(false); 237 this.viewer.clear(); 238 this.viewer = null; 239 this.getContentPane().removeAll(); 240 this.dispose(); 241 } 242 243 } 245 246 249 public void setZoomRatio(float zoomRatio) 250 { 251 viewer.setZoomRatio(zoomRatio); 252 } 253 254 255 258 public void setFitWidthZoomRatio() 259 { 260 viewer.setFitWidthZoomRatio(); 261 } 262 263 264 267 public void setFitPageZoomRatio() 268 { 269 viewer.setFitPageZoomRatio(); 270 } 271 272 273 276 public static void main(String args[]) 277 { 278 String fileName = null; 279 boolean isXMLFile = false; 280 281 if(args.length == 0) 282 { 283 usage(); 284 return; 285 } 286 287 int k = 0; 288 while ( args.length > k ) 289 { 290 if ( args[k].startsWith("-F") ) 291 fileName = args[k].substring(2); 292 if ( args[k].startsWith("-XML") ) 293 isXMLFile = true; 294 295 k++; 296 } 297 298 try 299 { 300 viewReport(fileName, isXMLFile); 301 } 302 catch (JRException e) 303 { 304 e.printStackTrace(); 305 System.exit(1); 306 } 307 } 308 309 310 313 private static void usage() 314 { 315 System.out.println( "JasperViewer usage:" ); 316 System.out.println( "\tjava JasperViewer -XML -Ffile" ); 317 } 318 319 320 323 public static void viewReport( 324 String sourceFile, 325 boolean isXMLFile 326 ) throws JRException 327 { 328 viewReport(sourceFile, isXMLFile, true, null); 329 } 330 331 334 public static void viewReport( 335 InputStream is, 336 boolean isXMLFile 337 ) throws JRException 338 { 339 viewReport(is, isXMLFile, true, null); 340 } 341 342 345 public static void viewReport( 346 JasperPrint jasperPrint 347 ) 348 { 349 viewReport(jasperPrint, true, null); 350 } 351 352 355 public static void viewReport( 356 String sourceFile, 357 boolean isXMLFile, 358 boolean isExitOnClose 359 ) throws JRException 360 { 361 viewReport(sourceFile, isXMLFile, isExitOnClose, null); 362 } 363 364 367 public static void viewReport( 368 InputStream is, 369 boolean isXMLFile, 370 boolean isExitOnClose 371 ) throws JRException 372 { 373 viewReport(is, isXMLFile, isExitOnClose, null); 374 } 375 376 379 public static void viewReport( 380 JasperPrint jasperPrint, 381 boolean isExitOnClose 382 ) 383 { 384 viewReport(jasperPrint, isExitOnClose, null); 385 } 386 387 390 public static void viewReport( 391 String sourceFile, 392 boolean isXMLFile, 393 boolean isExitOnClose, 394 Locale locale 395 ) throws JRException 396 { 397 JasperViewer jasperViewer = 398 new JasperViewer( 399 sourceFile, 400 isXMLFile, 401 isExitOnClose, 402 locale 403 ); 404 jasperViewer.setVisible(true); 405 } 406 407 410 public static void viewReport( 411 InputStream is, 412 boolean isXMLFile, 413 boolean isExitOnClose, 414 Locale locale 415 ) throws JRException 416 { 417 JasperViewer jasperViewer = 418 new JasperViewer( 419 is, 420 isXMLFile, 421 isExitOnClose, 422 locale 423 ); 424 jasperViewer.setVisible(true); 425 } 426 427 430 public static void viewReport( 431 JasperPrint jasperPrint, 432 boolean isExitOnClose, 433 Locale locale 434 ) 435 { 436 JasperViewer jasperViewer = 437 new JasperViewer( 438 jasperPrint, 439 isExitOnClose, 440 locale 441 ); 442 jasperViewer.setVisible(true); 443 } 444 445 446 private javax.swing.JPanel pnlMain; 448 450 } 451 | Popular Tags |