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