1 package tim.prune.load;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
6 import java.util.TreeSet;
8 import javax.swing.BorderFactory;
9 import javax.swing.BoxLayout;
10 import javax.swing.JButton;
11 import javax.swing.JCheckBox;
12 import javax.swing.JDialog;
13 import javax.swing.JFileChooser;
14 import javax.swing.JFrame;
15 import javax.swing.JLabel;
16 import javax.swing.JPanel;
17 import javax.swing.JProgressBar;
20 import tim.prune.I18nManager;
21 import tim.prune.config.Config;
22 import tim.prune.data.Altitude;
23 import tim.prune.data.DataPoint;
24 import tim.prune.data.Field;
25 import tim.prune.data.LatLonRectangle;
26 import tim.prune.data.Latitude;
27 import tim.prune.data.Longitude;
28 import tim.prune.data.Photo;
29 import tim.prune.data.Timestamp;
30 import tim.prune.jpeg.ExifGateway;
31 import tim.prune.jpeg.JpegData;
34 * Class to manage the loading of Jpegs and dealing with the GPS data from them
36 public class JpegLoader implements Runnable
38 private App _app = null;
39 private JFrame _parentFrame = null;
40 private JFileChooser _fileChooser = null;
41 private GenericFileFilter _fileFilter = null;
42 private JCheckBox _subdirCheckbox = null;
43 private JCheckBox _noExifCheckbox = null;
44 private JCheckBox _outsideAreaCheckbox = null;
45 private JDialog _progressDialog = null;
46 private JProgressBar _progressBar = null;
47 private int[] _fileCounts = null;
48 private boolean _cancelled = false;
49 private LatLonRectangle _trackRectangle = null;
50 private TreeSet<Photo> _photos = null;
55 * @param inApp Application object to inform of photo load
56 * @param inParentFrame parent frame to reference for dialogs
58 public JpegLoader(App inApp, JFrame inParentFrame)
61 _parentFrame = inParentFrame;
62 String[] fileTypes = {"jpg", "jpe", "jpeg"};
63 _fileFilter = new GenericFileFilter("filetype.jpeg", fileTypes);
68 * Open the GUI to select options and start the load
69 * @param inRectangle track rectangle
71 public void openDialog(LatLonRectangle inRectangle)
73 // Create file chooser if necessary
74 if (_fileChooser == null)
76 _fileChooser = new JFileChooser();
77 _fileChooser.setMultiSelectionEnabled(true);
78 _fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
79 _fileChooser.setFileFilter(_fileFilter);
80 _fileChooser.setDialogTitle(I18nManager.getText("menu.file.addphotos"));
81 _subdirCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.subdirectories"));
82 _subdirCheckbox.setSelected(true);
83 _noExifCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.loadjpegswithoutcoords"));
84 _noExifCheckbox.setSelected(true);
85 _outsideAreaCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.loadjpegsoutsidearea"));
86 _outsideAreaCheckbox.setSelected(true);
87 JPanel panel = new JPanel();
88 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
89 panel.add(_subdirCheckbox);
90 panel.add(_noExifCheckbox);
91 panel.add(_outsideAreaCheckbox);
92 _fileChooser.setAccessory(panel);
93 // start from directory in config if already set by other operations
94 String configDir = Config.getConfigString(Config.KEY_PHOTO_DIR);
95 if (configDir == null) {configDir = Config.getConfigString(Config.KEY_TRACK_DIR);}
96 if (configDir != null) {_fileChooser.setCurrentDirectory(new File(configDir));}
98 // enable/disable track checkbox
99 _trackRectangle = inRectangle;
100 _outsideAreaCheckbox.setEnabled(_trackRectangle != null && !_trackRectangle.isEmpty());
101 // Show file dialog to choose file / directory(ies)
102 if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
104 // Bring up dialog before starting
105 if (_progressDialog == null) {
106 createProgressDialog();
108 // reset dialog and show it
109 _progressBar.setValue(0);
110 _progressBar.setString("");
111 _progressDialog.setVisible(true);
112 // start thread for processing
113 new Thread(this).start();
119 * Create the dialog to show the progress
121 private void createProgressDialog()
123 _progressDialog = new JDialog(_parentFrame, I18nManager.getText("dialog.jpegload.progress.title"));
124 _progressDialog.setLocationRelativeTo(_parentFrame);
125 _progressBar = new JProgressBar(0, 100);
126 _progressBar.setValue(0);
127 _progressBar.setStringPainted(true);
128 _progressBar.setString("");
129 JPanel panel = new JPanel();
130 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
131 panel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
132 panel.add(new JLabel(I18nManager.getText("dialog.jpegload.progress")));
133 panel.add(_progressBar);
134 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
135 cancelButton.addActionListener(new ActionListener() {
136 public void actionPerformed(ActionEvent e)
141 panel.add(cancelButton);
142 _progressDialog.getContentPane().add(panel);
143 _progressDialog.pack();
148 * Run method for performing tasks in separate thread
152 // Initialise arrays, errors, summaries
153 _fileCounts = new int[4]; // files, jpegs, exifs, gps
154 _photos = new TreeSet<Photo>(new PhotoSorter());
155 File[] files = _fileChooser.getSelectedFiles();
156 // Loop recursively over selected files/directories to count files
157 int numFiles = countFileList(files, true, _subdirCheckbox.isSelected());
158 // Set up the progress bar for this number of files
159 _progressBar.setMaximum(numFiles);
160 _progressBar.setValue(0);
163 // Process the files recursively and build lists of photos
164 processFileList(files, true, _subdirCheckbox.isSelected());
165 _progressDialog.setVisible(false);
166 _progressDialog.dispose(); // Sometimes dialog doesn't disappear without this dispose
167 if (_cancelled) {return;}
169 //System.out.println("Finished - counts are: " + _fileCounts[0] + ", " + _fileCounts[1]
170 // + ", " + _fileCounts[2] + ", " + _fileCounts[3]);
171 if (_fileCounts[0] == 0)
173 // No files found at all
174 _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nofilesfound");
176 else if (_fileCounts[1] == 0)
179 _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nojpegsfound");
181 else if (!_noExifCheckbox.isSelected() && _fileCounts[2] == 0)
183 // Need coordinates but no exif found
184 _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.noexiffound");
186 else if (!_noExifCheckbox.isSelected() && _fileCounts[3] == 0)
188 // Need coordinates but no gps information found
189 _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nogpsfound");
193 // Found some photos to load - pass information back to app
194 _app.informPhotosLoaded(_photos);
200 * Process a list of files and/or directories
201 * @param inFiles array of file/directories
202 * @param inFirstDir true if first directory
203 * @param inDescend true to descend to subdirectories
205 private void processFileList(File[] inFiles, boolean inFirstDir, boolean inDescend)
209 // Loop over elements in array
210 for (int i=0; i<inFiles.length && !_cancelled; i++)
212 File file = inFiles[i];
213 if (file.exists() && file.canRead())
215 // Check whether it's a file or a directory
220 else if (file.isDirectory() && (inFirstDir || inDescend))
222 // Always process first directory,
223 // only process subdirectories if checkbox selected
224 File[] files = file.listFiles();
225 processFileList(files, false, inDescend);
228 // if file doesn't exist or isn't readable - ignore
235 * Process the given file, by attempting to extract its tags
236 * @param inFile file object to read
238 private void processFile(File inFile)
240 // Update progress bar
241 _fileCounts[0]++; // file found
242 _progressBar.setValue(_fileCounts[0]);
243 _progressBar.setString("" + _fileCounts[0] + " / " + _progressBar.getMaximum());
244 _progressBar.repaint();
246 // Check whether filename corresponds with accepted filenames
247 if (!_fileFilter.acceptFilename(inFile.getName())) {return;}
248 // If it's a Jpeg, we can use ExifReader to get coords, otherwise we could try exiftool (if it's installed)
250 // Create Photo object
251 Photo photo = new Photo(inFile);
252 if (inFile.exists() && inFile.canRead()) {
253 _fileCounts[1]++; // jpeg found
255 // Try to get information out of exif
256 JpegData jpegData = ExifGateway.getJpegData(inFile);
257 Timestamp timestamp = null;
258 if (jpegData != null)
260 if (jpegData.getExifDataPresent())
261 {_fileCounts[2]++;} // exif found
262 if (jpegData.isGpsValid())
264 timestamp = createTimestamp(jpegData.getGpsDatestamp(), jpegData.getGpsTimestamp());
265 // Make DataPoint and attach to Photo
266 DataPoint point = createDataPoint(jpegData);
267 point.setPhoto(photo);
268 point.setSegmentStart(true);
269 photo.setDataPoint(point);
270 photo.setOriginalStatus(Photo.Status.TAGGED);
273 // Use exif timestamp if gps timestamp not available
274 if (timestamp == null && jpegData.getOriginalTimestamp() != null) {
275 timestamp = createTimestamp(jpegData.getOriginalTimestamp());
277 photo.setExifThumbnail(jpegData.getThumbnailImage());
278 // Also extract orientation tag for setting rotation state of photo
279 photo.setRotation(jpegData.getRequiredRotation());
281 // Use file timestamp if exif timestamp isn't available
282 if (timestamp == null) {
283 timestamp = new Timestamp(inFile.lastModified());
285 // Apply timestamp to photo and its point (if any)
286 photo.setTimestamp(timestamp);
287 if (photo.getDataPoint() != null) {
288 photo.getDataPoint().setFieldValue(Field.TIMESTAMP, timestamp.getText(Timestamp.FORMAT_ISO_8601), false);
290 // Check the criteria for adding the photo - check whether the photo has coordinates and if so if they're within the rectangle
291 if ( (photo.getDataPoint() != null || _noExifCheckbox.isSelected())
292 && (photo.getDataPoint() == null || !_outsideAreaCheckbox.isEnabled()
293 || _outsideAreaCheckbox.isSelected() || _trackRectangle.containsPoint(photo.getDataPoint())))
301 * Recursively count the selected Files so we can draw a progress bar
302 * @param inFiles file list
303 * @param inFirstDir true if first directory
304 * @param inDescend true to descend to subdirectories
305 * @return count of the files selected
307 private int countFileList(File[] inFiles, boolean inFirstDir, boolean inDescend)
312 // Loop over elements in array
313 for (int i=0; i<inFiles.length; i++)
315 File file = inFiles[i];
316 if (file.exists() && file.canRead())
318 // Store first directory in config for later
319 if (i == 0 && inFirstDir) {
320 File workingDir = file.isDirectory()?file:file.getParentFile();
321 Config.setConfigString(Config.KEY_PHOTO_DIR, workingDir.getAbsolutePath());
323 // Check whether it's a file or a directory
328 else if (file.isDirectory() && (inFirstDir || inDescend))
330 fileCount += countFileList(file.listFiles(), false, inDescend);
340 * Create a DataPoint object from the given jpeg data
341 * @param inData Jpeg data including coordinates
342 * @return DataPoint object for Track
344 private static DataPoint createDataPoint(JpegData inData)
346 // Create model objects from jpeg data
347 double latval = getCoordinateDoubleValue(inData.getLatitude(),
348 inData.getLatitudeRef() == 'N' || inData.getLatitudeRef() == 'n');
349 Latitude latitude = new Latitude(latval, Latitude.FORMAT_DEG_MIN_SEC);
350 double lonval = getCoordinateDoubleValue(inData.getLongitude(),
351 inData.getLongitudeRef() == 'E' || inData.getLongitudeRef() == 'e');
352 Longitude longitude = new Longitude(lonval, Longitude.FORMAT_DEG_MIN_SEC);
353 Altitude altitude = null;
354 if (inData.hasAltitude()) {
355 altitude = new Altitude(inData.getAltitude(), Altitude.Format.METRES);
357 return new DataPoint(latitude, longitude, altitude);
362 * Convert an array of 3 doubles (deg-min-sec) into a double coordinate value
363 * @param inValues array of three doubles for deg-min-sec
364 * @param isPositive true for positive hemisphere, for positive double value
365 * @return double value of coordinate, either positive or negative
367 private static double getCoordinateDoubleValue(double[] inValues, boolean isPositive)
369 if (inValues == null || inValues.length != 3) return 0.0;
370 double value = inValues[0] // degrees
371 + inValues[1] / 60.0 // minutes
372 + inValues[2] / 60.0 / 60.0; // seconds
373 // make sure it's the correct sign
374 value = Math.abs(value);
375 if (!isPositive) value = -value;
381 * Use the given int values to create a timestamp
382 * @param inDate ints describing date
383 * @param inTime ints describing time
384 * @return Timestamp object corresponding to inputs
386 private static Timestamp createTimestamp(int[] inDate, int[] inTime)
388 if (inDate == null || inTime == null || inDate.length != 3 || inTime.length != 3) {
391 return new Timestamp(inDate[0], inDate[1], inDate[2],
392 inTime[0], inTime[1], inTime[2]);
397 * Use the given String value to create a timestamp
398 * @param inStamp timestamp from exif
399 * @return Timestamp object corresponding to input
401 private static Timestamp createTimestamp(String inStamp)
403 Timestamp stamp = null;
406 stamp = new Timestamp(Integer.parseInt(inStamp.substring(0, 4)),
407 Integer.parseInt(inStamp.substring(5, 7)),
408 Integer.parseInt(inStamp.substring(8, 10)),
409 Integer.parseInt(inStamp.substring(11, 13)),
410 Integer.parseInt(inStamp.substring(14, 16)),
411 Integer.parseInt(inStamp.substring(17)));
413 catch (NumberFormatException nfe) {}