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

Last change on this file since 8426 was 8426, checked in by Don-vip, 9 years ago

Accessibility - global use of JLabel.setLabelFor() + various fixes (javadoc, code style)

  • Property svn:eol-style set to native
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.Writer;
20import java.net.MalformedURLException;
21import java.net.URI;
22import java.net.URL;
23import java.nio.charset.StandardCharsets;
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.SwingConstants;
33
34import org.openstreetmap.josm.gui.layer.GpxLayer;
35import org.openstreetmap.josm.gui.layer.Layer;
36import org.openstreetmap.josm.gui.util.GuiHelper;
37import org.openstreetmap.josm.gui.widgets.JosmTextField;
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 lbl.setLabelFor(export);
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 GPX file on your local disk."));
71 /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));
72 include.setToolTipText(tr("Include GPX 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 JosmTextField tf = new JosmTextField();
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("GPX 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 @Override
110 public void actionPerformed(ActionEvent e) {
111 cl.show(cards, "link");
112 }
113 });
114 include.addActionListener(new ActionListener() {
115 @Override
116 public void actionPerformed(ActionEvent e) {
117 cl.show(cards, "include");
118 }
119 });
120
121 topRow.add(export, GBC.std());
122 topRow.add(lbl, GBC.std());
123 topRow.add(GBC.glue(1,0), GBC.std().fill(GBC.HORIZONTAL));
124 p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL));
125 p.add(lblData, GBC.std().insets(10,0,0,0));
126 p.add(link, GBC.std());
127 p.add(include, GBC.eol());
128 p.add(cards, GBC.eol().insets(15,0,3,3));
129
130 export.addItemListener(new ItemListener() {
131 @Override
132 public void itemStateChanged(ItemEvent e) {
133 if (e.getStateChange() == ItemEvent.DESELECTED) {
134 GuiHelper.setEnabledRec(p, false);
135 export.setEnabled(true);
136 } else {
137 GuiHelper.setEnabledRec(p, true);
138 link.setEnabled(file != null);
139 }
140 }
141 });
142 return p;
143 }
144
145 @Override
146 public boolean shallExport() {
147 return export.isSelected();
148 }
149
150 @Override
151 public boolean requiresZip() {
152 return include.isSelected();
153 }
154
155 @Override
156 public Element export(ExportSupport support) throws IOException {
157 Element layerEl = support.createElement("layer");
158 layerEl.setAttribute("type", "tracks");
159 layerEl.setAttribute("version", "0.1");
160
161 Element file = support.createElement("file");
162 layerEl.appendChild(file);
163
164 if (requiresZip()) {
165 String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.gpx";
166 file.appendChild(support.createTextNode(zipPath));
167 addDataFile(support.getOutputStreamZip(zipPath));
168 } else {
169 URI uri = layer.getAssociatedFile().toURI();
170 URL url = null;
171 try {
172 url = uri.toURL();
173 } catch (MalformedURLException e) {
174 throw new IOException(e);
175 }
176 file.appendChild(support.createTextNode(url.toString()));
177 }
178 return layerEl;
179 }
180
181 @SuppressWarnings("resource")
182 protected void addDataFile(OutputStream out) {
183 Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
184 GpxWriter w = new GpxWriter(new PrintWriter(writer));
185 w.write(layer.data);
186 w.flush();
187 }
188}
Note: See TracBrowser for help on using the repository browser.