1 package tim.prune.load;
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
6 import java.util.ArrayList;
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.JOptionPane;
17 import javax.swing.JPanel;
18 import javax.swing.JProgressBar;
21 import tim.prune.I18nManager;
22 import tim.prune.data.Altitude;
23 import tim.prune.data.DataPoint;
24 import tim.prune.data.Latitude;
25 import tim.prune.data.Longitude;
26 import tim.prune.data.Photo;
27 import tim.prune.data.PhotoStatus;
28 import tim.prune.data.Timestamp;
29 import tim.prune.drew.jpeg.ExifReader;
30 import tim.prune.drew.jpeg.JpegData;
31 import tim.prune.drew.jpeg.JpegException;
32 import tim.prune.drew.jpeg.Rational;
35 * Class to manage the loading of Jpegs and dealing with the GPS data from them
37 public class JpegLoader implements Runnable
39 private App _app = null;
40 private JFrame _parentFrame = null;
41 private JFileChooser _fileChooser = null;
42 private JCheckBox _subdirCheckbox = null;
43 private JCheckBox _noExifCheckbox = null;
44 private JDialog _progressDialog = null;
45 private JProgressBar _progressBar = null;
46 private int[] _fileCounts = null;
47 private boolean _cancelled = false;
48 private ArrayList _photos = null;
53 * @param inApp Application object to inform of photo load
54 * @param inParentFrame parent frame to reference for dialogs
56 public JpegLoader(App inApp, JFrame inParentFrame)
59 _parentFrame = inParentFrame;
64 * Select an input file and open the GUI frame
65 * to select load options
67 public void openFile()
69 if (_fileChooser == null)
71 _fileChooser = new JFileChooser();
72 _fileChooser.setMultiSelectionEnabled(true);
73 _fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
74 _fileChooser.setDialogTitle(I18nManager.getText("menu.file.addphotos"));
75 _subdirCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.subdirectories"));
76 _subdirCheckbox.setSelected(true);
77 _noExifCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.loadjpegswithoutcoords"));
78 _noExifCheckbox.setSelected(true);
79 JPanel panel = new JPanel();
80 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
81 panel.add(_subdirCheckbox);
82 panel.add(_noExifCheckbox);
83 _fileChooser.setAccessory(panel);
85 if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
87 // Bring up dialog before starting
89 new Thread(this).start();
95 * Show the main dialog
97 private void showDialog()
99 _progressDialog = new JDialog(_parentFrame, I18nManager.getText("dialog.jpegload.progress.title"));
100 _progressDialog.setLocationRelativeTo(_parentFrame);
101 _progressBar = new JProgressBar(0, 100);
102 _progressBar.setValue(0);
103 _progressBar.setStringPainted(true);
104 _progressBar.setString("");
105 JPanel panel = new JPanel();
106 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
107 panel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
108 panel.add(new JLabel(I18nManager.getText("dialog.jpegload.progress")));
109 panel.add(_progressBar);
110 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
111 cancelButton.addActionListener(new ActionListener() {
112 public void actionPerformed(ActionEvent e)
117 panel.add(cancelButton);
118 _progressDialog.getContentPane().add(panel);
119 _progressDialog.pack();
120 _progressDialog.show();
125 * Run method for performing tasks in separate thread
129 // Initialise arrays, errors, summaries
130 _fileCounts = new int[4]; // files, jpegs, exifs, gps
131 _photos = new ArrayList();
132 File[] files = _fileChooser.getSelectedFiles();
133 // Loop recursively over selected files/directories to count files
134 int numFiles = countFileList(files, true, _subdirCheckbox.isSelected());
135 // Set up the progress bar for this number of files
136 _progressBar.setMaximum(numFiles);
137 _progressBar.setValue(0);
140 // Process the files recursively and build lists of photos
141 processFileList(files, true, _subdirCheckbox.isSelected());
142 _progressDialog.hide();
143 if (_cancelled) {return;}
145 //System.out.println("Finished - counts are: " + _fileCounts[0] + ", " + _fileCounts[1]
146 // + ", " + _fileCounts[2] + ", " + _fileCounts[3]);
147 if (_fileCounts[0] == 0)
149 // No files found at all
150 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.nofilesfound"),
151 I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
153 else if (_fileCounts[1] == 0)
156 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.nojpegsfound"),
157 I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
159 else if (!_noExifCheckbox.isSelected() && _fileCounts[2] == 0)
161 // Need coordinates but no exif found
162 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.noexiffound"),
163 I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
165 else if (!_noExifCheckbox.isSelected() && _fileCounts[3] == 0)
167 // Need coordinates but no gps information found
168 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.nogpsfound"),
169 I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
173 // Found some photos to load
174 // TODO: Load jpeg information into dialog for confirmation?
175 // Pass information back to app
176 _app.informPhotosLoaded(_photos);
182 * Process a list of files and/or directories
183 * @param inFiles array of file/directories
184 * @param inFirstDir true if first directory
185 * @param inDescend true to descend to subdirectories
187 private void processFileList(File[] inFiles, boolean inFirstDir, boolean inDescend)
191 // Loop over elements in array
192 for (int i=0; i<inFiles.length; i++)
194 File file = inFiles[i];
195 if (file.exists() && file.canRead())
197 // Check whether it's a file or a directory
202 else if (file.isDirectory() && (inFirstDir || inDescend))
204 // Always process first directory,
205 // only process subdirectories if checkbox selected
206 File[] files = file.listFiles();
207 processFileList(files, false, inDescend);
212 // file doesn't exist or isn't readable - ignore error
214 // check for cancel button pressed
215 if (_cancelled) break;
222 * Process the given file, by attempting to extract its tags
223 * @param inFile file object to read
225 private void processFile(File inFile)
227 // Update progress bar
228 _fileCounts[0]++; // file found
229 _progressBar.setValue(_fileCounts[0]);
230 _progressBar.setString("" + _fileCounts[0] + " / " + _progressBar.getMaximum());
231 _progressBar.repaint();
233 // Check whether filename corresponds with accepted filenames
234 if (!acceptPhotoFilename(inFile.getName())) {return;}
236 // Create Photo object
237 Photo photo = new Photo(inFile);
238 // Try to get information out of exif
241 JpegData jpegData = new ExifReader(inFile).extract();
242 _fileCounts[1]++; // jpeg found (no exception thrown)
243 if (jpegData.getExifDataPresent())
244 {_fileCounts[2]++;} // exif found
245 if (jpegData.isValid())
247 if (jpegData.getDatestamp() != null && jpegData.getTimestamp() != null)
249 photo.setTimestamp(createTimestamp(jpegData.getDatestamp(), jpegData.getTimestamp()));
251 // Make DataPoint and attach to Photo
252 DataPoint point = createDataPoint(jpegData);
253 point.setPhoto(photo);
254 photo.setDataPoint(point);
255 photo.setOriginalStatus(PhotoStatus.TAGGED);
259 catch (JpegException jpe) { // don't list errors, just count them
261 // Use file timestamp if exif timestamp isn't available
262 if (photo.getTimestamp() == null)
264 photo.setTimestamp(new Timestamp(inFile.lastModified()));
265 //System.out.println("No exif, using timestamp from file: " + inFile.lastModified() + " -> " + photo.getTimestamp().getText());
269 //System.out.println("timestamp from file = " + photo.getTimestamp().getText());
271 // Add the photo if it's got a point or if pointless photos should be added
272 if (photo.getDataPoint() != null || _noExifCheckbox.isSelected())
280 * Recursively count the selected Files so we can draw a progress bar
281 * @param inFiles file list
282 * @param inFirstDir true if first directory
283 * @param inDescend true to descend to subdirectories
284 * @return count of the files selected
286 private int countFileList(File[] inFiles, boolean inFirstDir, boolean inDescend)
291 // Loop over elements in array
292 for (int i=0; i<inFiles.length; i++)
294 File file = inFiles[i];
295 if (file.exists() && file.canRead())
297 // Check whether it's a file or a directory
302 else if (file.isDirectory() && (inFirstDir || inDescend))
304 fileCount += countFileList(file.listFiles(), false, inDescend);
314 * Create a DataPoint object from the given jpeg data
315 * @param inData Jpeg data including coordinates
316 * @return DataPoint object for Track
318 private static DataPoint createDataPoint(JpegData inData)
320 // Create model objects from jpeg data
321 double latval = getCoordinateDoubleValue(inData.getLatitude(),
322 inData.getLatitudeRef() == 'N' || inData.getLatitudeRef() == 'n');
323 Latitude latitude = new Latitude(latval, Latitude.FORMAT_DEG_MIN_SEC);
324 double lonval = getCoordinateDoubleValue(inData.getLongitude(),
325 inData.getLongitudeRef() == 'E' || inData.getLongitudeRef() == 'e');
326 Longitude longitude = new Longitude(lonval, Longitude.FORMAT_DEG_MIN_SEC);
327 Altitude altitude = null;
328 if (inData.getAltitude() != null)
330 altitude = new Altitude(inData.getAltitude().intValue(), Altitude.FORMAT_METRES);
332 return new DataPoint(latitude, longitude, altitude);
337 * Convert an array of 3 Rational numbers into a double coordinate value
338 * @param inRationals array of three Rational objects
339 * @param isPositive true for positive hemisphere, for positive double value
340 * @return double value of coordinate, either positive or negative
342 private static double getCoordinateDoubleValue(Rational[] inRationals, boolean isPositive)
344 if (inRationals == null || inRationals.length != 3) return 0.0;
345 double value = inRationals[0].doubleValue() // degrees
346 + inRationals[1].doubleValue() / 60.0 // minutes
347 + inRationals[2].doubleValue() / 60.0 / 60.0; // seconds
348 // make sure it's the correct sign
349 value = Math.abs(value);
350 if (!isPositive) value = -value;
356 * Use the given Rational values to create a timestamp
357 * @param inDate rationals describing date
358 * @param inTime rationals describing time
359 * @return Timestamp object corresponding to inputs
361 private static Timestamp createTimestamp(Rational[] inDate, Rational[] inTime)
363 //System.out.println("Making timestamp for date (" + inDate[0].toString() + "," + inDate[1].toString() + "," + inDate[2].toString() + ") and time ("
364 // + inTime[0].toString() + "," + inTime[1].toString() + "," + inTime[2].toString() + ")");
365 return new Timestamp(inDate[0].intValue(), inDate[1].intValue(), inDate[2].intValue(),
366 inTime[0].intValue(), inTime[1].intValue(), inTime[2].intValue());
371 * Check whether to accept the given filename
372 * @param inName name of file
373 * @return true if accepted, false otherwise
375 private static boolean acceptPhotoFilename(String inName)
377 if (inName != null && inName.length() > 4)
379 // Check for three-character file extensions jpg and jpe
380 String lastFour = inName.substring(inName.length() - 4).toLowerCase();
381 if (lastFour.equals(".jpg") || lastFour.equals(".jpe"))
385 // If not found, check for file extension jpeg
386 if (inName.length() > 5)
388 String lastFive = inName.substring(inName.length() - 5).toLowerCase();
389 if (lastFive.equals(".jpeg"))
395 // Not matched so don't accept