1 /*
2 * Image/J Plugins
3 * Copyright (C) 2002 Jarek Sacha
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Latest release available at http://sourceforge.net/projects/ij-plugins/
20 */
21 package net.sf.ij.jaiio;
22
23 import java.awt;
24 import java.awt.image.BufferedImage;
25 import java.beans;
26 import java.io;
27 import javax.swing;
28 import net.sf.ij.swing.IconCanvas;
29
30 /**
31 * A utility for JAIFIleChooser that displays preview image, image file size,
32 * and image dimensions.
33 *
34 * @author Jarek Sacha
35 * @created January 9, 2001
36 * @version $Revision: 1.4 $
37 */
38 public class JAIFilePreviewer extends JPanel
39 implements PropertyChangeListener {
40
41 // final static String FILE_SIZE_PREFIX = "File Size: ";
42 final static String FILE_SIZE_PREFIX = "";
43 final static long SIZE_KB = 1024;
44 final static long SIZE_MB = SIZE_KB * 1024;
45 final static long SIZE_GB = SIZE_MB * 1024;
46
47 /** Description of the Field */
48 protected File file;
49 /** Description of the Field */
50 protected int iconSizeX = 200;
51 /** Description of the Field */
52 protected int iconSizeY = 150;
53
54 private JPanel infoPanel = new JPanel();
55 private GridBagLayout gridBagLayout1 = new GridBagLayout();
56 private JLabel fileSizeLabel = new JLabel();
57 private BorderLayout borderLayout1 = new BorderLayout();
58 private JLabel ImageIconLabel = new JLabel();
59
60
61 /** Constructor for the FilePreviewer object */
62 public JAIFilePreviewer() {
63 try {
64 jbInit();
65 }
66 catch (Exception e) {
67 e.printStackTrace();
68 }
69 }
70
71
72 /**
73 * Creates new FilePreviewer
74 *
75 * @param fc File chooser that this object is associated with.
76 */
77 public JAIFilePreviewer(JFileChooser fc) {
78 try {
79 jbInit();
80 }
81 catch (Exception e) {
82 e.printStackTrace();
83 }
84 fc.addPropertyChangeListener(this);
85 }
86
87
88 /**
89 * Updates image preview when received
90 * JFileChooser.SELECTED_FILE_CHANGED_PROPERTY event. This method should not
91 * be called directly.
92 *
93 * @param e Event.
94 */
95 public void propertyChange(PropertyChangeEvent e) {
96 String prop = e.getPropertyName();
97 if (prop == JFileChooser.SELECTED_FILE_CHANGED_PROPERTY) {
98 file = (File) e.getNewValue();
99 if (isShowing()) {
100 loadImage();
101 repaint();
102 }
103 }
104 }
105
106
107 /*
108 *
109 */
110 private String getFileSizeString(long fileSize) {
111 String fileSizeString = null;
112 if (fileSize < SIZE_KB) {
113 fileSizeString = FILE_SIZE_PREFIX + fileSize;
114 }
115 else if (fileSize < SIZE_MB) {
116 fileSizeString = FILE_SIZE_PREFIX +
117 (int) ((double) fileSize / SIZE_KB + 0.5) + "KB";
118 }
119 else if (fileSize < SIZE_GB) {
120 fileSizeString = FILE_SIZE_PREFIX +
121 (int) ((double) fileSize / SIZE_MB + 0.5) + "MB";
122 }
123 else {
124 fileSizeString = FILE_SIZE_PREFIX +
125 (int) ((double) fileSize / SIZE_GB + 0.5) + "GB";
126 }
127
128 return fileSizeString;
129 }
130
131
132 /**
133 * Load first image in the file.
134 *
135 * @return image info.
136 */
137 private JAIReader.ImageInfo loadImage() {
138 if (file == null || file.isDirectory()) {
139 ImageIconLabel.setIcon(null);
140 fileSizeLabel.setText(" ");
141 return null;
142 }
143
144 try {
145
146 JAIReader.ImageInfo imageInfo = JAIReader.readFirstImageAndInfo(file);
147 Image image = imageInfo.previewImage;
148
149 // Set image size label
150 StringBuffer label = new StringBuffer(getFileSizeString(file.length()));
151 if (image != null) {
152 int w = image.getWidth(null);
153 int h = image.getHeight(null);
154 if (w > 0 && h > 0) {
155 label.append(" [" + w + "x" + h);
156 if (imageInfo.numberOfPages > 1) {
157 label.append("x" + imageInfo.numberOfPages + "]");
158 }
159 else {
160 label.append("]");
161 }
162 }
163 }
164 fileSizeLabel.setText(label.toString());
165
166 int xSizeBuffered = image.getWidth(null);
167 int ySizeBuffered = image.getHeight(null);
168 if (xSizeBuffered > iconSizeX || ySizeBuffered > iconSizeY) {
169 // Replace image by its scaled version
170 double scaleX = (double) iconSizeX / xSizeBuffered;
171 double scaleY = (double) iconSizeY / ySizeBuffered;
172 Image scaledImage = null;
173 if (scaleX < scaleY) {
174 image = image.getScaledInstance(iconSizeX, -1, Image.SCALE_DEFAULT);
175 }
176 else {
177 image = image.getScaledInstance(-1, iconSizeY, Image.SCALE_DEFAULT);
178 }
179 }
180
181 ImageIcon imageIcon = new ImageIcon(image);
182 ImageIconLabel.setIcon(imageIcon);
183
184 return imageInfo;
185 }
186 catch (Throwable t) {
187 ImageIconLabel.setIcon(null);
188 return null;
189 }
190 }
191
192
193 /*
194 *
195 */
196 private void jbInit() throws Exception {
197 this.setLayout(gridBagLayout1);
198 fileSizeLabel.setHorizontalAlignment(SwingConstants.CENTER);
199 fileSizeLabel.setHorizontalTextPosition(SwingConstants.CENTER);
200 fileSizeLabel.setText(" ");
201 infoPanel.setLayout(borderLayout1);
202 ImageIconLabel.setPreferredSize(new Dimension(200, 150));
203 ImageIconLabel.setHorizontalAlignment(SwingConstants.CENTER);
204 this.add(infoPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0
205 , GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0), 0, 0));
206 infoPanel.add(fileSizeLabel, BorderLayout.CENTER);
207 this.add(ImageIconLabel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
208 , GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
209 }
210 }
211