]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/save/GpsSaver.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / save / GpsSaver.java
index e3777a23e88d28c9ef5739e9e0a2593dd479c9d0..5c74a7a3894d22dd7c06d9e3e3bdb4b3fca7f1c3 100644 (file)
@@ -6,6 +6,8 @@ import java.awt.FlowLayout;
 import java.awt.GridLayout;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.KeyAdapter;
+import java.awt.event.KeyEvent;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
@@ -25,10 +27,10 @@ import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
 
 import tim.prune.App;
-import tim.prune.Config;
 import tim.prune.ExternalTools;
 import tim.prune.GenericFunction;
 import tim.prune.I18nManager;
+import tim.prune.config.Config;
 
 /**
  * Class to manage the loading of GPS data using GpsBabel
@@ -65,7 +67,7 @@ public class GpsSaver extends GenericFunction implements Runnable
        public void begin()
        {
                // Check if gpsbabel looks like it's installed
-               if (_gpsBabelChecked || ExternalTools.isGpsbabelInstalled()
+               if (_gpsBabelChecked || ExternalTools.isToolInstalled(ExternalTools.TOOL_GPSBABEL)
                        || JOptionPane.showConfirmDialog(_dialog,
                                I18nManager.getText("dialog.gpsload.nogpsbabel"),
                                I18nManager.getText(getNameKey()),
@@ -106,12 +108,12 @@ public class GpsSaver extends GenericFunction implements Runnable
                JLabel deviceLabel = new JLabel(I18nManager.getText("dialog.gpsload.device"));
                deviceLabel.setHorizontalAlignment(SwingConstants.RIGHT);
                gridPanel.add(deviceLabel);
-               _deviceField = new JTextField(Config.getGpsDevice(), 12);
+               _deviceField = new JTextField(Config.getConfigString(Config.KEY_GPS_DEVICE), 12);
                gridPanel.add(_deviceField);
                JLabel formatLabel = new JLabel(I18nManager.getText("dialog.gpsload.format"));
                formatLabel.setHorizontalAlignment(SwingConstants.RIGHT);
                gridPanel.add(formatLabel);
-               _formatField = new JTextField(Config.getGpsFormat(), 12);
+               _formatField = new JTextField(Config.getConfigString(Config.KEY_GPS_FORMAT), 12);
                gridPanel.add(_formatField);
                JLabel nameLabel = new JLabel(I18nManager.getText("dialog.gpssend.trackname"));
                nameLabel.setHorizontalAlignment(SwingConstants.RIGHT);
@@ -121,6 +123,19 @@ public class GpsSaver extends GenericFunction implements Runnable
                gridPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
                gridPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 20));
                mainPanel.add(gridPanel);
+               // close dialog when escape pressed
+               KeyAdapter closer = new KeyAdapter() {
+                       public void keyReleased(KeyEvent e)
+                       {
+                               // close dialog if escape pressed
+                               if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
+                                       _dialog.dispose();
+                               }
+                       }
+               };
+               _deviceField.addKeyListener(closer);
+               _formatField.addKeyListener(closer);
+               _trackNameField.addKeyListener(closer);
 
                // checkboxes
                ChangeListener checkboxListener = new ChangeListener() {
@@ -228,21 +243,27 @@ public class GpsSaver extends GenericFunction implements Runnable
        private void callGpsBabel() throws Exception
        {
                // Set up command to call gpsbabel
+               final String command = Config.getConfigString(Config.KEY_GPSBABEL_PATH);
                String[] commands = null;
+               final String device = _deviceField.getText().trim();
+               final String format = _formatField.getText().trim();
                if (_waypointCheckbox.isSelected() && _trackCheckbox.isSelected()) {
                        // Both waypoints and track points selected
-                       commands = new String[] {"gpsbabel", "-w", "-t", "-i", "gpx", "-f", "-", "-o", _formatField.getText(),
-                               "-F", _deviceField.getText()};
+                       commands = new String[] {command, "-w", "-t", "-i", "gpx", "-f", "-", "-o", format,
+                               "-F", device};
                }
                else
                {
                        // Only waypoints OR trackpoints selected
-                       commands = new String[] {"gpsbabel", "-w", "-i", "gpx", "-f", "-", "-o", _formatField.getText(),
-                               "-F", _deviceField.getText()};
+                       commands = new String[] {command, "-w", "-i", "gpx", "-f", "-", "-o", format,
+                               "-F", device};
                        if (_trackCheckbox.isSelected()) {
                                commands[1] = "-t";
                        }
                }
+               // Save GPS settings in config
+               Config.setConfigString(Config.KEY_GPS_DEVICE, device);
+               Config.setConfigString(Config.KEY_GPS_FORMAT, format);
 
                String errorMessage = "";
                Process process = Runtime.getRuntime().exec(commands);
@@ -251,7 +272,8 @@ public class GpsSaver extends GenericFunction implements Runnable
                if (trackName == null || trackName.equals("")) {trackName = "prune";}
                // Generate the GPX file and send to the GPS
                OutputStreamWriter writer = new OutputStreamWriter(process.getOutputStream());
-               GpxExporter.exportData(writer, _app.getTrackInfo().getTrack(), trackName, null, true);
+               boolean[] saveFlags = {true, true, true, true, false, true}; // export everything
+               GpxExporter.exportData(writer, _app.getTrackInfo(), trackName, null, saveFlags, null);
                writer.close();
 
                // Read the error stream to see if there's a better error message there