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