source: josm/trunk/src/org/openstreetmap/josm/io/session/GpxTracksSessionExporter.java@ 5501

Last change on this file since 5501 was 5501, checked in by bastiK, 12 years ago

add session support for gpx layers (see #4029)

File size: 6.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.session;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.CardLayout;
7import java.awt.Component;
8import java.awt.Font;
9import java.awt.GridBagLayout;
10import java.awt.event.ActionEvent;
11import java.awt.event.ActionListener;
12import java.awt.event.ItemEvent;
13import java.awt.event.ItemListener;
14import java.io.File;
15import java.io.IOException;
16import java.io.OutputStream;
17import java.io.OutputStreamWriter;
18import java.io.PrintWriter;
19import java.io.UnsupportedEncodingException;
20import java.io.Writer;
21import java.net.MalformedURLException;
22import java.net.URI;
23import java.net.URL;
24import java.util.Collection;
25import java.util.Collections;
26
27import javax.swing.ButtonGroup;
28import javax.swing.JCheckBox;
29import javax.swing.JLabel;
30import javax.swing.JPanel;
31import javax.swing.JRadioButton;
32import javax.swing.JTextField;
33import javax.swing.SwingConstants;
34
35import org.openstreetmap.josm.gui.layer.GpxLayer;
36import org.openstreetmap.josm.gui.layer.Layer;
37import org.openstreetmap.josm.gui.util.GuiHelper;
38import org.openstreetmap.josm.io.GpxWriter;
39import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;
40import org.openstreetmap.josm.tools.GBC;
41import org.w3c.dom.Element;
42
43public 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}
Note: See TracBrowser for help on using the repository browser.