1 | //
|
---|
2 | // $Id: JUploadPanelImpl.java 295 2007-06-27 08:43:25 +0000 (mer., 27 juin 2007)
|
---|
3 | // etienne_sf $
|
---|
4 | //
|
---|
5 | // jupload - A file upload applet.
|
---|
6 | // Copyright 2007 The JUpload Team
|
---|
7 | //
|
---|
8 | // Last modified: $Date: 2007-06-27 08:43:25 +0000 (mer., 27 juin 2007) $
|
---|
9 | //
|
---|
10 | // This program is free software; you can redistribute it and/or modify it under
|
---|
11 | // the terms of the GNU General Public License as published by the Free Software
|
---|
12 | // Foundation; either version 2 of the License, or (at your option) any later
|
---|
13 | // version. This program is distributed in the hope that it will be useful, but
|
---|
14 | // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
---|
15 | // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
---|
16 | // details. You should have received a copy of the GNU General Public License
|
---|
17 | // along with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
18 | // 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 |
|
---|
20 | package wjhk.jupload2.gui.image;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * This class contains the accessory that displays the image preview, when in
|
---|
24 | * picture mode.
|
---|
25 | *
|
---|
26 | * @see PictureUploadPolicy
|
---|
27 | */
|
---|
28 | import java.awt.Cursor;
|
---|
29 | import java.awt.Dimension;
|
---|
30 | import java.awt.Graphics;
|
---|
31 | import java.beans.PropertyChangeEvent;
|
---|
32 | import java.beans.PropertyChangeListener;
|
---|
33 | import java.io.File;
|
---|
34 |
|
---|
35 | import javax.swing.ImageIcon;
|
---|
36 | import javax.swing.JComponent;
|
---|
37 | import javax.swing.JFileChooser;
|
---|
38 |
|
---|
39 | import wjhk.jupload2.exception.JUploadException;
|
---|
40 | import wjhk.jupload2.filedata.PictureFileData;
|
---|
41 | import wjhk.jupload2.policies.UploadPolicy;
|
---|
42 |
|
---|
43 | class LoadImageThread extends Thread {
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * That cursor that will be used each time the user select a new file, when
|
---|
47 | * resizing the picture before displaying in the preview accessory.
|
---|
48 | */
|
---|
49 | final Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR);
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * The file that is to be loaded.
|
---|
53 | */
|
---|
54 | File file;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * The preview, where the resulting picture must be displayed.
|
---|
58 | */
|
---|
59 | JUploadImagePreview jUploadImagePreview;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Only constructor, with the file to be loaded.
|
---|
63 | *
|
---|
64 | * @param file The file to load, once the thread is started.
|
---|
65 | */
|
---|
66 | LoadImageThread(JUploadImagePreview jUploadImagePreview, File file) {
|
---|
67 | this.file = file;
|
---|
68 | this.jUploadImagePreview = jUploadImagePreview;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * The work itself: it allows the loading and resizing of the picture in a
|
---|
73 | * separate thread, to avoid blocking the user interface.
|
---|
74 | */
|
---|
75 | @Override
|
---|
76 | public void run() {
|
---|
77 |
|
---|
78 | this.jUploadImagePreview.uploadPolicy.displayDebug(
|
---|
79 | "LoadImageThread.start (start)", 50);
|
---|
80 | this.jUploadImagePreview.jFileChooser.setCursor(this.waitCursor);
|
---|
81 | ImageIcon thumbnail = null;
|
---|
82 | try {
|
---|
83 | thumbnail = PictureFileData.getImageIcon(this.file,
|
---|
84 | this.jUploadImagePreview.getWidth(),
|
---|
85 | this.jUploadImagePreview.getHeight());
|
---|
86 | } catch (JUploadException e) {
|
---|
87 | this.jUploadImagePreview.uploadPolicy.displayErr(e);
|
---|
88 | }
|
---|
89 |
|
---|
90 | // A try to minimize memory footprint
|
---|
91 | PictureFileData.freeMemory("JUploadImagePreview.run()",
|
---|
92 | this.jUploadImagePreview.uploadPolicy);
|
---|
93 |
|
---|
94 | if (thumbnail != null) {
|
---|
95 | this.jUploadImagePreview.setThumbnail(thumbnail);
|
---|
96 | this.jUploadImagePreview.jFileChooser.setCursor(null);
|
---|
97 | }
|
---|
98 | this.jUploadImagePreview.uploadPolicy.displayDebug(
|
---|
99 | "LoadImageThread.start (end)", 50);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** ImagePreview.java by FileChooserDemo2.java. */
|
---|
104 | public class JUploadImagePreview extends JComponent implements
|
---|
105 | PropertyChangeListener {
|
---|
106 |
|
---|
107 | /** A generated serialVersionUID, to avoid warning during compilation */
|
---|
108 | private static final long serialVersionUID = -6882108570945459638L;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * The current upload policy.
|
---|
112 | */
|
---|
113 | UploadPolicy uploadPolicy;
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Current file chooser, which owns this file preview.
|
---|
117 | */
|
---|
118 | JFileChooser jFileChooser = null;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * The picture, resized to the preview size.
|
---|
122 | */
|
---|
123 | ImageIcon thumbnail = null;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * The selected picture, which should contain the picture to display.
|
---|
127 | * Currently useless, as it is used only in the {@link #setFile(File)}
|
---|
128 | * method. It may be useful, in the future..
|
---|
129 | */
|
---|
130 | File file = null;
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * The current thread, that is loading the picture. A new thread is created,
|
---|
134 | * each time a new picture is to be loaded.
|
---|
135 | */
|
---|
136 | LoadImageThread loadImageThread = null;
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * The standard constructor for this class.
|
---|
140 | *
|
---|
141 | * @param jFileChooser The current file chooser, which will contain this
|
---|
142 | * acessory.
|
---|
143 | * @param uploadPolicy The current upload policy.
|
---|
144 | */
|
---|
145 | public JUploadImagePreview(JFileChooser jFileChooser,
|
---|
146 | UploadPolicy uploadPolicy) {
|
---|
147 | this.jFileChooser = jFileChooser;
|
---|
148 | this.uploadPolicy = uploadPolicy;
|
---|
149 |
|
---|
150 | setPreferredSize(new Dimension(200, 200));
|
---|
151 | jFileChooser.addPropertyChangeListener(this);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Changes the current picture to display. This method is called by
|
---|
156 | * {@link LoadImageThread#start()} method, when the resized picture has been
|
---|
157 | * calculated.
|
---|
158 | *
|
---|
159 | * @param thumbnail
|
---|
160 | */
|
---|
161 | void setThumbnail(ImageIcon thumbnail) {
|
---|
162 | this.thumbnail = thumbnail;
|
---|
163 | repaint();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Changes the current file: this erases the current displayed picture, then
|
---|
168 | * call the {@link LoadImageThread#start()} method. This generate the
|
---|
169 | * picture asynchroneously. Directories are ignored.
|
---|
170 | */
|
---|
171 | void setFile(File fileParam) {
|
---|
172 | if (fileParam != null && fileParam.isDirectory()) {
|
---|
173 | this.file = null;
|
---|
174 | } else {
|
---|
175 | this.file = fileParam;
|
---|
176 | }
|
---|
177 |
|
---|
178 | // First: clear the current picture.
|
---|
179 | this.thumbnail = null;
|
---|
180 | repaint();
|
---|
181 |
|
---|
182 | // If a thread is running, let's stop it.
|
---|
183 | if (this.loadImageThread != null && this.loadImageThread.isAlive()) {
|
---|
184 | // Let's forget this thread.
|
---|
185 | this.loadImageThread.interrupt();
|
---|
186 | this.loadImageThread = null;
|
---|
187 | }
|
---|
188 |
|
---|
189 | // Next: load aysnchronously the picture.
|
---|
190 | if (this.file != null) {
|
---|
191 | this.loadImageThread = new LoadImageThread(this, this.file);
|
---|
192 | // We want this thread to be executed before the icon loading
|
---|
193 | // threads.
|
---|
194 | this.loadImageThread.setPriority(Thread.MAX_PRIORITY);
|
---|
195 | // Let's start the thread, and exit: the applet is not blocked.
|
---|
196 | this.loadImageThread.start();
|
---|
197 | repaint();
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Hum, we're interested in these events: DIRECTORY_CHANGED_PROPERTY and
|
---|
203 | * SELECTED_FILE_CHANGED_PROPERTY.
|
---|
204 | *
|
---|
205 | * @param e
|
---|
206 | */
|
---|
207 | public void propertyChange(PropertyChangeEvent e) {
|
---|
208 | String prop = e.getPropertyName();
|
---|
209 |
|
---|
210 | if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
|
---|
211 | // The directory changed, don't show an image.
|
---|
212 | setFile(null);
|
---|
213 | } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
|
---|
214 | // If a file became selected, find out which one.
|
---|
215 | setFile((File) e.getNewValue());
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Actual display of the picture. We just have to center the thumbnail,
|
---|
221 | * here.
|
---|
222 | */
|
---|
223 | @Override
|
---|
224 | protected void paintComponent(Graphics g) {
|
---|
225 | // Do we have a picture to display ?
|
---|
226 | if (this.thumbnail != null) {
|
---|
227 | int x = getWidth() / 2 - this.thumbnail.getIconWidth() / 2;
|
---|
228 | int y = getHeight() / 2 - this.thumbnail.getIconHeight() / 2;
|
---|
229 | if (y < 0) {
|
---|
230 | y = 0;
|
---|
231 | }
|
---|
232 | if (x < 5) {
|
---|
233 | x = 5;
|
---|
234 | }
|
---|
235 | this.thumbnail.paintIcon(this, g, x, y);
|
---|
236 | this.uploadPolicy.displayDebug(
|
---|
237 | "JUploadImagePreview.paintComponent, after paintIcon", 50);
|
---|
238 | }
|
---|
239 | }
|
---|
240 | }
|
---|