KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > awt > viewer > ImageProxyPanel


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
18 /* $Id: ImageProxyPanel.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.awt.viewer;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Graphics JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.image.BufferedImage JavaDoc;
26 import java.lang.ref.Reference JavaDoc;
27 import java.lang.ref.SoftReference JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29
30 import org.apache.fop.apps.FOPException;
31 import org.apache.fop.render.awt.AWTRenderer;
32
33 /**
34  * Panel used to display a single page of a document.
35  * This is basically a lazy-load display panel which
36  * gets the size of the image for layout purposes but
37  * doesn't get the actual image data until needed.
38  * The image data is then accessed via a soft reference,
39  * so it will be garbage collected when moving through
40  * large documents.
41  */

42 public class ImageProxyPanel extends JPanel JavaDoc {
43
44     /** The reference to the BufferedImage storing the page data */
45     private Reference JavaDoc imageRef;
46
47     /** The maximum and preferred size of the panel */
48     private Dimension JavaDoc size;
49
50     /** The renderer. Shared with PreviewPanel and PreviewDialog. */
51     private AWTRenderer renderer;
52
53     /** The page to be rendered. */
54     private int page;
55
56     /**
57      * Panel constructor. Doesn't allocate anything until needed.
58      * @param renderer the AWTRenderer instance to use for painting
59      * @param page initial page number to show
60      */

61     public ImageProxyPanel(AWTRenderer renderer, int page) {
62         this.renderer = renderer;
63         this.page = page;
64         // Allows single panel to appear behind page display.
65
// Important for textured L&Fs.
66
setOpaque(false);
67     }
68
69     /**
70      * @return the size of the page plus the border.
71      */

72     public Dimension JavaDoc getMinimumSize() {
73         return getPreferredSize();
74     }
75
76     /**
77      * @return the size of the page plus the border.
78      */

79     public Dimension JavaDoc getPreferredSize() {
80         if (size == null) {
81             try {
82                 Insets JavaDoc insets = getInsets();
83                 size = renderer.getPageImageSize(page);
84                 size = new Dimension JavaDoc(size.width + insets.left + insets.right,
85                                                          size.height + insets.top + insets.bottom);
86             } catch (FOPException fopEx) {
87                 // Arbitary size. Doesn't really matter what's returned here.
88
return new Dimension JavaDoc(10, 10);
89             }
90         }
91         return size;
92     }
93
94     /**
95      * Sets the number of the page to be displayed and refreshes the display.
96      * @param pg the page number
97      */

98     public void setPage(int pg) {
99         if (page != pg) {
100             page = pg;
101             imageRef = null;
102             repaint();
103         }
104     }
105
106     /**
107      * Gets the image data and paints it on screen. Will make
108      * calls to getPageImage as required.
109      * @param graphics
110      * @see javax.swing.JComponent#paintComponent(Graphics)
111      * @see org.apache.fop.render.java2d.Java2DRenderer#getPageImage(int)
112      */

113     public synchronized void paintComponent(Graphics JavaDoc graphics) {
114         try {
115             if (isOpaque()) { //paint background
116
graphics.setColor(getBackground());
117                 graphics.fillRect(0, 0, getWidth(), getHeight());
118             }
119
120             super.paintComponent(graphics);
121
122             BufferedImage JavaDoc image = null;
123             if (imageRef == null || imageRef.get() == null) {
124                 image = renderer.getPageImage(page);
125                 imageRef = new SoftReference JavaDoc(image);
126             } else {
127                 image = (BufferedImage JavaDoc)imageRef.get();
128             }
129
130             int x = (getWidth() - image.getWidth()) / 2;
131             int y = (getHeight() - image.getHeight()) / 2;
132             
133             graphics.drawImage(image, x, y, image.getWidth(), image.getHeight(), null);
134         } catch (FOPException fopEx) {
135             fopEx.printStackTrace();
136         }
137     }
138 }
139
Popular Tags