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.tools.GBC; 035import org.openstreetmap.josm.tools.ImageProvider; 036import org.w3c.dom.Element; 037 038/** 039 * Generic superclass of {@link OsmDataSessionExporter} and {@link GpxTracksSessionExporter} layer exporters. 040 * @param <T> Type of exported layer 041 * @since 9470 042 */ 043public abstract class GenericSessionExporter<T extends Layer> extends AbstractSessionExporter<T> { 044 045 private final String type; 046 private final String version; 047 private final String extension; 048 049 private final JRadioButton link; 050 private final JRadioButton include; 051 052 /** 053 * Constructs a new {@code GenericSessionExporter}. 054 * @param layer layer to export 055 * @param type layer session type 056 * @param version layer session version 057 * @param extension data file extension 058 */ 059 protected GenericSessionExporter(T layer, String type, String version, String extension) { 060 super(layer); 061 this.type = type; 062 this.version = version; 063 this.extension = extension; 064 /* I18n: Refer to a OSM/GPX data file in session file */ 065 this.link = new JRadioButton(tr("local file")); 066 /* I18n: Include OSM/GPX data in session file */ 067 this.include = new JRadioButton(tr("include")); 068 } 069 070 private class LayerSaveAction extends AbstractAction { 071 /** 072 * Constructs a new {@code LayerSaveAction}. 073 */ 074 LayerSaveAction() { 075 new ImageProvider("save").getResource().attachImageIcon(this); 076 putValue(SHORT_DESCRIPTION, ((SaveToFile) layer).requiresSaveToFile() ? 077 tr("Layer contains unsaved data - save to file.") : 078 tr("Layer does not contain unsaved data.")); 079 updateEnabledState(); 080 } 081 082 @Override 083 public void actionPerformed(ActionEvent e) { 084 SaveAction.getInstance().doSave(layer, true); 085 updateEnabledState(); 086 } 087 088 public final void updateEnabledState() { 089 setEnabled(((SaveToFile) layer).requiresSaveToFile()); 090 } 091 } 092 093 @Override 094 public JPanel getExportPanel() { 095 final JPanel p = new JPanel(new GridBagLayout()); 096 JPanel topRow = new JPanel(new GridBagLayout()); 097 export.setSelected(true); 098 final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEADING); 099 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 } else { 195 try { 196 File f = layer.getAssociatedFile(); 197 if (f != null) { 198 file.appendChild(support.createTextNode(f.toURI().toURL().toString())); 199 } 200 } catch (MalformedURLException e) { 201 throw new IOException(e); 202 } 203 } 204 return layerEl; 205 } 206 207 @Override 208 public boolean requiresZip() { 209 return include.isSelected(); 210 } 211 212 protected abstract void addDataFile(OutputStream out) throws IOException; 213}