Changeset 2923 in josm for trunk


Ignore:
Timestamp:
2010-02-02T18:30:24+01:00 (15 years ago)
Author:
mjulius
Message:

New action: DownloadPrimitiveAction
This allows to download a specific primitive from the API, optional including referrers

Location:
trunk
Files:
7 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/DownloadReferrersAction.java

    r2842 r2923  
    44import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
    55import static org.openstreetmap.josm.tools.I18n.tr;
    6 import static org.openstreetmap.josm.tools.I18n.trn;
    76
    87import java.awt.event.ActionEvent;
    98import java.awt.event.KeyEvent;
    10 import java.io.IOException;
    119import java.text.MessageFormat;
    1210import java.util.Collection;
    13 import java.util.HashMap;
    1411import java.util.Map;
    15 import java.util.Map.Entry;
    16 
    17 import javax.swing.JOptionPane;
    18 import javax.swing.SwingUtilities;
    1912
    2013import org.openstreetmap.josm.Main;
    21 import org.openstreetmap.josm.data.osm.DataSet;
    22 import org.openstreetmap.josm.data.osm.DataSetMerger;
     14import org.openstreetmap.josm.actions.downloadtasks.DownloadReferrersTask;
    2315import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2416import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    25 import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2617import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    27 import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    28 import org.openstreetmap.josm.io.OsmServerBackreferenceReader;
    29 import org.openstreetmap.josm.io.OsmTransferException;
    3018import org.openstreetmap.josm.tools.CheckParameterUtil;
    31 import org.openstreetmap.josm.tools.ExceptionUtil;
    3219import org.openstreetmap.josm.tools.Shortcut;
    33 import org.xml.sax.SAXException;
    3420
    3521/**
     
    10389    }
    10490
    105     /**
    106      * The asynchronous task for downloading referring primitives
    107      *
    108      */
    109     public static class DownloadReferrersTask extends PleaseWaitRunnable {
    110         private boolean cancelled;
    111         private Exception lastException;
    112         private OsmServerBackreferenceReader reader;
    113         /** the target layer */
    114         private OsmDataLayer targetLayer;
    115         /** the collection of child primitives */
    116         private Map<Long, OsmPrimitiveType> children;
    117         /** the parents */
    118         private DataSet parents;
    119 
    120         /**
    121          * constructor
    122          *
    123          * @param targetLayer  the target layer for the downloaded primitives. Must not be null.
    124          * @param children the collection of child primitives for which parents are to be downloaded
    125          *
    126          */
    127         public DownloadReferrersTask(OsmDataLayer targetLayer, Collection<OsmPrimitive> children) {
    128             super("Download referrers", false /* don't ignore exception*/);
    129             CheckParameterUtil.ensureParameterNotNull(targetLayer, "targetLayer");
    130             cancelled = false;
    131             this.children = new HashMap<Long, OsmPrimitiveType>();
    132             if (children != null) {
    133                 for (OsmPrimitive p: children) {
    134                     if (! p.isNew()) {
    135                         this.children.put(p.getId(), OsmPrimitiveType.from(p));
    136                     }
    137                 }
    138             }
    139             this.targetLayer = targetLayer;
    140             parents = new DataSet();
    141         }
    142 
    143         /**
    144          * constructor
    145          *
    146          * @param targetLayer  the target layer for the downloaded primitives. Must not be null.
    147          * @param primitives  the collection of children for which parents are to be downloaded. Children
    148          * are specified by their id and  their type.
    149          *
    150          */
    151         public DownloadReferrersTask(OsmDataLayer targetLayer, Map<Long, OsmPrimitiveType> children) {
    152             super("Download referrers", false /* don't ignore exception*/);
    153             CheckParameterUtil.ensureParameterNotNull(targetLayer, "targetLayer");
    154             cancelled = false;
    155             this.children = new HashMap<Long, OsmPrimitiveType>();
    156             if (children != null) {
    157                 for (Entry<Long, OsmPrimitiveType> entry : children.entrySet()) {
    158                     if (entry.getKey() > 0 && entry.getValue() != null) {
    159                         children.put(entry.getKey(), entry.getValue());
    160                     }
    161                 }
    162             }
    163             this.targetLayer = targetLayer;
    164             parents = new DataSet();
    165         }
    166 
    167         /**
    168          * constructor
    169          *
    170          * @param targetLayer  the target layer. Must not be null.
    171          * @param id the primitive id. id > 0 required.
    172          * @param type the primitive type. type != null required
    173          * @exception IllegalArgumentException thrown if id <= 0
    174          * @exception IllegalArgumentException thrown if type == null
    175          * @exception IllegalArgumentException thrown if targetLayer == null
    176          *
    177          */
    178         public DownloadReferrersTask(OsmDataLayer targetLayer, long id, OsmPrimitiveType type) throws IllegalArgumentException {
    179             super("Download referrers", false /* don't ignore exception*/);
    180             CheckParameterUtil.ensureParameterNotNull(targetLayer, "targetLayer");
    181             if (id <= 0)
    182                 throw new IllegalArgumentException(MessageFormat.format("Id > 0 required, got {0}", id));
    183             CheckParameterUtil.ensureParameterNotNull(type, "type");
    184             cancelled = false;
    185             this.children = new HashMap<Long, OsmPrimitiveType>();
    186             this.children.put(id, type);
    187             this.targetLayer = targetLayer;
    188             parents = new DataSet();
    189         }
    190 
    191         @Override
    192         protected void cancel() {
    193             cancelled = true;
    194             synchronized(this) {
    195                 if (reader != null) {
    196                     reader.cancel();
    197                 }
    198             }
    199         }
    200 
    201         @Override
    202         protected void finish() {
    203             if (cancelled)
    204                 return;
    205             if (lastException != null) {
    206                 ExceptionUtil.explainException(lastException);
    207                 return;
    208             }
    209 
    210             DataSetMerger visitor = new DataSetMerger(targetLayer.data, parents);
    211             visitor.merge();
    212             SwingUtilities.invokeLater(
    213                     new Runnable() {
    214                         public void run() {
    215                             targetLayer.fireDataChange();
    216                             targetLayer.onPostDownloadFromServer();
    217                             Main.map.mapView.repaint();
    218                         }
    219                     }
    220             );
    221             if (visitor.getConflicts().isEmpty())
    222                 return;
    223             targetLayer.getConflicts().add(visitor.getConflicts());
    224             JOptionPane.showMessageDialog(
    225                     Main.parent,
    226                     trn("There was {0} conflict during import.",
    227                             "There were {0} conflicts during import.",
    228                             visitor.getConflicts().size(),
    229                             visitor.getConflicts().size()
    230                     ),
    231                     trn("Conflict during download", "Conflicts during download", visitor.getConflicts().size()),
    232                     JOptionPane.WARNING_MESSAGE
    233             );
    234         }
    235 
    236         protected void downloadParents(long id, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException{
    237             reader = new OsmServerBackreferenceReader(id, type);
    238             DataSet ds = reader.parseOsm(progressMonitor);
    239             synchronized(this) { // avoid race condition in cancel()
    240                 reader = null;
    241             }
    242             DataSetMerger visitor = new DataSetMerger(parents, ds);
    243             visitor.merge();
    244         }
    245 
    246         @Override
    247         protected void realRun() throws SAXException, IOException, OsmTransferException {
    248             try {
    249                 progressMonitor.setTicksCount(children.size());
    250                 int i=1;
    251                 for (Entry<Long, OsmPrimitiveType> entry: children.entrySet()) {
    252                     if (cancelled)
    253                         return;
    254                     String msg = "";
    255                     switch(entry.getValue()) {
    256                     case NODE: msg = tr("({0}/{1}) Loading parents of node {2}", i+1,children.size(), entry.getKey()); break;
    257                     case WAY: msg = tr("({0}/{1}) Loading parents of way {2}", i+1,children.size(), entry.getKey()); break;
    258                     case RELATION: msg = tr("({0}/{1}) Loading parents of relation {2}", i+1,children.size(), entry.getKey()); break;
    259                     }
    260                     progressMonitor.subTask(msg);
    261                     downloadParents(entry.getKey(), entry.getValue(), progressMonitor.createSubTaskMonitor(1, false));
    262                     i++;
    263                 }
    264             } catch(Exception e) {
    265                 if (cancelled)
    266                     return;
    267                 lastException = e;
    268             }
    269         }
    270     }
    271 
    27291    @Override
    27392    protected void updateEnabledState() {
  • trunk/src/org/openstreetmap/josm/data/osm/OsmPrimitiveType.java

    r2845 r2923  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.data.osm;
     3import static org.openstreetmap.josm.tools.I18n.marktr;
    34import static org.openstreetmap.josm.tools.I18n.tr;
    45
     
    78public enum OsmPrimitiveType {
    89
    9     NODE ("node", Node.class, NodeData.class),
    10     WAY  ("way", Way.class, WayData.class),
    11     RELATION ("relation", Relation.class, RelationData.class);
     10    NODE (marktr("node"), Node.class, NodeData.class),
     11    WAY  (marktr("way"), Way.class, WayData.class),
     12    RELATION (marktr("relation"), Relation.class, RelationData.class);
    1213
    1314    private final String apiTypeName;
     
    7172    }
    7273
     74    @Override
     75    public String toString() {
     76        return tr(getAPIName());
     77    }
    7378}
  • trunk/src/org/openstreetmap/josm/gui/MainMenu.java

    r2902 r2923  
    2929import org.openstreetmap.josm.actions.DistributeAction;
    3030import org.openstreetmap.josm.actions.DownloadAction;
     31import org.openstreetmap.josm.actions.DownloadPrimitiveAction;
    3132import org.openstreetmap.josm.actions.DownloadReferrersAction;
    3233import org.openstreetmap.josm.actions.DuplicateAction;
     
    100101    public final JosmAction gpxExport = new GpxExportAction();
    101102    public final DownloadAction download = new DownloadAction();
     103    public final DownloadPrimitiveAction downloadPrimitive = new DownloadPrimitiveAction();
    102104    public final DownloadReferrersAction downloadReferrers = new DownloadReferrersAction();
    103105    public final CloseChangesetAction closeChangesetAction = new CloseChangesetAction();
     
    210212        fileMenu.addSeparator();
    211213        add(fileMenu, download);
     214        add(fileMenu, downloadPrimitive);
    212215        add(fileMenu, downloadReferrers);
    213216        add(fileMenu, update);
Note: See TracChangeset for help on using the changeset viewer.