]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/load/JpegLoader.java
Version 9, February 2010
[GpsPrune.git] / tim / prune / load / JpegLoader.java
index c506236f44eb3e3e3aa2a37ce6683cd382da85de..08b29fad02e6b32ec2cfc6bafa663869d76cbefa 100644 (file)
@@ -13,20 +13,18 @@ import javax.swing.JDialog;
 import javax.swing.JFileChooser;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
-import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JProgressBar;
 
 import tim.prune.App;
-import tim.prune.Config;
 import tim.prune.I18nManager;
+import tim.prune.config.Config;
 import tim.prune.data.Altitude;
 import tim.prune.data.DataPoint;
 import tim.prune.data.LatLonRectangle;
 import tim.prune.data.Latitude;
 import tim.prune.data.Longitude;
 import tim.prune.data.Photo;
-import tim.prune.data.PhotoStatus;
 import tim.prune.data.Timestamp;
 import tim.prune.drew.jpeg.ExifReader;
 import tim.prune.drew.jpeg.JpegData;
@@ -50,7 +48,7 @@ public class JpegLoader implements Runnable
        private int[] _fileCounts = null;
        private boolean _cancelled = false;
        private LatLonRectangle _trackRectangle = null;
-       private TreeSet _photos = null;
+       private TreeSet<Photo> _photos = null;
 
 
        /**
@@ -94,8 +92,9 @@ public class JpegLoader implements Runnable
                        panel.add(_outsideAreaCheckbox);
                        _fileChooser.setAccessory(panel);
                        // start from directory in config if already set by other operations
-                       File configDir = Config.getWorkingDirectory();
-                       if (configDir != null) {_fileChooser.setCurrentDirectory(configDir);}
+                       String configDir = Config.getConfigString(Config.KEY_PHOTO_DIR);
+                       if (configDir == null) {configDir = Config.getConfigString(Config.KEY_TRACK_DIR);}
+                       if (configDir != null) {_fileChooser.setCurrentDirectory(new File(configDir));}
                }
                // enable/disable track checkbox
                _trackRectangle = inRectangle;
@@ -104,16 +103,23 @@ public class JpegLoader implements Runnable
                if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
                {
                        // Bring up dialog before starting
-                       showDialog();
+                       if (_progressDialog == null) {
+                               createProgressDialog();
+                       }
+                       // reset dialog and show it
+                       _progressBar.setValue(0);
+                       _progressBar.setString("");
+                       _progressDialog.setVisible(true);
+                       // start thread for processing
                        new Thread(this).start();
                }
        }
 
 
        /**
-        * Show the main dialog
+        * Create the dialog to show the progress
         */
-       private void showDialog()
+       private void createProgressDialog()
        {
                _progressDialog = new JDialog(_parentFrame, I18nManager.getText("dialog.jpegload.progress.title"));
                _progressDialog.setLocationRelativeTo(_parentFrame);
@@ -136,7 +142,6 @@ public class JpegLoader implements Runnable
                panel.add(cancelButton);
                _progressDialog.getContentPane().add(panel);
                _progressDialog.pack();
-               _progressDialog.show();
        }
 
 
@@ -147,7 +152,7 @@ public class JpegLoader implements Runnable
        {
                // Initialise arrays, errors, summaries
                _fileCounts = new int[4]; // files, jpegs, exifs, gps
-               _photos = new TreeSet(new PhotoSorter());
+               _photos = new TreeSet<Photo>(new PhotoSorter());
                File[] files = _fileChooser.getSelectedFiles();
                // Loop recursively over selected files/directories to count files
                int numFiles = countFileList(files, true, _subdirCheckbox.isSelected());
@@ -158,7 +163,8 @@ public class JpegLoader implements Runnable
 
                // Process the files recursively and build lists of photos
                processFileList(files, true, _subdirCheckbox.isSelected());
-               _progressDialog.hide();
+               _progressDialog.setVisible(false);
+               _progressDialog.dispose(); // Sometimes dialog doesn't disappear without this dispose
                if (_cancelled) {return;}
 
                //System.out.println("Finished - counts are: " + _fileCounts[0] + ", " + _fileCounts[1]
@@ -166,26 +172,22 @@ public class JpegLoader implements Runnable
                if (_fileCounts[0] == 0)
                {
                        // No files found at all
-                       JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.nofilesfound"),
-                               I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
+                       _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nofilesfound");
                }
                else if (_fileCounts[1] == 0)
                {
                        // No jpegs found
-                       JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.nojpegsfound"),
-                               I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
+                       _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nojpegsfound");
                }
                else if (!_noExifCheckbox.isSelected() && _fileCounts[2] == 0)
                {
                        // Need coordinates but no exif found
-                       JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.noexiffound"),
-                               I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
+                       _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.noexiffound");
                }
                else if (!_noExifCheckbox.isSelected() && _fileCounts[3] == 0)
                {
                        // Need coordinates but no gps information found
-                       JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.jpegload.nogpsfound"),
-                               I18nManager.getText("error.jpegload.dialogtitle"), JOptionPane.ERROR_MESSAGE);
+                       _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nogpsfound");
                }
                else
                {
@@ -206,7 +208,7 @@ public class JpegLoader implements Runnable
                if (inFiles != null)
                {
                        // Loop over elements in array
-                       for (int i=0; i<inFiles.length; i++)
+                       for (int i=0; i<inFiles.length && !_cancelled; i++)
                        {
                                File file = inFiles[i];
                                if (file.exists() && file.canRead())
@@ -224,12 +226,7 @@ public class JpegLoader implements Runnable
                                                processFileList(files, false, inDescend);
                                        }
                                }
-                               else
-                               {
-                                       // file doesn't exist or isn't readable - ignore error
-                               }
-                               // check for cancel button pressed
-                               if (_cancelled) break;
+                               // if file doesn't exist or isn't readable - ignore
                        }
                }
        }
@@ -271,7 +268,7 @@ public class JpegLoader implements Runnable
                                point.setPhoto(photo);
                                point.setSegmentStart(true);
                                photo.setDataPoint(point);
-                               photo.setOriginalStatus(PhotoStatus.TAGGED);
+                               photo.setOriginalStatus(Photo.Status.TAGGED);
                                _fileCounts[3]++;
                        }
                        // Use exif timestamp if gps timestamp not available
@@ -280,6 +277,8 @@ public class JpegLoader implements Runnable
                                photo.setTimestamp(createTimestamp(jpegData.getOriginalTimestamp()));
                        }
                        photo.setExifThumbnail(jpegData.getThumbnailImage());
+                       // Also extract orientation tag for setting rotation state of photo
+                       photo.setRotation(jpegData.getRequiredRotation());
                }
                catch (JpegException jpe) { // don't list errors, just count them
                }
@@ -317,7 +316,8 @@ public class JpegLoader implements Runnable
                                {
                                        // Store first directory in config for later
                                        if (i == 0 && inFirstDir) {
-                                               Config.setWorkingDirectory(file.isDirectory()?file:file.getParentFile());
+                                               File workingDir = file.isDirectory()?file:file.getParentFile();
+                                               Config.setConfigString(Config.KEY_PHOTO_DIR, workingDir.getAbsolutePath());
                                        }
                                        // Check whether it's a file or a directory
                                        if (file.isFile())
@@ -352,7 +352,7 @@ public class JpegLoader implements Runnable
                Altitude altitude = null;
                if (inData.getAltitude() != null)
                {
-                       altitude = new Altitude(inData.getAltitude().intValue(), Altitude.FORMAT_METRES);
+                       altitude = new Altitude(inData.getAltitude().intValue(), Altitude.Format.METRES);
                }
                return new DataPoint(latitude, longitude, altitude);
        }
@@ -385,8 +385,6 @@ public class JpegLoader implements Runnable
         */
        private static Timestamp createTimestamp(Rational[] inDate, Rational[] inTime)
        {
-               //System.out.println("Making timestamp for date (" + inDate[0].toString() + "," + inDate[1].toString() + "," + inDate[2].toString() + ") and time ("
-               //      + inTime[0].toString() + "," + inTime[1].toString() + "," + inTime[2].toString() + ")");
                return new Timestamp(inDate[0].intValue(), inDate[1].intValue(), inDate[2].intValue(),
                        inTime[0].intValue(), inTime[1].intValue(), inTime[2].intValue());
        }