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