1 package tim.prune.save;
4 import java.io.FileWriter;
5 import java.io.IOException;
8 import javax.swing.JFileChooser;
9 import javax.swing.JFrame;
10 import javax.swing.JOptionPane;
11 import javax.swing.filechooser.FileFilter;
14 import tim.prune.I18nManager;
15 import tim.prune.data.Coordinate;
16 import tim.prune.data.DataPoint;
17 import tim.prune.data.Field;
18 import tim.prune.data.Track;
21 * Class to export track information
22 * into a specified Kml file
24 public class KmlExporter
26 private App _app = null;
27 private JFrame _parentFrame = null;
28 private Track _track = null;
29 private JFileChooser _fileChooser = null;
33 * Constructor giving App object, frame and track
34 * @param inApp application object to inform of success
35 * @param inParentFrame parent frame
36 * @param inTrack track object to save
38 public KmlExporter(App inApp, JFrame inParentFrame, Track inTrack)
41 _parentFrame = inParentFrame;
47 * Show the dialog to select options and export file
49 public boolean showDialog()
51 boolean fileSaved = false;
52 Object description = JOptionPane.showInputDialog(_parentFrame,
53 I18nManager.getText("dialog.exportkml.text"),
54 I18nManager.getText("dialog.exportkml.title"),
55 JOptionPane.QUESTION_MESSAGE, null, null, "");
56 if (description != null)
58 // OK pressed, so choose output file
59 if (_fileChooser == null)
60 _fileChooser = new JFileChooser();
61 _fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
62 _fileChooser.setFileFilter(new FileFilter() {
63 public boolean accept(File f)
65 return (f != null && (f.isDirectory() || f.getName().toLowerCase().endsWith(".kml")));
67 public String getDescription()
72 _fileChooser.setAcceptAllFileFilterUsed(false);
73 // Allow choose again if an existing file is selected
74 boolean chooseAgain = false;
78 if (_fileChooser.showSaveDialog(_parentFrame) == JFileChooser.APPROVE_OPTION)
80 // OK pressed and file chosen
81 File file = _fileChooser.getSelectedFile();
82 if (!file.getName().toLowerCase().endsWith(".kml"))
84 file = new File(file.getAbsolutePath() + ".kml");
86 // Check if file exists - if so don't overwrite
89 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.save.fileexists"),
90 I18nManager.getText("error.save.dialogtitle"), JOptionPane.ERROR_MESSAGE);
95 if (exportFile(file, description.toString()))
105 } while (chooseAgain);
112 * Export the track data to the specified file with description
113 * @param inFile File object to save to
114 * @param inDescription description to use, if any
116 private boolean exportFile(File inFile, String inDescription)
120 FileWriter writer = new FileWriter(inFile);
121 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://earth.google.com/kml/2.1\">\n<Folder>\n");
122 writer.write("\t<name>");
123 writer.write(inDescription);
124 writer.write("</name>\n");
127 DataPoint point = null;
128 boolean hasTrackpoints = false;
129 // Loop over waypoints
130 int numPoints = _track.getNumPoints();
131 for (i=0; i<numPoints; i++)
133 point = _track.getPoint(i);
134 if (point.isWaypoint())
136 exportWaypoint(point, writer);
140 hasTrackpoints = true;
145 writer.write("\t<Placemark>\n\t\t<name>track</name>\n\t\t<Style>\n\t\t\t<LineStyle>\n"
146 + "\t\t\t\t<color>cc0000cc</color>\n\t\t\t\t<width>4</width>\n\t\t\t</LineStyle>\n"
147 + "\t\t</Style>\n\t\t<LineString>\n\t\t\t<coordinates>");
148 // Loop over track points
149 for (i=0; i<numPoints; i++)
151 point = _track.getPoint(i);
152 if (!point.isWaypoint())
154 exportTrackpoint(point, writer);
157 writer.write("\t\t\t</coordinates>\n\t\t</LineString>\n\t</Placemark>");
159 writer.write("</Folder>\n</kml>");
161 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.save.ok1")
162 + " " + numPoints + " " + I18nManager.getText("dialog.save.ok2")
163 + inFile.getAbsolutePath(),
164 I18nManager.getText("dialog.save.oktitle"), JOptionPane.INFORMATION_MESSAGE);
167 catch (IOException ioe)
169 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.save.failed") + ioe.getMessage(),
170 I18nManager.getText("error.save.dialogtitle"), JOptionPane.ERROR_MESSAGE);
177 * Export the specified waypoint into the file
178 * @param inPoint waypoint to export
179 * @param inWriter writer object
181 private void exportWaypoint(DataPoint inPoint, Writer inWriter) throws IOException
183 inWriter.write("\t<Placemark>\n\t\t<name>");
184 inWriter.write(inPoint.getFieldValue(Field.WAYPT_NAME).trim());
185 inWriter.write("</name>\n");
186 inWriter.write("\t\t<Point>\n\t\t\t<coordinates>");
187 inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
189 inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
190 inWriter.write(",0</coordinates>\n\t\t</Point>\n\t</Placemark>\n");
195 * Export the specified trackpoint into the file
196 * @param inPoint trackpoint to export
197 * @param inWriter writer object
199 private void exportTrackpoint(DataPoint inPoint, Writer inWriter) throws IOException
201 inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
203 inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
204 // Altitude not exported, locked to ground by Google Earth
205 inWriter.write(",0\n");