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.Font;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.Insets;
|
---|
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.AbstractAction;
|
---|
28 | import javax.swing.ButtonGroup;
|
---|
29 | import javax.swing.JButton;
|
---|
30 | import javax.swing.JCheckBox;
|
---|
31 | import javax.swing.JLabel;
|
---|
32 | import javax.swing.JPanel;
|
---|
33 | import javax.swing.JRadioButton;
|
---|
34 | import javax.swing.SwingConstants;
|
---|
35 |
|
---|
36 | import org.w3c.dom.Element;
|
---|
37 |
|
---|
38 | import org.openstreetmap.josm.actions.SaveAction;
|
---|
39 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
40 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
41 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
---|
42 | import org.openstreetmap.josm.gui.widgets.JosmTextField;
|
---|
43 | import org.openstreetmap.josm.io.OsmWriter;
|
---|
44 | import org.openstreetmap.josm.io.OsmWriterFactory;
|
---|
45 | import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;
|
---|
46 | import org.openstreetmap.josm.tools.GBC;
|
---|
47 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
48 |
|
---|
49 | public class OsmDataSessionExporter implements SessionLayerExporter {
|
---|
50 |
|
---|
51 | private OsmDataLayer layer;
|
---|
52 | private JRadioButton link, include;
|
---|
53 | private JCheckBox export;
|
---|
54 |
|
---|
55 | public OsmDataSessionExporter(OsmDataLayer layer) {
|
---|
56 | this.layer = layer;
|
---|
57 | }
|
---|
58 |
|
---|
59 | public OsmDataSessionExporter() {
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public Collection<Layer> getDependencies() {
|
---|
64 | return Collections.emptySet();
|
---|
65 | }
|
---|
66 |
|
---|
67 | private class LayerSaveAction extends AbstractAction {
|
---|
68 | public LayerSaveAction() {
|
---|
69 | putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get());
|
---|
70 | putValue(SHORT_DESCRIPTION, layer.requiresSaveToFile() ?
|
---|
71 | tr("Layer contains unsaved data - save to file.") :
|
---|
72 | tr("Layer does not contain unsaved data."));
|
---|
73 | updateEnabledState();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public void actionPerformed(ActionEvent e) {
|
---|
77 | SaveAction.getInstance().doSave(layer);
|
---|
78 | updateEnabledState();
|
---|
79 | }
|
---|
80 |
|
---|
81 | public void updateEnabledState() {
|
---|
82 | setEnabled(layer.requiresSaveToFile());
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public JPanel getExportPanel() {
|
---|
88 | final JPanel p = new JPanel(new GridBagLayout());
|
---|
89 | JPanel topRow = new JPanel(new GridBagLayout());
|
---|
90 | export = new JCheckBox();
|
---|
91 | export.setSelected(true);
|
---|
92 | final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
|
---|
93 | lbl.setToolTipText(layer.getToolTipText());
|
---|
94 |
|
---|
95 | JLabel lblData = new JLabel(tr("Data:"));
|
---|
96 | /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file"));
|
---|
97 | link.putClientProperty("actionname", "link");
|
---|
98 | link.setToolTipText(tr("Link to a OSM data file on your local disk."));
|
---|
99 | /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));
|
---|
100 | include.setToolTipText(tr("Include OSM data in the .joz session file."));
|
---|
101 | include.putClientProperty("actionname", "include");
|
---|
102 | ButtonGroup group = new ButtonGroup();
|
---|
103 | group.add(link);
|
---|
104 | group.add(include);
|
---|
105 |
|
---|
106 | JPanel cardLink = new JPanel(new GridBagLayout());
|
---|
107 | final File file = layer.getAssociatedFile();
|
---|
108 | final LayerSaveAction saveAction = new LayerSaveAction();
|
---|
109 | final JButton save = new JButton(saveAction);
|
---|
110 | if (file != null) {
|
---|
111 | JosmTextField tf = new JosmTextField();
|
---|
112 | tf.setText(file.getPath());
|
---|
113 | tf.setEditable(false);
|
---|
114 | cardLink.add(tf, GBC.std());
|
---|
115 | save.setMargin(new Insets(0,0,0,0));
|
---|
116 | cardLink.add(save, GBC.eol().insets(2,0,0,0));
|
---|
117 | } else {
|
---|
118 | cardLink.add(new JLabel(tr("No file association")), GBC.eol());
|
---|
119 | }
|
---|
120 |
|
---|
121 | JPanel cardInclude = new JPanel(new GridBagLayout());
|
---|
122 | JLabel lblIncl = new JLabel(tr("OSM data will be included in the session file."));
|
---|
123 | lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN));
|
---|
124 | cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
125 |
|
---|
126 | final CardLayout cl = new CardLayout();
|
---|
127 | final JPanel cards = new JPanel(cl);
|
---|
128 | cards.add(cardLink, "link");
|
---|
129 | cards.add(cardInclude, "include");
|
---|
130 |
|
---|
131 | if (file != null) {
|
---|
132 | link.setSelected(true);
|
---|
133 | } else {
|
---|
134 | link.setEnabled(false);
|
---|
135 | link.setToolTipText(tr("No file association"));
|
---|
136 | include.setSelected(true);
|
---|
137 | cl.show(cards, "include");
|
---|
138 | }
|
---|
139 |
|
---|
140 | link.addActionListener(new ActionListener() {
|
---|
141 | public void actionPerformed(ActionEvent e) {
|
---|
142 | cl.show(cards, "link");
|
---|
143 | }
|
---|
144 | });
|
---|
145 | include.addActionListener(new ActionListener() {
|
---|
146 | public void actionPerformed(ActionEvent e) {
|
---|
147 | cl.show(cards, "include");
|
---|
148 | }
|
---|
149 | });
|
---|
150 |
|
---|
151 | topRow.add(export, GBC.std());
|
---|
152 | topRow.add(lbl, GBC.std());
|
---|
153 | topRow.add(GBC.glue(1,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
154 | p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
155 | p.add(lblData, GBC.std().insets(10,0,0,0));
|
---|
156 | p.add(link, GBC.std());
|
---|
157 | p.add(include, GBC.eol());
|
---|
158 | p.add(cards, GBC.eol().insets(15,0,3,3));
|
---|
159 |
|
---|
160 | export.addItemListener(new ItemListener() {
|
---|
161 | public void itemStateChanged(ItemEvent e) {
|
---|
162 | if (e.getStateChange() == ItemEvent.DESELECTED) {
|
---|
163 | GuiHelper.setEnabledRec(p, false);
|
---|
164 | export.setEnabled(true);
|
---|
165 | } else {
|
---|
166 | GuiHelper.setEnabledRec(p, true);
|
---|
167 | save.setEnabled(saveAction.isEnabled());
|
---|
168 | link.setEnabled(file != null);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | });
|
---|
172 | return p;
|
---|
173 | }
|
---|
174 |
|
---|
175 | @Override
|
---|
176 | public boolean shallExport() {
|
---|
177 | return export.isSelected();
|
---|
178 | }
|
---|
179 |
|
---|
180 | @Override
|
---|
181 | public boolean requiresZip() {
|
---|
182 | return include.isSelected();
|
---|
183 | }
|
---|
184 |
|
---|
185 | @Override
|
---|
186 | public Element export(ExportSupport support) throws IOException {
|
---|
187 | Element layerEl = support.createElement("layer");
|
---|
188 | layerEl.setAttribute("type", "osm-data");
|
---|
189 | layerEl.setAttribute("version", "0.1");
|
---|
190 |
|
---|
191 | Element file = support.createElement("file");
|
---|
192 | layerEl.appendChild(file);
|
---|
193 |
|
---|
194 | if (requiresZip()) {
|
---|
195 | String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.osm";
|
---|
196 | file.appendChild(support.createTextNode(zipPath));
|
---|
197 | addDataFile(support.getOutputStreamZip(zipPath));
|
---|
198 | } else {
|
---|
199 | URI uri = layer.getAssociatedFile().toURI();
|
---|
200 | URL url = null;
|
---|
201 | try {
|
---|
202 | url = uri.toURL();
|
---|
203 | } catch (MalformedURLException e) {
|
---|
204 | throw new IOException(e);
|
---|
205 | }
|
---|
206 | file.appendChild(support.createTextNode(url.toString()));
|
---|
207 | }
|
---|
208 | return layerEl;
|
---|
209 | }
|
---|
210 |
|
---|
211 | protected void addDataFile(OutputStream out) throws IOException {
|
---|
212 | Writer writer = null;
|
---|
213 | try {
|
---|
214 | writer = new OutputStreamWriter(out, "UTF-8");
|
---|
215 | } catch (UnsupportedEncodingException e) {
|
---|
216 | throw new RuntimeException(e);
|
---|
217 | }
|
---|
218 | OsmWriter w = OsmWriterFactory.createOsmWriter(new PrintWriter(writer), false, layer.data.getVersion());
|
---|
219 | layer.data.getReadLock().lock();
|
---|
220 | try {
|
---|
221 | w.writeLayer(layer);
|
---|
222 | w.flush();
|
---|
223 | } finally {
|
---|
224 | layer.data.getReadLock().unlock();
|
---|
225 | }
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|