1 /***
2 * Created on May 27, 2005, Copyright UC Regents
3 */
4 package org.telscenter.pas.steps;
5
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8
9 import java.awt.Component;
10 import java.awt.Container;
11 import java.awt.Dimension;
12 import java.awt.FlowLayout;
13 import java.awt.Image;
14 import java.awt.MediaTracker;
15 import java.awt.Toolkit;
16 import java.awt.datatransfer.DataFlavor;
17 import java.awt.datatransfer.Transferable;
18 import java.awt.datatransfer.UnsupportedFlavorException;
19 import java.io.File;
20 import java.io.IOException;
21 import java.net.MalformedURLException;
22 import java.net.URL;
23 import java.net.URLConnection;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import javax.swing.ImageIcon;
28 import javax.swing.JComponent;
29 import javax.swing.JLabel;
30 import javax.swing.JPanel;
31 import javax.swing.TransferHandler;
32
33 import org.telscenter.pas.beans.PasStep;
34
35
36 /***
37 * @author turadg
38 */
39 public class ImageCollector extends PasStep {
40 /***
41 * Logger for this class
42 */
43 private static final Logger logger = Logger.getLogger(ImageCollector.class
44 .getName());
45
46 JPanel panel;
47
48 static final int THUMBNAIL_MAX = 128;
49
50 public Component getComponent() {
51 if (panel != null)
52 return panel;
53
54 panel = new JPanel();
55 panel.setTransferHandler(new ImageFileTransferHandler());
56 panel.setLayout(new FlowLayout());
57
58 return panel;
59 }
60
61 Image thumbnailOf(Image original) {
62
63 MediaTracker mediaTracker = new MediaTracker(new Container());
64 mediaTracker.addImage(original, 0);
65 try {
66 mediaTracker.waitForID(0);
67 } catch (InterruptedException e) {
68
69 logger.severe("Image - : exception: " + e);
70 }
71
72 int originalWidth = original.getWidth(new Container());
73 int originalHeight = original.getHeight(new Container());
74
75 if (originalWidth <= THUMBNAIL_MAX && originalHeight <= THUMBNAIL_MAX) {
76
77 return original;
78 }
79
80
81 int thumbWidth;
82 int thumbHeight;
83
84 if (originalWidth > originalHeight) {
85 thumbWidth = THUMBNAIL_MAX;
86 thumbHeight = (int) (((float) thumbWidth / (float) originalWidth) * originalHeight);
87 } else {
88 thumbHeight = THUMBNAIL_MAX;
89 thumbWidth = (int) (((float) thumbHeight / (float) originalHeight) * originalWidth);
90 }
91
92 Image thumbImage = original.getScaledInstance(thumbWidth, thumbHeight,
93 Image.SCALE_SMOOTH);
94
95 return thumbImage;
96 }
97
98 void addImage(URL url) throws IOException {
99 if (logger.isLoggable(Level.CONFIG)) {
100 logger.config("adding image from url " + url);
101 }
102 URLConnection conn = url.openConnection();
103 String contentType = conn.getContentType();
104 if (contentType.startsWith("image/")) {
105 Image image = (Image) conn.getContent(new Class[] { Image.class });
106 Image thumbnail = thumbnailOf(image);
107 ImageIcon imageIcon = new ImageIcon(thumbnail);
108 imageIcon.setDescription(url.toString());
109 JLabel label = new JLabel(imageIcon);
110 label.setMaximumSize(new Dimension(THUMBNAIL_MAX, THUMBNAIL_MAX));
111 panel.add(label);
112 panel.validate();
113 }
114 }
115
116 /***
117 * derived from code at
118 * http://www.java2s.com/ExampleCode/Swing-JFC/DroppershowFileDropTargetfromDragnDrop.htm
119 *
120 * @author turadg
121 */
122 class ImageFileTransferHandler extends TransferHandler {
123
124 public boolean canImport(JComponent comp, DataFlavor[] flavors) {
125 boolean canImport = false;
126 for (int i = 0; i < flavors.length; i++) {
127 DataFlavor flavor = flavors[i];
128 if (flavor.equals(DataFlavor.javaFileListFlavor))
129 canImport = true;
130 if (flavor.equals(DataFlavor.stringFlavor))
131 canImport = true;
132 }
133 return canImport;
134 }
135
136 @SuppressWarnings("unchecked")
137 public boolean importData(JComponent comp, Transferable t) {
138 DataFlavor[] flavors = t.getTransferDataFlavors();
139 logger.info("Trying to import:" + t);
140 logger.info("... which has " + flavors.length + " flavors.");
141 for (int i = 0; i < flavors.length; i++) {
142 DataFlavor flavor = flavors[i];
143 try {
144 if (flavor.equals(DataFlavor.javaFileListFlavor)) {
145 logger.info("importData: FileListFlavor");
146
147 List l = (List) t
148 .getTransferData(DataFlavor.javaFileListFlavor);
149 Iterator iter = l.iterator();
150 while (iter.hasNext()) {
151 File file = (File) iter.next();
152 logger.info("GOT FILE: "
153 + file.getCanonicalPath());
154
155 addImage(file.toURL());
156 }
157 return true;
158 } else if (flavor.equals(DataFlavor.stringFlavor)) {
159 logger.info("importData: String Flavor");
160 String fileOrURL = (String) t.getTransferData(flavor);
161 logger.info("GOT STRING: " + fileOrURL);
162 try {
163 URL url = new URL(fileOrURL);
164 logger.info("Valid URL: " + url.toString());
165
166 addImage(url);
167 return true;
168 } catch (MalformedURLException ex) {
169 System.err.println("Not a valid URL");
170 return false;
171 }
172
173
174 } else {
175 logger.info("importData rejected: " + flavor);
176
177 }
178 } catch (IOException ex) {
179 logger.warning("IOError getting data: " + ex);
180 } catch (UnsupportedFlavorException e) {
181 logger.warning("Unsupported Flavor: " + e);
182 }
183 }
184
185
186 Toolkit.getDefaultToolkit().beep();
187 return false;
188 }
189 }
190
191 }