source: josm/trunk/src/org/openstreetmap/josm/io/session/GenericSessionExporter.java@ 18466

Last change on this file since 18466 was 18466, checked in by taylor.smock, 23 months ago

Fix #21813: Improve marker handling in sessions and #21923: Improve session workflow/Add "save session" (patch by Bjoeni)

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