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.Track;
20 * Class to export track information
21 * into a specified Kml file
23 public class KmlExporter
25 private App _app = null;
26 private JFrame _parentFrame = null;
27 private Track _track = null;
28 private JFileChooser _fileChooser = null;
32 * Constructor giving App object, frame and track
33 * @param inApp application object to inform of success
34 * @param inParentFrame parent frame
35 * @param inTrack track object to save
37 public KmlExporter(App inApp, JFrame inParentFrame, Track inTrack)
40 _parentFrame = inParentFrame;
46 * Show the dialog to select options and export file
48 public boolean showDialog()
50 boolean fileSaved = false;
51 Object description = JOptionPane.showInputDialog(_parentFrame,
52 I18nManager.getText("dialog.exportkml.text"),
53 I18nManager.getText("dialog.exportkml.title"),
54 JOptionPane.QUESTION_MESSAGE, null, null, "");
55 // TODO: Make dialog window including colour selection, line width, track description
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()
69 return I18nManager.getText("dialog.exportkml.filetype");
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 and if necessary prompt for overwrite
87 Object[] buttonTexts = {I18nManager.getText("button.overwrite"), I18nManager.getText("button.cancel")};
88 if (!file.exists() || JOptionPane.showOptionDialog(_parentFrame,
89 I18nManager.getText("dialog.save.overwrite.text"),
90 I18nManager.getText("dialog.save.overwrite.title"), JOptionPane.YES_NO_OPTION,
91 JOptionPane.WARNING_MESSAGE, null, buttonTexts, buttonTexts[1])
92 == JOptionPane.YES_OPTION)
94 if (exportFile(file, description.toString()))
108 } while (chooseAgain);
115 * Export the track data to the specified file with description
116 * @param inFile File object to save to
117 * @param inDescription description to use, if any
119 private boolean exportFile(File inFile, String inDescription)
121 FileWriter writer = null;
124 writer = new FileWriter(inFile);
125 writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<kml xmlns=\"http://earth.google.com/kml/2.1\">\n<Folder>\n");
126 writer.write("\t<name>");
127 writer.write(inDescription);
128 writer.write("</name>\n");
131 DataPoint point = null;
132 boolean hasTrackpoints = false;
133 // Loop over waypoints
134 boolean writtenPhotoHeader = false;
135 int numPoints = _track.getNumPoints();
136 for (i=0; i<numPoints; i++)
138 point = _track.getPoint(i);
139 if (point.isWaypoint())
141 exportWaypoint(point, writer);
143 else if (point.getPhoto() != null)
145 if (!writtenPhotoHeader)
147 writer.write("<Style id=\"camera_icon\"><IconStyle><Icon><href>http://maps.google.com/mapfiles/kml/pal4/icon46.png</href></Icon></IconStyle></Style>");
148 writtenPhotoHeader = true;
150 exportPhotoPoint(point, writer);
154 hasTrackpoints = true;
159 writer.write("\t<Placemark>\n\t\t<name>track</name>\n\t\t<Style>\n\t\t\t<LineStyle>\n"
160 + "\t\t\t\t<color>cc0000cc</color>\n\t\t\t\t<width>4</width>\n\t\t\t</LineStyle>\n"
161 + "\t\t</Style>\n\t\t<LineString>\n\t\t\t<coordinates>");
162 // Loop over track points
163 for (i=0; i<numPoints; i++)
165 point = _track.getPoint(i);
166 if (!point.isWaypoint())
168 exportTrackpoint(point, writer);
171 writer.write("\t\t\t</coordinates>\n\t\t</LineString>\n\t</Placemark>");
173 writer.write("</Folder>\n</kml>");
175 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("dialog.save.ok1")
176 + " " + numPoints + " " + I18nManager.getText("dialog.save.ok2")
177 + " " + inFile.getAbsolutePath(),
178 I18nManager.getText("dialog.save.oktitle"), JOptionPane.INFORMATION_MESSAGE);
181 catch (IOException ioe)
184 if (writer != null) writer.close();
186 catch (IOException ioe2) {}
187 JOptionPane.showMessageDialog(_parentFrame, I18nManager.getText("error.save.failed") + ioe.getMessage(),
188 I18nManager.getText("error.save.dialogtitle"), JOptionPane.ERROR_MESSAGE);
195 * Export the specified waypoint into the file
196 * @param inPoint waypoint to export
197 * @param inWriter writer object
198 * @throws IOException on write failure
200 private void exportWaypoint(DataPoint inPoint, Writer inWriter) throws IOException
202 inWriter.write("\t<Placemark>\n\t\t<name>");
203 inWriter.write(inPoint.getWaypointName().trim());
204 inWriter.write("</name>\n");
205 inWriter.write("\t\t<Point>\n\t\t\t<coordinates>");
206 inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
208 inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
209 inWriter.write(",0</coordinates>\n\t\t</Point>\n\t</Placemark>\n");
214 * Export the specified photo into the file
215 * @param inPoint data point including photo
216 * @param inWriter writer object
217 * @throws IOException on write failure
219 private void exportPhotoPoint(DataPoint inPoint, Writer inWriter) throws IOException
221 // TODO: Export photos to KML too - for photos need kmz!
222 inWriter.write("\t<Placemark>\n\t\t<name>");
223 inWriter.write(inPoint.getPhoto().getFile().getName());
224 inWriter.write("</name>\n");
225 inWriter.write("<styleUrl>#camera_icon</styleUrl>\n");
226 inWriter.write("\t\t<Point>\n\t\t\t<coordinates>");
227 inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
229 inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
230 inWriter.write(",0</coordinates>\n\t\t</Point>\n\t</Placemark>\n");
235 * Export the specified trackpoint into the file
236 * @param inPoint trackpoint to export
237 * @param inWriter writer object
239 private void exportTrackpoint(DataPoint inPoint, Writer inWriter) throws IOException
241 inWriter.write(inPoint.getLongitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
243 inWriter.write(inPoint.getLatitude().output(Coordinate.FORMAT_DEG_WITHOUT_CARDINAL));
244 // Altitude not exported, locked to ground by Google Earth
245 inWriter.write(",0\n");