Changeset 9471 in josm for trunk/src


Ignore:
Timestamp:
2016-01-15T21:25:13+01:00 (4 years ago)
Author:
Don-vip
Message:

session - refactoring to reduce code duplication

Location:
trunk/src/org/openstreetmap/josm/io/session
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/session/AbstractSessionExporter.java

    r9455 r9471  
    1010
    1111/**
    12  * Generic superclass of all session layer exporters.
     12 * Abstract superclass of all session layer exporters.
    1313 * @param <T> Type of exported layer
    1414 * @since 9455
  • trunk/src/org/openstreetmap/josm/io/session/GpxTracksSessionExporter.java

    r9455 r9471  
    22package org.openstreetmap.josm.io.session;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.CardLayout;
    7 import java.awt.Component;
    8 import java.awt.Font;
    9 import java.awt.GridBagLayout;
    10 import java.awt.event.ActionEvent;
    11 import java.awt.event.ActionListener;
    12 import java.awt.event.ItemEvent;
    13 import java.awt.event.ItemListener;
    14 import java.io.File;
    15 import java.io.IOException;
    164import java.io.OutputStream;
    175import java.io.OutputStreamWriter;
    186import java.io.PrintWriter;
    197import java.io.Writer;
    20 import java.net.MalformedURLException;
    21 import java.net.URI;
    22 import java.net.URL;
    238import java.nio.charset.StandardCharsets;
    249
    25 import javax.swing.ButtonGroup;
    26 import javax.swing.JLabel;
    27 import javax.swing.JPanel;
    28 import javax.swing.JRadioButton;
    29 import javax.swing.SwingConstants;
    30 
    3110import org.openstreetmap.josm.gui.layer.GpxLayer;
    32 import org.openstreetmap.josm.gui.util.GuiHelper;
    33 import org.openstreetmap.josm.gui.widgets.JosmTextField;
    3411import org.openstreetmap.josm.io.GpxWriter;
    35 import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;
    36 import org.openstreetmap.josm.tools.GBC;
    37 import org.w3c.dom.Element;
    3812
    3913/**
     
    4115 * @since 5501
    4216 */
    43 public class GpxTracksSessionExporter extends AbstractSessionExporter<GpxLayer> {
    44 
    45     private JRadioButton link;
    46     private JRadioButton include;
     17public class GpxTracksSessionExporter extends GenericSessionExporter<GpxLayer> {
    4718
    4819    /**
     
    5122     */
    5223    public GpxTracksSessionExporter(GpxLayer layer) {
    53         super(layer);
     24        super(layer, "tracks", "0.1", "gpx");
    5425    }
    5526
    5627    @Override
    57     public Component getExportPanel() {
    58         final JPanel p = new JPanel(new GridBagLayout());
    59         JPanel topRow = new JPanel(new GridBagLayout());
    60         export.setSelected(true);
    61         final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
    62         lbl.setToolTipText(layer.getToolTipText());
    63         lbl.setLabelFor(export);
    64         JLabel lblData = new JLabel(tr("Data:"));
    65         /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file"));
    66         link.putClientProperty("actionname", "link");
    67         link.setToolTipText(tr("Link to a GPX file on your local disk."));
    68         /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));
    69         include.setToolTipText(tr("Include GPX data in the .joz session file."));
    70         include.putClientProperty("actionname", "include");
    71         ButtonGroup group = new ButtonGroup();
    72         group.add(link);
    73         group.add(include);
    74 
    75         JPanel cardLink = new JPanel(new GridBagLayout());
    76         final File file = layer.getAssociatedFile();
    77         if (file != null && file.exists()) {
    78             JosmTextField tf = new JosmTextField();
    79             tf.setText(file.getPath());
    80             tf.setEditable(false);
    81             cardLink.add(tf, GBC.std());
    82         } else {
    83             cardLink.add(new JLabel(tr("No file association")), GBC.eol());
    84         }
    85 
    86         JPanel cardInclude = new JPanel(new GridBagLayout());
    87         JLabel lblIncl = new JLabel(tr("GPX data will be included in the session file."));
    88         lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN));
    89         cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL));
    90 
    91         final CardLayout cl = new CardLayout();
    92         final JPanel cards = new JPanel(cl);
    93         cards.add(cardLink, "link");
    94         cards.add(cardInclude, "include");
    95 
    96         if (file != null && file.exists()) {
    97             link.setSelected(true);
    98         } else {
    99             link.setEnabled(false);
    100             link.setToolTipText(tr("No file association"));
    101             include.setSelected(true);
    102             cl.show(cards, "include");
    103         }
    104 
    105         link.addActionListener(new ActionListener() {
    106             @Override
    107             public void actionPerformed(ActionEvent e) {
    108                 cl.show(cards, "link");
    109             }
    110         });
    111         include.addActionListener(new ActionListener() {
    112             @Override
    113             public void actionPerformed(ActionEvent e) {
    114                 cl.show(cards, "include");
    115             }
    116         });
    117 
    118         topRow.add(export, GBC.std());
    119         topRow.add(lbl, GBC.std());
    120         topRow.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    121         p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL));
    122         p.add(lblData, GBC.std().insets(10, 0, 0, 0));
    123         p.add(link, GBC.std());
    124         p.add(include, GBC.eol());
    125         p.add(cards, GBC.eol().insets(15, 0, 3, 3));
    126 
    127         export.addItemListener(new ItemListener() {
    128             @Override
    129             public void itemStateChanged(ItemEvent e) {
    130                 if (e.getStateChange() == ItemEvent.DESELECTED) {
    131                     GuiHelper.setEnabledRec(p, false);
    132                     export.setEnabled(true);
    133                 } else {
    134                     GuiHelper.setEnabledRec(p, true);
    135                     link.setEnabled(file != null && file.exists());
    136                 }
    137             }
    138         });
    139         return p;
    140     }
    141 
    142     @Override
    143     public boolean requiresZip() {
    144         return include.isSelected();
    145     }
    146 
    147     @Override
    148     public Element export(ExportSupport support) throws IOException {
    149         Element layerEl = support.createElement("layer");
    150         layerEl.setAttribute("type", "tracks");
    151         layerEl.setAttribute("version", "0.1");
    152 
    153         Element file = support.createElement("file");
    154         layerEl.appendChild(file);
    155 
    156         if (requiresZip()) {
    157             String zipPath = "layers/" + String.format("%02d", support.getLayerIndex()) + "/data.gpx";
    158             file.appendChild(support.createTextNode(zipPath));
    159             addDataFile(support.getOutputStreamZip(zipPath));
    160         } else {
    161             URI uri = layer.getAssociatedFile().toURI();
    162             URL url = null;
    163             try {
    164                 url = uri.toURL();
    165             } catch (MalformedURLException e) {
    166                 throw new IOException(e);
    167             }
    168             file.appendChild(support.createTextNode(url.toString()));
    169         }
    170         return layerEl;
    171     }
    172 
    17328    @SuppressWarnings("resource")
    17429    protected void addDataFile(OutputStream out) {
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java

    r9455 r9471  
    22package org.openstreetmap.josm.io.session;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.CardLayout;
    7 import java.awt.Font;
    8 import java.awt.GridBagLayout;
    9 import java.awt.Insets;
    10 import java.awt.event.ActionEvent;
    11 import java.awt.event.ActionListener;
    12 import java.awt.event.ItemEvent;
    13 import java.awt.event.ItemListener;
    14 import java.io.File;
    15 import java.io.IOException;
    164import java.io.OutputStream;
    175import java.io.OutputStreamWriter;
    186import java.io.PrintWriter;
    197import java.io.Writer;
    20 import java.net.MalformedURLException;
    21 import java.net.URI;
    22 import java.net.URL;
    238import java.nio.charset.StandardCharsets;
    249
    25 import javax.swing.AbstractAction;
    26 import javax.swing.ButtonGroup;
    27 import javax.swing.JButton;
    28 import javax.swing.JLabel;
    29 import javax.swing.JPanel;
    30 import javax.swing.JRadioButton;
    31 import javax.swing.SwingConstants;
    32 
    33 import org.openstreetmap.josm.actions.SaveAction;
    3410import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    35 import org.openstreetmap.josm.gui.util.GuiHelper;
    36 import org.openstreetmap.josm.gui.widgets.JosmTextField;
    3711import org.openstreetmap.josm.io.OsmWriter;
    3812import org.openstreetmap.josm.io.OsmWriterFactory;
    39 import org.openstreetmap.josm.io.session.SessionWriter.ExportSupport;
    40 import org.openstreetmap.josm.tools.GBC;
    41 import org.openstreetmap.josm.tools.ImageProvider;
    42 import org.w3c.dom.Element;
    4313
    4414/**
     
    4616 * @since 4685
    4717 */
    48 public class OsmDataSessionExporter extends AbstractSessionExporter<OsmDataLayer> {
    49 
    50     private JRadioButton link;
    51     private JRadioButton include;
     18public class OsmDataSessionExporter extends GenericSessionExporter<OsmDataLayer> {
    5219
    5320    /**
     
    5623     */
    5724    public OsmDataSessionExporter(OsmDataLayer layer) {
    58         super(layer);
    59     }
    60 
    61     private class LayerSaveAction extends AbstractAction {
    62         /**
    63          * Constructs a new {@code LayerSaveAction}.
    64          */
    65         LayerSaveAction() {
    66             putValue(SMALL_ICON, new ImageProvider("save").setWidth(16).get());
    67             putValue(SHORT_DESCRIPTION, layer.requiresSaveToFile() ?
    68                     tr("Layer contains unsaved data - save to file.") :
    69                     tr("Layer does not contain unsaved data."));
    70             updateEnabledState();
    71         }
    72 
    73         @Override
    74         public void actionPerformed(ActionEvent e) {
    75             SaveAction.getInstance().doSave(layer);
    76             updateEnabledState();
    77         }
    78 
    79         public final void updateEnabledState() {
    80             setEnabled(layer.requiresSaveToFile());
    81         }
     25        super(layer, "osm-data", "0.1", "osm");
    8226    }
    8327
    8428    @Override
    85     public JPanel getExportPanel() {
    86         final JPanel p = new JPanel(new GridBagLayout());
    87         JPanel topRow = new JPanel(new GridBagLayout());
    88         export.setSelected(true);
    89         final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
    90         lbl.setToolTipText(layer.getToolTipText());
    91         lbl.setLabelFor(export);
    92         JLabel lblData = new JLabel(tr("Data:"));
    93         /* I18n: Refer to a OSM data file in session file */ link = new JRadioButton(tr("local file"));
    94         link.putClientProperty("actionname", "link");
    95         link.setToolTipText(tr("Link to a OSM data file on your local disk."));
    96         /* I18n: Include OSM data in session file */ include = new JRadioButton(tr("include"));
    97         include.setToolTipText(tr("Include OSM data in the .joz session file."));
    98         include.putClientProperty("actionname", "include");
    99         ButtonGroup group = new ButtonGroup();
    100         group.add(link);
    101         group.add(include);
    102 
    103         JPanel cardLink = new JPanel(new GridBagLayout());
    104         final File file = layer.getAssociatedFile();
    105         final LayerSaveAction saveAction = new LayerSaveAction();
    106         final JButton save = new JButton(saveAction);
    107         if (file != null && file.exists()) {
    108             JosmTextField tf = new JosmTextField();
    109             tf.setText(file.getPath());
    110             tf.setEditable(false);
    111             cardLink.add(tf, GBC.std());
    112             save.setMargin(new Insets(0, 0, 0, 0));
    113             cardLink.add(save, GBC.eol().insets(2, 0, 0, 0));
    114         } else {
    115             cardLink.add(new JLabel(tr("No file association")), GBC.eol());
    116         }
    117 
    118         JPanel cardInclude = new JPanel(new GridBagLayout());
    119         JLabel lblIncl = new JLabel(tr("OSM data will be included in the session file."));
    120         lblIncl.setFont(lblIncl.getFont().deriveFont(Font.PLAIN));
    121         cardInclude.add(lblIncl, GBC.eol().fill(GBC.HORIZONTAL));
    122 
    123         final CardLayout cl = new CardLayout();
    124         final JPanel cards = new JPanel(cl);
    125         cards.add(cardLink, "link");
    126         cards.add(cardInclude, "include");
    127 
    128         if (file != null && file.exists()) {
    129             link.setSelected(true);
    130         } else {
    131             link.setEnabled(false);
    132             link.setToolTipText(tr("No file association"));
    133             include.setSelected(true);
    134             cl.show(cards, "include");
    135         }
    136 
    137         link.addActionListener(new ActionListener() {
    138             @Override
    139             public void actionPerformed(ActionEvent e) {
    140                 cl.show(cards, "link");
    141             }
    142         });
    143         include.addActionListener(new ActionListener() {
    144             @Override
    145             public void actionPerformed(ActionEvent e) {
    146                 cl.show(cards, "include");
    147             }
    148         });
    149 
    150         topRow.add(export, GBC.std());
    151         topRow.add(lbl, GBC.std());
    152         topRow.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    153         p.add(topRow, GBC.eol().fill(GBC.HORIZONTAL));
    154         p.add(lblData, GBC.std().insets(10, 0, 0, 0));
    155         p.add(link, GBC.std());
    156         p.add(include, GBC.eol());
    157         p.add(cards, GBC.eol().insets(15, 0, 3, 3));
    158 
    159         export.addItemListener(new ItemListener() {
    160             @Override
    161             public void itemStateChanged(ItemEvent e) {
    162                 if (e.getStateChange() == ItemEvent.DESELECTED) {
    163                     GuiHelper.setEnabledRec(p, false);
    164                     export.setEnabled(true);
    165                 } else {
    166                     GuiHelper.setEnabledRec(p, true);
    167                     save.setEnabled(saveAction.isEnabled());
    168                     link.setEnabled(file != null && file.exists());
    169                 }
    170             }
    171         });
    172         return p;
    173     }
    174 
    175     @Override
    176     public boolean requiresZip() {
    177         return include.isSelected();
    178     }
    179 
    180     @Override
    181     public Element export(ExportSupport support) throws IOException {
    182         Element layerEl = support.createElement("layer");
    183         layerEl.setAttribute("type", "osm-data");
    184         layerEl.setAttribute("version", "0.1");
    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.osm";
    191             file.appendChild(support.createTextNode(zipPath));
    192             addDataFile(support.getOutputStreamZip(zipPath));
    193         } else {
    194             URI uri = layer.getAssociatedFile().toURI();
    195             URL url = null;
    196             try {
    197                 url = uri.toURL();
    198             } catch (MalformedURLException e) {
    199                 throw new IOException(e);
    200             }
    201             file.appendChild(support.createTextNode(url.toString()));
    202         }
    203         return layerEl;
    204     }
    205 
    20629    protected void addDataFile(OutputStream out) {
    20730        Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8);
Note: See TracChangeset for help on using the changeset viewer.