Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ImageProxyPanel.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.awt.viewer;
  19. import java.awt.Dimension;
  20. import java.awt.Graphics;
  21. import java.awt.Insets;
  22. import java.awt.image.BufferedImage;
  23. import java.lang.ref.Reference;
  24. import java.lang.ref.SoftReference;
  25. import javax.swing.JPanel;
  26. import org.apache.fop.apps.FOPException;
  27. import org.apache.fop.render.awt.AWTRenderer;
  28. /**
  29. * Panel used to display a single page of a document.
  30. * This is basically a lazy-load display panel which
  31. * gets the size of the image for layout purposes but
  32. * doesn't get the actual image data until needed.
  33. * The image data is then accessed via a soft reference,
  34. * so it will be garbage collected when moving through
  35. * large documents.
  36. */
  37. public class ImageProxyPanel extends JPanel {
  38. /** The reference to the BufferedImage storing the page data */
  39. private Reference imageRef;
  40. /** The maximum and preferred size of the panel */
  41. private Dimension size;
  42. /** The renderer. Shared with PreviewPanel and PreviewDialog. */
  43. private AWTRenderer renderer;
  44. /** The page to be rendered. */
  45. private int page;
  46. /**
  47. * Panel constructor. Doesn't allocate anything until needed.
  48. * @param renderer the AWTRenderer instance to use for painting
  49. * @param page initial page number to show
  50. */
  51. public ImageProxyPanel(AWTRenderer renderer, int page) {
  52. this.renderer = renderer;
  53. this.page = page;
  54. // Allows single panel to appear behind page display.
  55. // Important for textured L&Fs.
  56. setOpaque(false);
  57. }
  58. /**
  59. * @return the size of the page plus the border.
  60. */
  61. public Dimension getMinimumSize() {
  62. return getPreferredSize();
  63. }
  64. /**
  65. * @return the size of the page plus the border.
  66. */
  67. public Dimension getPreferredSize() {
  68. if (size == null) {
  69. try {
  70. Insets insets = getInsets();
  71. size = renderer.getPageImageSize(page);
  72. size = new Dimension(size.width + insets.left + insets.right,
  73. size.height + insets.top + insets.bottom);
  74. } catch (FOPException fopEx) {
  75. // Arbitary size. Doesn't really matter what's returned here.
  76. return new Dimension(10, 10);
  77. }
  78. }
  79. return size;
  80. }
  81. /**
  82. * Sets the number of the page to be displayed and refreshes the display.
  83. * @param pg the page number
  84. */
  85. public void setPage(int pg) {
  86. if (page != pg) {
  87. page = pg;
  88. imageRef = null;
  89. repaint();
  90. }
  91. }
  92. /**
  93. * Gets the image data and paints it on screen. Will make
  94. * calls to getPageImage as required.
  95. * @param graphics a graphics context
  96. * @see javax.swing.JComponent#paintComponent(Graphics)
  97. * @see org.apache.fop.render.java2d.Java2DRenderer#getPageImage(int)
  98. */
  99. public synchronized void paintComponent(Graphics graphics) {
  100. try {
  101. if (isOpaque()) { //paint background
  102. graphics.setColor(getBackground());
  103. graphics.fillRect(0, 0, getWidth(), getHeight());
  104. }
  105. super.paintComponent(graphics);
  106. BufferedImage image = null;
  107. if (imageRef == null || imageRef.get() == null) {
  108. image = renderer.getPageImage(page);
  109. imageRef = new SoftReference(image);
  110. } else {
  111. image = (BufferedImage)imageRef.get();
  112. }
  113. if (image != null) {
  114. int x = (getWidth() - image.getWidth()) / 2;
  115. int y = (getHeight() - image.getHeight()) / 2;
  116. graphics.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
  117. }
  118. } catch (FOPException fopEx) {
  119. fopEx.printStackTrace();
  120. }
  121. }
  122. }