1 28 29 35 package net.sf.jasperreports.view; 36 37 import java.awt.Component ; 38 import java.awt.Container ; 39 import java.awt.Cursor ; 40 import java.awt.Dimension ; 41 import java.awt.Graphics ; 42 import java.awt.Graphics2D ; 43 import java.awt.Image ; 44 import java.awt.Point ; 45 import java.awt.Rectangle ; 46 import java.awt.Toolkit ; 47 import java.awt.event.MouseEvent ; 48 import java.awt.event.MouseListener ; 49 import java.awt.event.MouseMotionListener ; 50 import java.io.File ; 51 import java.io.InputStream ; 52 import java.lang.reflect.Method ; 53 import java.text.DecimalFormat ; 54 import java.text.MessageFormat ; 55 import java.text.ParseException ; 56 import java.util.ArrayList ; 57 import java.util.Arrays ; 58 import java.util.Collection ; 59 import java.util.HashMap ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 import java.util.Locale ; 63 import java.util.Map ; 64 import java.util.ResourceBundle ; 65 66 import javax.swing.DefaultComboBoxModel ; 67 import javax.swing.ImageIcon ; 68 import javax.swing.JFileChooser ; 69 import javax.swing.JLabel ; 70 import javax.swing.JOptionPane ; 71 import javax.swing.JPanel ; 72 import javax.swing.JViewport ; 73 import javax.swing.filechooser.FileFilter ; 74 75 import net.sf.jasperreports.engine.JRException; 76 import net.sf.jasperreports.engine.JRExporterParameter; 77 import net.sf.jasperreports.engine.JRHyperlink; 78 import net.sf.jasperreports.engine.JRImageMapRenderer; 79 import net.sf.jasperreports.engine.JRPrintAnchorIndex; 80 import net.sf.jasperreports.engine.JRPrintElement; 81 import net.sf.jasperreports.engine.JRPrintHyperlink; 82 import net.sf.jasperreports.engine.JRPrintImage; 83 import net.sf.jasperreports.engine.JRPrintImageAreaHyperlink; 84 import net.sf.jasperreports.engine.JRPrintPage; 85 import net.sf.jasperreports.engine.JRRenderable; 86 import net.sf.jasperreports.engine.JRRuntimeException; 87 import net.sf.jasperreports.engine.JasperPrint; 88 import net.sf.jasperreports.engine.JasperPrintManager; 89 import net.sf.jasperreports.engine.export.JRGraphics2DExporter; 90 import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter; 91 import net.sf.jasperreports.engine.print.JRPrinterAWT; 92 import net.sf.jasperreports.engine.util.JRClassLoader; 93 import net.sf.jasperreports.engine.util.JRLoader; 94 import net.sf.jasperreports.engine.util.JRProperties; 95 import net.sf.jasperreports.engine.xml.JRPrintXmlLoader; 96 import net.sf.jasperreports.view.save.JRPrintSaveContributor; 97 98 99 103 public class JRViewer extends javax.swing.JPanel implements JRHyperlinkListener 104 { 105 106 117 public static final String VIEWER_RENDER_BUFFER_MAX_SIZE = JRProperties.PROPERTY_PREFIX + "viewer.render.buffer.max.size"; 118 119 122 private static final int TYPE_FILE_NAME = 1; 123 private static final int TYPE_INPUT_STREAM = 2; 124 private static final int TYPE_JASPER_PRINT = 3; 125 126 129 public static final int REPORT_RESOLUTION = 72; 130 131 protected float MIN_ZOOM = 0.5f; 132 protected float MAX_ZOOM = 10f; 133 protected int zooms[] = {50, 75, 100, 125, 150, 175, 200, 250, 400, 800}; 134 protected int defaultZoomIndex = 2; 135 136 private int type = TYPE_FILE_NAME; 137 private boolean isXML = false; 138 private String reportFileName = null; 139 JasperPrint jasperPrint = null; 140 private int pageIndex = 0; 141 private float zoom = 0f; 142 143 private JRGraphics2DExporter exporter = null; 144 145 148 private int screenResolution = REPORT_RESOLUTION; 149 150 153 protected float realZoom = 0f; 154 155 private DecimalFormat zoomDecimalFormat = new DecimalFormat ("#.##"); 156 private ResourceBundle resourceBundle = null; 157 158 private int downX = 0; 159 private int downY = 0; 160 161 private java.util.List hyperlinkListeners = new ArrayList (); 162 private Map linksMap = new HashMap (); 163 private MouseListener mouseListener = 164 new java.awt.event.MouseAdapter () 165 { 166 public void mouseClicked(java.awt.event.MouseEvent evt) 167 { 168 hyperlinkClicked(evt); 169 } 170 }; 171 172 private java.util.List saveContributors = new ArrayList (); 173 174 175 176 public JRViewer(String fileName, boolean isXML) throws JRException 177 { 178 this(fileName, isXML, null); 179 } 180 181 182 183 public JRViewer(InputStream is, boolean isXML) throws JRException 184 { 185 this(is, isXML, null); 186 } 187 188 189 190 public JRViewer(JasperPrint jrPrint) 191 { 192 this(jrPrint, null); 193 } 194 195 196 197 public JRViewer(String fileName, boolean isXML, Locale locale) throws JRException 198 { 199 this(fileName, isXML, locale, null); 200 } 201 202 203 204 public JRViewer(InputStream is, boolean isXML, Locale locale) throws JRException 205 { 206 this(is, isXML, locale, null); 207 } 208 209 210 211 public JRViewer(JasperPrint jrPrint, Locale locale) 212 { 213 this(jrPrint, locale, null); 214 } 215 216 217 218 public JRViewer(String fileName, boolean isXML, Locale locale, ResourceBundle resBundle) throws JRException 219 { 220 initResources(locale, resBundle); 221 222 setScreenDetails(); 223 224 setZooms(); 225 226 initComponents(); 227 228 loadReport(fileName, isXML); 229 230 cmbZoom.setSelectedIndex(defaultZoomIndex); 231 232 initSaveContributors(); 233 234 addHyperlinkListener(this); 235 } 236 237 238 239 public JRViewer(InputStream is, boolean isXML, Locale locale, ResourceBundle resBundle) throws JRException 240 { 241 initResources(locale, resBundle); 242 243 setScreenDetails(); 244 245 setZooms(); 246 247 initComponents(); 248 249 loadReport(is, isXML); 250 251 cmbZoom.setSelectedIndex(defaultZoomIndex); 252 253 initSaveContributors(); 254 255 addHyperlinkListener(this); 256 } 257 258 259 260 public JRViewer(JasperPrint jrPrint, Locale locale, ResourceBundle resBundle) 261 { 262 initResources(locale, resBundle); 263 264 setScreenDetails(); 265 266 setZooms(); 267 268 initComponents(); 269 270 loadReport(jrPrint); 271 272 cmbZoom.setSelectedIndex(defaultZoomIndex); 273 274 initSaveContributors(); 275 276 addHyperlinkListener(this); 277 } 278 279 280 private void setScreenDetails() 281 { 282 screenResolution = Toolkit.getDefaultToolkit().getScreenResolution(); 283 } 284 285 286 289 public void clear() 290 { 291 emptyContainer(this); 292 jasperPrint = null; 293 } 294 295 296 299 protected void setZooms() 300 { 301 } 302 303 304 307 public void addSaveContributor(JRSaveContributor contributor) 308 { 309 saveContributors.add(contributor); 310 } 311 312 313 316 public void removeSaveContributor(JRSaveContributor contributor) 317 { 318 saveContributors.remove(contributor); 319 } 320 321 322 325 public JRSaveContributor[] getSaveContributors() 326 { 327 return (JRSaveContributor[])saveContributors.toArray(new JRSaveContributor[saveContributors.size()]); 328 } 329 330 331 334 public void addHyperlinkListener(JRHyperlinkListener listener) 335 { 336 hyperlinkListeners.add(listener); 337 } 338 339 340 343 public void removeHyperlinkListener(JRHyperlinkListener listener) 344 { 345 hyperlinkListeners.remove(listener); 346 } 347 348 349 352 public JRHyperlinkListener[] getHyperlinkListeners() 353 { 354 return (JRHyperlinkListener[])hyperlinkListeners.toArray(new JRHyperlinkListener[hyperlinkListeners.size()]); 355 } 356 357 358 361 protected void initResources(Locale locale, ResourceBundle resBundle) 362 { 363 if (locale != null) 364 setLocale(locale); 365 else 366 setLocale(Locale.getDefault()); 367 368 if (resBundle == null) 369 { 370 this.resourceBundle = ResourceBundle.getBundle("net/sf/jasperreports/view/viewer", getLocale()); 371 } 372 else 373 { 374 this.resourceBundle = resBundle; 375 } 376 } 377 378 379 382 protected String getBundleString(String key) 383 { 384 return resourceBundle.getString(key); 385 } 386 387 388 391 protected void initSaveContributors() 392 { 393 final String [] DEFAULT_CONTRIBUTORS = 394 { 395 "net.sf.jasperreports.view.save.JRPrintSaveContributor", 396 "net.sf.jasperreports.view.save.JRPdfSaveContributor", 397 "net.sf.jasperreports.view.save.JRRtfSaveContributor", 398 "net.sf.jasperreports.view.save.JRHtmlSaveContributor", 399 "net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor", 400 "net.sf.jasperreports.view.save.JRMultipleSheetsXlsSaveContributor", 401 "net.sf.jasperreports.view.save.JRCsvSaveContributor", 402 "net.sf.jasperreports.view.save.JRXmlSaveContributor", 403 "net.sf.jasperreports.view.save.JREmbeddedImagesXmlSaveContributor" 404 }; 405 406 for(int i = 0; i < DEFAULT_CONTRIBUTORS.length; i++) 407 { 408 try 409 { 410 Class saveContribClass = JRClassLoader.loadClassForName(DEFAULT_CONTRIBUTORS[i]); 411 Method method = saveContribClass.getMethod("getInstance", (Class [])null); 412 JRSaveContributor saveContrib = (JRSaveContributor)method.invoke(null, (Object [])null); 413 saveContributors.add(saveContrib); 414 } 415 catch (Exception e) 416 { 417 } 418 } 419 } 420 421 422 425 public void gotoHyperlink(JRPrintHyperlink hyperlink) 426 { 427 switch(hyperlink.getHyperlinkType()) 428 { 429 case JRHyperlink.HYPERLINK_TYPE_REFERENCE : 430 { 431 if (isOnlyHyperlinkListener()) 432 { 433 System.out.println("Hyperlink reference : " + hyperlink.getHyperlinkReference()); 434 System.out.println("Implement your own JRHyperlinkListener to manage this type of event."); 435 } 436 break; 437 } 438 case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR : 439 { 440 if (hyperlink.getHyperlinkAnchor() != null) 441 { 442 Map anchorIndexes = jasperPrint.getAnchorIndexes(); 443 JRPrintAnchorIndex anchorIndex = (JRPrintAnchorIndex)anchorIndexes.get(hyperlink.getHyperlinkAnchor()); 444 if (anchorIndex.getPageIndex() != pageIndex) 445 { 446 setPageIndex(anchorIndex.getPageIndex()); 447 refreshPage(); 448 } 449 Container container = pnlInScroll.getParent(); 450 if (container instanceof JViewport ) 451 { 452 JViewport viewport = (JViewport ) container; 453 454 int newX = (int)(anchorIndex.getElementAbsoluteX() * realZoom); 455 int newY = (int)(anchorIndex.getElementAbsoluteY() * realZoom); 456 457 int maxX = pnlInScroll.getWidth() - viewport.getWidth(); 458 int maxY = pnlInScroll.getHeight() - viewport.getHeight(); 459 460 if (newX < 0) 461 { 462 newX = 0; 463 } 464 if (newX > maxX) 465 { 466 newX = maxX; 467 } 468 if (newY < 0) 469 { 470 newY = 0; 471 } 472 if (newY > maxY) 473 { 474 newY = maxY; 475 } 476 477 viewport.setViewPosition(new Point (newX, newY)); 478 } 479 } 480 481 break; 482 } 483 case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE : 484 { 485 int page = pageIndex + 1; 486 if (hyperlink.getHyperlinkPage() != null) 487 { 488 page = hyperlink.getHyperlinkPage().intValue(); 489 } 490 491 if (page >= 1 && page <= jasperPrint.getPages().size() && page != pageIndex + 1) 492 { 493 setPageIndex(page - 1); 494 refreshPage(); 495 Container container = pnlInScroll.getParent(); 496 if (container instanceof JViewport ) 497 { 498 JViewport viewport = (JViewport ) container; 499 viewport.setViewPosition(new Point (0, 0)); 500 } 501 } 502 503 break; 504 } 505 case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR : 506 { 507 if (isOnlyHyperlinkListener()) 508 { 509 System.out.println("Hyperlink reference : " + hyperlink.getHyperlinkReference()); 510 System.out.println("Hyperlink anchor : " + hyperlink.getHyperlinkAnchor()); 511 System.out.println("Implement your own JRHyperlinkListener to manage this type of event."); 512 } 513 break; 514 } 515 case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE : 516 { 517 if (isOnlyHyperlinkListener()) 518 { 519 System.out.println("Hyperlink reference : " + hyperlink.getHyperlinkReference()); 520 System.out.println("Hyperlink page : " + hyperlink.getHyperlinkPage()); 521 System.out.println("Implement your own JRHyperlinkListener to manage this type of event."); 522 } 523 break; 524 } 525 case JRHyperlink.HYPERLINK_TYPE_CUSTOM: 526 { 527 if (isOnlyHyperlinkListener()) 528 { 529 System.out.println("Hyperlink of type " + hyperlink.getLinkType()); 530 System.out.println("Implement your own JRHyperlinkListener to manage this type of event."); 531 } 532 break; 533 } 534 case JRHyperlink.HYPERLINK_TYPE_NONE : 535 default : 536 { 537 break; 538 } 539 } 540 } 541 542 543 protected boolean isOnlyHyperlinkListener() 544 { 545 int listenerCount; 546 if (hyperlinkListeners == null) 547 { 548 listenerCount = 0; 549 } 550 else 551 { 552 listenerCount = hyperlinkListeners.size(); 553 if (hyperlinkListeners.contains(this)) 554 { 555 --listenerCount; 556 } 557 } 558 return listenerCount == 0; 559 } 560 561 562 567 private void initComponents() { 569 java.awt.GridBagConstraints gridBagConstraints; 570 571 tlbToolBar = new javax.swing.JPanel (); 572 btnSave = new javax.swing.JButton (); 573 btnPrint = new javax.swing.JButton (); 574 btnReload = new javax.swing.JButton (); 575 pnlSep01 = new javax.swing.JPanel (); 576 btnFirst = new javax.swing.JButton (); 577 btnPrevious = new javax.swing.JButton (); 578 btnNext = new javax.swing.JButton (); 579 btnLast = new javax.swing.JButton (); 580 txtGoTo = new javax.swing.JTextField (); 581 pnlSep02 = new javax.swing.JPanel (); 582 btnActualSize = new javax.swing.JToggleButton (); 583 btnFitPage = new javax.swing.JToggleButton (); 584 btnFitWidth = new javax.swing.JToggleButton (); 585 pnlSep03 = new javax.swing.JPanel (); 586 btnZoomIn = new javax.swing.JButton (); 587 btnZoomOut = new javax.swing.JButton (); 588 cmbZoom = new javax.swing.JComboBox (); 589 DefaultComboBoxModel model = new DefaultComboBoxModel (); 590 for(int i = 0; i < zooms.length; i++) 591 { 592 model.addElement("" + zooms[i] + "%"); 593 } 594 cmbZoom.setModel(model); 595 596 pnlMain = new javax.swing.JPanel (); 597 scrollPane = new javax.swing.JScrollPane (); 598 scrollPane.getHorizontalScrollBar().setUnitIncrement(5); 599 scrollPane.getVerticalScrollBar().setUnitIncrement(5); 600 601 pnlInScroll = new javax.swing.JPanel (); 602 pnlPage = new javax.swing.JPanel (); 603 jPanel4 = new javax.swing.JPanel (); 604 pnlLinks = new javax.swing.JPanel (); 605 jPanel5 = new javax.swing.JPanel (); 606 jPanel6 = new javax.swing.JPanel (); 607 jPanel7 = new javax.swing.JPanel (); 608 jPanel8 = new javax.swing.JPanel (); 609 jLabel1 = new javax.swing.JLabel (); 610 jPanel9 = new javax.swing.JPanel (); 611 lblPage = new PageRenderer(this); 612 pnlStatus = new javax.swing.JPanel (); 613 lblStatus = new javax.swing.JLabel (); 614 615 setLayout(new java.awt.BorderLayout ()); 616 617 setMinimumSize(new java.awt.Dimension (450, 150)); 618 setPreferredSize(new java.awt.Dimension (450, 150)); 619 tlbToolBar.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.LEFT, 0, 2)); 620 621 btnSave.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/save.GIF"))); 622 btnSave.setToolTipText(getBundleString("save")); 623 btnSave.setMargin(new java.awt.Insets (2, 2, 2, 2)); 624 btnSave.setMaximumSize(new java.awt.Dimension (23, 23)); 625 btnSave.setMinimumSize(new java.awt.Dimension (23, 23)); 626 btnSave.setPreferredSize(new java.awt.Dimension (23, 23)); 627 btnSave.addActionListener(new java.awt.event.ActionListener () { 628 public void actionPerformed(java.awt.event.ActionEvent evt) { 629 btnSaveActionPerformed(evt); 630 } 631 }); 632 633 tlbToolBar.add(btnSave); 634 635 btnPrint.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/print.GIF"))); 636 btnPrint.setToolTipText(getBundleString("print")); 637 btnPrint.setMargin(new java.awt.Insets (2, 2, 2, 2)); 638 btnPrint.setMaximumSize(new java.awt.Dimension (23, 23)); 639 btnPrint.setMinimumSize(new java.awt.Dimension (23, 23)); 640 btnPrint.setPreferredSize(new java.awt.Dimension (23, 23)); 641 btnPrint.addActionListener(new java.awt.event.ActionListener () { 642 public void actionPerformed(java.awt.event.ActionEvent evt) { 643 btnPrintActionPerformed(evt); 644 } 645 }); 646 647 tlbToolBar.add(btnPrint); 648 649 btnReload.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/reload.GIF"))); 650 btnReload.setToolTipText(getBundleString("reload")); 651 btnReload.setMargin(new java.awt.Insets (2, 2, 2, 2)); 652 btnReload.setMaximumSize(new java.awt.Dimension (23, 23)); 653 btnReload.setMinimumSize(new java.awt.Dimension (23, 23)); 654 btnReload.setPreferredSize(new java.awt.Dimension (23, 23)); 655 btnReload.addActionListener(new java.awt.event.ActionListener () { 656 public void actionPerformed(java.awt.event.ActionEvent evt) { 657 btnReloadActionPerformed(evt); 658 } 659 }); 660 661 tlbToolBar.add(btnReload); 662 663 pnlSep01.setMaximumSize(new java.awt.Dimension (10, 10)); 664 tlbToolBar.add(pnlSep01); 665 666 btnFirst.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/first.GIF"))); 667 btnFirst.setToolTipText(getBundleString("first.page")); 668 btnFirst.setMargin(new java.awt.Insets (2, 2, 2, 2)); 669 btnFirst.setMaximumSize(new java.awt.Dimension (23, 23)); 670 btnFirst.setMinimumSize(new java.awt.Dimension (23, 23)); 671 btnFirst.setPreferredSize(new java.awt.Dimension (23, 23)); 672 btnFirst.addActionListener(new java.awt.event.ActionListener () { 673 public void actionPerformed(java.awt.event.ActionEvent evt) { 674 btnFirstActionPerformed(evt); 675 } 676 }); 677 678 tlbToolBar.add(btnFirst); 679 680 btnPrevious.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/previous.GIF"))); 681 btnPrevious.setToolTipText(getBundleString("previous.page")); 682 btnPrevious.setMargin(new java.awt.Insets (2, 2, 2, 2)); 683 btnPrevious.setMaximumSize(new java.awt.Dimension (23, 23)); 684 btnPrevious.setMinimumSize(new java.awt.Dimension (23, 23)); 685 btnPrevious.setPreferredSize(new java.awt.Dimension (23, 23)); 686 btnPrevious.addActionListener(new java.awt.event.ActionListener () { 687 public void actionPerformed(java.awt.event.ActionEvent evt) { 688 btnPreviousActionPerformed(evt); 689 } 690 }); 691 692 tlbToolBar.add(btnPrevious); 693 694 btnNext.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/next.GIF"))); 695 btnNext.setToolTipText(getBundleString("next.page")); 696 btnNext.setMargin(new java.awt.Insets (2, 2, 2, 2)); 697 btnNext.setMaximumSize(new java.awt.Dimension (23, 23)); 698 btnNext.setMinimumSize(new java.awt.Dimension (23, 23)); 699 btnNext.setPreferredSize(new java.awt.Dimension (23, 23)); 700 btnNext.addActionListener(new java.awt.event.ActionListener () { 701 public void actionPerformed(java.awt.event.ActionEvent evt) { 702 btnNextActionPerformed(evt); 703 } 704 }); 705 706 tlbToolBar.add(btnNext); 707 708 btnLast.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/last.GIF"))); 709 btnLast.setToolTipText(getBundleString("last.page")); 710 btnLast.setMargin(new java.awt.Insets (2, 2, 2, 2)); 711 btnLast.setMaximumSize(new java.awt.Dimension (23, 23)); 712 btnLast.setMinimumSize(new java.awt.Dimension (23, 23)); 713 btnLast.setPreferredSize(new java.awt.Dimension (23, 23)); 714 btnLast.addActionListener(new java.awt.event.ActionListener () { 715 public void actionPerformed(java.awt.event.ActionEvent evt) { 716 btnLastActionPerformed(evt); 717 } 718 }); 719 720 tlbToolBar.add(btnLast); 721 722 txtGoTo.setToolTipText(getBundleString("go.to.page")); 723 txtGoTo.setMaximumSize(new java.awt.Dimension (40, 23)); 724 txtGoTo.setMinimumSize(new java.awt.Dimension (40, 23)); 725 txtGoTo.setPreferredSize(new java.awt.Dimension (40, 23)); 726 txtGoTo.addActionListener(new java.awt.event.ActionListener () { 727 public void actionPerformed(java.awt.event.ActionEvent evt) { 728 txtGoToActionPerformed(evt); 729 } 730 }); 731 732 tlbToolBar.add(txtGoTo); 733 734 pnlSep02.setMaximumSize(new java.awt.Dimension (10, 10)); 735 tlbToolBar.add(pnlSep02); 736 737 btnActualSize.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/actualsize.GIF"))); 738 btnActualSize.setToolTipText(getBundleString("actual.size")); 739 btnActualSize.setMargin(new java.awt.Insets (2, 2, 2, 2)); 740 btnActualSize.setMaximumSize(new java.awt.Dimension (23, 23)); 741 btnActualSize.setMinimumSize(new java.awt.Dimension (23, 23)); 742 btnActualSize.setPreferredSize(new java.awt.Dimension (23, 23)); 743 btnActualSize.addActionListener(new java.awt.event.ActionListener () { 744 public void actionPerformed(java.awt.event.ActionEvent evt) { 745 btnActualSizeActionPerformed(evt); 746 } 747 }); 748 749 tlbToolBar.add(btnActualSize); 750 751 btnFitPage.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/fitpage.GIF"))); 752 btnFitPage.setToolTipText(getBundleString("fit.page")); 753 btnFitPage.setMargin(new java.awt.Insets (2, 2, 2, 2)); 754 btnFitPage.setMaximumSize(new java.awt.Dimension (23, 23)); 755 btnFitPage.setMinimumSize(new java.awt.Dimension (23, 23)); 756 btnFitPage.setPreferredSize(new java.awt.Dimension (23, 23)); 757 btnFitPage.addActionListener(new java.awt.event.ActionListener () { 758 public void actionPerformed(java.awt.event.ActionEvent evt) { 759 btnFitPageActionPerformed(evt); 760 } 761 }); 762 763 tlbToolBar.add(btnFitPage); 764 765 btnFitWidth.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/fitwidth.GIF"))); 766 btnFitWidth.setToolTipText(getBundleString("fit.width")); 767 btnFitWidth.setMargin(new java.awt.Insets (2, 2, 2, 2)); 768 btnFitWidth.setMaximumSize(new java.awt.Dimension (23, 23)); 769 btnFitWidth.setMinimumSize(new java.awt.Dimension (23, 23)); 770 btnFitWidth.setPreferredSize(new java.awt.Dimension (23, 23)); 771 btnFitWidth.addActionListener(new java.awt.event.ActionListener () { 772 public void actionPerformed(java.awt.event.ActionEvent evt) { 773 btnFitWidthActionPerformed(evt); 774 } 775 }); 776 777 tlbToolBar.add(btnFitWidth); 778 779 pnlSep03.setMaximumSize(new java.awt.Dimension (10, 10)); 780 tlbToolBar.add(pnlSep03); 781 782 btnZoomIn.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/zoomin.GIF"))); 783 btnZoomIn.setToolTipText(getBundleString("zoom.in")); 784 btnZoomIn.setMargin(new java.awt.Insets (2, 2, 2, 2)); 785 btnZoomIn.setMaximumSize(new java.awt.Dimension (23, 23)); 786 btnZoomIn.setMinimumSize(new java.awt.Dimension (23, 23)); 787 btnZoomIn.setPreferredSize(new java.awt.Dimension (23, 23)); 788 btnZoomIn.addActionListener(new java.awt.event.ActionListener () { 789 public void actionPerformed(java.awt.event.ActionEvent evt) { 790 btnZoomInActionPerformed(evt); 791 } 792 }); 793 794 tlbToolBar.add(btnZoomIn); 795 796 btnZoomOut.setIcon(new javax.swing.ImageIcon (getClass().getResource("/net/sf/jasperreports/view/images/zoomout.GIF"))); 797 btnZoomOut.setToolTipText(getBundleString("zoom.out")); 798 btnZoomOut.setMargin(new java.awt.Insets (2, 2, 2, 2)); 799 btnZoomOut.setMaximumSize(new java.awt.Dimension (23, 23)); 800 btnZoomOut.setMinimumSize(new java.awt.Dimension (23, 23)); 801 btnZoomOut.setPreferredSize(new java.awt.Dimension (23, 23)); 802 btnZoomOut.addActionListener(new java.awt.event.ActionListener () { 803 public void actionPerformed(java.awt.event.ActionEvent evt) { 804 btnZoomOutActionPerformed(evt); 805 } 806 }); 807 808 tlbToolBar.add(btnZoomOut); 809 810 cmbZoom.setEditable(true); 811 cmbZoom.setToolTipText(getBundleString("zoom.ratio")); 812 cmbZoom.setMaximumSize(new java.awt.Dimension (80, 23)); 813 cmbZoom.setMinimumSize(new java.awt.Dimension (80, 23)); 814 cmbZoom.setPreferredSize(new java.awt.Dimension (80, 23)); 815 cmbZoom.addActionListener(new java.awt.event.ActionListener () { 816 public void actionPerformed(java.awt.event.ActionEvent evt) { 817 cmbZoomActionPerformed(evt); 818 } 819 }); 820 cmbZoom.addItemListener(new java.awt.event.ItemListener () { 821 public void itemStateChanged(java.awt.event.ItemEvent evt) { 822 cmbZoomItemStateChanged(evt); 823 } 824 }); 825 826 tlbToolBar.add(cmbZoom); 827 828 add(tlbToolBar, java.awt.BorderLayout.NORTH); 829 830 pnlMain.setLayout(new java.awt.BorderLayout ()); 831 832 pnlMain.addComponentListener(new java.awt.event.ComponentAdapter () { 833 public void componentResized(java.awt.event.ComponentEvent evt) { 834 pnlMainComponentResized(evt); 835 } 836 }); 837 838 scrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); 839 scrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 840 pnlInScroll.setLayout(new java.awt.GridBagLayout ()); 841 842 pnlPage.setLayout(new java.awt.BorderLayout ()); 843 844 pnlPage.setMinimumSize(new java.awt.Dimension (100, 100)); 845 pnlPage.setPreferredSize(new java.awt.Dimension (100, 100)); 846 jPanel4.setLayout(new java.awt.GridBagLayout ()); 847 848 jPanel4.setMinimumSize(new java.awt.Dimension (100, 120)); 849 jPanel4.setPreferredSize(new java.awt.Dimension (100, 120)); 850 pnlLinks.setLayout(null); 851 852 pnlLinks.setMinimumSize(new java.awt.Dimension (5, 5)); 853 pnlLinks.setPreferredSize(new java.awt.Dimension (5, 5)); 854 pnlLinks.setOpaque(false); 855 pnlLinks.addMouseListener(new java.awt.event.MouseAdapter () { 856 public void mousePressed(java.awt.event.MouseEvent evt) { 857 pnlLinksMousePressed(evt); 858 } 859 public void mouseReleased(java.awt.event.MouseEvent evt) { 860 pnlLinksMouseReleased(evt); 861 } 862 }); 863 pnlLinks.addMouseMotionListener(new java.awt.event.MouseMotionAdapter () { 864 public void mouseDragged(java.awt.event.MouseEvent evt) { 865 pnlLinksMouseDragged(evt); 866 } 867 }); 868 869 gridBagConstraints = new java.awt.GridBagConstraints (); 870 gridBagConstraints.gridx = 0; 871 gridBagConstraints.gridy = 0; 872 gridBagConstraints.gridwidth = 2; 873 gridBagConstraints.gridheight = 2; 874 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 875 jPanel4.add(pnlLinks, gridBagConstraints); 876 877 jPanel5.setBackground(java.awt.Color.gray); 878 jPanel5.setMinimumSize(new java.awt.Dimension (5, 5)); 879 jPanel5.setPreferredSize(new java.awt.Dimension (5, 5)); 880 gridBagConstraints = new java.awt.GridBagConstraints (); 881 gridBagConstraints.gridx = 2; 882 gridBagConstraints.gridy = 1; 883 gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL; 884 jPanel4.add(jPanel5, gridBagConstraints); 885 886 jPanel6.setMinimumSize(new java.awt.Dimension (5, 5)); 887 jPanel6.setPreferredSize(new java.awt.Dimension (5, 5)); 888 gridBagConstraints = new java.awt.GridBagConstraints (); 889 gridBagConstraints.gridx = 0; 890 gridBagConstraints.gridy = 2; 891 jPanel4.add(jPanel6, gridBagConstraints); 892 893 jPanel7.setBackground(java.awt.Color.gray); 894 jPanel7.setMinimumSize(new java.awt.Dimension (5, 5)); 895 jPanel7.setPreferredSize(new java.awt.Dimension (5, 5)); 896 gridBagConstraints = new java.awt.GridBagConstraints (); 897 gridBagConstraints.gridx = 1; 898 gridBagConstraints.gridy = 2; 899 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 900 jPanel4.add(jPanel7, gridBagConstraints); 901 902 jPanel8.setBackground(java.awt.Color.gray); 903 jPanel8.setMinimumSize(new java.awt.Dimension (5, 5)); 904 jPanel8.setPreferredSize(new java.awt.Dimension (5, 5)); 905 jLabel1.setText("jLabel1"); 906 jPanel8.add(jLabel1); 907 908 gridBagConstraints = new java.awt.GridBagConstraints (); 909 gridBagConstraints.gridx = 2; 910 gridBagConstraints.gridy = 2; 911 jPanel4.add(jPanel8, gridBagConstraints); 912 913 jPanel9.setMinimumSize(new java.awt.Dimension (5, 5)); 914 jPanel9.setPreferredSize(new java.awt.Dimension (5, 5)); 915 gridBagConstraints = new java.awt.GridBagConstraints (); 916 gridBagConstraints.gridx = 2; 917 gridBagConstraints.gridy = 0; 918 jPanel4.add(jPanel9, gridBagConstraints); 919 920 lblPage.setBackground(java.awt.Color.white); 921 lblPage.setBorder(new javax.swing.border.LineBorder (new java.awt.Color (0, 0, 0))); 922 lblPage.setOpaque(true); 923 gridBagConstraints = new java.awt.GridBagConstraints (); 924 gridBagConstraints.gridx = 0; 925 gridBagConstraints.gridy = 0; 926 gridBagConstraints.gridwidth = 2; 927 gridBagConstraints.gridheight = 2; 928 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 929 gridBagConstraints.weightx = 1.0; 930 gridBagConstraints.weighty = 1.0; 931 jPanel4.add(lblPage, gridBagConstraints); 932 933 pnlPage.add(jPanel4, java.awt.BorderLayout.CENTER); 934 935 gridBagConstraints = new java.awt.GridBagConstraints (); 936 gridBagConstraints.insets = new java.awt.Insets (5, 5, 5, 5); 937 pnlInScroll.add(pnlPage, gridBagConstraints); 938 939 scrollPane.setViewportView(pnlInScroll); 940 941 pnlMain.add(scrollPane, java.awt.BorderLayout.CENTER); 942 943 add(pnlMain, java.awt.BorderLayout.CENTER); 944 945 pnlStatus.setLayout(new java.awt.FlowLayout (java.awt.FlowLayout.CENTER, 0, 0)); 946 947 lblStatus.setFont(new java.awt.Font ("Dialog", 1, 10)); 948 lblStatus.setText("Page i of n"); 949 pnlStatus.add(lblStatus); 950 951 add(pnlStatus, java.awt.BorderLayout.SOUTH); 952 953 } 954 956 void txtGoToActionPerformed(java.awt.event.ActionEvent evt) { try 958 { 959 int pageNumber = Integer.parseInt(txtGoTo.getText()); 960 if ( 961 pageNumber != pageIndex + 1 962 && pageNumber > 0 963 && pageNumber <= jasperPrint.getPages().size() 964 ) 965 { 966 setPageIndex(pageNumber - 1); 967 refreshPage(); 968 } 969 } 970 catch(NumberFormatException e) 971 { 972 } 973 } 975 void cmbZoomItemStateChanged(java.awt.event.ItemEvent evt) { btnActualSize.setSelected(false); 978 btnFitPage.setSelected(false); 979 btnFitWidth.setSelected(false); 980 } 982 void pnlMainComponentResized(java.awt.event.ComponentEvent evt) { if (btnFitPage.isSelected()) 985 { 986 fitPage(); 987 btnFitPage.setSelected(true); 988 } 989 else if (btnFitWidth.isSelected()) 990 { 991 setRealZoomRatio(((float)pnlInScroll.getVisibleRect().getWidth() - 20f) / jasperPrint.getPageWidth()); 992 btnFitWidth.setSelected(true); 993 } 994 995 } 997 void btnActualSizeActionPerformed(java.awt.event.ActionEvent evt) { if (btnActualSize.isSelected()) 1000 { 1001 btnFitPage.setSelected(false); 1002 btnFitWidth.setSelected(false); 1003 cmbZoom.setSelectedIndex(-1); 1004 setZoomRatio(1); 1005 btnActualSize.setSelected(true); 1006 } 1007 } 1009 void btnFitWidthActionPerformed(java.awt.event.ActionEvent evt) { if (btnFitWidth.isSelected()) 1012 { 1013 btnActualSize.setSelected(false); 1014 btnFitPage.setSelected(false); 1015 cmbZoom.setSelectedIndex(-1); 1016 setRealZoomRatio(((float)pnlInScroll.getVisibleRect().getWidth() - 20f) / jasperPrint.getPageWidth()); 1017 btnFitWidth.setSelected(true); 1018 } 1019 } 1021 void btnFitPageActionPerformed(java.awt.event.ActionEvent evt) { if (btnFitPage.isSelected()) 1024 { 1025 btnActualSize.setSelected(false); 1026 btnFitWidth.setSelected(false); 1027 cmbZoom.setSelectedIndex(-1); 1028 fitPage(); 1029 btnFitPage.setSelected(true); 1030 } 1031 } 1033 void btnSaveActionPerformed(java.awt.event.ActionEvent evt) { 1036 JFileChooser fileChooser = new JFileChooser (); 1037 1038 for(int i = 0; i < saveContributors.size(); i++) 1039 { 1040 fileChooser.addChoosableFileFilter((JRSaveContributor)saveContributors.get(i)); 1041 } 1042 1043 if (saveContributors.size() > 0) 1044 { 1045 fileChooser.setFileFilter((JRSaveContributor)saveContributors.get(0)); 1046 } 1047 1048 int retValue = fileChooser.showSaveDialog(this); 1049 if (retValue == JFileChooser.APPROVE_OPTION) 1050 { 1051 FileFilter fileFilter = fileChooser.getFileFilter(); 1052 File file = fileChooser.getSelectedFile(); 1053 1054 JRSaveContributor contributor = null; 1055 1056 if (fileFilter instanceof JRSaveContributor) 1057 { 1058 contributor = (JRSaveContributor)fileFilter; 1059 } 1060 else 1061 { 1062 int i = 0; 1063 while(contributor == null && i < saveContributors.size()) 1064 { 1065 contributor = (JRSaveContributor)saveContributors.get(i++); 1066 if (!contributor.accept(file)) 1067 { 1068 contributor = null; 1069 } 1070 } 1071 1072 if (contributor == null) 1073 { 1074 contributor = new JRPrintSaveContributor(); 1075 } 1076 } 1077 1078 try 1079 { 1080 contributor.save(jasperPrint, file); 1081 } 1082 catch (JRException e) 1083 { 1084 e.printStackTrace(); 1085 JOptionPane.showMessageDialog(this, getBundleString("error.saving")); 1086 } 1087 } 1088 } 1090 void pnlLinksMouseDragged(java.awt.event.MouseEvent evt) { 1093 Container container = pnlInScroll.getParent(); 1094 if (container instanceof JViewport ) 1095 { 1096 JViewport viewport = (JViewport ) container; 1097 Point point = viewport.getViewPosition(); 1098 int newX = point.x - (evt.getX() - downX); 1099 int newY = point.y - (evt.getY() - downY); 1100 1101 int maxX = pnlInScroll.getWidth() - viewport.getWidth(); 1102 int maxY = pnlInScroll.getHeight() - viewport.getHeight(); 1103 1104 if (newX < 0) 1105 { 1106 newX = 0; 1107 } 1108 if (newX > maxX) 1109 { 1110 newX = maxX; 1111 } 1112 if (newY < 0) 1113 { 1114 newY = 0; 1115 } 1116 if (newY > maxY) 1117 { 1118 newY = maxY; 1119 } 1120 1121 viewport.setViewPosition(new Point (newX, newY)); 1122 } 1123 } 1125 void pnlLinksMouseReleased(java.awt.event.MouseEvent evt) { pnlLinks.setCursor(new Cursor (Cursor.DEFAULT_CURSOR)); 1128 } 1130 void pnlLinksMousePressed(java.awt.event.MouseEvent evt) { pnlLinks.setCursor(new Cursor (Cursor.MOVE_CURSOR)); 1133 1134 downX = evt.getX(); 1135 downY = evt.getY(); 1136 } 1138 void btnPrintActionPerformed(java.awt.event.ActionEvent evt) { 1142 Thread thread = 1143 new Thread ( 1144 new Runnable () 1145 { 1146 public void run() 1147 { 1148 try 1149 { 1150 JasperPrintManager.printReport(jasperPrint, true); 1151 } 1152 catch (Exception ex) 1153 { 1154 ex.printStackTrace(); 1155 JOptionPane.showMessageDialog(JRViewer.this, getBundleString("error.printing")); 1156 } 1157 } 1158 } 1159 ); 1160 1161 thread.start(); 1162 1163 } 1165 void btnLastActionPerformed(java.awt.event.ActionEvent evt) { setPageIndex(jasperPrint.getPages().size() - 1); 1169 refreshPage(); 1170 } 1172 void btnNextActionPerformed(java.awt.event.ActionEvent evt) { setPageIndex(pageIndex + 1); 1176 refreshPage(); 1177 } 1179 void btnPreviousActionPerformed(java.awt.event.ActionEvent evt) { setPageIndex(pageIndex - 1); 1183 refreshPage(); 1184 } 1186 void btnFirstActionPerformed(java.awt.event.ActionEvent evt) { setPageIndex(0); 1190 refreshPage(); 1191 } 1193 void btnReloadActionPerformed(java.awt.event.ActionEvent evt) { if (type == TYPE_FILE_NAME) 1197 { 1198 try 1199 { 1200 loadReport(reportFileName, isXML); 1201 } 1202 catch (JRException e) 1203 { 1204 e.printStackTrace(); 1205 1206 jasperPrint = null; 1207 setPageIndex(0); 1208 refreshPage(); 1209 1210 JOptionPane.showMessageDialog(this, getBundleString("error.loading")); 1211 } 1212 1213 zoom = 0; realZoom = 0f; 1215 setZoomRatio(1); 1216 } 1217 } 1219 void btnZoomInActionPerformed(java.awt.event.ActionEvent evt) { btnActualSize.setSelected(false); 1223 btnFitPage.setSelected(false); 1224 btnFitWidth.setSelected(false); 1225 1226 int newZoomInt = (int)(100 * getZoomRatio()); 1227 int index = Arrays.binarySearch(zooms, newZoomInt); 1228 if (index < 0) 1229 { 1230 setZoomRatio(zooms[- index - 1] / 100f); 1231 } 1232 else if (index < cmbZoom.getModel().getSize() - 1) 1233 { 1234 setZoomRatio(zooms[index + 1] / 100f); 1235 } 1236 } 1238 void btnZoomOutActionPerformed(java.awt.event.ActionEvent evt) { btnActualSize.setSelected(false); 1242 btnFitPage.setSelected(false); 1243 btnFitWidth.setSelected(false); 1244 1245 int newZoomInt = (int)(100 * getZoomRatio()); 1246 int index = Arrays.binarySearch(zooms, newZoomInt); 1247 if (index > 0) 1248 { 1249 setZoomRatio(zooms[index - 1] / 100f); 1250 } 1251 else if (index < -1) 1252 { 1253 setZoomRatio(zooms[- index - 2] / 100f); 1254 } 1255 } 1257 void cmbZoomActionPerformed(java.awt.event.ActionEvent evt) { float newZoom = getZoomRatio(); 1261 1262 if (newZoom < MIN_ZOOM) 1263 { 1264 newZoom = MIN_ZOOM; 1265 } 1266 1267 if (newZoom > MAX_ZOOM) 1268 { 1269 newZoom = MAX_ZOOM; 1270 } 1271 1272 setZoomRatio(newZoom); 1273 } 1275 1276 1278 void hyperlinkClicked(MouseEvent evt) 1279 { 1280 JPanel link = (JPanel )evt.getSource(); 1281 JRPrintHyperlink element = (JRPrintHyperlink)linksMap.get(link); 1282 hyperlinkClicked(element); 1283 } 1284 1285 1286 protected void hyperlinkClicked(JRPrintHyperlink hyperlink) 1287 { 1288 try 1289 { 1290 JRHyperlinkListener listener = null; 1291 for(int i = 0; i < hyperlinkListeners.size(); i++) 1292 { 1293 listener = (JRHyperlinkListener)hyperlinkListeners.get(i); 1294 listener.gotoHyperlink(hyperlink); 1295 } 1296 } 1297 catch(JRException e) 1298 { 1299 e.printStackTrace(); 1300 JOptionPane.showMessageDialog(this, getBundleString("error.hyperlink")); 1301 } 1302 } 1303 1304 1305 1307 private void setPageIndex(int index) 1308 { 1309 pageIndex = index; 1310 if ( 1311 jasperPrint != null && 1312 jasperPrint.getPages() != null && 1313 jasperPrint.getPages().size() > 0 1314 ) 1315 { 1316 btnFirst.setEnabled( (pageIndex > 0) ); 1317 btnPrevious.setEnabled( (pageIndex > 0) ); 1318 btnNext.setEnabled( (pageIndex < jasperPrint.getPages().size() - 1) ); 1319 btnLast.setEnabled( (pageIndex < jasperPrint.getPages().size() - 1) ); 1320 txtGoTo.setEnabled(btnFirst.isEnabled() || btnLast.isEnabled()); 1321 txtGoTo.setText("" + (pageIndex + 1)); 1322 lblStatus.setText( 1323 MessageFormat.format( 1324 getBundleString("page"), 1325 new Object []{new Integer (pageIndex + 1), new Integer (jasperPrint.getPages().size())} 1326 ) 1327 ); 1328 } 1329 else 1330 { 1331 btnFirst.setEnabled(false); 1332 btnPrevious.setEnabled(false); 1333 btnNext.setEnabled(false); 1334 btnLast.setEnabled(false); 1335 txtGoTo.setEnabled(false); 1336 txtGoTo.setText(""); 1337 lblStatus.setText(""); 1338 } 1339 } 1340 1341 1342 1344 protected void loadReport(String fileName, boolean isXmlReport) throws JRException 1345 { 1346 if (isXmlReport) 1347 { 1348 jasperPrint = JRPrintXmlLoader.load(fileName); 1349 } 1350 else 1351 { 1352 jasperPrint = (JasperPrint)JRLoader.loadObject(fileName); 1353 } 1354 1355 type = TYPE_FILE_NAME; 1356 this.isXML = isXmlReport; 1357 reportFileName = fileName; 1358 btnReload.setEnabled(true); 1359 setPageIndex(0); 1360 } 1361 1362 1363 1365 protected void loadReport(InputStream is, boolean isXmlReport) throws JRException 1366 { 1367 if (isXmlReport) 1368 { 1369 jasperPrint = JRPrintXmlLoader.load(is); 1370 } 1371 else 1372 { 1373 jasperPrint = (JasperPrint)JRLoader.loadObject(is); 1374 } 1375 1376 type = TYPE_INPUT_STREAM; 1377 this.isXML = isXmlReport; 1378 btnReload.setEnabled(false); 1379 setPageIndex(0); 1380 } 1381 1382 1383 1385 protected void loadReport(JasperPrint jrPrint) 1386 { 1387 jasperPrint = jrPrint; 1388 type = TYPE_JASPER_PRINT; 1389 isXML = false; 1390 btnReload.setEnabled(false); 1391 setPageIndex(0); 1392 } 1393 1394 1395 1397 protected void refreshPage() 1398 { 1399 if ( 1400 jasperPrint == null || 1401 jasperPrint.getPages() == null || 1402 jasperPrint.getPages().size() == 0 1403 ) 1404 { 1405 pnlPage.setVisible(false); 1406 btnSave.setEnabled(false); 1407 btnPrint.setEnabled(false); 1408 btnActualSize.setEnabled(false); 1409 btnFitPage.setEnabled(false); 1410 btnFitWidth.setEnabled(false); 1411 btnZoomIn.setEnabled(false); 1412 btnZoomOut.setEnabled(false); 1413 cmbZoom.setEnabled(false); 1414 1415 if (jasperPrint != null) 1416 { 1417 JOptionPane.showMessageDialog(this, getBundleString("no.pages")); 1418 } 1419 1420 return; 1421 } 1422 1423 pnlPage.setVisible(true); 1424 btnSave.setEnabled(true); 1425 btnPrint.setEnabled(true); 1426 btnActualSize.setEnabled(true); 1427 btnFitPage.setEnabled(true); 1428 btnFitWidth.setEnabled(true); 1429 btnZoomIn.setEnabled(zoom < MAX_ZOOM); 1430 btnZoomOut.setEnabled(zoom > MIN_ZOOM); 1431 cmbZoom.setEnabled(true); 1432 1433 Dimension dim = new Dimension ( 1434 (int)(jasperPrint.getPageWidth() * realZoom) + 8, (int)(jasperPrint.getPageHeight() * realZoom) + 8 1436 ); 1437 pnlPage.setMaximumSize(dim); 1438 pnlPage.setMinimumSize(dim); 1439 pnlPage.setPreferredSize(dim); 1440 1441 long maxImageSize = JRProperties.getLongProperty(VIEWER_RENDER_BUFFER_MAX_SIZE); 1442 boolean renderImage; 1443 if (maxImageSize <= 0) 1444 { 1445 renderImage = false; 1446 } 1447 else 1448 { 1449 long imageSize = JRPrinterAWT.getImageSize(jasperPrint, realZoom); 1450 renderImage = imageSize <= maxImageSize; 1451 } 1452 1453 lblPage.setRenderImage(renderImage); 1454 1455 if (renderImage) 1456 { 1457 Image image = null; 1458 ImageIcon imageIcon = null; 1459 try 1460 { 1461 image = JasperPrintManager.printPageToImage(jasperPrint, pageIndex, realZoom); 1462 imageIcon = new ImageIcon (image); 1463 } 1464 catch (Exception e) 1465 { 1466 e.printStackTrace(); 1467 JOptionPane.showMessageDialog(this, java.util.ResourceBundle.getBundle("net/sf/jasperreports/view/viewer").getString("error.displaying")); 1468 } 1469 1470 lblPage.setIcon(imageIcon); 1471 } 1472 1473 pnlLinks.removeAll(); 1474 linksMap = new HashMap (); 1475 1476 createHyperlinks(); 1477 1478 if (!renderImage) 1479 { 1480 lblPage.setIcon(null); 1481 1482 pnlMain.validate(); 1483 pnlMain.repaint(); 1484 } 1485 } 1486 1487 1488 protected void createHyperlinks() 1489 { 1490 java.util.List pages = jasperPrint.getPages(); 1491 JRPrintPage page = (JRPrintPage)pages.get(pageIndex); 1492 Collection elements = page.getElements(); 1493 if(elements != null && elements.size() > 0) 1494 { 1495 for(Iterator it = elements.iterator(); it.hasNext();) 1496 { 1497 JRPrintElement element = (JRPrintElement)it.next(); 1498 1499 JRImageMapRenderer imageMap = null; 1500 if (element instanceof JRPrintImage) 1501 { 1502 JRRenderable renderer = ((JRPrintImage) element).getRenderer(); 1503 if (renderer instanceof JRImageMapRenderer) 1504 { 1505 imageMap = (JRImageMapRenderer) renderer; 1506 } 1507 } 1508 boolean hasImageMap = imageMap != null; 1509 1510 JRPrintHyperlink hyperlink = null; 1511 if (!hasImageMap && element instanceof JRPrintHyperlink) 1512 { 1513 hyperlink = (JRPrintHyperlink) element; 1514 } 1515 boolean hasHyperlink = hyperlink != null && hyperlink.getHyperlinkType() != JRHyperlink.HYPERLINK_TYPE_NONE; 1516 1517 if (hasHyperlink || hasImageMap) 1518 { 1519 JPanel link; 1520 if (hasHyperlink) 1521 { 1522 link = new JPanel (); 1523 link.addMouseListener(mouseListener); 1524 } 1525 else { 1527 Rectangle renderingArea = new Rectangle (0, 0, element.getWidth(), element.getHeight()); 1528 link = new ImageMapPanel(renderingArea, imageMap); 1529 } 1530 1531 if (hasHyperlink) 1532 { 1533 link.setCursor(new Cursor (Cursor.HAND_CURSOR)); 1534 } 1535 1536 link.setLocation( 1537 (int)(element.getX() * realZoom), 1538 (int)(element.getY() * realZoom) 1539 ); 1540 link.setSize( 1541 (int)(element.getWidth() * realZoom), 1542 (int)(element.getHeight() * realZoom) 1543 ); 1544 link.setOpaque(false); 1545 1546 String toolTip; 1547 if (hasHyperlink) 1548 { 1549 toolTip = getHyperlinkTooltip(hyperlink); 1550 } 1551 else { 1553 toolTip = ""; } 1555 link.setToolTipText(toolTip); 1556 1557 pnlLinks.add(link); 1558 linksMap.put(link, element); 1559 } 1560 } 1561 } 1562 } 1563 1564 1565 protected class ImageMapPanel extends JPanel implements MouseListener , MouseMotionListener 1566 { 1567 protected final List imageAreaHyperlinks; 1568 1569 public ImageMapPanel(Rectangle renderingArea, JRImageMapRenderer imageMap) 1570 { 1571 try 1572 { 1573 imageAreaHyperlinks = imageMap.getImageAreaHyperlinks(renderingArea); 1574 } 1575 catch (JRException e) 1576 { 1577 throw new JRRuntimeException(e); 1578 } 1579 1580 addMouseListener(this); 1581 addMouseMotionListener(this); 1582 } 1583 1584 public String getToolTipText(MouseEvent event) 1585 { 1586 String tooltip = null; 1587 JRPrintImageAreaHyperlink imageMapArea = getImageMapArea(event); 1588 if (imageMapArea != null) 1589 { 1590 tooltip = getHyperlinkTooltip(imageMapArea.getHyperlink()); 1591 } 1592 1593 if (tooltip == null) 1594 { 1595 tooltip = super.getToolTipText(event); 1596 } 1597 1598 return tooltip; 1599 } 1600 1601 public void mouseDragged(MouseEvent e) 1602 { 1603 } 1604 1605 public void mouseMoved(MouseEvent e) 1606 { 1607 JRPrintImageAreaHyperlink imageArea = getImageMapArea(e); 1608 if (imageArea != null 1609 && imageArea.getHyperlink().getHyperlinkType() != JRHyperlink.HYPERLINK_TYPE_NONE) 1610 { 1611 e.getComponent().setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); 1612 } 1613 else 1614 { 1615 e.getComponent().setCursor(Cursor.getDefaultCursor()); 1616 } 1617 } 1618 1619 protected JRPrintImageAreaHyperlink getImageMapArea(MouseEvent e) 1620 { 1621 return getImageMapArea((int) (e.getX() / realZoom), (int) (e.getY() / realZoom)); 1622 } 1623 1624 protected JRPrintImageAreaHyperlink getImageMapArea(int x, int y) 1625 { 1626 JRPrintImageAreaHyperlink image = null; 1627 for (Iterator it = imageAreaHyperlinks.iterator(); image == null && it.hasNext();) 1628 { 1629 JRPrintImageAreaHyperlink area = (JRPrintImageAreaHyperlink) it.next(); 1630 if (area.getArea().containsPoint(x, y)) 1631 { 1632 image = area; 1633 } 1634 } 1635 return image; 1636 } 1637 1638 public void mouseClicked(MouseEvent e) 1639 { 1640 JRPrintImageAreaHyperlink imageMapArea = getImageMapArea(e); 1641 if (imageMapArea != null) 1642 { 1643 hyperlinkClicked(imageMapArea.getHyperlink()); 1644 } 1645 } 1646 1647 public void mouseEntered(MouseEvent e) 1648 { 1649 } 1650 1651 public void mouseExited(MouseEvent e) 1652 { 1653 } 1654 1655 public void mousePressed(MouseEvent e) 1656 { 1657 } 1658 1659 public void mouseReleased(MouseEvent e) 1660 { 1661 } 1662 } 1663 1664 1665 protected String getHyperlinkTooltip(JRPrintHyperlink hyperlink) 1666 { 1667 String toolTip; 1668 toolTip = hyperlink.getHyperlinkTooltip(); 1669 if (toolTip == null) 1670 { 1671 toolTip = getFallbackTooltip(hyperlink); 1672 } 1673 return toolTip; 1674 } 1675 1676 1677 protected String getFallbackTooltip(JRPrintHyperlink hyperlink) 1678 { 1679 String toolTip = null; 1680 switch(hyperlink.getHyperlinkType()) 1681 { 1682 case JRHyperlink.HYPERLINK_TYPE_REFERENCE : 1683 { 1684 toolTip = hyperlink.getHyperlinkReference(); 1685 break; 1686 } 1687 case JRHyperlink.HYPERLINK_TYPE_LOCAL_ANCHOR : 1688 { 1689 if (hyperlink.getHyperlinkAnchor() != null) 1690 { 1691 toolTip = "#" + hyperlink.getHyperlinkAnchor(); 1692 } 1693 break; 1694 } 1695 case JRHyperlink.HYPERLINK_TYPE_LOCAL_PAGE : 1696 { 1697 if (hyperlink.getHyperlinkPage() != null) 1698 { 1699 toolTip = "#page " + hyperlink.getHyperlinkPage(); 1700 } 1701 break; 1702 } 1703 case JRHyperlink.HYPERLINK_TYPE_REMOTE_ANCHOR : 1704 { 1705 toolTip = ""; 1706 if (hyperlink.getHyperlinkReference() != null) 1707 { 1708 toolTip = toolTip + hyperlink.getHyperlinkReference(); 1709 } 1710 if (hyperlink.getHyperlinkAnchor() != null) 1711 { 1712 toolTip = toolTip + "#" + hyperlink.getHyperlinkAnchor(); 1713 } 1714 break; 1715 } 1716 case JRHyperlink.HYPERLINK_TYPE_REMOTE_PAGE : 1717 { 1718 toolTip = ""; 1719 if (hyperlink.getHyperlinkReference() != null) 1720 { 1721 toolTip = toolTip + hyperlink.getHyperlinkReference(); 1722 } 1723 if (hyperlink.getHyperlinkPage() != null) 1724 { 1725 toolTip = toolTip + "#page " + hyperlink.getHyperlinkPage(); 1726 } 1727 break; 1728 } 1729 default : 1730 { 1731 break; 1732 } 1733 } 1734 return toolTip; 1735 } 1736 1737 1738 1740 private void emptyContainer(Container container) 1741 { 1742 Component [] components = container.getComponents(); 1743 1744 if (components != null) 1745 { 1746 for(int i = 0; i < components.length; i++) 1747 { 1748 if (components[i] instanceof Container ) 1749 { 1750 emptyContainer((Container )components[i]); 1751 } 1752 } 1753 } 1754 1755 components = null; 1756 container.removeAll(); 1757 container = null; 1758 } 1759 1760 1761 1763 private float getZoomRatio() 1764 { 1765 float newZoom = zoom; 1766 1767 try 1768 { 1769 newZoom = 1770 zoomDecimalFormat.parse( 1771 String.valueOf(cmbZoom.getEditor().getItem()) 1772 ).floatValue() / 100f; 1773 } 1774 catch(ParseException e) 1775 { 1776 } 1777 1778 return newZoom; 1779 } 1780 1781 1782 1784 public void setZoomRatio(float newZoom) 1785 { 1786 if (newZoom > 0) 1787 { 1788 cmbZoom.getEditor().setItem( 1789 zoomDecimalFormat.format(newZoom * 100) + "%" 1790 ); 1791 1792 if (zoom != newZoom) 1793 { 1794 zoom = newZoom; 1795 realZoom = zoom * screenResolution / REPORT_RESOLUTION; 1796 1797 refreshPage(); 1798 } 1799 } 1800 } 1801 1802 1803 1805 private void setRealZoomRatio(float newZoom) 1806 { 1807 if (newZoom > 0 && realZoom != newZoom) 1808 { 1809 zoom = newZoom * REPORT_RESOLUTION / screenResolution; 1810 realZoom = newZoom; 1811 1812 cmbZoom.getEditor().setItem( 1813 zoomDecimalFormat.format(zoom * 100) + "%" 1814 ); 1815 1816 refreshPage(); 1817 } 1818 } 1819 1820 1821 1824 public void setFitWidthZoomRatio() 1825 { 1826 setRealZoomRatio(((float)pnlInScroll.getVisibleRect().getWidth() - 20f) / jasperPrint.getPageWidth()); 1827 1828 } 1829 1830 public void setFitPageZoomRatio() 1831 { 1832 setRealZoomRatio(((float)pnlInScroll.getVisibleRect().getHeight() - 20f) / jasperPrint.getPageHeight()); 1833 } 1834 1835 1836 1838 protected void paintPage(Graphics2D grx) 1839 { 1840 try 1841 { 1842 if (exporter == null) 1843 { 1844 exporter = new JRGraphics2DExporter(); 1845 } 1846 else 1847 { 1848 exporter.reset(); 1849 } 1850 1851 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 1852 exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, grx); 1853 exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer (pageIndex)); 1854 exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float (realZoom)); 1855 exporter.setParameter(JRExporterParameter.OFFSET_X, new Integer (1)); exporter.setParameter(JRExporterParameter.OFFSET_Y, new Integer (1)); 1857 exporter.exportReport(); 1858 } 1859 catch(Exception e) 1860 { 1861 e.printStackTrace(); 1862 JOptionPane.showMessageDialog(this, getBundleString("error.displaying")); 1863 } 1864 1865 } 1866 1867 1870 private void fitPage(){ 1871 float heightRatio = ((float)pnlInScroll.getVisibleRect().getHeight() - 20f) / jasperPrint.getPageHeight(); 1872 float widthRatio = ((float)pnlInScroll.getVisibleRect().getWidth() - 20f) / jasperPrint.getPageWidth(); 1873 setRealZoomRatio(heightRatio < widthRatio ? heightRatio : widthRatio); 1874 } 1875 1876 1878 class PageRenderer extends JLabel 1879 { 1880 private boolean renderImage; 1881 JRViewer viewer = null; 1882 1883 public PageRenderer(JRViewer viewer) 1884 { 1885 this.viewer = viewer; 1886 } 1887 1888 public void paintComponent(Graphics g) 1889 { 1890 if (isRenderImage()) 1891 { 1892 super.paintComponent(g); 1893 } 1894 else 1895 { 1896 viewer.paintPage((Graphics2D )g.create()); 1897 } 1898 } 1899 1900 public boolean isRenderImage() 1901 { 1902 return renderImage; 1903 } 1904 1905 public void setRenderImage(boolean renderImage) 1906 { 1907 this.renderImage = renderImage; 1908 } 1909 } 1910 1911 1912 protected javax.swing.JToggleButton btnActualSize; 1914 protected javax.swing.JButton btnFirst; 1915 protected javax.swing.JToggleButton btnFitPage; 1916 protected javax.swing.JToggleButton btnFitWidth; 1917 protected javax.swing.JButton btnLast; 1918 protected javax.swing.JButton btnNext; 1919 protected javax.swing.JButton btnPrevious; 1920 protected javax.swing.JButton btnPrint; 1921 protected javax.swing.JButton btnReload; 1922 protected javax.swing.JButton btnSave; 1923 protected javax.swing.JButton btnZoomIn; 1924 protected javax.swing.JButton btnZoomOut; 1925 protected javax.swing.JComboBox cmbZoom; 1926 private javax.swing.JLabel jLabel1; 1927 private javax.swing.JPanel jPanel4; 1928 private javax.swing.JPanel jPanel5; 1929 private javax.swing.JPanel jPanel6; 1930 private javax.swing.JPanel jPanel7; 1931 private javax.swing.JPanel jPanel8; 1932 private javax.swing.JPanel jPanel9; 1933 private PageRenderer lblPage; 1934 protected javax.swing.JLabel lblStatus; 1935 private javax.swing.JPanel pnlInScroll; 1936 private javax.swing.JPanel pnlLinks; 1937 private javax.swing.JPanel pnlMain; 1938 private javax.swing.JPanel pnlPage; 1939 protected javax.swing.JPanel pnlSep01; 1940 protected javax.swing.JPanel pnlSep02; 1941 protected javax.swing.JPanel pnlSep03; 1942 protected javax.swing.JPanel pnlStatus; 1943 private javax.swing.JScrollPane scrollPane; 1944 protected javax.swing.JPanel tlbToolBar; 1945 protected javax.swing.JTextField txtGoTo; 1946 1948} 1949 | Popular Tags |