]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/save/FileSaver.java
Version 8, September 2009
[GpsPrune.git] / tim / prune / save / FileSaver.java
index c42a644c4c7cf3562a7e85f78367f6bc25d79f4d..3ea60db55961c093a826e659aab5a6a0ef79c88c 100644 (file)
@@ -32,6 +32,7 @@ import javax.swing.ListSelectionModel;
 import javax.swing.table.TableModel;
 
 import tim.prune.App;
+import tim.prune.Config;
 import tim.prune.I18nManager;
 import tim.prune.UpdateMessageBroker;
 import tim.prune.data.Altitude;
@@ -41,6 +42,7 @@ import tim.prune.data.Field;
 import tim.prune.data.FieldList;
 import tim.prune.data.Timestamp;
 import tim.prune.data.Track;
+import tim.prune.load.GenericFileFilter;
 import tim.prune.load.OneCharDocument;
 
 /**
@@ -63,12 +65,14 @@ public class FileSaver
        private JRadioButton[] _delimiterRadios = null;
        private JTextField _otherDelimiterText = null;
        private JCheckBox _headerRowCheckbox = null;
+       private PointTypeSelector _pointTypeSelector = null;
        private JRadioButton[] _coordUnitsRadios = null;
        private JRadioButton[] _altitudeUnitsRadios = null;
        private JRadioButton[] _timestampUnitsRadios = null;
+
        private static final int[] FORMAT_COORDS = {Coordinate.FORMAT_NONE, Coordinate.FORMAT_DEG_MIN_SEC,
                Coordinate.FORMAT_DEG_MIN, Coordinate.FORMAT_DEG};
-       private static final int[] FORMAT_ALTS = {Altitude.FORMAT_NONE, Altitude.FORMAT_METRES, Altitude.FORMAT_FEET};
+       private static final Altitude.Format[] FORMAT_ALTS = {Altitude.Format.NO_FORMAT, Altitude.Format.METRES, Altitude.Format.FEET};
        private static final int[] FORMAT_TIMES = {Timestamp.FORMAT_ORIGINAL, Timestamp.FORMAT_LOCALE, Timestamp.FORMAT_ISO_8601};
 
 
@@ -111,7 +115,7 @@ public class FileSaver
                }
                // Initialise dialog and show it
                initDialog(_model, inDefaultDelimiter);
-               _dialog.show();
+               _dialog.setVisible(true);
        }
 
 
@@ -220,15 +224,20 @@ public class FileSaver
 
                // header checkbox
                firstCard.add(Box.createRigidArea(new Dimension(0,10)));
-               _headerRowCheckbox = new JCheckBox(I18nManager.getText("dialog.save.headerrow"));
+               _headerRowCheckbox = new JCheckBox(I18nManager.getText("dialog.save.headerrow"), true);
                firstCard.add(_headerRowCheckbox);
-
                _cards.add(firstCard, "card1");
 
+               // Second card
                JPanel secondCard = new JPanel();
                secondCard.setLayout(new BorderLayout());
                JPanel secondCardHolder = new JPanel();
                secondCardHolder.setLayout(new BoxLayout(secondCardHolder, BoxLayout.Y_AXIS));
+               // point type selector
+               secondCardHolder.add(Box.createRigidArea(new Dimension(0,10)));
+               _pointTypeSelector = new PointTypeSelector();
+               _pointTypeSelector.setAlignmentX(JLabel.LEFT_ALIGNMENT);
+               secondCardHolder.add(_pointTypeSelector);
                JLabel coordLabel = new JLabel(I18nManager.getText("dialog.save.coordinateunits"));
                coordLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
                secondCardHolder.add(coordLabel);
@@ -249,7 +258,7 @@ public class FileSaver
                }
                coordsUnitsPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
                secondCardHolder.add(coordsUnitsPanel);
-               secondCardHolder.add(Box.createRigidArea(new Dimension(0,10)));
+               secondCardHolder.add(Box.createRigidArea(new Dimension(0,7)));
                // altitude units
                JLabel altUnitsLabel = new JLabel(I18nManager.getText("dialog.save.altitudeunits"));
                altUnitsLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
@@ -270,7 +279,7 @@ public class FileSaver
                }
                altUnitsPanel.setAlignmentX(JPanel.LEFT_ALIGNMENT);
                secondCardHolder.add(altUnitsPanel);
-               secondCardHolder.add(Box.createRigidArea(new Dimension(0,10)));
+               secondCardHolder.add(Box.createRigidArea(new Dimension(0,7)));
                // Selection of format of timestamps
                JLabel timestampLabel = new JLabel(I18nManager.getText("dialog.save.timestampformat"));
                timestampLabel.setAlignmentX(JLabel.LEFT_ALIGNMENT);
@@ -365,6 +374,7 @@ public class FileSaver
                        default   : _delimiterRadios[4].setSelected(true);
                                                _otherDelimiterText.setText("" + inDefaultDelimiter);
                }
+               _pointTypeSelector.init(_app.getTrackInfo());
                // set card and enable buttons
                CardLayout cl = (CardLayout) _cards.getLayout();
                cl.first(_cards);
@@ -379,11 +389,23 @@ public class FileSaver
         */
        private boolean saveToFile()
        {
+               // TODO: Shorten method
+               if (!_pointTypeSelector.getAnythingSelected()) {return false;}
                boolean saveOK = true;
                FileWriter writer = null;
                if (_fileChooser == null)
+               {
                        _fileChooser = new JFileChooser();
-               _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
+                       _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
+                       _fileChooser.addChoosableFileFilter(new GenericFileFilter("filetype.txt", new String[] {"txt", "text"}));
+                       _fileChooser.addChoosableFileFilter(new GenericFileFilter("filetype.gpx", new String[] {"gpx"}));
+                       _fileChooser.addChoosableFileFilter(new GenericFileFilter("filetype.kml", new String[] {"kml"}));
+                       _fileChooser.setAcceptAllFileFilterUsed(true);
+                       // start from directory in config which should be set
+                       String configDir = Config.getConfigString(Config.KEY_TRACK_DIR);
+                       if (configDir == null) {configDir = Config.getConfigString(Config.KEY_PHOTO_DIR);}
+                       if (configDir != null) {_fileChooser.setCurrentDirectory(new File(configDir));}
+               }
                if (_fileChooser.showSaveDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
                {
                        File saveFile = _fileChooser.getSelectedFile();
@@ -393,7 +415,7 @@ public class FileSaver
                        for (int i=0; i<_coordUnitsRadios.length; i++)
                                if (_coordUnitsRadios[i].isSelected())
                                        coordFormat = FORMAT_COORDS[i];
-                       int altitudeFormat = Altitude.FORMAT_NONE;
+                       Altitude.Format altitudeFormat = Altitude.Format.NO_FORMAT;
                        for (int i=0; i<_altitudeUnitsRadios.length; i++)
                        {
                                if (_altitudeUnitsRadios[i].isSelected())
@@ -455,10 +477,16 @@ public class FileSaver
                                        }
 
                                        // Loop over points outputting each in turn to buffer
-                                       int numPoints = _track.getNumPoints();
+                                       final int numPoints = _track.getNumPoints();
+                                       int numSaved = 0;
                                        for (int p=0; p<numPoints; p++)
                                        {
                                                DataPoint point = _track.getPoint(p);
+                                               boolean savePoint = ((point.isWaypoint() && _pointTypeSelector.getWaypointsSelected())
+                                                       || (!point.isWaypoint() && point.getPhoto()==null && _pointTypeSelector.getTrackpointsSelected())
+                                                       || (!point.isWaypoint() && point.getPhoto()!=null && _pointTypeSelector.getPhotopointsSelected()));
+                                               if (!savePoint) {continue;}
+                                               numSaved++;
                                                firstField = true;
                                                buffer = new StringBuffer();
                                                for (int f=0; f<numFields; f++)
@@ -485,7 +513,7 @@ public class FileSaver
                                                                {
                                                                        try
                                                                        {
-                                                                               buffer.append(point.getAltitude().getValue(altitudeFormat));
+                                                                               buffer.append(point.getAltitude().getStringValue(altitudeFormat));
                                                                        }
                                                                        catch (NullPointerException npe) {}
                                                                }
@@ -518,27 +546,25 @@ public class FileSaver
                                                writer.write(buffer.toString());
                                                writer.write(lineSeparator);
                                        }
+                                       // Store directory in config for later
+                                       Config.setConfigString(Config.KEY_TRACK_DIR, saveFile.getParentFile().getAbsolutePath());
                                        // Save successful
                                        UpdateMessageBroker.informSubscribers(I18nManager.getText("confirm.save.ok1")
-                                                + " " + numPoints + " " + I18nManager.getText("confirm.save.ok2")
+                                                + " " + numSaved + " " + I18nManager.getText("confirm.save.ok2")
                                                 + " " + saveFile.getAbsolutePath());
                                        _app.informDataSaved();
                                }
                                catch (IOException ioe)
                                {
                                        saveOK = false;
-                                       JOptionPane.showMessageDialog(_parentFrame,
-                                               I18nManager.getText("error.save.failed") + ioe.getMessage(),
-                                               I18nManager.getText("error.save.dialogtitle"),
-                                               JOptionPane.ERROR_MESSAGE);
+                                       _app.showErrorMessageNoLookup("error.save.dialogtitle",
+                                               I18nManager.getText("error.save.failed") + " : " + ioe.getMessage());
                                }
                                finally
                                {
                                        // try to close file if it's open
-                                       try
-                                       {
-                                               if (writer != null)
-                                               {
+                                       try {
+                                               if (writer != null) {
                                                        writer.close();
                                                }
                                        }