1 /*
2 * JDiskCatalog
3 *
4 * Copyright 2007 Przemek Więch
5 *
6 * This file is part of JDiskCatalog.
7 *
8 * JDiskCatalog is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 package net.sf.jdiskcatalog.view;
24
25 import java.awt.Graphics;
26 import java.awt.Image;
27 import java.awt.Rectangle;
28 import java.awt.Shape;
29
30 import javax.swing.Icon;
31 import javax.swing.text.Element;
32 import javax.swing.text.View;
33
34 /**
35 * View of an image loaded from memory.
36 *
37 * @author Przemek Wiech <pwiech@losthive.org>
38 */
39 public class ImageView extends javax.swing.text.html.ImageView {
40
41 /**
42 * The loaded image.
43 */
44 private Image image;
45
46 /**
47 * Image width.
48 */
49 private int width;
50
51 /**
52 * Image height.
53 */
54 private int height;
55
56 /**
57 * Creates a new view that represents an IMG element.
58 *
59 * @param image the image to display
60 */
61 public ImageView(Element elem, Image image) {
62 super(elem);
63 this.image = image;
64 if (image != null)
65 {
66 width = image.getWidth(null);
67 height = image.getHeight(null);
68 }
69 else
70 {
71 Icon icon = getNoImageIcon();
72 if (icon != null) {
73 width = getNoImageIcon().getIconWidth();
74 height = getNoImageIcon().getIconHeight();
75 }
76 }
77 }
78
79 /**
80 * Paints the View.
81 *
82 * @param g the rendering surface to use
83 * @param a the allocated region to render into
84 */
85 @Override
86 public void paint(Graphics g, Shape a) {
87
88 Rectangle rect = (a instanceof Rectangle) ? (Rectangle) a : a.getBounds();
89 Rectangle clip = g.getClipBounds();
90
91 if (clip != null)
92 g.clipRect(rect.x, rect.y, rect.width, rect.height);
93
94 if (image != null)
95 g.drawImage(image, rect.x, rect.y, width, height, null);
96 else {
97 Icon icon = getNoImageIcon();
98 if (icon != null)
99 icon.paintIcon(getContainer(), g, rect.x, rect.y);
100 }
101
102 if (clip != null)
103 g.setClip(clip.x, clip.y, clip.width, clip.height);
104 }
105
106 /**
107 * Determines the preferred span for this view along an axis.
108 */
109 @Override
110 public float getPreferredSpan(int axis) {
111 if (axis == View.X_AXIS)
112 return width;
113 return height;
114 }
115
116 /**
117 * Does nothing. Has to be here though.
118 */
119 @Override
120 public void setSize(float width, float height) {
121 }
122 }