001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.io.session; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.CardLayout; 007import java.awt.Font; 008import java.awt.GridBagLayout; 009import java.awt.Insets; 010import java.awt.event.ActionEvent; 011import java.awt.event.ItemEvent; 012import java.io.File; 013import java.io.IOException; 014import java.io.OutputStream; 015import java.net.MalformedURLException; 016 017import javax.swing.AbstractAction; 018import javax.swing.ButtonGroup; 019import javax.swing.JButton; 020import javax.swing.JLabel; 021import javax.swing.JPanel; 022import javax.swing.JRadioButton; 023import javax.swing.SwingConstants; 024 025import org.openstreetmap.josm.actions.SaveAction; 026import org.openstreetmap.josm.gui.layer.AbstractModifiableLayer; 027import org.openstreetmap.josm.gui.layer.GpxLayer; 028import org.openstreetmap.josm.gui.layer.Layer; 029import org.openstreetmap.josm.gui.layer.OsmDataLayer; 030import org.openstreetmap.josm.gui.layer.SaveToFile; 031import org.openstreetmap.josm.gui.util.GuiHelper; 032import org.openstreetmap.josm.gui.widgets.JosmTextField; 033import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport; 034import org.openstreetmap.josm.plugins.PluginHandler; 035import org.openstreetmap.josm.tools.GBC; 036import org.openstreetmap.josm.tools.ImageProvider; 037import org.w3c.dom.Element; 038 039/** 040 * Generic superclass of {@link OsmDataSessionExporter} and {@link GpxTracksSessionExporter} layer exporters. 041 * @param <T> Type of exported layer 042 * @since 9470 043 */ 044public abstract class GenericSessionExporter<T extends Layer> extends AbstractSessionExporter<T> { 045 046 private final String type; 047 private final String version; 048 private final String extension; 049 050 private final JRadioButton link; 051 private final JRadioButton include; 052 053 /** 054 * Constructs a new {@code GenericSessionExporter}. 055 * @param layer layer to export 056 * @param type layer session type 057 * @param version layer session version 058 * @param extension data file extension 059 */ 060 protected GenericSessionExporter(T layer, String type, String version, String extension) { 061 super(layer); 062 this.type = type; 063 this.version = version; 064 this.extension = extension; 065 /* I18n: Refer to a OSM/GPX data file in session file */ 066 this.link = new JRadioButton(tr("local file")); 067 /* I18n: Include OSM/GPX data in session file */ 068 this.include = new JRadioButton(tr("include")); 069 } 070 071 private class LayerSaveAction extends AbstractAction { 072 /** 073 * Constructs a new {@code LayerSaveAction}. 074 */ 075 LayerSaveAction() { 076 new ImageProvider("save").getResource().attachImageIcon(this); 077 putValue(SHORT_DESCRIPTION, ((SaveToFile) layer).requiresSaveToFile() ? 078 tr("Layer contains unsaved data - save to file.") : 079 tr("Layer does not contain unsaved data.")); 080 updateEnabledState(); 081 } 082 083 @Override 084 public void actionPerformed(ActionEvent e) { 085 SaveAction.getInstance().doSave(layer, true); 086 updateEnabledState(); 087 } 088 089 public final void updateEnabledState() { 090 setEnabled(((SaveToFile) layer).requiresSaveToFile()); 091 } 092 } 093 094 @Override 095 public JPanel getExportPanel() { 096 final JPanel p = new JPanel(new GridBagLayout()); 097 JPanel topRow = new JPanel(new GridBagLayout()); 098 export.setSelected(true); 099 final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEADING); 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(e -> cl.show(cards, "link")); 156 include.addActionListener(e -> cl.show(cards, "include")); 157 158 topRow.add(export, GBC.std()); 159 topRow.add(lbl, GBC.std()); 160 topRow.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL)); 161 p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL)); 162 p.add(lblData, GBC.std().insets(10, 0, 0, 0)); 163 p.add(link, GBC.std()); 164 p.add(include, GBC.eol()); 165 p.add(cards, GBC.eol().insets(15, 0, 3, 3)); 166 167 export.addItemListener(e -> { 168 if (e.getStateChange() == ItemEvent.DESELECTED) { 169 GuiHelper.setEnabledRec(p, false); 170 export.setEnabled(true); 171 } else { 172 GuiHelper.setEnabledRec(p, true); 173 if (save != null && saveAction != null) { 174 save.setEnabled(saveAction.isEnabled()); 175 } 176 link.setEnabled(file != null && file.exists()); 177 } 178 }); 179 return p; 180 } 181 182 @Override 183 public Element export(ExportSupport support) throws IOException { 184 Element layerEl = support.createElement("layer"); 185 layerEl.setAttribute("type", type); 186 layerEl.setAttribute("version", version); 187 188 Element file = support.createElement("file"); 189 layerEl.appendChild(file); 190 191 if (requiresZip()) { 192 String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data." + extension; 193 file.appendChild(support.createTextNode(zipPath)); 194 addDataFile(support.getOutputStreamZip(zipPath)); 195 layer.setAssociatedFile(null); 196 if (layer instanceof AbstractModifiableLayer) { 197 ((AbstractModifiableLayer) layer).onPostSaveToFile(); 198 } 199 } else { 200 try { 201 File f = layer.getAssociatedFile(); 202 if (f != null) { 203 file.appendChild(support.createTextNode(f.toURI().toURL().toString())); 204 } 205 } catch (MalformedURLException e) { 206 throw new IOException(e); 207 } 208 } 209 return layerEl; 210 } 211 212 @Override 213 public boolean requiresZip() { 214 if (include.isSelected()) { 215 return true; 216 } 217 for (PluginSessionExporter exporter : PluginHandler.load(PluginSessionExporter.class)) { 218 if (exporter.requiresSaving()) { 219 return true; 220 } 221 } 222 return false; 223 } 224 225 protected abstract void addDataFile(OutputStream out) throws IOException; 226}