[1949] | 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.ActionListener;
|
---|
| 8 | import java.awt.event.KeyAdapter;
|
---|
| 9 | import java.awt.event.KeyEvent;
|
---|
| 10 | import java.io.File;
|
---|
| 11 | import java.io.IOException;
|
---|
[6716] | 12 | import java.io.OutputStream;
|
---|
[2852] | 13 | import java.text.MessageFormat;
|
---|
[11035] | 14 | import java.time.Year;
|
---|
[1949] | 15 |
|
---|
| 16 | import javax.swing.JButton;
|
---|
| 17 | import javax.swing.JCheckBox;
|
---|
| 18 | import javax.swing.JLabel;
|
---|
| 19 | import javax.swing.JList;
|
---|
| 20 | import javax.swing.JOptionPane;
|
---|
| 21 | import javax.swing.JPanel;
|
---|
| 22 | import javax.swing.JScrollPane;
|
---|
| 23 | import javax.swing.ListSelectionModel;
|
---|
| 24 |
|
---|
| 25 | import org.openstreetmap.josm.Main;
|
---|
[5681] | 26 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
[1949] | 27 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
| 28 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
| 29 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
| 30 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
| 31 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
[5886] | 32 | import org.openstreetmap.josm.gui.widgets.JosmTextArea;
|
---|
| 33 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
[2852] | 34 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[1949] | 35 | import org.openstreetmap.josm.tools.GBC;
|
---|
| 36 |
|
---|
[7414] | 37 | /**
|
---|
| 38 | * Exports data to a .gpx file. Data may be native GPX or OSM data which will be converted.
|
---|
| 39 | * @since 1949
|
---|
| 40 | */
|
---|
[5681] | 41 | public class GpxExporter extends FileExporter implements GpxConstants {
|
---|
[7414] | 42 |
|
---|
| 43 | private static final String GPL_WARNING = "<html><font color='red' size='-2'>"
|
---|
[1949] | 44 | + tr("Note: GPL is not compatible with the OSM license. Do not upload GPL licensed tracks.") + "</html>";
|
---|
| 45 |
|
---|
[8846] | 46 | private static final String[] LICENSES = {
|
---|
| 47 | "Creative Commons By-SA",
|
---|
| 48 | "Open Database License (ODbL)",
|
---|
| 49 | "public domain",
|
---|
| 50 | "GNU Lesser Public License (LGPL)",
|
---|
| 51 | "BSD License (MIT/X11)"};
|
---|
| 52 |
|
---|
| 53 | private static final String[] URLS = {
|
---|
| 54 | "https://creativecommons.org/licenses/by-sa/3.0",
|
---|
| 55 | "http://opendatacommons.org/licenses/odbl/1.0",
|
---|
| 56 | "public domain",
|
---|
| 57 | "https://www.gnu.org/copyleft/lesser.html",
|
---|
| 58 | "http://www.opensource.org/licenses/bsd-license.php"};
|
---|
| 59 |
|
---|
[5886] | 60 | /**
|
---|
| 61 | * Constructs a new {@code GpxExporter}.
|
---|
| 62 | */
|
---|
[1949] | 63 | public GpxExporter() {
|
---|
[9461] | 64 | super(GpxImporter.getFileFilter());
|
---|
[1949] | 65 | }
|
---|
| 66 |
|
---|
| 67 | @Override
|
---|
| 68 | public boolean acceptFile(File pathname, Layer layer) {
|
---|
| 69 | if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
| 70 | return false;
|
---|
| 71 | return super.acceptFile(pathname, layer);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | @Override
|
---|
| 75 | public void exportData(File file, Layer layer) throws IOException {
|
---|
[2852] | 76 | CheckParameterUtil.ensureParameterNotNull(layer, "layer");
|
---|
[1949] | 77 | if (!(layer instanceof OsmDataLayer) && !(layer instanceof GpxLayer))
|
---|
[2852] | 78 | throw new IllegalArgumentException(MessageFormat.format("Expected instance of OsmDataLayer or GpxLayer. Got ''{0}''.", layer
|
---|
[1949] | 79 | .getClass().getName()));
|
---|
[2852] | 80 | CheckParameterUtil.ensureParameterNotNull(file, "file");
|
---|
[1949] | 81 |
|
---|
| 82 | String fn = file.getPath();
|
---|
| 83 | if (fn.indexOf('.') == -1) {
|
---|
| 84 | fn += ".gpx";
|
---|
| 85 | file = new File(fn);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // open the dialog asking for options
|
---|
| 89 | JPanel p = new JPanel(new GridBagLayout());
|
---|
| 90 |
|
---|
[5396] | 91 | GpxData gpxData;
|
---|
| 92 | // At this moment, we only need to know the attributes of the GpxData,
|
---|
[7414] | 93 | // conversion of OsmDataLayer (if needed) will be done after the dialog is closed.
|
---|
[5396] | 94 | if (layer instanceof GpxLayer) {
|
---|
| 95 | gpxData = ((GpxLayer) layer).data;
|
---|
| 96 | } else {
|
---|
| 97 | gpxData = new GpxData();
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[2181] | 100 | p.add(new JLabel(tr("GPS track description")), GBC.eol());
|
---|
[5886] | 101 | JosmTextArea desc = new JosmTextArea(3, 40);
|
---|
[1949] | 102 | desc.setWrapStyleWord(true);
|
---|
| 103 | desc.setLineWrap(true);
|
---|
[7518] | 104 | desc.setText(gpxData.getString(META_DESC));
|
---|
[1949] | 105 | p.add(new JScrollPane(desc), GBC.eop().fill(GBC.BOTH));
|
---|
| 106 |
|
---|
| 107 | JCheckBox author = new JCheckBox(tr("Add author information"), Main.pref.getBoolean("lastAddAuthor", true));
|
---|
| 108 | p.add(author, GBC.eol());
|
---|
[8426] | 109 |
|
---|
[1949] | 110 | JLabel nameLabel = new JLabel(tr("Real name"));
|
---|
| 111 | p.add(nameLabel, GBC.std().insets(10, 0, 5, 0));
|
---|
[5886] | 112 | JosmTextField authorName = new JosmTextField();
|
---|
[1949] | 113 | p.add(authorName, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[8426] | 114 | nameLabel.setLabelFor(authorName);
|
---|
| 115 |
|
---|
[1949] | 116 | JLabel emailLabel = new JLabel(tr("E-Mail"));
|
---|
| 117 | p.add(emailLabel, GBC.std().insets(10, 0, 5, 0));
|
---|
[5886] | 118 | JosmTextField email = new JosmTextField();
|
---|
[1949] | 119 | p.add(email, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[8426] | 120 | emailLabel.setLabelFor(email);
|
---|
| 121 |
|
---|
[1949] | 122 | JLabel copyrightLabel = new JLabel(tr("Copyright (URL)"));
|
---|
| 123 | p.add(copyrightLabel, GBC.std().insets(10, 0, 5, 0));
|
---|
[5886] | 124 | JosmTextField copyright = new JosmTextField();
|
---|
[1949] | 125 | p.add(copyright, GBC.std().fill(GBC.HORIZONTAL));
|
---|
[8426] | 126 | copyrightLabel.setLabelFor(copyright);
|
---|
| 127 |
|
---|
[1949] | 128 | JButton predefined = new JButton(tr("Predefined"));
|
---|
| 129 | p.add(predefined, GBC.eol().insets(5, 0, 0, 0));
|
---|
[8426] | 130 |
|
---|
[1949] | 131 | JLabel copyrightYearLabel = new JLabel(tr("Copyright year"));
|
---|
| 132 | p.add(copyrightYearLabel, GBC.std().insets(10, 0, 5, 5));
|
---|
[5886] | 133 | JosmTextField copyrightYear = new JosmTextField("");
|
---|
[1949] | 134 | p.add(copyrightYear, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
[8426] | 135 | copyrightYearLabel.setLabelFor(copyrightYear);
|
---|
| 136 |
|
---|
[1949] | 137 | JLabel warning = new JLabel("<html><font size='-2'> </html");
|
---|
| 138 | p.add(warning, GBC.eol().fill(GBC.HORIZONTAL).insets(15, 0, 0, 0));
|
---|
[5396] | 139 | addDependencies(gpxData, author, authorName, email, copyright, predefined, copyrightYear, nameLabel, emailLabel,
|
---|
[1949] | 140 | copyrightLabel, copyrightYearLabel, warning);
|
---|
| 141 |
|
---|
| 142 | p.add(new JLabel(tr("Keywords")), GBC.eol());
|
---|
[5886] | 143 | JosmTextField keywords = new JosmTextField();
|
---|
[7518] | 144 | keywords.setText(gpxData.getString(META_KEYWORDS));
|
---|
[1949] | 145 | p.add(keywords, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
| 146 |
|
---|
[2032] | 147 | ExtendedDialog ed = new ExtendedDialog(Main.parent,
|
---|
| 148 | tr("Export options"),
|
---|
[8443] | 149 | new String[] {tr("Export and Save"), tr("Cancel")});
|
---|
| 150 | ed.setButtonIcons(new String[] {"exportgpx", "cancel"});
|
---|
[2032] | 151 | ed.setContent(p);
|
---|
| 152 | ed.showDialog();
|
---|
| 153 |
|
---|
[6815] | 154 | if (ed.getValue() != 1) {
|
---|
| 155 | setCanceled(true);
|
---|
[1949] | 156 | return;
|
---|
[6815] | 157 | }
|
---|
| 158 | setCanceled(false);
|
---|
[1949] | 159 |
|
---|
| 160 | Main.pref.put("lastAddAuthor", author.isSelected());
|
---|
[8461] | 161 | if (!authorName.getText().isEmpty()) {
|
---|
[1949] | 162 | Main.pref.put("lastAuthorName", authorName.getText());
|
---|
| 163 | }
|
---|
[8461] | 164 | if (!copyright.getText().isEmpty()) {
|
---|
[1949] | 165 | Main.pref.put("lastCopyright", copyright.getText());
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (layer instanceof OsmDataLayer) {
|
---|
| 169 | gpxData = ((OsmDataLayer) layer).toGpxData();
|
---|
| 170 | } else if (layer instanceof GpxLayer) {
|
---|
| 171 | gpxData = ((GpxLayer) layer).data;
|
---|
| 172 | } else {
|
---|
[10446] | 173 | gpxData = OsmDataLayer.toGpxData(Main.getLayerManager().getEditDataSet(), file);
|
---|
[1949] | 174 | }
|
---|
| 175 |
|
---|
| 176 | // add author and copyright details to the gpx data
|
---|
| 177 | if (author.isSelected()) {
|
---|
[8461] | 178 | if (!authorName.getText().isEmpty()) {
|
---|
[7518] | 179 | gpxData.put(META_AUTHOR_NAME, authorName.getText());
|
---|
| 180 | gpxData.put(META_COPYRIGHT_AUTHOR, authorName.getText());
|
---|
[1949] | 181 | }
|
---|
[8461] | 182 | if (!email.getText().isEmpty()) {
|
---|
[7518] | 183 | gpxData.put(META_AUTHOR_EMAIL, email.getText());
|
---|
[1949] | 184 | }
|
---|
[8461] | 185 | if (!copyright.getText().isEmpty()) {
|
---|
[7518] | 186 | gpxData.put(META_COPYRIGHT_LICENSE, copyright.getText());
|
---|
[1949] | 187 | }
|
---|
[8461] | 188 | if (!copyrightYear.getText().isEmpty()) {
|
---|
[7518] | 189 | gpxData.put(META_COPYRIGHT_YEAR, copyrightYear.getText());
|
---|
[1949] | 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | // add the description to the gpx data
|
---|
[8461] | 194 | if (!desc.getText().isEmpty()) {
|
---|
[7518] | 195 | gpxData.put(META_DESC, desc.getText());
|
---|
[1949] | 196 | }
|
---|
| 197 |
|
---|
| 198 | // add keywords to the gpx data
|
---|
[8461] | 199 | if (!keywords.getText().isEmpty()) {
|
---|
[7518] | 200 | gpxData.put(META_KEYWORDS, keywords.getText());
|
---|
[1949] | 201 | }
|
---|
| 202 |
|
---|
[7037] | 203 | try (OutputStream fo = Compression.getCompressedFileOutputStream(file)) {
|
---|
[1949] | 204 | new GpxWriter(fo).write(gpxData);
|
---|
| 205 | fo.flush();
|
---|
| 206 | } catch (IOException x) {
|
---|
[6643] | 207 | Main.error(x);
|
---|
[2017] | 208 | JOptionPane.showMessageDialog(Main.parent, tr("Error while exporting {0}:\n{1}", fn, x.getMessage()),
|
---|
[1949] | 209 | tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 |
|
---|
[5886] | 213 | private static void enableCopyright(final GpxData data, final JosmTextField copyright, final JButton predefined,
|
---|
| 214 | final JosmTextField copyrightYear, final JLabel copyrightLabel, final JLabel copyrightYearLabel,
|
---|
[1949] | 215 | final JLabel warning, boolean enable) {
|
---|
| 216 | copyright.setEnabled(enable);
|
---|
| 217 | predefined.setEnabled(enable);
|
---|
| 218 | copyrightYear.setEnabled(enable);
|
---|
| 219 | copyrightLabel.setEnabled(enable);
|
---|
| 220 | copyrightYearLabel.setEnabled(enable);
|
---|
[7414] | 221 | warning.setText(enable ? GPL_WARNING : "<html><font size='-2'> </html");
|
---|
[1949] | 222 |
|
---|
[5396] | 223 | if (enable) {
|
---|
[7834] | 224 | if (copyrightYear.getText().isEmpty()) {
|
---|
[7518] | 225 | String sCopyrightYear = data.getString(META_COPYRIGHT_YEAR);
|
---|
[5396] | 226 | if (sCopyrightYear == null) {
|
---|
[11035] | 227 | sCopyrightYear = Year.now().toString();
|
---|
[5396] | 228 | }
|
---|
| 229 | copyrightYear.setText(sCopyrightYear);
|
---|
| 230 | }
|
---|
[7834] | 231 | if (copyright.getText().isEmpty()) {
|
---|
[7518] | 232 | String sCopyright = data.getString(META_COPYRIGHT_LICENSE);
|
---|
[5396] | 233 | if (sCopyright == null) {
|
---|
[6920] | 234 | sCopyright = Main.pref.get("lastCopyright", "https://creativecommons.org/licenses/by-sa/2.5");
|
---|
[5396] | 235 | }
|
---|
| 236 | copyright.setText(sCopyright);
|
---|
| 237 | copyright.setCaretPosition(0);
|
---|
| 238 | }
|
---|
| 239 | } else {
|
---|
[1949] | 240 | copyrightYear.setText("");
|
---|
| 241 | copyright.setText("");
|
---|
| 242 | }
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[8540] | 245 | // CHECKSTYLE.OFF: ParameterNumber
|
---|
| 246 |
|
---|
[1949] | 247 | /**
|
---|
| 248 | * Add all those listeners to handle the enable state of the fields.
|
---|
[9231] | 249 | * @param data GPX data
|
---|
| 250 | * @param author Author checkbox
|
---|
| 251 | * @param authorName Author name textfield
|
---|
| 252 | * @param email E-mail textfield
|
---|
| 253 | * @param copyright Copyright textfield
|
---|
| 254 | * @param predefined Predefined button
|
---|
| 255 | * @param copyrightYear Copyright year textfield
|
---|
| 256 | * @param nameLabel Name label
|
---|
| 257 | * @param emailLabel E-mail label
|
---|
| 258 | * @param copyrightLabel Copyright label
|
---|
| 259 | * @param copyrightYearLabel Copyright year label
|
---|
| 260 | * @param warning Warning label
|
---|
[1949] | 261 | */
|
---|
| 262 | private static void addDependencies(
|
---|
[5396] | 263 | final GpxData data,
|
---|
[1949] | 264 | final JCheckBox author,
|
---|
[5886] | 265 | final JosmTextField authorName,
|
---|
| 266 | final JosmTextField email,
|
---|
| 267 | final JosmTextField copyright,
|
---|
[1949] | 268 | final JButton predefined,
|
---|
[5886] | 269 | final JosmTextField copyrightYear,
|
---|
[1949] | 270 | final JLabel nameLabel,
|
---|
| 271 | final JLabel emailLabel,
|
---|
| 272 | final JLabel copyrightLabel,
|
---|
| 273 | final JLabel copyrightYearLabel,
|
---|
| 274 | final JLabel warning) {
|
---|
| 275 |
|
---|
[8540] | 276 | // CHECKSTYLE.ON: ParameterNumber
|
---|
[10615] | 277 | ActionListener authorActionListener = e -> {
|
---|
| 278 | boolean b = author.isSelected();
|
---|
| 279 | authorName.setEnabled(b);
|
---|
| 280 | email.setEnabled(b);
|
---|
| 281 | nameLabel.setEnabled(b);
|
---|
| 282 | emailLabel.setEnabled(b);
|
---|
| 283 | if (b) {
|
---|
| 284 | String sAuthorName = data.getString(META_AUTHOR_NAME);
|
---|
| 285 | if (sAuthorName == null) {
|
---|
| 286 | sAuthorName = Main.pref.get("lastAuthorName");
|
---|
[5396] | 287 | }
|
---|
[10615] | 288 | authorName.setText(sAuthorName);
|
---|
| 289 | String sEmail = data.getString(META_AUTHOR_EMAIL);
|
---|
| 290 | if (sEmail == null) {
|
---|
| 291 | sEmail = Main.pref.get("lastAuthorEmail");
|
---|
| 292 | }
|
---|
| 293 | email.setText(sEmail);
|
---|
| 294 | } else {
|
---|
| 295 | authorName.setText("");
|
---|
| 296 | email.setText("");
|
---|
[1949] | 297 | }
|
---|
[10615] | 298 | boolean isAuthorSet = !authorName.getText().isEmpty();
|
---|
| 299 | GpxExporter.enableCopyright(data, copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning,
|
---|
| 300 | b && isAuthorSet);
|
---|
[1949] | 301 | };
|
---|
| 302 | author.addActionListener(authorActionListener);
|
---|
| 303 |
|
---|
[8510] | 304 | KeyAdapter authorNameListener = new KeyAdapter() {
|
---|
[1949] | 305 | @Override public void keyReleased(KeyEvent e) {
|
---|
[8461] | 306 | boolean b = !authorName.getText().isEmpty() && author.isSelected();
|
---|
[5396] | 307 | GpxExporter.enableCopyright(data, copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning, b);
|
---|
[1949] | 308 | }
|
---|
| 309 | };
|
---|
| 310 | authorName.addKeyListener(authorNameListener);
|
---|
| 311 |
|
---|
[10615] | 312 | predefined.addActionListener(e -> {
|
---|
| 313 | JList<String> l = new JList<>(LICENSES);
|
---|
| 314 | l.setVisibleRowCount(LICENSES.length);
|
---|
| 315 | l.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
| 316 | int answer = JOptionPane.showConfirmDialog(
|
---|
| 317 | Main.parent,
|
---|
| 318 | new JScrollPane(l),
|
---|
| 319 | tr("Choose a predefined license"),
|
---|
| 320 | JOptionPane.OK_CANCEL_OPTION,
|
---|
| 321 | JOptionPane.QUESTION_MESSAGE
|
---|
| 322 | );
|
---|
| 323 | if (answer != JOptionPane.OK_OPTION || l.getSelectedIndex() == -1)
|
---|
| 324 | return;
|
---|
| 325 | StringBuilder license = new StringBuilder();
|
---|
| 326 | for (int i : l.getSelectedIndices()) {
|
---|
| 327 | if (i == 2) {
|
---|
| 328 | license = new StringBuilder("public domain");
|
---|
| 329 | break;
|
---|
[1949] | 330 | }
|
---|
[10615] | 331 | if (license.length() > 0) {
|
---|
| 332 | license.append(", ");
|
---|
| 333 | }
|
---|
| 334 | license.append(URLS[i]);
|
---|
[1949] | 335 | }
|
---|
[10615] | 336 | copyright.setText(license.toString());
|
---|
| 337 | copyright.setCaretPosition(0);
|
---|
[1949] | 338 | });
|
---|
| 339 |
|
---|
| 340 | authorActionListener.actionPerformed(null);
|
---|
| 341 | authorNameListener.keyReleased(null);
|
---|
| 342 | }
|
---|
| 343 | }
|
---|