1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.session;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.CardLayout;
|
---|
7 | import java.awt.Component;
|
---|
8 | import java.awt.Font;
|
---|
9 | import java.awt.GridBagLayout;
|
---|
10 | import java.awt.event.ActionEvent;
|
---|
11 | import java.awt.event.ActionListener;
|
---|
12 | import java.awt.event.ItemEvent;
|
---|
13 | import java.awt.event.ItemListener;
|
---|
14 | import java.io.File;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.io.OutputStream;
|
---|
17 | import java.io.OutputStreamWriter;
|
---|
18 | import java.io.PrintWriter;
|
---|
19 | import java.io.UnsupportedEncodingException;
|
---|
20 | import java.io.Writer;
|
---|
21 | import java.net.MalformedURLException;
|
---|
22 | import java.net.URI;
|
---|
23 | import java.net.URL;
|
---|
24 | import java.util.Collection;
|
---|
25 | import java.util.Collections;
|
---|
26 |
|
---|
27 | import javax.swing.ButtonGroup;
|
---|
28 | import javax.swing.JCheckBox;
|
---|
29 | import javax.swing.JLabel;
|
---|
30 | import javax.swing.JPanel;
|
---|
31 | import javax.swing.JRadioButton;
|
---|
32 | import javax.swing.JTextField;
|
---|
33 | import javax.swing.SwingConstants;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
36 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
37 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
38 | import org.openstreetmap.josm.io.GpxWriter;
|
---|
39 | import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;
|
---|
40 | import org.openstreetmap.josm.tools.GBC;
|
---|
41 | import org.w3c.dom.Element;
|
---|
42 |
|
---|
43 | public class GpxTracksSessionExporter implements SessionLayerExporter {
|
---|
44 |
|
---|
45 | private GpxLayer layer;
|
---|
46 | private JRadioButton link, include;
|
---|
47 | private JCheckBox export;
|
---|
48 |
|
---|
49 | public GpxTracksSessionExporter(GpxLayer layer) {
|
---|
50 | this.layer = layer;
|
---|
51 | }
|
---|
52 |
|
---|
53 | @Override
|
---|
54 | public Collection<Layer> getDependencies() {
|
---|
55 | return Collections.emptySet();
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | public Component getExportPanel() {
|
---|
60 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
61 | JPanel topRow = new JPanel(new GridBagLayout());
|
---|
62 | export = new JCheckBox();
|
---|
63 | export.setSelected(true);
|
---|
64 | final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
|
---|
65 | lbl.setToolTipText(layer.getToolTipText());
|
---|
66 |
|
---|
67 | JLabel lblData = new JLabel(tr("Data:"));
|
---|
68 | /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file"));
|
---|
69 | link.putClientProperty("actionname", "link");
|
---|
70 | link.setToolTipText(tr("Link to a OSM data file on your local disk."));
|
---|
71 | /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));
|
---|
72 | include.setToolTipText(tr("Include OSM data in the .joz session file."));
|
---|
73 | include.putClientProperty("actionname", "include");
|
---|
74 | ButtonGroup group = new ButtonGroup();
|
---|
75 | group.add(link);
|
---|
76 | group.add(include);
|
---|
77 |
|
---|
78 | JPanel cardLink = new JPanel(new GridBagLayout());
|
---|
79 | final File file = layer.getAssociatedFile();
|
---|
80 | if (file != null) {
|
---|
81 | JTextField tf = new JTextField();
|
---|
82 | tf.setText(file.getPath());
|
---|
83 | tf.setEditable(false);
|
---|
84 | cardLink.add(tf, GBC.std());
|
---|
85 | } else {
|
---|
86 | cardLink.add(new JLabel(tr("No file association")), GBC.eol());
|
---|
87 | }
|
---|
88 |
|
---|
89 | JPanel cardInclude = new JPanel(new GridBagLayout());
|
---|
90 | JLabel lblIncl = new JLabel(tr("OSM data will be included in the session file."));
|
---|
91 | lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN));
|
---|
92 | cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
93 |
|
---|
94 | final CardLayout cl = new CardLayout();
|
---|
95 | final JPanel cards = new JPanel(cl);
|
---|
96 | cards.add(cardLink, "link");
|
---|
97 | cards.add(cardInclude, "include");
|
---|
98 |
|
---|
99 | if (file != null) {
|
---|
100 | link.setSelected(true);
|
---|
101 | } else {
|
---|
102 | link.setEnabled(false);
|
---|
103 | link.setToolTipText(tr("No file association"));
|
---|
104 | include.setSelected(true);
|
---|
105 | cl.show(cards, "include");
|
---|
106 | }
|
---|
107 |
|
---|
108 | link.addActionListener(new ActionListener() {
|
---|
109 | public void actionPerformed(ActionEvent e) {
|
---|
110 | cl.show(cards, "link");
|
---|
111 | }
|
---|
112 | });
|
---|
113 | include.addActionListener(new ActionListener() {
|
---|
114 | public void actionPerformed(ActionEvent e) {
|
---|
115 | cl.show(cards, "include");
|
---|
116 | }
|
---|
117 | });
|
---|
118 |
|
---|
119 | topRow.add(export, GBC.std());
|
---|
120 | topRow.add(lbl, GBC.std());
|
---|
121 | topRow.add(GBC.glue(1,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
122 | p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
123 | p.add(lblData, GBC.std().insets(10,0,0,0));
|
---|
124 | p.add(link, GBC.std());
|
---|
125 | p.add(include, GBC.eol());
|
---|
126 | p.add(cards, GBC.eol().insets(15,0,3,3));
|
---|
127 |
|
---|
128 | export.addItemListener(new ItemListener() {
|
---|
129 | public void itemStateChanged(ItemEvent e) {
|
---|
130 | if (e.getStateChange() == ItemEvent.DESELECTED) {
|
---|
131 | GuiHelper.setEnabledRec(p, false);
|
---|
132 | export.setEnabled(true);
|
---|
133 | } else {
|
---|
134 | GuiHelper.setEnabledRec(p, true);
|
---|
135 | link.setEnabled(file != null);
|
---|
136 | }
|
---|
137 | }
|
---|
138 | });
|
---|
139 | return p;
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public boolean shallExport() {
|
---|
144 | return export.isSelected();
|
---|
145 | }
|
---|
146 |
|
---|
147 | @Override
|
---|
148 | public boolean requiresZip() {
|
---|
149 | return include.isSelected();
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Override
|
---|
153 | public Element export(ExportSupport support) throws IOException {
|
---|
154 | Element layerEl = support.createElement("layer");
|
---|
155 | layerEl.setAttribute("type", "tracks");
|
---|
156 | layerEl.setAttribute("version", "0.1");
|
---|
157 |
|
---|
158 | Element file = support.createElement("file");
|
---|
159 | layerEl.appendChild(file);
|
---|
160 |
|
---|
161 | if (requiresZip()) {
|
---|
162 | String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.gpx";
|
---|
163 | file.appendChild(support.createTextNode(zipPath));
|
---|
164 | addDataFile(support.getOutputStreamZip(zipPath));
|
---|
165 | } else {
|
---|
166 | URI uri = layer.getAssociatedFile().toURI();
|
---|
167 | URL url = null;
|
---|
168 | try {
|
---|
169 | url = uri.toURL();
|
---|
170 | } catch (MalformedURLException e) {
|
---|
171 | throw new IOException(e);
|
---|
172 | }
|
---|
173 | file.appendChild(support.createTextNode(url.toString()));
|
---|
174 | }
|
---|
175 | return layerEl;
|
---|
176 | }
|
---|
177 |
|
---|
178 | protected void addDataFile(OutputStream out) throws IOException {
|
---|
179 | Writer writer = null;
|
---|
180 | try {
|
---|
181 | writer = new OutputStreamWriter(out, "UTF-8");
|
---|
182 | } catch (UnsupportedEncodingException e) {
|
---|
183 | throw new RuntimeException(e);
|
---|
184 | }
|
---|
185 | GpxWriter w = new GpxWriter(new PrintWriter(writer));
|
---|
186 | w.write(layer.data);
|
---|
187 | w.flush();
|
---|
188 | }
|
---|
189 |
|
---|
190 | }
|
---|