Ignore:
Timestamp:
2009-07-19T17:38:55+02:00 (15 years ago)
Author:
jttt
Message:

PleaseWait refactoring. Progress is now reported using ProgressMonitor interface, that is available through PleaseWaitRunnable.

File:
1 edited

Legend:

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

    r1806 r1811  
    88import java.util.Collection;
    99
    10 import org.openstreetmap.josm.Main;
    1110import org.openstreetmap.josm.data.osm.DataSet;
    1211import org.openstreetmap.josm.data.osm.OsmPrimitive;
     
    1514import org.openstreetmap.josm.data.osm.Way;
    1615import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
     16import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1717
    1818/**
     
    2121 * which refer to the node. For a {@see Way} or a {@see Relation}, only relations are
    2222 * read.
    23  * 
     23 *
    2424 * OsmServerBackreferenceReader uses the API calls <code>[node|way|relation]/#id/relations</code>
    2525 * and  <code>node/#id/ways</code> to retrieve the referring primitives. The default behaviour
    2626 * of these calls is to reply incomplete primitives only.
    27  * 
     27 *
    2828 * If you set {@see #setReadFull(boolean)} to true this reader uses a {@see MultiFetchServerObjectReader}
    2929 * to complete incomplete primitives.
    30  * 
     30 *
    3131 *
    3232 */
     
    4242    /**
    4343     * constructor
    44      * 
     44     *
    4545     * @param primitive  the primitive to be read. Must not be null. primitive.id > 0 expected
    46      * 
     46     *
    4747     * @exception IllegalArgumentException thrown if primitive is null
    4848     * @exception IllegalArgumentException thrown if primitive.id <= 0
     
    6060    /**
    6161     * constructor
    62      * 
     62     *
    6363     * @param id  the id of the primitive. > 0 expected
    6464     * @param type the type of the primitive. Must not be null.
    65      * 
     65     *
    6666     * @exception IllegalArgumentException thrown if id <= 0
    6767     * @exception IllegalArgumentException thrown if type is null
    68      * 
     68     *
    6969     */
    7070    public OsmServerBackreferenceReader(long id, OsmPrimitiveType type) throws IllegalArgumentException   {
     
    8080    /**
    8181     * constructor
    82      * 
     82     *
    8383     * @param id  the id of the primitive. > 0 expected
    8484     * @param type the type of the primitive. Must not be null.
    8585     * @param readFull true, if referers should be read fully (i.e. including their immediate children)
    86      * 
     86     *
    8787     */
    8888    public OsmServerBackreferenceReader(OsmPrimitive primitive, boolean readFull) {
     
    9393    /**
    9494     * constructor
    95      * 
     95     *
    9696     * @param primitive the primitive whose referers are to be read
    9797     * @param readFull true, if referers should be read fully (i.e. including their immediate children)
    98      * 
     98     *
    9999     * @exception IllegalArgumentException thrown if id <= 0
    100100     * @exception IllegalArgumentException thrown if type is null
    101      * 
     101     *
    102102     */
    103103    public OsmServerBackreferenceReader(long id, OsmPrimitiveType type, boolean readFull) throws IllegalArgumentException  {
     
    108108    /**
    109109     * Replies true if this reader also reads immediate children of referring primitives
    110      * 
     110     *
    111111     * @return true if this reader also reads immediate children of referring primitives
    112112     */
     
    117117    /**
    118118     * Set true if this reader should reads immediate children of referring primitives too. False, otherweise.
    119      * 
     119     *
    120120     * @param readFull true if this reader should reads immediate children of referring primitives too. False, otherweise.
    121121     */
     
    126126    /**
    127127     * Reads referring ways from the API server and replies them in a {@see DataSet}
    128      * 
     128     *
    129129     * @return the data set
    130130     * @throws OsmTransferException
    131131     */
    132     protected DataSet getReferringWays() throws OsmTransferException {
     132    protected DataSet getReferringWays(ProgressMonitor progressMonitor) throws OsmTransferException {
    133133        InputStream in = null;
    134         try {
    135             Main.pleaseWaitDlg.progress.setValue(0);
    136             Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
     134        progressMonitor.beginTask(null, 2);
     135        try {
     136            progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
    137137            StringBuffer sb = new StringBuffer();
    138138            sb.append(primitiveType.getAPIName())
    139139            .append("/").append(id).append("/ways");
    140140
    141             in = getInputStream(sb.toString(), Main.pleaseWaitDlg);
     141            in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
    142142            if (in == null)
    143143                return null;
    144             Main.pleaseWaitDlg.currentAction.setText(tr("Downloading referring ways ..."));
    145             return OsmReader.parseDataSet(in,Main.pleaseWaitDlg);
     144            progressMonitor.subTask(tr("Downloading referring ways ..."));
     145            return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
    146146        } catch(OsmTransferException e) {
    147147            throw e;
     
    151151            throw new OsmTransferException(e);
    152152        } finally {
     153            progressMonitor.finishTask();
    153154            if (in != null) {
    154155                try {
     
    162163
    163164     * Reads referring relations from the API server and replies them in a {@see DataSet}
    164      * 
     165     *
    165166     * @return the data set
    166167     * @throws OsmTransferException
    167168     */
    168     protected DataSet getReferringRelations() throws OsmTransferException {
     169    protected DataSet getReferringRelations(ProgressMonitor progressMonitor) throws OsmTransferException {
    169170        InputStream in = null;
    170         try {
    171             Main.pleaseWaitDlg.progress.setValue(0);
    172             Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
     171        progressMonitor.beginTask(null, 2);
     172        try {
     173            progressMonitor.subTask(tr("Contacting OSM Server..."));
    173174            StringBuffer sb = new StringBuffer();
    174175            sb.append(primitiveType.getAPIName())
    175176            .append("/").append(id).append("/relations");
    176177
    177             in = getInputStream(sb.toString(), Main.pleaseWaitDlg);
     178            in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
    178179            if (in == null)
    179180                return null;
    180             Main.pleaseWaitDlg.currentAction.setText(tr("Downloading referring relations ..."));
    181             return OsmReader.parseDataSet(in,Main.pleaseWaitDlg);
     181            progressMonitor.subTask(tr("Downloading referring relations ..."));
     182            return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, true));
    182183        } catch(OsmTransferException e) {
    183184            throw e;
     
    187188            throw new OsmTransferException(e);
    188189        } finally {
     190            progressMonitor.finishTask();
    189191            if (in != null) {
    190192                try {
     
    200202     * incomplete primitives are read from the server with an individual <tt>/api/0.6/[way,relation]/#id/full</tt>
    201203     * request.
    202      * 
     204     *
    203205     * <ul>
    204206     *   <li>if this reader reads referers for an {@see Node}, referring ways are always
     
    207209     *    are only read fully if {@see #setReadFull(boolean)} is set to true.</li>
    208210     * </ul>
    209      * 
     211     *
    210212     * The method replies the modified dataset.
    211      * 
     213     *
    212214     * @param ds the original dataset
    213215     * @return the modified dataset
    214216     * @throws OsmTransferException thrown if an exception occurs.
    215217     */
    216     protected DataSet readIncompletePrimitives(DataSet ds) throws OsmTransferException {
    217         Collection<Way> waysToCheck = new ArrayList<Way>(ds.ways);
    218         if (isReadFull() ||primitiveType.equals(OsmPrimitiveType.NODE)) {
    219             for (Way way: waysToCheck) {
    220                 if (way.id > 0 && way.incomplete) {
    221                     OsmServerObjectReader reader = new OsmServerObjectReader(way.id, OsmPrimitiveType.from(way), true /* read full */);
    222                     DataSet wayDs = reader.parseOsm();
    223                     MergeVisitor visitor = new MergeVisitor(ds, wayDs);
    224                     visitor.merge();
     218    protected DataSet readIncompletePrimitives(DataSet ds, ProgressMonitor progressMonitor) throws OsmTransferException {
     219        progressMonitor.beginTask(null, 2);
     220        try {
     221            Collection<Way> waysToCheck = new ArrayList<Way>(ds.ways);
     222            if (isReadFull() ||primitiveType.equals(OsmPrimitiveType.NODE)) {
     223                for (Way way: waysToCheck) {
     224                    if (way.id > 0 && way.incomplete) {
     225                        OsmServerObjectReader reader = new OsmServerObjectReader(way.id, OsmPrimitiveType.from(way), true /* read full */);
     226                        DataSet wayDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
     227                        MergeVisitor visitor = new MergeVisitor(ds, wayDs);
     228                        visitor.merge();
     229                    }
    225230                }
    226231            }
    227         }
    228         if (isReadFull()) {
    229             Collection<Relation> relationsToCheck  = new ArrayList<Relation>(ds.relations);
    230             for (Relation relation: relationsToCheck) {
    231                 if (relation.id > 0 && relation.incomplete) {
    232                     OsmServerObjectReader reader = new OsmServerObjectReader(relation.id, OsmPrimitiveType.from(relation), true /* read full */);
    233                     DataSet wayDs = reader.parseOsm();
    234                     MergeVisitor visitor = new MergeVisitor(ds, wayDs);
    235                     visitor.merge();
     232            if (isReadFull()) {
     233                Collection<Relation> relationsToCheck  = new ArrayList<Relation>(ds.relations);
     234                for (Relation relation: relationsToCheck) {
     235                    if (relation.id > 0 && relation.incomplete) {
     236                        OsmServerObjectReader reader = new OsmServerObjectReader(relation.id, OsmPrimitiveType.from(relation), true /* read full */);
     237                        DataSet wayDs = reader.parseOsm(progressMonitor.createSubTaskMonitor(1, false));
     238                        MergeVisitor visitor = new MergeVisitor(ds, wayDs);
     239                        visitor.merge();
     240                    }
    236241                }
    237242            }
    238         }
    239         return ds;
     243            return ds;
     244        } finally {
     245            progressMonitor.finishTask();
     246        }
    240247    }
    241248
     
    243250     * Reads the referring primitives from the OSM server, parses them and
    244251     * replies them as {@see DataSet}
    245      * 
     252     *
    246253     * @return the dataset with the referring primitives
    247254     * @exception OsmTransferException thrown if an error occurs while communicating with the server
    248255     */
    249256    @Override
    250     public DataSet parseOsm() throws OsmTransferException {
    251         DataSet ret = new DataSet();
    252         if (primitiveType.equals(OsmPrimitiveType.NODE)) {
    253             DataSet ds = getReferringWays();
     257    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
     258        progressMonitor.beginTask(null, 3);
     259        try {
     260            DataSet ret = new DataSet();
     261            if (primitiveType.equals(OsmPrimitiveType.NODE)) {
     262                DataSet ds = getReferringWays(progressMonitor.createSubTaskMonitor(1, false));
     263                MergeVisitor visitor = new MergeVisitor(ret,ds);
     264                visitor.merge();
     265                ret = visitor.getMyDataSet();
     266            }
     267            DataSet ds = getReferringRelations(progressMonitor.createSubTaskMonitor(1, false));
    254268            MergeVisitor visitor = new MergeVisitor(ret,ds);
    255269            visitor.merge();
    256270            ret = visitor.getMyDataSet();
    257         }
    258         DataSet ds = getReferringRelations();
    259         MergeVisitor visitor = new MergeVisitor(ret,ds);
    260         visitor.merge();
    261         ret = visitor.getMyDataSet();
    262         readIncompletePrimitives(ret);
    263         return ret;
     271            readIncompletePrimitives(ret, progressMonitor.createSubTaskMonitor(1, false));
     272            return ret;
     273        } finally {
     274            progressMonitor.finishTask();
     275        }
    264276    }
    265277}
Note: See TracChangeset for help on using the changeset viewer.