1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
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.util.Calendar;
|
---|
15 |
|
---|
16 | import javax.swing.JButton;
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JFileChooser;
|
---|
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.data.gpx.GpxData;
|
---|
30 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
31 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
32 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
33 | import org.openstreetmap.josm.io.GpxWriter;
|
---|
34 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
35 | import org.openstreetmap.josm.tools.GBC;
|
---|
36 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Exports data to gpx.
|
---|
40 | */
|
---|
41 | public class GpxExportAction extends DiskAccessAction {
|
---|
42 |
|
---|
43 | private final static String warningGpl = "<html><font color='red' size='-2'>"+tr("Note: GPL is not compatible with the OSM license. Do not upload GPL licensed tracks.")+"</html>";
|
---|
44 |
|
---|
45 | private final Layer layer;
|
---|
46 |
|
---|
47 | public GpxExportAction(Layer layer) {
|
---|
48 | super(tr("Export to GPX..."), "exportgpx", tr("Export the data to GPX file."),
|
---|
49 | Shortcut.registerShortcut("file:exportgpx", tr("Export to GPX..."), KeyEvent.VK_E, Shortcut.GROUP_MENU));
|
---|
50 | this.layer = layer;
|
---|
51 | }
|
---|
52 |
|
---|
53 | public void actionPerformed(ActionEvent e) {
|
---|
54 | if (layer == null && Main.map == null) {
|
---|
55 | JOptionPane.showMessageDialog(Main.parent, tr("Nothing to export. Get some data first."));
|
---|
56 | return;
|
---|
57 | }
|
---|
58 |
|
---|
59 | JFileChooser fc = createAndOpenFileChooser(false, false, null);
|
---|
60 | if (fc == null)
|
---|
61 | return;
|
---|
62 | File file = fc.getSelectedFile();
|
---|
63 | if (file == null)
|
---|
64 | return;
|
---|
65 |
|
---|
66 | exportGpx(file, this.layer == null ? Main.main.editLayer() : this.layer);
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static void exportGpx(File file, Layer layer) {
|
---|
70 | String fn = file.getPath();
|
---|
71 | if (fn.indexOf('.') == -1) {
|
---|
72 | fn += ".gpx";
|
---|
73 | file = new File(fn);
|
---|
74 | }
|
---|
75 |
|
---|
76 | // open the dialog asking for options
|
---|
77 | JPanel p = new JPanel(new GridBagLayout());
|
---|
78 |
|
---|
79 | p.add(new JLabel(tr("gps track description")), GBC.eol());
|
---|
80 | JTextArea desc = new JTextArea(3,40);
|
---|
81 | desc.setWrapStyleWord(true);
|
---|
82 | desc.setLineWrap(true);
|
---|
83 | p.add(new JScrollPane(desc), GBC.eop().fill(GBC.BOTH));
|
---|
84 |
|
---|
85 | JCheckBox author = new JCheckBox(tr("Add author information"), Main.pref.getBoolean("lastAddAuthor", true));
|
---|
86 | author.setSelected(true);
|
---|
87 | p.add(author, GBC.eol());
|
---|
88 | JLabel nameLabel = new JLabel(tr("Real name"));
|
---|
89 | p.add(nameLabel, GBC.std().insets(10,0,5,0));
|
---|
90 | JTextField authorName = new JTextField(Main.pref.get("lastAuthorName"));
|
---|
91 | p.add(authorName, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
92 | JLabel emailLabel = new JLabel(tr("Email"));
|
---|
93 | p.add(emailLabel, GBC.std().insets(10,0,5,0));
|
---|
94 | JTextField email = new JTextField(Main.pref.get("osm-server.username"));
|
---|
95 | p.add(email, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
96 | JLabel copyrightLabel = new JLabel(tr("Copyright (URL)"));
|
---|
97 | p.add(copyrightLabel, GBC.std().insets(10,0,5,0));
|
---|
98 | JTextField copyright = new JTextField();
|
---|
99 | p.add(copyright, GBC.std().fill(GBC.HORIZONTAL));
|
---|
100 | JButton predefined = new JButton(tr("Predefined"));
|
---|
101 | p.add(predefined, GBC.eol().insets(5,0,0,0));
|
---|
102 | JLabel copyrightYearLabel = new JLabel(tr("Copyright year"));
|
---|
103 | p.add(copyrightYearLabel, GBC.std().insets(10,0,5,5));
|
---|
104 | JTextField copyrightYear = new JTextField("");
|
---|
105 | p.add(copyrightYear, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
106 | JLabel warning = new JLabel("<html><font size='-2'> </html");
|
---|
107 | p.add(warning, GBC.eol().fill(GBC.HORIZONTAL).insets(15,0,0,0));
|
---|
108 | addDependencies(author, authorName, email, copyright, predefined, copyrightYear, nameLabel, emailLabel, copyrightLabel, copyrightYearLabel, warning);
|
---|
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 | int answer = new ExtendedDialog(Main.parent,
|
---|
115 | tr("Export options"),
|
---|
116 | p,
|
---|
117 | new String[] {tr("Export and Save"), tr("Cancel")},
|
---|
118 | new String[] {"exportgpx.png", "cancel.png"}).getValue();
|
---|
119 | if (answer != 1)
|
---|
120 | return;
|
---|
121 |
|
---|
122 | Main.pref.put("lastAddAuthor", author.isSelected());
|
---|
123 | if (authorName.getText().length() != 0)
|
---|
124 | Main.pref.put("lastAuthorName", authorName.getText());
|
---|
125 | if (copyright.getText().length() != 0)
|
---|
126 | Main.pref.put("lastCopyright", copyright.getText());
|
---|
127 |
|
---|
128 | GpxData gpxData;
|
---|
129 | if (layer instanceof OsmDataLayer)
|
---|
130 | gpxData = ((OsmDataLayer)layer).toGpxData();
|
---|
131 | else if (layer instanceof GpxLayer)
|
---|
132 | gpxData = ((GpxLayer)layer).data;
|
---|
133 | else
|
---|
134 | gpxData = OsmDataLayer.toGpxData(Main.ds);
|
---|
135 | // TODO: add copyright, etc.
|
---|
136 | try {
|
---|
137 | FileOutputStream fo = new FileOutputStream(file);
|
---|
138 | new GpxWriter(fo).write(gpxData);
|
---|
139 | fo.flush();
|
---|
140 | fo.close();
|
---|
141 | } catch (IOException x) {
|
---|
142 | x.printStackTrace();
|
---|
143 | JOptionPane.showMessageDialog(Main.parent, tr("Error while exporting {0}:\n{1}", fn,x.getMessage()), tr("Error"), JOptionPane.ERROR_MESSAGE);
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Add all those listeners to handle the enable state of the fields.
|
---|
149 | * @param copyrightYearLabel
|
---|
150 | * @param copyrightLabel
|
---|
151 | * @param emailLabel
|
---|
152 | * @param nameLabel
|
---|
153 | * @param warning
|
---|
154 | */
|
---|
155 | private static void addDependencies(
|
---|
156 | final JCheckBox author,
|
---|
157 | final JTextField authorName,
|
---|
158 | final JTextField email,
|
---|
159 | final JTextField copyright,
|
---|
160 | final JButton predefined,
|
---|
161 | final JTextField copyrightYear,
|
---|
162 | final JLabel nameLabel,
|
---|
163 | final JLabel emailLabel,
|
---|
164 | final JLabel copyrightLabel,
|
---|
165 | final JLabel copyrightYearLabel,
|
---|
166 | final JLabel warning) {
|
---|
167 |
|
---|
168 | ActionListener authorActionListener = new ActionListener(){
|
---|
169 | public void actionPerformed(ActionEvent e) {
|
---|
170 | boolean b = author.isSelected();
|
---|
171 | authorName.setEnabled(b);
|
---|
172 | email.setEnabled(b);
|
---|
173 | nameLabel.setEnabled(b);
|
---|
174 | emailLabel.setEnabled(b);
|
---|
175 | authorName.setText(b ? Main.pref.get("lastAuthorName") : "");
|
---|
176 | email.setText(b ? Main.pref.get("osm-server.username") : "");
|
---|
177 |
|
---|
178 | boolean authorSet = authorName.getText().length() != 0;
|
---|
179 | enableCopyright(copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning, b && authorSet);
|
---|
180 | }
|
---|
181 | };
|
---|
182 | author.addActionListener(authorActionListener);
|
---|
183 |
|
---|
184 | KeyAdapter authorNameListener = new KeyAdapter(){
|
---|
185 | @Override public void keyReleased(KeyEvent e) {
|
---|
186 | boolean b = authorName.getText().length()!=0 && author.isSelected();
|
---|
187 | enableCopyright(copyright, predefined, copyrightYear, copyrightLabel, copyrightYearLabel, warning, b);
|
---|
188 | }
|
---|
189 | };
|
---|
190 | authorName.addKeyListener(authorNameListener);
|
---|
191 |
|
---|
192 | predefined.addActionListener(new ActionListener(){
|
---|
193 | public void actionPerformed(ActionEvent e) {
|
---|
194 | JList l = new JList(new String[]{"Creative Commons By-SA", "public domain", "GNU Lesser Public License (LGPL)", "BSD License (MIT/X11)"});
|
---|
195 | l.setVisibleRowCount(4);
|
---|
196 | l.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
|
---|
197 | int answer = JOptionPane.showConfirmDialog(Main.parent, new JScrollPane(l),tr("Choose a predefined license"), JOptionPane.OK_CANCEL_OPTION);
|
---|
198 | if (answer != JOptionPane.OK_OPTION || l.getSelectedIndex() == -1)
|
---|
199 | return;
|
---|
200 | final String[] urls = {
|
---|
201 | "http://creativecommons.org/licenses/by-sa/2.5",
|
---|
202 | "public domain",
|
---|
203 | "http://www.gnu.org/copyleft/lesser.html",
|
---|
204 | "http://www.opensource.org/licenses/bsd-license.php"};
|
---|
205 | String license = "";
|
---|
206 | for (int i : l.getSelectedIndices()) {
|
---|
207 | if (i == 1) {
|
---|
208 | license = "public domain";
|
---|
209 | break;
|
---|
210 | }
|
---|
211 | license += license.length()==0 ? urls[i] : ", "+urls[i];
|
---|
212 | }
|
---|
213 | copyright.setText(license);
|
---|
214 | copyright.setCaretPosition(0);
|
---|
215 | }
|
---|
216 | });
|
---|
217 |
|
---|
218 | authorActionListener.actionPerformed(null);
|
---|
219 | authorNameListener.keyReleased(null);
|
---|
220 | }
|
---|
221 |
|
---|
222 | private static void enableCopyright(final JTextField copyright, final JButton predefined, final JTextField copyrightYear, final JLabel copyrightLabel, final JLabel copyrightYearLabel, final JLabel warning, boolean enable) {
|
---|
223 | copyright.setEnabled(enable);
|
---|
224 | predefined.setEnabled(enable);
|
---|
225 | copyrightYear.setEnabled(enable);
|
---|
226 | copyrightLabel.setEnabled(enable);
|
---|
227 | copyrightYearLabel.setEnabled(enable);
|
---|
228 | warning.setText(enable ? warningGpl : "<html><font size='-2'> </html");
|
---|
229 |
|
---|
230 | if (enable && copyrightYear.getText().length()==0) {
|
---|
231 | copyrightYear.setText(enable ? Integer.toString(Calendar.getInstance().get(Calendar.YEAR)) : "");
|
---|
232 | } else if (!enable)
|
---|
233 | copyrightYear.setText("");
|
---|
234 |
|
---|
235 | if (enable && copyright.getText().length()==0) {
|
---|
236 | copyright.setText(enable ? Main.pref.get("lastCopyright", "http://creativecommons.org/licenses/by-sa/2.5") : "");
|
---|
237 | copyright.setCaretPosition(0);
|
---|
238 | } else if (!enable)
|
---|
239 | copyright.setText("");
|
---|
240 | }
|
---|
241 | }
|
---|