1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.awt.event.KeyAdapter;
|
---|
10 | import java.awt.event.KeyEvent;
|
---|
11 | import java.io.File;
|
---|
12 | import java.io.FileOutputStream;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.text.MessageFormat;
|
---|
15 | import java.util.Calendar;
|
---|
16 |
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JCheckBox;
|
---|
19 | import javax.swing.JLabel;
|
---|
20 | import javax.swing.JList;
|
---|
21 | import javax.swing.JOptionPane;
|
---|
22 | import javax.swing.JPanel;
|
---|
23 | import javax.swing.JScrollPane;
|
---|
24 | import javax.swing.JTextArea;
|
---|
25 | import javax.swing.JTextField;
|
---|
26 | import javax.swing.ListSelectionModel;
|
---|
27 |
|
---|
28 | import org.openstreetmap.josm.Main;
|
---|
29 | import org.openstreetmap.josm.actions.ExtensionFileFilter;
|
---|
30 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
31 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
32 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
33 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
34 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
35 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
36 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
37 | import org.openstreetmap.josm.tools.GBC;
|
---|
38 |
|
---|
39 | public class GpxExporter extends FileExporter {
|
---|
40 | private final static String warningGpl = "<html><font color='red' size='-2'>"
|
---|
41 | + tr("Note: GPL is not compatible with the OSM license. Do not upload GPL licensed tracks.") + "</html>";
|
---|
42 |
|
---|
43 | public GpxExporter() {
|
---|
44 | super(new ExtensionFileFilter("gpx,gpx.gz", "gpx", tr("GPX Files") + " (*.gpx *.gpx.gz)"));
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public boolean acceptFile(File pathname, Layer layer) {
|
---|
49 | if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
50 | return false;
|
---|
51 | return super.acceptFile(pathname, layer);
|
---|
52 | }
|
---|
53 |
|
---|
54 | @Override
|
---|
55 | public void exportData(File file, Layer layer) throws IOException {
|
---|
56 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
57 | if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
58 | throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.", layer
|
---|
59 | .getClass().getName()));
|
---|
60 | CheckParameterUtil.ensureParameterNotNull(file, "file");
|
---|
61 |
|
---|
62 | String fn = file.getPath();
|
---|
63 | if (fn.indexOf('.') == -1) {
|
---|
64 | fn += ".gpx";
|
---|
65 | file = new File(fn);
|
---|
66 | }
|
---|
67 |
|
---|
68 | // open the dialog asking for options
|
---|
69 | JPanel p = new JPanel(new GridBagLayout());
|
---|
70 |
|
---|
71 | p.add(new JLabel(tr("GPS track description")), GBC.eol());
|
---|
72 | JTextArea desc = new JTextArea(3, 40);
|
---|
73 | desc.setWrapStyleWord(true);
|
---|
74 | desc.setLineWrap(true);
|
---|
75 | p.add(new JScrollPane(desc), GBC.eop().fill(GBC.BOTH));
|
---|
76 |
|
---|
77 | JCheckBox author = new JCheckBox(tr("Add author information"), Main.pref.getBoolean("lastAddAuthor", true));
|
---|
78 | author.setSelected(true);
|
---|
79 | p.add(author, GBC.eol());
|
---|
80 | JLabel nameLabel = new JLabel(tr("Real name"));
|
---|
81 | p.add(nameLabel, GBC.std().insets(10, 0, 5, 0));
|
---|
82 | JTextField authorName = new JTextField(Main.pref.get("lastAuthorName"));
|
---|
83 | p.add(authorName, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
84 | JLabel emailLabel = new JLabel(tr("E-Mail"));
|
---|
85 | p.add(emailLabel, GBC.std().insets(10, 0, 5, 0));
|
---|
86 | JTextField email = new JTextField(Main.pref.get("osm-server.username"));
|
---|
87 | p.add(email, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
88 | JLabel copyrightLabel = new JLabel(tr("Copyright (URL)"));
|
---|
89 | p.add(copyrightLabel, GBC.std().insets(10, 0, 5, 0));
|
---|
90 | JTextField copyright = new JTextField();
|
---|
91 | p.add(copyright, GBC.std().fill(GBC.HORIZONTAL));
|
---|
92 | JButton predefined = new JButton(tr("Predefined"));
|
---|
93 | p.add(predefined, GBC.eol().insets(5, 0, 0, 0));
|
---|
94 | JLabel copyrightYearLabel = new JLabel(tr("Copyright year"));
|
---|
95 | p.add(copyrightYearLabel, GBC.std().insets(10, 0, 5, 5));
|
---|
96 | JTextField copyrightYear = new JTextField("");
|
---|
97 | p.add(copyrightYear, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
98 | JLabel warning = new JLabel("<html><font size='-2'> </html");
|
---|
99 | p.add(warning, GBC.eol().fill(GBC.HORIZONTAL).insets(15, 0, 0, 0));
|
---|
100 | addDependencies(author, authorName, email, copyright, predefined, copyrightYear, nameLabel, emailLabel,
|
---|
101 | copyrightLabel, copyrightYearLabel, warning);
|
---|
102 |
|
---|
103 | // if the user name is not the email address, but the osm user name
|
---|
104 | // move it from the email textfield to the author textfield
|
---|
105 | if (!email.getText().contains("@")) {
|
---|
106 | authorName.setText(email.getText());
|
---|
107 | email.setText("");
|
---|
108 | }
|
---|
109 |
|
---|
110 | p.add(new JLabel(tr("Keywords")), GBC.eol());
|
---|
111 | JTextField keywords = new JTextField();
|
---|
112 | p.add(keywords, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
113 |
|
---|
114 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
115 | tr("Export options"),
|
---|
116 | new String[] { tr("Export and Save"), tr("Cancel") });
|
---|
117 | ed.setButtonIcons(new String[] { "exportgpx.png", "cancel.png" });
|
---|
118 | ed.setContent(p);
|
---|
119 | ed.showDialog();
|
---|
120 |
|
---|
121 | if (ed.getValue() != 1)
|
---|
122 | return;
|
---|
123 |
|
---|
124 | Main.pref.put("lastAddAuthor", author.isSelected());
|
---|
125 | if (authorName.getText().length() != 0) {
|
---|
126 | Main.pref.put("lastAuthorName", authorName.getText());
|
---|
127 | }
|
---|
128 | if (copyright.getText().length() != 0) {
|
---|
129 | Main.pref.put("lastCopyright", copyright.getText());
|
---|
130 | }
|
---|
131 |
|
---|
132 | GpxData gpxData;
|
---|
133 | if (layer instanceof OsmDataLayer) {
|
---|
134 | gpxData = ((OsmDataLayer) layer).toGpxData();
|
---|
135 | } else if (layer instanceof GpxLayer) {
|
---|
136 | gpxData = ((GpxLayer) layer).data;
|
---|
137 | } else {
|
---|
138 | gpxData = OsmDataLayer.toGpxData(getCurrentDataSet(), file);
|
---|
139 | }
|
---|
140 |
|
---|
141 | // add author and copyright details to the gpx data
|
---|
142 | if (author.isSelected()) {
|
---|
143 | if (authorName.getText().length() > 0) {
|
---|
144 | gpxData.attr.put(GpxData.META_AUTHOR_NAME, authorName.getText());
|
---|
145 | gpxData.attr.put(GpxData.META_COPYRIGHT_AUTHOR, authorName.getText());
|
---|
146 | }
|
---|
147 | if (email.getText().length() > 0) {
|
---|
148 | gpxData.attr.put(GpxData.META_AUTHOR_EMAIL, email.getText());
|
---|
149 | }
|
---|
150 | if (copyright.getText().length() > 0) {
|
---|
151 | gpxData.attr.put(GpxData.META_COPYRIGHT_LICENSE, copyright.getText());
|
---|
152 | }
|
---|
153 | if (copyrightYear.getText().length() > 0) {
|
---|
154 | gpxData.attr.put(GpxData.META_COPYRIGHT_YEAR, copyrightYear.getText());
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | // add the description to the gpx data
|
---|
159 | if (desc.getText().length() > 0) {
|
---|
160 | gpxData.attr.put(GpxData.META_DESC, desc.getText());
|
---|
161 | }
|
---|
162 |
|
---|
163 | // add keywords to the gpx data
|
---|
164 | if (keywords.getText().length() > 0) {
|
---|
165 | gpxData.attr.put(GpxData.META_KEYWORDS, keywords.getText());
|
---|
166 | }
|
---|
167 |
|
---|
168 | try {
|
---|
169 | FileOutputStream fo = new FileOutputStream(file);
|
---|
170 | new GpxWriter(fo).write(gpxData);
|
---|
171 | fo.flush();
|
---|
172 | fo.close();
|
---|
173 | } catch (IOException x) {
|
---|
174 | x.printStackTrace();
|
---|
175 | JOptionPane.showMessageDialog(Main.parent, tr("Error while exporting {0}:\n{1}", fn, x.getMessage()),
|
---|
176 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
177 | }
|
---|
178 |
|
---|
179 | }
|
---|
180 |
|
---|
181 | private static void enableCopyright(final JTextField copyright, final JButton predefined,
|
---|
182 | final JTextField copyrightYear, final JLabel copyrightLabel, final JLabel copyrightYearLabel,
|
---|
183 | final JLabel warning, boolean enable) {
|
---|
184 | copyright.setEnabled(enable);
|
---|
185 | predefined.setEnabled(enable);
|
---|
186 | copyrightYear.setEnabled(enable);
|
---|
187 | copyrightLabel.setEnabled(enable);
|
---|
188 | copyrightYearLabel.setEnabled(enable);
|
---|
189 | warning.setText(enable ? warningGpl : "<html><font size='-2'> </html");
|
---|
190 |
|
---|
191 | if (enable && copyrightYear.getText().length()==0) {
|
---|
192 | copyrightYear.setText(enable ? Integer.toString(Calendar.getInstance().get(Calendar.YEAR)) : "");
|
---|
193 | } else if (!enable) {
|
---|
194 | copyrightYear.setText("");
|
---|
195 | }
|
---|
196 |
|
---|
197 | if (enable && copyright.getText().length()==0) {
|
---|
198 | copyright.setText(enable ? Main.pref.get("lastCopyright", "http://creativecommons.org/licenses/by-sa/2.5") : "");
|
---|
199 | copyright.setCaretPosition(0);
|
---|
200 | } else if (!enable) {
|
---|
201 | copyright.setText("");
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * Add all those listeners to handle the enable state of the fields.
|
---|
207 | * @param copyrightYearLabel
|
---|
208 | * @param copyrightLabel
|
---|
209 | * @param emailLabel
|
---|
210 | * @param nameLabel
|
---|
211 | * @param warning
|
---|
212 | */
|
---|
213 | private static void addDependencies(
|
---|
214 | final JCheckBox author,
|
---|
215 | final JTextField authorName,
|
---|
216 | final JTextField email,
|
---|
217 | final JTextField copyright,
|
---|
218 | final JButton predefined,
|
---|
219 | final JTextField copyrightYear,
|
---|
220 | final JLabel nameLabel,
|
---|
221 | final JLabel emailLabel,
|
---|
222 | final JLabel copyrightLabel,
|
---|
223 | final JLabel copyrightYearLabel,
|
---|
224 | final JLabel warning) {
|
---|
225 |
|
---|
226 | ActionListener authorActionListener = new ActionListener(){
|
---|
227 | public void actionPerformed(ActionEvent e) {
|
---|
228 | boolean b = author.isSelected();
|
---|
229 | authorName.setEnabled(b);
|
---|
230 | email.setEnabled(b);
|
---|
231 | nameLabel.setEnabled(b);
|
---|
232 | emailLabel.setEnabled(b);
|
---|
233 | authorName.setText(b ? Main.pref.get("lastAuthorName") : "");
|
---|
234 | email.setText(b ? Main.pref.get("osm-server.username") : "");
|
---|
235 |
|
---|
236 | boolean authorSet = authorName.getText().length() != 0;
|
---|
237 | GpxExporter.enableCopyright(copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning, b && authorSet);
|
---|
238 | }
|
---|
239 | };
|
---|
240 | author.addActionListener(authorActionListener);
|
---|
241 |
|
---|
242 | KeyAdapter authorNameListener = new KeyAdapter(){
|
---|
243 | @Override public void keyReleased(KeyEvent e) {
|
---|
244 | boolean b = authorName.getText().length()!=0 && author.isSelected();
|
---|
245 | GpxExporter.enableCopyright(copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning, b);
|
---|
246 | }
|
---|
247 | };
|
---|
248 | authorName.addKeyListener(authorNameListener);
|
---|
249 |
|
---|
250 | predefined.addActionListener(new ActionListener(){
|
---|
251 | public void actionPerformed(ActionEvent e) {
|
---|
252 | JList l = new JList(new String[]{"Creative Commons By-SA", "public domain", "GNU Lesser Public License (LGPL)", "BSD License (MIT/X11)"});
|
---|
253 | l.setVisibleRowCount(4);
|
---|
254 | l.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
255 | int answer = JOptionPane.showConfirmDialog(
|
---|
256 | Main.parent,
|
---|
257 | new JScrollPane(l),
|
---|
258 | tr("Choose a predefined license"),
|
---|
259 | JOptionPane.OK_CANCEL_OPTION,
|
---|
260 | JOptionPane.QUESTION_MESSAGE
|
---|
261 | );
|
---|
262 | if (answer != JOptionPane.OK_OPTION || l.getSelectedIndex() == -1)
|
---|
263 | return;
|
---|
264 | final String[] urls = {
|
---|
265 | "http://creativecommons.org/licenses/by-sa/2.5",
|
---|
266 | "public domain",
|
---|
267 | "http://www.gnu.org/copyleft/lesser.html",
|
---|
268 | "http://www.opensource.org/licenses/bsd-license.php"};
|
---|
269 | String license = "";
|
---|
270 | for (int i : l.getSelectedIndices()) {
|
---|
271 | if (i == 1) {
|
---|
272 | license = "public domain";
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | license += license.length()==0 ? urls[i] : ", "+urls[i];
|
---|
276 | }
|
---|
277 | copyright.setText(license);
|
---|
278 | copyright.setCaretPosition(0);
|
---|
279 | }
|
---|
280 | });
|
---|
281 |
|
---|
282 | authorActionListener.actionPerformed(null);
|
---|
283 | authorNameListener.keyReleased(null);
|
---|
284 | }
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * Replies the current dataset
|
---|
288 | *
|
---|
289 | * @return the current dataset. null, if no current dataset exists
|
---|
290 | */
|
---|
291 | private DataSet getCurrentDataSet() {
|
---|
292 | return Main.main.getCurrentDataSet();
|
---|
293 | }
|
---|
294 |
|
---|
295 | }
|
---|