package tim.prune.load; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.util.TreeSet; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JProgressBar; import tim.prune.App; import tim.prune.Config; import tim.prune.I18nManager; 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.Timestamp; import tim.prune.drew.jpeg.ExifReader; import tim.prune.drew.jpeg.JpegData; import tim.prune.drew.jpeg.JpegException; import tim.prune.drew.jpeg.Rational; /** * Class to manage the loading of Jpegs and dealing with the GPS data from them */ public class JpegLoader implements Runnable { private App _app = null; private JFrame _parentFrame = null; private JFileChooser _fileChooser = null; private GenericFileFilter _fileFilter = null; private JCheckBox _subdirCheckbox = null; private JCheckBox _noExifCheckbox = null; private JCheckBox _outsideAreaCheckbox = null; private JDialog _progressDialog = null; private JProgressBar _progressBar = null; private int[] _fileCounts = null; private boolean _cancelled = false; private LatLonRectangle _trackRectangle = null; private TreeSet _photos = null; /** * Constructor * @param inApp Application object to inform of photo load * @param inParentFrame parent frame to reference for dialogs */ public JpegLoader(App inApp, JFrame inParentFrame) { _app = inApp; _parentFrame = inParentFrame; String[] fileTypes = {"jpg", "jpe", "jpeg"}; _fileFilter = new GenericFileFilter("filetype.jpeg", fileTypes); } /** * Open the GUI to select options and start the load * @param inRectangle track rectangle */ public void openDialog(LatLonRectangle inRectangle) { // Create file chooser if necessary if (_fileChooser == null) { _fileChooser = new JFileChooser(); _fileChooser.setMultiSelectionEnabled(true); _fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); _fileChooser.setFileFilter(_fileFilter); _fileChooser.setDialogTitle(I18nManager.getText("menu.file.addphotos")); _subdirCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.subdirectories")); _subdirCheckbox.setSelected(true); _noExifCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.loadjpegswithoutcoords")); _noExifCheckbox.setSelected(true); _outsideAreaCheckbox = new JCheckBox(I18nManager.getText("dialog.jpegload.loadjpegsoutsidearea")); _outsideAreaCheckbox.setSelected(true); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(_subdirCheckbox); panel.add(_noExifCheckbox); panel.add(_outsideAreaCheckbox); _fileChooser.setAccessory(panel); // start from directory in config if already set by other operations 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; _outsideAreaCheckbox.setEnabled(_trackRectangle != null && !_trackRectangle.isEmpty()); // Show file dialog to choose file / directory(ies) if (_fileChooser.showOpenDialog(_parentFrame) == JFileChooser.APPROVE_OPTION) { // Bring up dialog before starting showDialog(); new Thread(this).start(); } } /** * Show the main dialog */ private void showDialog() { _progressDialog = new JDialog(_parentFrame, I18nManager.getText("dialog.jpegload.progress.title")); _progressDialog.setLocationRelativeTo(_parentFrame); _progressBar = new JProgressBar(0, 100); _progressBar.setValue(0); _progressBar.setStringPainted(true); _progressBar.setString(""); JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3)); panel.add(new JLabel(I18nManager.getText("dialog.jpegload.progress"))); panel.add(_progressBar); JButton cancelButton = new JButton(I18nManager.getText("button.cancel")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { _cancelled = true; } }); panel.add(cancelButton); _progressDialog.getContentPane().add(panel); _progressDialog.pack(); _progressDialog.setVisible(true); } /** * Run method for performing tasks in separate thread */ public void run() { // Initialise arrays, errors, summaries _fileCounts = new int[4]; // files, jpegs, exifs, gps _photos = new TreeSet(new PhotoSorter()); File[] files = _fileChooser.getSelectedFiles(); // Loop recursively over selected files/directories to count files int numFiles = countFileList(files, true, _subdirCheckbox.isSelected()); // Set up the progress bar for this number of files _progressBar.setMaximum(numFiles); _progressBar.setValue(0); _cancelled = false; // Process the files recursively and build lists of photos processFileList(files, true, _subdirCheckbox.isSelected()); _progressDialog.setVisible(false); if (_cancelled) {return;} //System.out.println("Finished - counts are: " + _fileCounts[0] + ", " + _fileCounts[1] // + ", " + _fileCounts[2] + ", " + _fileCounts[3]); if (_fileCounts[0] == 0) { // No files found at all _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nofilesfound"); } else if (_fileCounts[1] == 0) { // No jpegs found _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nojpegsfound"); } else if (!_noExifCheckbox.isSelected() && _fileCounts[2] == 0) { // Need coordinates but no exif found _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.noexiffound"); } else if (!_noExifCheckbox.isSelected() && _fileCounts[3] == 0) { // Need coordinates but no gps information found _app.showErrorMessage("error.jpegload.dialogtitle", "error.jpegload.nogpsfound"); } else { // Found some photos to load - pass information back to app _app.informPhotosLoaded(_photos); } } /** * Process a list of files and/or directories * @param inFiles array of file/directories * @param inFirstDir true if first directory * @param inDescend true to descend to subdirectories */ private void processFileList(File[] inFiles, boolean inFirstDir, boolean inDescend) { if (inFiles != null) { // Loop over elements in array for (int i=0; i