package tim.prune.save; 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.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; 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.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import tim.prune.Config; import tim.prune.GpsPruner; import tim.prune.I18nManager; import tim.prune.UpdateMessageBroker; import tim.prune.data.Altitude; import tim.prune.data.Coordinate; import tim.prune.data.DataPoint; import tim.prune.data.Timestamp; import tim.prune.data.Track; import tim.prune.data.TrackInfo; import tim.prune.load.GenericFileFilter; /** * Class to export track information * into a specified Gpx file */ public class GpxExporter implements Runnable { private JFrame _parentFrame = null; private Track _track = null; private JDialog _dialog = null; private JTextField _nameField = null; private JTextField _descriptionField = null; private JCheckBox _timestampsCheckbox = null; private JFileChooser _fileChooser = null; private File _exportFile = null; /** version number of Gpx */ private static final String GPX_VERSION_NUMBER = "1.1"; /** this program name */ private static final String GPX_CREATOR = "Prune v" + GpsPruner.VERSION_NUMBER + " activityworkshop.net"; /** * Constructor giving frame and track * @param inParentFrame parent frame * @param inTrackInfo track info object to save */ public GpxExporter(JFrame inParentFrame, TrackInfo inTrackInfo) { _parentFrame = inParentFrame; _track = inTrackInfo.getTrack(); } /** * Show the dialog to select options and export file */ public void showDialog() { // Make dialog window if (_dialog == null) { _dialog = new JDialog(_parentFrame, I18nManager.getText("dialog.exportgpx.title"), true); _dialog.setLocationRelativeTo(_parentFrame); _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); _dialog.getContentPane().add(makeDialogComponents()); _dialog.pack(); } _dialog.show(); } /** * Create dialog components * @return Panel containing all gui elements in dialog */ private Component makeDialogComponents() { JPanel dialogPanel = new JPanel(); dialogPanel.setLayout(new BorderLayout()); JPanel mainPanel = new JPanel(); mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); // Make a central panel with the text boxes JPanel descPanel = new JPanel(); descPanel.setLayout(new GridLayout(2, 2)); descPanel.add(new JLabel(I18nManager.getText("dialog.exportgpx.name"))); _nameField = new JTextField(10); descPanel.add(_nameField); descPanel.add(new JLabel(I18nManager.getText("dialog.exportgpx.desc"))); _descriptionField = new JTextField(10); descPanel.add(_descriptionField); mainPanel.add(descPanel); // checkbox for timestamps _timestampsCheckbox = new JCheckBox(I18nManager.getText("dialog.exportgpx.includetimestamps")); _timestampsCheckbox.setSelected(true); mainPanel.add(_timestampsCheckbox); dialogPanel.add(mainPanel, BorderLayout.CENTER); // button panel at bottom JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT)); JButton okButton = new JButton(I18nManager.getText("button.ok")); ActionListener okListener = new ActionListener() { public void actionPerformed(ActionEvent e) { startExport(); } }; okButton.addActionListener(okListener); _descriptionField.addActionListener(okListener); buttonPanel.add(okButton); JButton cancelButton = new JButton(I18nManager.getText("button.cancel")); cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { _dialog.dispose(); } }); buttonPanel.add(cancelButton); dialogPanel.add(buttonPanel, BorderLayout.SOUTH); dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15)); return dialogPanel; } /** * Start the export process based on the input parameters */ private void startExport() { // OK pressed, so choose output file if (_fileChooser == null) { _fileChooser = new JFileChooser(); _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG); _fileChooser.setFileFilter(new GenericFileFilter("filetype.gpx", new String[] {"gpx"})); _fileChooser.setAcceptAllFileFilterUsed(false); // start from directory in config which should be set File configDir = Config.getWorkingDirectory(); if (configDir != null) {_fileChooser.setCurrentDirectory(configDir);} } // Allow choose again if an existing file is selected boolean chooseAgain = false; do { chooseAgain = false; if (_fileChooser.showSaveDialog(_parentFrame) == JFileChooser.APPROVE_OPTION) { // OK pressed and file chosen File file = _fileChooser.getSelectedFile(); // Check file extension if (!file.getName().toLowerCase().endsWith(".gpx")) { file = new File(file.getAbsolutePath() + ".gpx"); } // Check if file exists and if necessary prompt for overwrite Object[] buttonTexts = {I18nManager.getText("button.overwrite"), I18nManager.getText("button.cancel")}; if (!file.exists() || JOptionPane.showOptionDialog(_parentFrame, I18nManager.getText("dialog.save.overwrite.text"), I18nManager.getText("dialog.save.overwrite.title"), JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1]) == JOptionPane.YES_OPTION) { // New file or overwrite confirmed, so initiate export in separate thread _exportFile = file; new Thread(this).start(); } else { chooseAgain = true; } } } while (chooseAgain); } /** * Run method for controlling separate thread for exporting */ public void run() { OutputStreamWriter writer = null; try { // normal writing to file writer = new OutputStreamWriter(new FileOutputStream(_exportFile)); // write file int numPoints = exportData(writer); // close file writer.close(); // Store directory in config for later Config.setWorkingDirectory(_exportFile.getParentFile()); // Show confirmation UpdateMessageBroker.informSubscribers(I18nManager.getText("confirm.save.ok1") + " " + numPoints + " " + I18nManager.getText("confirm.save.ok2") + " " + _exportFile.getAbsolutePath()); // export successful so need to close dialog and return _dialog.dispose(); return; } catch (IOException ioe) { // System.out.println("Exception: " + ioe.getClass().getName() + " - " + ioe.getMessage()); try { if (writer != null) writer.close(); } catch (IOException ioe2) {} JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.save.failed") + " : " + ioe.getMessage(), I18nManager.getText("error.save.dialogtitle"), JOptionPane.ERROR_MESSAGE); } // if not returned already, export failed so need to recall the file selection startExport(); } /** * Export the information to the given writer * @param inWriter writer object * @return number of points written */ private int exportData(OutputStreamWriter inWriter) throws IOException { inWriter.write("\n\n"); // Name field if (_nameField != null && _nameField.getText() != null && !_nameField.getText().equals("")) { inWriter.write("\t"); inWriter.write(_nameField.getText()); inWriter.write("\n"); } // Description field inWriter.write("\t"); if (_descriptionField != null && _descriptionField.getText() != null && !_descriptionField.getText().equals("")) { inWriter.write(_descriptionField.getText()); } else { inWriter.write("Export from Prune"); } inWriter.write("\n"); int i = 0; DataPoint point = null; boolean hasTrackpoints = false; // Loop over waypoints int numPoints = _track.getNumPoints(); for (i=0; i\n"); // Loop over track points for (i=0; i\n\t\n"); } if (!point.isWaypoint()) { // export the track point exportTrackpoint(point, inWriter); firstPoint = false; } } inWriter.write("\t\n"); } inWriter.write("\n"); return numPoints; } /** * Export the specified waypoint into the file * @param inPoint waypoint to export * @param inWriter writer object * @throws IOException on write failure */ private void exportWaypoint(DataPoint inPoint, Writer inWriter) throws IOException { inWriter.write("\t\n"); inWriter.write("\t\t"); inWriter.write(inPoint.getWaypointName().trim()); inWriter.write("\n"); // altitude if available if (inPoint.hasAltitude()) { inWriter.write("\t\t"); inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.FORMAT_METRES)); inWriter.write("\n"); } // timestamp if available (point might have altitude and then be turned into a waypoint) if (inPoint.hasTimestamp() && _timestampsCheckbox.isSelected()) { inWriter.write("\t\t\n"); } // TODO: Include waypt type in Gpx inWriter.write("\t\n"); } /** * Export the specified trackpoint into the file * @param inPoint trackpoint to export * @param inWriter writer object */ private void exportTrackpoint(DataPoint inPoint, Writer inWriter) throws IOException { inWriter.write("\t\t"); // altitude if (inPoint.hasAltitude()) { inWriter.write(""); inWriter.write("" + inPoint.getAltitude().getStringValue(Altitude.FORMAT_METRES)); inWriter.write(""); } // timestamp if available (and selected) if (inPoint.hasTimestamp() && _timestampsCheckbox.isSelected()) { inWriter.write(""); } inWriter.write("\n"); } }