Changeset 9455 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-01-15T01:31:57+01:00 (9 years ago)
Author:
Don-vip
Message:

session - refactor duplicated code, add javadoc, fix warnings

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

Legend:

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

    r9078 r9455  
    88import java.util.Collections;
    99
    10 import javax.swing.JCheckBox;
    1110import javax.swing.JLabel;
    1211import javax.swing.JPanel;
     
    2019import org.w3c.dom.Element;
    2120
    22 public class GeoImageSessionExporter implements SessionLayerExporter {
     21/**
     22 * Session exporter for {@link GeoImageLayer}.
     23 * @since 5505
     24 */
     25public class GeoImageSessionExporter extends AbstractSessionExporter<GeoImageLayer> {
    2326
    24     private final GeoImageLayer layer;
    25 
     27    /**
     28     * Constructs a new {@code GeoImageSessionExporter}.
     29     * @param layer GeoImage layer to export
     30     */
    2631    public GeoImageSessionExporter(GeoImageLayer layer) {
    27         this.layer = layer;
     32        super(layer);
    2833    }
    29 
    30     private JCheckBox export;
    3134
    3235    @Override
     
    4144    public Component getExportPanel() {
    4245        final JPanel p = new JPanel(new GridBagLayout());
    43         export = new JCheckBox();
    4446        export.setSelected(true);
    4547        final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
     
    5052        p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    5153        return p;
    52     }
    53 
    54     @Override
    55     public boolean shallExport() {
    56         return export.isSelected();
    57     }
    58 
    59     @Override
    60     public boolean requiresZip() {
    61         return false;
    6254    }
    6355
  • trunk/src/org/openstreetmap/josm/io/session/GeoImageSessionImporter.java

    r8846 r9455  
    2222import org.w3c.dom.NodeList;
    2323
     24/**
     25 * Session importer for {@link GeoImageLayer}.
     26 * @since 5505
     27 */
    2428public class GeoImageSessionImporter implements SessionLayerImporter {
    2529
     
    114118        return new GeoImageLayer(entries, gpxLayer, useThumbs);
    115119    }
    116 
    117120}
  • trunk/src/org/openstreetmap/josm/io/session/GpxTracksSessionExporter.java

    r9215 r9455  
    2222import java.net.URL;
    2323import java.nio.charset.StandardCharsets;
    24 import java.util.Collection;
    25 import java.util.Collections;
    2624
    2725import javax.swing.ButtonGroup;
    28 import javax.swing.JCheckBox;
    2926import javax.swing.JLabel;
    3027import javax.swing.JPanel;
     
    3330
    3431import org.openstreetmap.josm.gui.layer.GpxLayer;
    35 import org.openstreetmap.josm.gui.layer.Layer;
    3632import org.openstreetmap.josm.gui.util.GuiHelper;
    3733import org.openstreetmap.josm.gui.widgets.JosmTextField;
     
    4137import org.w3c.dom.Element;
    4238
    43 public class GpxTracksSessionExporter implements SessionLayerExporter {
     39/**
     40 * Session exporter for {@link GpxLayer}.
     41 * @since 5501
     42 */
     43public class GpxTracksSessionExporter extends AbstractSessionExporter<GpxLayer> {
    4444
    45     private final GpxLayer layer;
    4645    private JRadioButton link;
    4746    private JRadioButton include;
    48     private JCheckBox export;
    4947
    5048    /**
     
    5351     */
    5452    public GpxTracksSessionExporter(GpxLayer layer) {
    55         this.layer = layer;
    56     }
    57 
    58     @Override
    59     public Collection<Layer> getDependencies() {
    60         return Collections.emptySet();
     53        super(layer);
    6154    }
    6255
     
    6558        final JPanel p = new JPanel(new GridBagLayout());
    6659        JPanel topRow = new JPanel(new GridBagLayout());
    67         export = new JCheckBox();
    6860        export.setSelected(true);
    6961        final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
     
    149141
    150142    @Override
    151     public boolean shallExport() {
    152         return export.isSelected();
    153     }
    154 
    155     @Override
    156143    public boolean requiresZip() {
    157144        return include.isSelected();
  • trunk/src/org/openstreetmap/josm/io/session/GpxTracksSessionImporter.java

    r8540 r9455  
    1313import javax.xml.xpath.XPathFactory;
    1414
     15import org.openstreetmap.josm.gui.layer.GpxLayer;
    1516import org.openstreetmap.josm.gui.layer.Layer;
    1617import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     
    2021import org.w3c.dom.Element;
    2122
     23/**
     24 * Session exporter for {@link GpxLayer}.
     25 * @since 5501
     26 */
    2227public class GpxTracksSessionImporter implements SessionLayerImporter {
    2328
     
    3944
    4045            try (InputStream in = support.getInputStream(fileStr)) {
    41                 GpxImporter.GpxImporterData importData = null;
     46                GpxImporter.GpxImporterData importData;
    4247
    4348                if (NMEAImporter.FILE_FILTER.acceptName(fileStr)) {
  • trunk/src/org/openstreetmap/josm/io/session/ImagerySessionExporter.java

    r9001 r9455  
    55import java.awt.GridBagLayout;
    66import java.io.IOException;
    7 import java.util.Collection;
    8 import java.util.Collections;
    97import java.util.LinkedHashMap;
    108import java.util.Map;
    119
    12 import javax.swing.JCheckBox;
    1310import javax.swing.JLabel;
    1411import javax.swing.JPanel;
     
    1916import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    2017import org.openstreetmap.josm.gui.layer.ImageryLayer;
    21 import org.openstreetmap.josm.gui.layer.Layer;
    2218import org.openstreetmap.josm.gui.layer.TMSLayer;
    2319import org.openstreetmap.josm.gui.layer.WMSLayer;
     
    2824
    2925/**
    30  * Session exporter for TMSLayer and WMSLayer.
     26 * Session exporter for {@link TMSLayer}, {@link WMSLayer} and {@link WMTSLayer}.
     27 * @since 5391
    3128 */
    32 public class ImagerySessionExporter implements SessionLayerExporter {
     29public class ImagerySessionExporter extends AbstractSessionExporter<ImageryLayer> {
    3330
    34     private ImageryLayer layer;
    35     private JCheckBox export;
    36 
     31    /**
     32     * Constructs a new {@code ImagerySessionExporter}.
     33     * @param layer imagery layer to export
     34     */
    3735    public ImagerySessionExporter(ImageryLayer layer) {
    38         this.layer = layer;
     36        super(layer);
    3937    }
    4038
     39    /**
     40     * Constructs a new {@code ImagerySessionExporter}.
     41     * @param layer TMS layer to export
     42     */
    4143    public ImagerySessionExporter(TMSLayer layer) {
    42         this((ImageryLayer) layer);
     44        super(layer);
    4345    }
    4446
     47    /**
     48     * Constructs a new {@code ImagerySessionExporter}.
     49     * @param layer WMS layer to export
     50     */
    4551    public ImagerySessionExporter(WMSLayer layer) {
    46         this((ImageryLayer) layer);
     52        super(layer);
    4753    }
    4854
     55    /**
     56     * Constructs a new {@code ImagerySessionExporter}.
     57     * @param layer WMTS layer to export
     58     */
    4959    public ImagerySessionExporter(WMTSLayer layer) {
    50         this((ImageryLayer) layer);
    51     }
    52 
    53     @Override
    54     public Collection<Layer> getDependencies() {
    55         return Collections.emptySet();
     60        super(layer);
    5661    }
    5762
     
    5964    public Component getExportPanel() {
    6065        final JPanel p = new JPanel(new GridBagLayout());
    61         export = new JCheckBox();
    6266        export.setSelected(true);
    6367        final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
     
    6872        p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    6973        return p;
    70     }
    71 
    72     @Override
    73     public boolean shallExport() {
    74         return export.isSelected();
    75     }
    76 
    77     @Override
    78     public boolean requiresZip() {
    79         return false;
    8074    }
    8175
     
    10296        return layerElem;
    10397    }
    104 
    10598}
  • trunk/src/org/openstreetmap/josm/io/session/ImagerySessionImporter.java

    r8846 r9455  
    1414import org.openstreetmap.josm.gui.layer.ImageryLayer;
    1515import org.openstreetmap.josm.gui.layer.Layer;
     16import org.openstreetmap.josm.gui.layer.TMSLayer;
     17import org.openstreetmap.josm.gui.layer.WMSLayer;
     18import org.openstreetmap.josm.gui.layer.WMTSLayer;
    1619import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1720import org.openstreetmap.josm.io.IllegalDataException;
     
    2225
    2326/**
    24  * Session importer for both TMSLayer and WMSLayer.
     27 * Session importer for {@link TMSLayer}, {@link WMSLayer} and {@link WMTSLayer}.
     28 * @since 5391
    2529 */
    2630public class ImagerySessionImporter implements SessionLayerImporter {
  • trunk/src/org/openstreetmap/josm/io/session/MarkerSessionExporter.java

    r9078 r9455  
    1313import java.util.Collections;
    1414
    15 import javax.swing.JCheckBox;
    1615import javax.swing.JLabel;
    1716import javax.swing.JPanel;
     
    2827import org.w3c.dom.Element;
    2928
    30 public class MarkerSessionExporter implements SessionLayerExporter {
     29/**
     30 * Session exporter for {@link MarkerLayer}.
     31 * @since 5684
     32 */
     33public class MarkerSessionExporter extends AbstractSessionExporter<MarkerLayer> {
    3134
    32     private final MarkerLayer layer;
    33     private JCheckBox export;
    34 
     35    /**
     36     * Constructs a new {@code MarkerSessionExporter}.
     37     * @param layer marker layer to export
     38     */
    3539    public MarkerSessionExporter(MarkerLayer layer) {
    36         this.layer = layer;
     40        super(layer);
    3741    }
    3842
     
    4852    public Component getExportPanel() {
    4953        final JPanel p = new JPanel(new GridBagLayout());
    50         export = new JCheckBox();
    5154        export.setSelected(true);
    5255        final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
     
    5760        p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    5861        return p;
    59     }
    60 
    61     @Override
    62     public boolean shallExport() {
    63         return export.isSelected();
    6462    }
    6563
     
    9391    }
    9492
     93    /**
     94     * Writes GPX file from marker data.
     95     */
    9596    public static class MarkerWriter extends GpxWriter {
    9697
     98        /**
     99         * Constructs a new {@code MarkerWriter}.
     100         * @param out The output writer
     101         */
    97102        public MarkerWriter(PrintWriter out) {
    98103            super(out);
    99104        }
    100105
     106        /**
     107         * Writes the given markers data.
     108         * @param layer The layer data to write
     109         */
    101110        public void write(MarkerLayer layer) {
    102111            GpxData data = new GpxData();
  • trunk/src/org/openstreetmap/josm/io/session/MarkerSessionImporter.java

    r8846 r9455  
    2323import org.w3c.dom.Element;
    2424
     25/**
     26 * Session importer for {@link MarkerLayer}.
     27 * @since 5684
     28 */
    2529public class MarkerSessionImporter implements SessionLayerImporter {
    2630
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionExporter.java

    r9215 r9455  
    2222import java.net.URL;
    2323import java.nio.charset.StandardCharsets;
    24 import java.util.Collection;
    25 import java.util.Collections;
    2624
    2725import javax.swing.AbstractAction;
    2826import javax.swing.ButtonGroup;
    2927import javax.swing.JButton;
    30 import javax.swing.JCheckBox;
    3128import javax.swing.JLabel;
    3229import javax.swing.JPanel;
     
    3532
    3633import org.openstreetmap.josm.actions.SaveAction;
    37 import org.openstreetmap.josm.gui.layer.Layer;
    3834import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    3935import org.openstreetmap.josm.gui.util.GuiHelper;
     
    4642import org.w3c.dom.Element;
    4743
    48 public class OsmDataSessionExporter implements SessionLayerExporter {
    49 
    50     private final OsmDataLayer layer;
     44/**
     45 * Session exporter for {@link OsmDataLayer}.
     46 * @since 4685
     47 */
     48public class OsmDataSessionExporter extends AbstractSessionExporter<OsmDataLayer> {
     49
    5150    private JRadioButton link;
    5251    private JRadioButton include;
    53     private JCheckBox export;
    5452
    5553    /**
     
    5856     */
    5957    public OsmDataSessionExporter(OsmDataLayer layer) {
    60         this.layer = layer;
    61     }
    62 
    63     @Override
    64     public Collection<Layer> getDependencies() {
    65         return Collections.emptySet();
     58        super(layer);
    6659    }
    6760
     
    9386        final JPanel p = new JPanel(new GridBagLayout());
    9487        JPanel topRow = new JPanel(new GridBagLayout());
    95         export = new JCheckBox();
    9688        export.setSelected(true);
    9789        final JLabel lbl = new JLabel(layer.getName(), layer.getIcon(), SwingConstants.LEFT);
     
    182174
    183175    @Override
    184     public boolean shallExport() {
    185         return export.isSelected();
    186     }
    187 
    188     @Override
    189176    public boolean requiresZip() {
    190177        return include.isSelected();
  • trunk/src/org/openstreetmap/josm/io/session/OsmDataSessionImporter.java

    r8540 r9455  
    1414
    1515import org.openstreetmap.josm.gui.layer.Layer;
     16import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1617import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1718import org.openstreetmap.josm.io.IllegalDataException;
     
    2021import org.w3c.dom.Element;
    2122
     23/**
     24 * Session importer for {@link OsmDataLayer}.
     25 * @since 4685
     26 */
    2227public class OsmDataSessionImporter implements SessionLayerImporter {
    2328
     
    5055    }
    5156}
    52 
  • trunk/src/org/openstreetmap/josm/io/session/SessionLayerExporter.java

    r8926 r9455  
    1010import org.w3c.dom.Element;
    1111
     12/**
     13 * Session layer exporter.
     14 * @since 4685
     15 */
    1216public interface SessionLayerExporter {
    1317
  • trunk/src/org/openstreetmap/josm/io/session/SessionLayerImporter.java

    r8926 r9455  
    1010import org.w3c.dom.Element;
    1111
     12/**
     13 * Session layer importer.
     14 * @since 4668
     15 */
    1216public interface SessionLayerImporter {
    1317
  • trunk/src/org/openstreetmap/josm/io/session/SessionReader.java

    r9231 r9455  
    5454/**
    5555 * Reads a .jos session file and loads the layers in the process.
    56  *
     56 * @since 4668
    5757 */
    5858public class SessionReader {
    5959
    6060    private static final Map<String, Class<? extends SessionLayerImporter>> sessionLayerImporters = new HashMap<>();
     61
     62    private URI sessionFileURI;
     63    private boolean zip; // true, if session file is a .joz file; false if it is a .jos file
     64    private ZipFile zipFile;
     65    private List<Layer> layers = new ArrayList<>();
     66    private int active = -1;
     67    private final List<Runnable> postLoadTasks = new ArrayList<>();
     68    private ViewportData viewport;
    6169
    6270    static {
     
    6876    }
    6977
     78    /**
     79     * Register a session layer importer.
     80     *
     81     * @param layerType layer type
     82     * @param importer importer for this layer class
     83     */
    7084    public static void registerSessionLayerImporter(String layerType, Class<? extends SessionLayerImporter> importer) {
    7185        sessionLayerImporters.put(layerType, importer);
    7286    }
    7387
     88    /**
     89     * Returns the session layer importer for the given layer type.
     90     * @param layerType layer type to import
     91     * @return session layer importer for the given layer
     92     */
    7493    public static SessionLayerImporter getSessionLayerImporter(String layerType) {
    7594        Class<? extends SessionLayerImporter> importerClass = sessionLayerImporters.get(layerType);
     
    85104    }
    86105
    87     private URI sessionFileURI;
    88     private boolean zip; // true, if session file is a .joz file; false if it is a .jos file
    89     private ZipFile zipFile;
    90     private List<Layer> layers = new ArrayList<>();
    91     private int active = -1;
    92     private final List<Runnable> postLoadTasks = new ArrayList<>();
    93     private ViewportData viewport;
    94 
    95106    /**
    96107     * @return list of layers that are later added to the mapview
     
    124135    }
    125136
     137    /**
     138     * A class that provides some context for the individual {@link SessionLayerImporter}
     139     * when doing the import.
     140     */
    126141    public class ImportSupport {
    127142
     
    130145        private final List<LayerDependency> layerDependencies;
    131146
     147        /**
     148         * Path of the file inside the zip archive.
     149         * Used as alternative return value for getFile method.
     150         */
     151        private String inZipPath;
     152
     153        /**
     154         * Constructs a new {@code ImportSupport}.
     155         * @param layerName layer name
     156         * @param layerIndex layer index
     157         * @param layerDependencies layer dependencies
     158         */
    132159        public ImportSupport(String layerName, int layerIndex, List<LayerDependency> layerDependencies) {
    133160            this.layerName = layerName;
     
    135162            this.layerDependencies = layerDependencies;
    136163        }
    137 
    138         /**
    139          * Path of the file inside the zip archive.
    140          * Used as alternative return value for getFile method.
    141          */
    142         private String inZipPath;
    143164
    144165        /**
     
    556577    }
    557578
     579    /**
     580     * Loads session from the given file.
     581     * @param sessionFile session file to load
     582     * @param zip {@code true} if it's a zipped session (.joz)
     583     * @param progressMonitor progress monitor
     584     * @throws IllegalDataException if invalid data is detected
     585     * @throws IOException if any I/O error occurs
     586     */
    558587    public void loadSession(File sessionFile, boolean zip, ProgressMonitor progressMonitor) throws IllegalDataException, IOException {
    559         if (progressMonitor == null) {
    560             progressMonitor = NullProgressMonitor.INSTANCE;
    561         }
    562 
    563588        try (InputStream josIS = createInputStream(sessionFile, zip)) {
    564             loadSession(josIS, sessionFile.toURI(), zip, progressMonitor);
     589            loadSession(josIS, sessionFile.toURI(), zip, progressMonitor != null ? progressMonitor : NullProgressMonitor.INSTANCE);
    565590        }
    566591    }
     
    621646    private static Element getElementByTagName(Element root, String name) {
    622647        NodeList els = root.getElementsByTagName(name);
    623         if (els.getLength() == 0) return null;
    624         return (Element) els.item(0);
     648        return els.getLength() > 0 ? (Element) els.item(0) : null;
    625649    }
    626650}
  • trunk/src/org/openstreetmap/josm/io/session/SessionWriter.java

    r9231 r9455  
    4747import org.w3c.dom.Text;
    4848
     49/**
     50 * Writes a .jos session file from current supported layers.
     51 * @since 4685
     52 */
    4953public class SessionWriter {
    5054
    5155    private static Map<Class<? extends Layer>, Class<? extends SessionLayerExporter>> sessionLayerExporters = new HashMap<>();
     56
     57    private final List<Layer> layers;
     58    private final int active;
     59    private final Map<Layer, SessionLayerExporter> exporters;
     60    private final MultiMap<Layer, Layer> dependencies;
     61    private final boolean zip;
     62
     63    private ZipOutputStream zipOut;
    5264
    5365    static {
     
    7284    }
    7385
     86    /**
     87     * Returns the session layer exporter for the given layer.
     88     * @param layer layer to export
     89     * @return session layer exporter for the given layer
     90     */
    7491    public static SessionLayerExporter getSessionLayerExporter(Layer layer) {
    7592        Class<? extends Layer> layerClass = layer.getClass();
    7693        Class<? extends SessionLayerExporter> exporterClass = sessionLayerExporters.get(layerClass);
    77         if (exporterClass == null) return null;
     94        if (exporterClass == null)
     95            return null;
    7896        try {
    7997            Constructor<? extends SessionLayerExporter> constructor = exporterClass.getConstructor(layerClass);
     
    83101        }
    84102    }
    85 
    86     private final List<Layer> layers;
    87     private final int active;
    88     private final Map<Layer, SessionLayerExporter> exporters;
    89     private final MultiMap<Layer, Layer> dependencies;
    90     private final boolean zip;
    91 
    92     private ZipOutputStream zipOut;
    93103
    94104    /**
     
    118128        private final int layerIndex;
    119129
     130        /**
     131         * Constructs a new {@code ExportSupport}.
     132         * @param doc XML document
     133         * @param layerIndex layer index
     134         */
    120135        public ExportSupport(Document doc, int layerIndex) {
    121136            this.doc = doc;
     
    123138        }
    124139
     140        /**
     141         * Creates an element of the type specified.
     142         * @param name The name of the element type to instantiate
     143         * @return A new {@code Element} object
     144         * @see Document#createElement
     145         */
    125146        public Element createElement(String name) {
    126147            return doc.createElement(name);
    127148        }
    128149
     150        /**
     151         * Creates a text node given the specified string.
     152         * @param text The data for the node.
     153         * @return The new {@code Text} object.
     154         * @see Document#createTextNode
     155         */
    129156        public Text createTextNode(String text) {
    130157            return doc.createTextNode(text);
     
    168195    }
    169196
     197    /**
     198     * Creates XML (.jos) session document.
     199     * @return new document
     200     * @throws IOException if any I/O error occurs
     201     */
    170202    public Document createJosDocument() throws IOException {
    171203        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
     
    219251                for (Layer depLayer : deps) {
    220252                    int depIndex = layers.indexOf(depLayer);
    221                     if (depIndex == -1) throw new AssertionError();
     253                    if (depIndex == -1)
     254                        throw new AssertionError();
    222255                    depsInt.add(depIndex+1);
    223256                }
     
    229262    }
    230263
     264    /**
     265     * Writes given .jos document to an output stream.
     266     * @param doc session document
     267     * @param out output stream
     268     * @throws IOException if any I/O error occurs
     269     */
    231270    public void writeJos(Document doc, OutputStream out) throws IOException {
    232271        try {
     
    246285    }
    247286
     287    /**
     288     * Writes session to given file.
     289     * @param f output file
     290     * @throws IOException if any I/O error occurs
     291     */
    248292    public void write(File f) throws IOException {
    249293        try (OutputStream out = new FileOutputStream(f)) {
     
    254298    }
    255299
     300    /**
     301     * Writes session to given output stream.
     302     * @param out output stream
     303     * @throws IOException if any I/O error occurs
     304     */
    256305    public void write(OutputStream out) throws IOException {
    257306        if (zip) {
Note: See TracChangeset for help on using the changeset viewer.