KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > applet > upload > ImagePreview


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-components/org/opencms/applet/upload/ImagePreview.java,v $
3  * Date : $Date: 2006/10/17 13:33:11 $
4  * Version: $Revision: 1.12 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.applet.upload;
33
34 import java.awt.Color JavaDoc;
35 import java.awt.Dimension JavaDoc;
36 import java.awt.Font JavaDoc;
37 import java.awt.Graphics JavaDoc;
38 import java.awt.Image JavaDoc;
39 import java.beans.PropertyChangeEvent JavaDoc;
40 import java.beans.PropertyChangeListener JavaDoc;
41 import java.io.BufferedReader JavaDoc;
42 import java.io.File JavaDoc;
43 import java.io.FileReader JavaDoc;
44 import java.io.IOException JavaDoc;
45
46 import javax.swing.ImageIcon JavaDoc;
47 import javax.swing.JComponent JavaDoc;
48 import javax.swing.JFileChooser JavaDoc;
49
50 /**
51  * Image preview for the file select box.<p>
52  *
53  * Based on the Java 1.4 example.<p>
54  *
55  * @author Michael Emmerich
56  *
57  * @version $Revision: 1.12 $
58  *
59  * @since 6.0.0
60  */

61 public class ImagePreview extends JComponent JavaDoc implements PropertyChangeListener JavaDoc {
62
63     private static final int C_MODE_EMPTY = 0;
64
65     private static final int C_MODE_IMAGE = 1;
66     private static final int C_MODE_TEXT = 2;
67     /** Serial version UID required for safe serialization. */
68     private static final long serialVersionUID = 4851280416316056303L;
69
70     private File JavaDoc m_file;
71     private Font JavaDoc m_font;
72     private String JavaDoc m_messageNoPreview;
73     private int m_mode;
74     private String JavaDoc[] m_text;
75     private ImageIcon JavaDoc m_thumbnail;
76
77     /**
78      * Constructor, creates a new ImagePreview.<p>
79      *
80      * @param fc The fileselector box
81      * @param messageNoPreview localized message for no preview
82      */

83     public ImagePreview(JFileChooser JavaDoc fc, String JavaDoc messageNoPreview) {
84
85         setPreferredSize(new Dimension JavaDoc(200, 100));
86         fc.addPropertyChangeListener(this);
87         m_font = new java.awt.Font JavaDoc(null, Font.PLAIN, 9);
88         m_messageNoPreview = messageNoPreview;
89         m_mode = C_MODE_EMPTY;
90     }
91
92     /**
93      * Loads the image for the preview.<p>
94      */

95     public void loadImage() {
96
97         if (m_file == null) {
98             m_thumbnail = null;
99             return;
100         }
101         // load the image
102
ImageIcon JavaDoc tmpIcon = new ImageIcon JavaDoc(m_file.getPath());
103         if (tmpIcon.getIconWidth() > 190) {
104             // scale it to the maximum width of 190 pixel if newscessary
105
m_thumbnail = new ImageIcon JavaDoc(tmpIcon.getImage().getScaledInstance(190, -1, Image.SCALE_DEFAULT));
106         } else { //no need to miniaturize
107
m_thumbnail = tmpIcon;
108         }
109     }
110
111     /**
112      * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
113      */

114     public void paintComponent(Graphics JavaDoc g) {
115
116         if (m_mode == C_MODE_IMAGE) {
117             // if we are in image mode, draw the image
118
if (m_thumbnail == null) {
119                 loadImage();
120             }
121             if (m_thumbnail != null) {
122                 int x = getWidth() / 2 - m_thumbnail.getIconWidth() / 2;
123                 int y = getHeight() / 2 - m_thumbnail.getIconHeight() / 2;
124
125                 if (y < 0) {
126                     y = 0;
127                 }
128
129                 if (x < 5) {
130                     x = 5;
131                 }
132                 m_thumbnail.paintIcon(this, g, x, y);
133             }
134         } else if (m_mode == C_MODE_TEXT) {
135             // if we are in text mode, draw the text preview
136
g.setColor(Color.white);
137             g.fillRect(10, 0, getWidth() - 1, getHeight() - 1);
138             g.setColor(Color.black);
139             g.drawRect(10, 0, getWidth() - 11, getHeight() - 1);
140             g.setFont(m_font);
141             for (int i = 0; i < 35; i++) {
142                 if (m_text[i] != null) {
143                     g.drawString(m_text[i], 13, (i + 1) * 11);
144                 }
145             }
146         } else {
147             g.setColor(Color.black);
148             g.setFont(m_font);
149             g.drawString(m_messageNoPreview, 30, getHeight() / 2);
150         }
151     }
152
153     /**
154      * Exchange the image if a new file is selected in the fileselector box.<p>
155      *
156      * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
157      */

158     public void propertyChange(PropertyChangeEvent JavaDoc e) {
159
160         boolean update = false;
161         String JavaDoc prop = e.getPropertyName();
162
163         if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
164             // if the directory changed, don't show an image
165
m_file = null;
166             m_text = null;
167             update = true;
168             m_mode = C_MODE_EMPTY;
169
170         } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
171             // if a file was selected, find out which one
172
m_file = (File JavaDoc)e.getNewValue();
173             m_mode = getMode();
174             update = true;
175         }
176
177         // update the preview accordingly
178
if (update) {
179             m_thumbnail = null;
180             if (isShowing()) {
181                 if (m_mode == (C_MODE_IMAGE)) {
182                     loadImage();
183                     repaint();
184                 } else if (m_mode == (C_MODE_TEXT)) {
185                     loadText();
186                     repaint();
187                 } else {
188                     repaint();
189                 }
190             }
191         }
192     }
193
194     /**
195      * Gets the preview draw mode, depending on the file extension.<p>
196      *
197      * @return preview draw mode
198      */

199     private int getMode() {
200
201         int mode = C_MODE_EMPTY;
202         String JavaDoc extension = FileUploadUtils.getExtension(m_file);
203         if (FileUploadUtils.isImageExtension(extension)) {
204             mode = C_MODE_IMAGE;
205         } else if (FileUploadUtils.isTextExtension(extension)) {
206             mode = C_MODE_TEXT;
207         }
208         return mode;
209     }
210
211     /**
212      * Reads the first 10 lines of e text file.<p>
213      */

214     private void loadText() {
215
216         m_text = new String JavaDoc[35];
217         BufferedReader JavaDoc fileStream = null;
218         try {
219             fileStream = new BufferedReader JavaDoc(new FileReader JavaDoc(m_file));
220             for (int i = 0; i < 35; i++) {
221                 m_text[i] = fileStream.readLine();
222             }
223         } catch (Exception JavaDoc e) {
224             // ignore
225
} finally {
226             try {
227                 if (fileStream != null) {
228                     fileStream.close();
229                 }
230             } catch (IOException JavaDoc e) {
231                 // ignore
232
}
233         }
234     }
235 }
Popular Tags