1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package wjhk.jupload2.gui.image;
21
22
23
24
25
26
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
47
48
49 final Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR);
50
51
52
53
54 File file;
55
56
57
58
59 JUploadImagePreview jUploadImagePreview;
60
61
62
63
64
65
66 LoadImageThread(JUploadImagePreview jUploadImagePreview, File file) {
67 this.file = file;
68 this.jUploadImagePreview = jUploadImagePreview;
69 }
70
71
72
73
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 this.jUploadImagePreview.uploadPolicy);
87 } catch (JUploadException e) {
88 this.jUploadImagePreview.uploadPolicy.displayErr(e);
89 }
90
91
92 PictureFileData.freeMemory("JUploadImagePreview.run()",
93 this.jUploadImagePreview.uploadPolicy);
94
95 if (thumbnail != null) {
96 this.jUploadImagePreview.setThumbnail(thumbnail);
97 }
98 this.jUploadImagePreview.jFileChooser.setCursor(null);
99 this.jUploadImagePreview.uploadPolicy.displayDebug(
100 "LoadImageThread.start (end)", 50);
101 }
102 }
103
104
105 public class JUploadImagePreview extends JComponent implements
106 PropertyChangeListener {
107
108
109 private static final long serialVersionUID = -6882108570945459638L;
110
111
112
113
114 UploadPolicy uploadPolicy;
115
116
117
118
119 JFileChooser jFileChooser = null;
120
121
122
123
124 ImageIcon thumbnail = null;
125
126
127
128
129
130
131 File file = null;
132
133
134
135
136
137 LoadImageThread loadImageThread = null;
138
139
140
141
142
143
144
145
146 public JUploadImagePreview(JFileChooser jFileChooser,
147 UploadPolicy uploadPolicy) {
148 this.jFileChooser = jFileChooser;
149 this.uploadPolicy = uploadPolicy;
150
151 setPreferredSize(new Dimension(200, 200));
152 jFileChooser.addPropertyChangeListener(this);
153 }
154
155
156
157
158
159
160
161
162 void setThumbnail(ImageIcon thumbnail) {
163 this.thumbnail = thumbnail;
164 repaint();
165 }
166
167
168
169
170
171
172 void setFile(File fileParam) {
173 if (fileParam != null && fileParam.isDirectory()) {
174 this.file = null;
175 } else {
176 this.file = fileParam;
177 }
178
179
180 this.thumbnail = null;
181 repaint();
182
183
184 if (this.loadImageThread != null && this.loadImageThread.isAlive()) {
185
186 this.loadImageThread.interrupt();
187 this.loadImageThread = null;
188 }
189
190
191 if (this.file != null) {
192 this.loadImageThread = new LoadImageThread(this, this.file);
193
194
195 this.loadImageThread.setPriority(Thread.MAX_PRIORITY);
196
197 this.loadImageThread.start();
198 repaint();
199 }
200 }
201
202
203
204
205
206
207
208 public void propertyChange(PropertyChangeEvent e) {
209 String prop = e.getPropertyName();
210
211 if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) {
212
213 setFile(null);
214 } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) {
215
216 setFile((File) e.getNewValue());
217 }
218 }
219
220
221
222
223
224 @Override
225 protected void paintComponent(Graphics g) {
226
227 if (this.thumbnail != null) {
228 int x = getWidth() / 2 - this.thumbnail.getIconWidth() / 2;
229 int y = getHeight() / 2 - this.thumbnail.getIconHeight() / 2;
230 if (y < 0) {
231 y = 0;
232 }
233 if (x < 5) {
234 x = 5;
235 }
236 this.thumbnail.paintIcon(this, g, x, y);
237 this.uploadPolicy.displayDebug(
238 "JUploadImagePreview.paintComponent, after paintIcon", 50);
239 }
240 }
241 }