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

Last change on this file since 13228 was 10615, checked in by Don-vip, 8 years ago

see #11390 - sonar - squid:S1604 - Java 8: Anonymous inner classes containing only one method should become lambdas

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