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.util.GuiHelper;
031import org.openstreetmap.josm.gui.widgets.JosmTextField;
032import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;
033import org.openstreetmap.josm.tools.GBC;
034import org.openstreetmap.josm.tools.ImageProvider;
035import org.w3c.dom.Element;
036
037/**
038 * Generic superclass of {@link OsmDataSessionExporter} and {@link GpxTracksSessionExporter} layer exporters.
039 * @param <T> Type of exported layer
040 * @since 9470
041 */
042public abstract class GenericSessionExporter<T extends Layer> extends AbstractSessionExporter<T> {
043
044    private final String type;
045    private final String version;
046    private final String extension;
047
048    private final JRadioButton link;
049    private final JRadioButton include;
050
051    /**
052     * Constructs a new {@code GenericSessionExporter}.
053     * @param layer layer to export
054     * @param type layer session type
055     * @param version layer session version
056     * @param extension data file extension
057     */
058    protected GenericSessionExporter(T layer, String type, String version, String extension) {
059        super(layer);
060        this.type = type;
061        this.version = version;
062        this.extension = extension;
063        /* I18n: Refer to a OSM/GPX data file in session file */
064        this.link = new JRadioButton(tr("local file"));
065        /* I18n: Include OSM/GPX data in session file */
066        this.include = new JRadioButton(tr("include"));
067    }
068
069    private class LayerSaveAction extends AbstractAction {
070        /**
071         * Constructs a new {@code LayerSaveAction}.
072         */
073        LayerSaveAction() {
074            new ImageProvider("save").getResource().attachImageIcon(this);
075            putValue(SHORT_DESCRIPTION, ((AbstractModifiableLayer) layer).requiresSaveToFile() ?
076                    tr("Layer contains unsaved data - save to file.") :
077                    tr("Layer does not contain unsaved data."));
078            updateEnabledState();
079        }
080
081        @Override
082        public void actionPerformed(ActionEvent e) {
083            SaveAction.getInstance().doSave(layer);
084            updateEnabledState();
085        }
086
087        public final void updateEnabledState() {
088            setEnabled(((AbstractModifiableLayer) layer).requiresSaveToFile());
089        }
090    }
091
092    @Override
093    public JPanel getExportPanel() {
094        final JPanel p = new JPanel(new GridBagLayout());
095        JPanel topRow = new JPanel(new GridBagLayout());
096        export.setSelected(true);
097        final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
098        lbl.setToolTipText(layer.getToolTipText());
099        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}