1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package wjhk.jupload2.policies;
23
24 import java.awt.Cursor;
25 import java.awt.GridLayout;
26 import java.awt.Image;
27 import java.awt.SystemColor;
28 import java.awt.event.ActionEvent;
29 import java.awt.event.ActionListener;
30 import java.awt.image.ImageObserver;
31 import java.io.File;
32
33 import javax.swing.BorderFactory;
34 import javax.swing.Icon;
35 import javax.swing.ImageIcon;
36 import javax.swing.JButton;
37 import javax.swing.JOptionPane;
38 import javax.swing.JPanel;
39
40 import wjhk.jupload2.context.JUploadContext;
41 import wjhk.jupload2.exception.JUploadException;
42 import wjhk.jupload2.exception.JUploadExceptionStopAddingFiles;
43 import wjhk.jupload2.exception.JUploadIOException;
44 import wjhk.jupload2.filedata.FileData;
45 import wjhk.jupload2.filedata.PictureFileData;
46 import wjhk.jupload2.filedata.helper.ImageFileConversionInfo;
47 import wjhk.jupload2.gui.JUploadFileChooser;
48 import wjhk.jupload2.gui.JUploadPanel;
49 import wjhk.jupload2.gui.image.JUploadImagePreview;
50 import wjhk.jupload2.gui.image.PictureDialog;
51 import wjhk.jupload2.gui.image.PicturePanel;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 public class PictureUploadPolicy extends DefaultUploadPolicy implements ActionListener, ImageObserver {
96
97
98
99
100
101
102
103
104
105
106
107 private boolean storeBufferedImage;
108
109
110
111
112
113
114
115 private String targetPictureFormat;
116
117
118
119
120
121
122 private boolean keepOrigExtension;
123
124
125
126
127 private ImageFileConversionInfo imageFileConversionInfo = new ImageFileConversionInfo("");
128
129
130
131
132
133
134 private boolean fileChooserImagePreview = UploadPolicy.DEFAULT_FILE_CHOOSER_IMAGE_PREVIEW;
135
136
137
138
139 private boolean highQualityPreview;
140
141
142
143
144
145
146
147
148 private int maxWidth = -1;
149
150
151
152
153
154
155
156
157 private int maxHeight = -1;
158
159
160
161
162
163
164 private float pictureCompressionQuality = UploadPolicy.DEFAULT_PICTURE_COMPRESSION_QUALITY;
165
166
167
168
169
170 private boolean pictureTransmitMetadata;
171
172
173
174
175 private int realMaxWidth = -1;
176
177
178
179
180 private int realMaxHeight = -1;
181
182
183
184
185 private JButton rotateLeftButton;
186
187
188
189
190 private JButton rotateRightButton;
191
192
193
194
195 private PicturePanel picturePanel;
196
197
198
199
200
201
202
203 public PictureUploadPolicy(JUploadContext juploadContext) throws JUploadException {
204 super(juploadContext);
205
206
207
208 setFileChooserImagePreview(juploadContext.getParameter(PROP_FILE_CHOOSER_IMAGE_PREVIEW,
209 DEFAULT_FILE_CHOOSER_IMAGE_PREVIEW));
210 setHighQualityPreview(juploadContext.getParameter(PROP_HIGH_QUALITY_PREVIEW, DEFAULT_HIGH_QUALITY_PREVIEW));
211 setMaxHeight(juploadContext.getParameter(PROP_MAX_HEIGHT, DEFAULT_MAX_HEIGHT));
212 setMaxWidth(juploadContext.getParameter(PROP_MAX_WIDTH, DEFAULT_MAX_WIDTH));
213 setPictureCompressionQuality(juploadContext.getParameter(PROP_PICTURE_COMPRESSION_QUALITY,
214 DEFAULT_PICTURE_COMPRESSION_QUALITY));
215 setPictureTransmitMetadata(juploadContext.getParameter(PROP_PICTURE_TRANSMIT_METADATA,
216 DEFAULT_PICTURE_TRANSMIT_METADATA));
217 setRealMaxHeight(juploadContext.getParameter(PROP_REAL_MAX_HEIGHT, DEFAULT_REAL_MAX_HEIGHT));
218 setRealMaxWidth(juploadContext.getParameter(PROP_REAL_MAX_WIDTH, DEFAULT_REAL_MAX_WIDTH));
219 setTargetPictureFormat(juploadContext.getParameter(PROP_TARGET_PICTURE_FORMAT, DEFAULT_TARGET_PICTURE_FORMAT));
220
221 setKeepOrigExtension(juploadContext.getParameter(PROP_KEEP_ORIG_EXTENSION, DEFAULT_KEEP_ORIG_EXTENSION));
222
223 displayDebug("[PictureUploadPolicy] end of constructor", 30);
224 }
225
226
227
228
229
230
231
232
233
234 @Override
235 public FileData createFileData(File file) throws JUploadExceptionStopAddingFiles {
236
237 FileData defaultFileData = super.createFileData(file);
238
239 if (defaultFileData == null) {
240
241 return null;
242 } else {
243
244 PictureFileData pfd = null;
245 try {
246 pfd = new PictureFileData(file, this);
247 } catch (JUploadIOException e) {
248 displayErr(e);
249 }
250
251
252 if (pfd != null && pfd.isPicture()) {
253 return pfd;
254 } else if (getAllowedFileExtensions() != null) {
255
256
257
258 return defaultFileData;
259 } else {
260
261
262 String msg = getLocalizedString("notAPicture", file.getName());
263
264
265 displayWarn(msg);
266 if (JOptionPane.showConfirmDialog(null, msg, "alert", JOptionPane.OK_CANCEL_OPTION,
267 JOptionPane.WARNING_MESSAGE) == JOptionPane.CANCEL_OPTION) {
268
269
270
271
272 throw new JUploadExceptionStopAddingFiles("Stopped by the user");
273 }
274 return null;
275 }
276 }
277 }
278
279
280
281
282
283
284
285
286
287
288 @Override
289 public JPanel createTopPanel(JButton browse, JButton remove, JButton removeAll, JUploadPanel jUploadPanel) {
290
291
292
293
294
295 this.rotateLeftButton = new JButton(getLocalizedString("buttonRotateLeft"));
296 this.rotateLeftButton.setIcon(new ImageIcon(getClass().getResource("/images/rotateLeft.gif")));
297 this.rotateLeftButton.addActionListener(this);
298 this.rotateLeftButton.addMouseListener(jUploadPanel.getMouseListener());
299 this.rotateLeftButton.setEnabled(false);
300
301 this.rotateRightButton = new JButton(getLocalizedString("buttonRotateRight"));
302 this.rotateRightButton.setIcon(new ImageIcon(getClass().getResource("/images/rotateRight.gif")));
303 this.rotateRightButton.addActionListener(this);
304 this.rotateRightButton.addMouseListener(jUploadPanel.getMouseListener());
305 this.rotateRightButton.setEnabled(false);
306
307
308 JPanel buttonPanel = new JPanel();
309 buttonPanel.setLayout(new GridLayout(5, 1, 5, 5));
310 buttonPanel.setBorder(BorderFactory.createEmptyBorder(5, 10, 5, 5));
311 buttonPanel.add(browse);
312 buttonPanel.add(this.rotateLeftButton);
313 buttonPanel.add(this.rotateRightButton);
314 buttonPanel.add(removeAll);
315 buttonPanel.add(remove);
316
317
318 JPanel pPanel = new JPanel();
319 pPanel.setLayout(new GridLayout(1, 1));
320 pPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 10));
321
322 this.picturePanel = new PicturePanel(true, this);
323 this.picturePanel.addMouseListener(jUploadPanel.getMouseListener());
324 pPanel.add(this.picturePanel);
325
326
327 setCursor(null);
328
329
330 JPanel topPanel = new JPanel();
331 topPanel.setLayout(new GridLayout(1, 2));
332 topPanel.add(buttonPanel);
333 topPanel.add(pPanel);
334
335 jUploadPanel.getJComponent().setBorder(BorderFactory.createLineBorder(SystemColor.controlDkShadow));
336
337 return topPanel;
338 }
339
340
341
342
343
344
345
346 public void actionPerformed(ActionEvent e) {
347 displayInfo("Action : " + e.getActionCommand());
348 if (e.getActionCommand() == this.rotateLeftButton.getActionCommand()) {
349 this.picturePanel.rotate(-1);
350 } else if (e.getActionCommand() == this.rotateRightButton.getActionCommand()) {
351 this.picturePanel.rotate(1);
352 }
353 }
354
355
356
357
358 @Override
359 public void onFileSelected(FileData fileData) {
360 if (fileData != null) {
361 displayDebug("File selected: " + fileData.getFileName(), 30);
362 }
363 if (this.picturePanel != null) {
364
365 if (fileData instanceof PictureFileData) {
366 Cursor previousCursor = setWaitCursor();
367 this.picturePanel.setPictureFile((PictureFileData) fileData, this.rotateLeftButton,
368 this.rotateRightButton);
369 setCursor(previousCursor);
370 } else {
371 this.picturePanel.setPictureFile(null, this.rotateLeftButton, this.rotateRightButton);
372 }
373 }
374 }
375
376
377
378
379
380
381
382 @Override
383 public void onFileDoubleClicked(FileData pictureFileData) {
384 if (pictureFileData == null) {
385
386 } else if (!(pictureFileData instanceof PictureFileData)) {
387 displayWarn("PictureUploadPolicy: received a non PictureFileData in onFileDoubleClicked");
388 } else {
389 new PictureDialog(null, (PictureFileData) pictureFileData, this);
390 }
391 }
392
393
394 @Override
395 public boolean beforeUpload() {
396
397
398
399 getContext().getUploadPanel().getFilePanel().clearSelection();
400 if (this.picturePanel != null) {
401 this.picturePanel.setPictureFile(null, this.rotateLeftButton, this.rotateRightButton);
402 }
403
404
405 return super.beforeUpload();
406 }
407
408
409
410
411
412
413
414
415
416
417
418
419 public boolean getFileChooserImagePreview() {
420 return this.fileChooserImagePreview;
421 }
422
423
424
425
426
427
428
429 public void setFileChooserImagePreview(boolean fileChooserImagePreview) {
430 this.fileChooserImagePreview = fileChooserImagePreview;
431 }
432
433
434 public boolean getHighQualityPreview() {
435 return this.highQualityPreview;
436 }
437
438
439 void setHighQualityPreview(boolean highQualityPreview) {
440 this.highQualityPreview = highQualityPreview;
441 }
442
443
444
445
446 public int getMaxHeight() {
447 return this.maxHeight;
448 }
449
450
451 void setMaxHeight(int maxHeight) {
452 if (maxHeight <= 0) {
453 this.maxHeight = Integer.MAX_VALUE;
454 displayWarn("[setMaxHeight] maxHeight switched from " + maxHeight + " to " + this.maxHeight);
455 } else {
456 this.maxHeight = maxHeight;
457 }
458 }
459
460
461
462
463 public int getMaxWidth() {
464 return this.maxWidth;
465 }
466
467
468 void setMaxWidth(int maxWidth) {
469 if (maxWidth <= 0) {
470 this.maxWidth = Integer.MAX_VALUE;
471 displayWarn("[setMaxWidth] maxWidth switched from " + maxWidth + " to " + this.maxWidth);
472 } else {
473 this.maxWidth = maxWidth;
474 }
475 }
476
477
478
479
480 public float getPictureCompressionQuality() {
481 return this.pictureCompressionQuality;
482 }
483
484
485
486
487
488 void setPictureCompressionQuality(float pictureCompressionQuality) {
489 this.pictureCompressionQuality = pictureCompressionQuality;
490 }
491
492
493
494
495 public boolean getPictureTransmitMetadata() {
496 return this.pictureTransmitMetadata;
497 }
498
499
500
501
502
503 void setPictureTransmitMetadata(boolean pictureTransmitMetadata) {
504 this.pictureTransmitMetadata = pictureTransmitMetadata;
505 }
506
507
508
509
510 public int getRealMaxHeight() {
511 return (this.realMaxHeight == Integer.MAX_VALUE) ? this.maxHeight : this.realMaxHeight;
512 }
513
514
515 void setRealMaxHeight(int realMaxHeight) {
516 this.realMaxHeight = realMaxHeight;
517 }
518
519
520
521
522 public int getRealMaxWidth() {
523 return (this.realMaxWidth == Integer.MAX_VALUE) ? this.maxWidth : this.realMaxWidth;
524 }
525
526
527 void setRealMaxWidth(int realMaxWidth) {
528 this.realMaxWidth = realMaxWidth;
529 }
530
531
532 public String getTargetPictureFormat() {
533 return this.targetPictureFormat;
534 }
535
536
537
538
539 public ImageFileConversionInfo getImageFileConversionInfo() {
540 return this.imageFileConversionInfo;
541 }
542
543
544
545
546
547
548
549 void setTargetPictureFormat(String targetPictureFormat) throws JUploadException {
550 this.targetPictureFormat = targetPictureFormat;
551 this.imageFileConversionInfo = new ImageFileConversionInfo(targetPictureFormat);
552 }
553
554
555
556
557
558
559
560
561 public boolean getKeepOrigExtension() {
562 return this.keepOrigExtension;
563 }
564
565
566
567
568
569 void setKeepOrigExtension(boolean keepOrigExtension) throws JUploadException {
570 this.keepOrigExtension = keepOrigExtension;
571 }
572
573
574
575
576
577
578
579
580
581 @Override
582 public void setProperty(String prop, String value) throws JUploadException {
583
584 if (prop.equals(PROP_FILE_CHOOSER_IMAGE_PREVIEW)) {
585 setFileChooserImagePreview(getContext().parseBoolean(value, getFileChooserImagePreview()));
586 } else if (prop.equals(PROP_HIGH_QUALITY_PREVIEW)) {
587 setHighQualityPreview(getContext().parseBoolean(value, this.highQualityPreview));
588 } else if (prop.equals(PROP_MAX_HEIGHT)) {
589 setMaxHeight(getContext().parseInt(value, this.maxHeight));
590 } else if (prop.equals(PROP_MAX_WIDTH)) {
591 setMaxWidth(getContext().parseInt(value, this.maxWidth));
592 } else if (prop.equals(PROP_PICTURE_COMPRESSION_QUALITY)) {
593 setPictureCompressionQuality(getContext().parseFloat(value, this.pictureCompressionQuality));
594 } else if (prop.equals(PROP_PICTURE_TRANSMIT_METADATA)) {
595 setPictureTransmitMetadata(getContext().parseBoolean(value, this.pictureTransmitMetadata));
596 } else if (prop.equals(PROP_REAL_MAX_HEIGHT)) {
597 setRealMaxHeight(getContext().parseInt(value, this.realMaxHeight));
598 } else if (prop.equals(PROP_REAL_MAX_WIDTH)) {
599 setRealMaxWidth(getContext().parseInt(value, this.realMaxWidth));
600 } else if (prop.equals(PROP_TARGET_PICTURE_FORMAT)) {
601 setTargetPictureFormat(value);
602 } else if (prop.equals(PROP_KEEP_ORIG_EXTENSION)) {
603 setKeepOrigExtension(getContext().parseBoolean(value, this.keepOrigExtension));
604 } else {
605
606 super.setProperty(prop, value);
607 }
608 }
609
610
611 @Override
612 public void displayParameterStatus() {
613 super.displayParameterStatus();
614
615 displayDebug("======= Parameters managed by PictureUploadPolicy", 30);
616 displayDebug(PROP_FILE_CHOOSER_IMAGE_PREVIEW + ": " + getFileChooserImagePreview(), 30);
617 displayDebug(PROP_HIGH_QUALITY_PREVIEW + " : " + this.highQualityPreview, 30);
618 displayDebug(PROP_PICTURE_COMPRESSION_QUALITY + " : " + getPictureCompressionQuality(), 30);
619 displayDebug(PROP_PICTURE_TRANSMIT_METADATA + " : " + getPictureTransmitMetadata(), 30);
620 displayDebug(PROP_MAX_WIDTH + " : " + this.maxWidth + ", " + PROP_MAX_HEIGHT + " : " + this.maxHeight, 30);
621 displayDebug(PROP_REAL_MAX_WIDTH + " : " + this.realMaxWidth + ", " + PROP_REAL_MAX_HEIGHT + " : "
622 + this.realMaxHeight, 30);
623 displayDebug(PROP_STORE_BUFFERED_IMAGE + " : " + this.storeBufferedImage, 30);
624 displayDebug(PROP_TARGET_PICTURE_FORMAT + " : " + this.targetPictureFormat, 30);
625 displayDebug("Format conversions : " + getImageFileConversionInfo(), 40);
626 displayDebug("", 30);
627 }
628
629
630
631
632
633
634 @Override
635 public Cursor setWaitCursor() {
636 Cursor previousCursor = super.setWaitCursor();
637 this.picturePanel.setCursor(null);
638 return previousCursor;
639 }
640
641
642
643
644
645
646 @Override
647 public Cursor setCursor(Cursor cursor) {
648 Cursor oldCursor = super.setCursor(null);
649 this.picturePanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
650 return oldCursor;
651 }
652
653
654
655
656
657
658 @Override
659 public JUploadFileChooser createFileChooser() {
660 JUploadFileChooser jufc = super.createFileChooser();
661 if (getFileChooserImagePreview()) {
662 jufc.setAccessory(new JUploadImagePreview(jufc, this));
663 }
664 return jufc;
665 }
666
667
668
669
670
671
672
673
674
675
676
677 @Override
678 public Icon fileViewGetIcon(File file) {
679 try {
680 return PictureFileData.getImageIcon(file, getFileChooserIconSize(), getFileChooserIconSize(), this);
681 } catch (JUploadException e) {
682 displayErr(e);
683 return null;
684 }
685 }
686
687
688
689
690 @Override
691 public String getUploadFilename(FileData fileData, int index) throws JUploadException {
692 String fileName = fileData.getFileName();
693 if (!this.keepOrigExtension) {
694 String targetFormatOrNull = this.imageFileConversionInfo.getTargetFormatOrNull(fileData.getFileExtension());
695 if (targetFormatOrNull != null) {
696
697 int endIndex = fileName.length() - fileData.getFileExtension().length();
698 StringBuilder newFilename = new StringBuilder(fileName.substring(0, endIndex));
699 newFilename.append(targetFormatOrNull);
700 fileName = newFilename.toString();
701 }
702 }
703
704 return getEncodedFilename(fileName);
705 }
706
707
708
709
710
711
712
713
714
715
716
717
718 public boolean imageUpdate(Image arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
719 return true;
720 }
721 }