]> gitweb.fperrin.net Git - GpsPrune.git/blobdiff - tim/prune/load/BabelLoader.java
Version 13, August 2011
[GpsPrune.git] / tim / prune / load / BabelLoader.java
similarity index 53%
rename from tim/prune/load/GpsLoader.java
rename to tim/prune/load/BabelLoader.java
index 2196e0c3ff29695a13ac72f8abdf3e8bf528dcea..b970665c0c36d3f19de4d3d3583d4da9b1ce1c2a 100644 (file)
@@ -1,32 +1,17 @@
 package tim.prune.load;
 
-import java.awt.BorderLayout;
-import java.awt.Component;
-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.File;
 import java.io.InputStreamReader;
 import java.util.ArrayList;
 
-import javax.swing.BorderFactory;
-import javax.swing.BoxLayout;
 import javax.swing.JButton;
 import javax.swing.JCheckBox;
 import javax.swing.JDialog;
-import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.JPanel;
 import javax.swing.JProgressBar;
-import javax.swing.JTextField;
-import javax.swing.SwingConstants;
 import javax.swing.SwingUtilities;
-import javax.swing.event.ChangeEvent;
-import javax.swing.event.ChangeListener;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 
@@ -42,35 +27,32 @@ import tim.prune.load.xml.XmlHandler;
 import tim.prune.save.GpxExporter;
 
 /**
- * Class to manage the loading of GPS data using GpsBabel
+ * Superclass to manage the loading of data using GpsBabel
+ * Subclasses handle either from GPS or from file
  */
-public class GpsLoader extends GenericFunction implements Runnable
+public abstract class BabelLoader extends GenericFunction implements Runnable
 {
        private boolean _gpsBabelChecked = false;
-       private JDialog _dialog = null;
-       private JTextField _deviceField = null, _formatField = null;
-       private JCheckBox _waypointCheckbox = null, _trackCheckbox = null;
-       private JCheckBox _saveCheckbox = null;
-       private JButton _okButton = null;
-       private JProgressBar _progressBar = null;
-       private File _saveFile = null;
-       private boolean _cancelled = false;
+       protected JDialog _dialog = null;
+       // Checkboxes for which kinds of points to load
+       protected JCheckBox _waypointCheckbox = null, _trackCheckbox = null;
+       // Checkbox to save to file or not
+       protected JCheckBox _saveCheckbox = null;
+       protected JButton _okButton = null;
+       protected JProgressBar _progressBar = null;
+       protected File _saveFile = null;
+       protected boolean _cancelled = false;
 
 
        /**
         * Constructor
         * @param inApp Application object to inform of data load
         */
-       public GpsLoader(App inApp)
+       public BabelLoader(App inApp)
        {
                super(inApp);
        }
 
-       /** Get the name key */
-       public String getNameKey() {
-               return "function.loadfromgps";
-       }
-
        /**
         * Open the GUI to select options and start the load
         */
@@ -96,6 +78,7 @@ public class GpsLoader extends GenericFunction implements Runnable
                        // Initialise progress bars, buttons
                        enableOkButton();
                        setupProgressBar(true);
+                       initDialog(); // do any subclass-specific init here
                        _dialog.setVisible(true);
                }
        }
@@ -104,91 +87,11 @@ public class GpsLoader extends GenericFunction implements Runnable
        /**
         * @return a panel containing the main dialog components
         */
-       private JPanel makeDialogComponents()
-       {
-               JPanel outerPanel = new JPanel();
-               outerPanel.setLayout(new BorderLayout());
-               // Main panel with options etc
-               JPanel mainPanel = new JPanel();
-               mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
-
-               // text fields for options
-               JPanel gridPanel = new JPanel();
-               gridPanel.setLayout(new GridLayout(0, 2, 10, 3));
-               JLabel deviceLabel = new JLabel(I18nManager.getText("dialog.gpsload.device"));
-               deviceLabel.setHorizontalAlignment(SwingConstants.RIGHT);
-               gridPanel.add(deviceLabel);
-               _deviceField = new JTextField(Config.getConfigString(Config.KEY_GPS_DEVICE), 12);
-               _deviceField.addKeyListener(new KeyAdapter() {
-                       public void keyReleased(KeyEvent e)
-                       {
-                               // close dialog if escape pressed
-                               if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
-                                       _dialog.dispose();
-                               }
-                       }
-               });
-               gridPanel.add(_deviceField);
-               JLabel formatLabel = new JLabel(I18nManager.getText("dialog.gpsload.format"));
-               formatLabel.setHorizontalAlignment(SwingConstants.RIGHT);
-               gridPanel.add(formatLabel);
-               _formatField = new JTextField(Config.getConfigString(Config.KEY_GPS_FORMAT), 12);
-               gridPanel.add(_formatField);
-               gridPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
-               gridPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 5, 20));
-               mainPanel.add(gridPanel);
-
-               // checkboxes
-               ChangeListener checkboxListener = new ChangeListener() {
-                       public void stateChanged(ChangeEvent e)
-                       {
-                               enableOkButton();
-                       }
-               };
-               _waypointCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.getwaypoints"), true);
-               _waypointCheckbox.addChangeListener(checkboxListener);
-               _waypointCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
-               mainPanel.add(_waypointCheckbox);
-               _trackCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.gettracks"), true);
-               _trackCheckbox.addChangeListener(checkboxListener);
-               _trackCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
-               mainPanel.add(_trackCheckbox);
-               // Checkbox for immediately saving to file
-               _saveCheckbox = new JCheckBox(I18nManager.getText("dialog.gpsload.save"));
-               _saveCheckbox.setAlignmentX(Component.CENTER_ALIGNMENT);
-               mainPanel.add(_saveCheckbox);
+       protected abstract JPanel makeDialogComponents();
 
-               // progress bar (initially invisible)
-               _progressBar = new JProgressBar(0, 10);
-               mainPanel.add(_progressBar);
-               outerPanel.add(mainPanel, BorderLayout.NORTH);
-
-               // Lower panel with ok and cancel buttons
-               JPanel buttonPanel = new JPanel();
-               buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
-               _okButton = new JButton(I18nManager.getText("button.ok"));
-               _okButton.addActionListener(new ActionListener() {
-                       public void actionPerformed(ActionEvent e)
-                       {
-                               // start thread to call gpsbabel
-                               _cancelled = false;
-                               new Thread(GpsLoader.this).start();
-                       }
-               });
-               buttonPanel.add(_okButton);
-               JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
-               cancelButton.addActionListener(new ActionListener() {
-                       public void actionPerformed(ActionEvent e)
-                       {
-                               _cancelled = true;
-                               _dialog.dispose();
-                       }
-               });
-               buttonPanel.add(cancelButton);
-               outerPanel.add(buttonPanel, BorderLayout.SOUTH);
-               return outerPanel;
-       }
 
+       /** Do any subclass-specific dialog initialisation necessary */
+       protected void initDialog() {}
 
        /**
         * @param inStart true if the dialog is restarting
@@ -206,11 +109,15 @@ public class GpsLoader extends GenericFunction implements Runnable
        /**
         * Enable or disable the ok button
         */
-       private void enableOkButton()
+       protected void enableOkButton()
        {
-               _okButton.setEnabled(_waypointCheckbox.isSelected() || _trackCheckbox.isSelected());
+               _okButton.setEnabled(isInputOk());
        }
 
+       /**
+        * @return true if input fields of dialog are valid
+        */
+       protected abstract boolean isInputOk();
 
        /**
         * Run method for performing tasks in separate thread
@@ -219,7 +126,7 @@ public class GpsLoader extends GenericFunction implements Runnable
        {
                _okButton.setEnabled(false);
                setupProgressBar(false);
-               if (_waypointCheckbox.isSelected() || _trackCheckbox.isSelected())
+               if (isInputOk())
                {
                        _progressBar.setIndeterminate(true);
                        _saveFile = null;
@@ -249,12 +156,9 @@ public class GpsLoader extends GenericFunction implements Runnable
        private void callGpsBabel() throws Exception
        {
                // Set up command to call gpsbabel
-               final String device = _deviceField.getText().trim();
-               final String format = _formatField.getText().trim();
-               String[] commands = getCommandArray(device, format);
+               String[] commands = getCommandArray();
                // Save GPS settings in config
-               Config.setConfigString(Config.KEY_GPS_DEVICE, device);
-               Config.setConfigString(Config.KEY_GPS_FORMAT, format);
+               saveConfigValues();
 
                String errorMessage = "", errorMessage2 = "";
                XmlHandler handler = null;
@@ -326,7 +230,7 @@ public class GpsLoader extends GenericFunction implements Runnable
 
                        // Send data back to app
                        _app.informDataLoaded(handler.getFieldArray(), handler.getDataArray(), Altitude.Format.METRES,
-                               new SourceInfo(_deviceField.getText(), SourceInfo.FILE_TYPE.GPSBABEL),
+                               getSourceInfo(),
                                handler.getTrackNameList());
                }
        }
@@ -334,11 +238,9 @@ public class GpsLoader extends GenericFunction implements Runnable
 
        /**
         * Get the commands to call
-        * @param inDevice device name to use
-        * @param inFormat format to use
         * @return String array containing commands
         */
-       private String[] getCommandArray(String inDevice, String inFormat)
+       private String[] getCommandArray()
        {
                String[] commands = null;
                final String command = Config.getConfigString(Config.KEY_GPSBABEL_PATH);
@@ -346,14 +248,14 @@ public class GpsLoader extends GenericFunction implements Runnable
                final boolean loadTrack = _trackCheckbox.isSelected();
                if (loadWaypoints && loadTrack) {
                        // Both waypoints and track points selected
-                       commands = new String[] {command, "-w", "-t", "-i", inFormat,
-                               "-f", inDevice, "-o", "gpx", "-F", "-"};
+                       commands = new String[] {command, "-w", "-t", "-i", getInputFormat(),
+                               "-f", getFilePath(), "-o", "gpx", "-F", "-"};
                }
                else
                {
                        // Only waypoints OR track points selected
-                       commands = new String[] {command, "-w", "-i", inFormat,
-                               "-f", inDevice, "-o", "gpx", "-F", "-"};
+                       commands = new String[] {command, "-w", "-i", getInputFormat(),
+                               "-f", getFilePath(), "-o", "gpx", "-F", "-"};
                        if (loadTrack) {
                                commands[1] = "-t";
                        }
@@ -368,4 +270,24 @@ public class GpsLoader extends GenericFunction implements Runnable
                }
                return commands;
        }
+
+       /**
+        * @return SourceInfo object corresponding to the load
+        */
+       protected abstract SourceInfo getSourceInfo();
+
+       /**
+        * @return complete file path or device path for gpsbabel call
+        */
+       protected abstract String getFilePath();
+
+       /**
+        * @return file name or device name
+        */
+       protected abstract String getInputFormat();
+
+       /**
+        * Save any config values necessary
+        */
+       protected abstract void saveConfigValues();
 }