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

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

Location:
trunk/src/org/openstreetmap/josm/io
Files:
15 edited

Legend:

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

    r1772 r1811  
    88
    99import org.openstreetmap.josm.Main;
     10import org.openstreetmap.josm.data.gpx.GpxData;
    1011import org.openstreetmap.josm.data.osm.DataSet;
    11 import org.openstreetmap.josm.data.gpx.GpxData;
     12import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1213import org.xml.sax.SAXException;
    1314
     
    3940     *      ways.
    4041     */
    41     public GpxData parseRawGps() throws IOException, SAXException {
    42         Main.pleaseWaitDlg.progress.setValue(0);
    43         Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
     42    public GpxData parseRawGps(ProgressMonitor progressMonitor) throws IOException, SAXException {
     43        progressMonitor.beginTask("", 1);
    4444        try {
     45            progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
    4546            String url = "trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page=";
    4647
     
    4849            GpxData result = null;
    4950            for (int i = 0;!done;++i) {
    50                 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
    51                 InputStream in = getInputStream(url+i, Main.pleaseWaitDlg);
     51                progressMonitor.subTask(tr("Downloading points {0} to {1}...", i * 5000, ((i + 1) * 5000)));
     52                InputStream in = getInputStream(url+i, progressMonitor.createSubTaskMonitor(1, true));
    5253                if (in == null) {
    5354                    break;
    5455                }
     56                progressMonitor.setTicks(0);
    5557                GpxData currentGpx = new GpxReader(in, null).data;
    5658                if (result == null) {
     
    8385                throw (RuntimeException)e;
    8486            throw new RuntimeException(e);
     87        } finally {
     88            progressMonitor.finishTask();
    8589        }
    8690    }
     
    9195     */
    9296    @Override
    93     public DataSet parseOsm() throws OsmTransferException {
     97    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
     98        progressMonitor.beginTask(tr("Contacting OSM Server..."), 10);
    9499        try {
    95             Main.pleaseWaitDlg.progress.setValue(0);
    96             Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
    97             Main.pleaseWaitDlg.setIndeterminate(true);
    98             final InputStream in = getInputStream("map?bbox="+lon1+","+lat1+","+lon2+","+lat2, Main.pleaseWaitDlg);
    99             Main.pleaseWaitDlg.setIndeterminate(false);
     100            progressMonitor.indeterminateSubTask(null);
     101            final InputStream in = getInputStream("map?bbox="+lon1+","+lat1+","+lon2+","+lat2, progressMonitor.createSubTaskMonitor(9, false));
    100102            if (in == null)
    101103                return null;
    102             Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
    103             final DataSet data = OsmReader.parseDataSet(in,Main.pleaseWaitDlg);
     104            final DataSet data = OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
    104105            in.close();
    105106            activeConnection = null;
     
    117118                return null;
    118119            throw new OsmTransferException(e);
     120        } finally {
     121            progressMonitor.finishTask();
    119122        }
    120123    }
  • trunk/src/org/openstreetmap/josm/io/DiffResultReader.java

    r1523 r1811  
    1818import org.openstreetmap.josm.data.osm.Way;
    1919import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
    20 import org.openstreetmap.josm.gui.PleaseWaitDialog;
     20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2121import org.xml.sax.Attributes;
    2222import org.xml.sax.InputSource;
     
    6464     * Parse the given input source and return the dataset.
    6565     */
    66     public static void parseDiffResult(String source, Collection<OsmPrimitive> osm, Collection<OsmPrimitive> processed, Map<OsmPrimitive,Long> newIdMap, PleaseWaitDialog pleaseWaitDlg)
     66    public static void parseDiffResult(String source, Collection<OsmPrimitive> osm, Collection<OsmPrimitive> processed, Map<OsmPrimitive,Long> newIdMap, ProgressMonitor progressMonitor)
    6767    throws SAXException, IOException {
    6868
    69        DiffResultReader drr = new DiffResultReader();
    70        drr.processed = processed;
    71        drr.newIdMap = newIdMap;
    72        InputSource inputSource = new InputSource(new StringReader(source));
    73        try {
    74            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, drr.new Parser());
    75        } catch (ParserConfigurationException e1) {
    76            e1.printStackTrace(); // broken SAXException chaining
    77            throw new SAXException(e1);
    78        }
     69        progressMonitor.beginTask(tr("Preparing data..."));
     70        try {
    7971
    80        if (pleaseWaitDlg != null) {
    81            pleaseWaitDlg.progress.setValue(0);
    82            pleaseWaitDlg.currentAction.setText(tr("Preparing data..."));
    83        }
     72            DiffResultReader drr = new DiffResultReader();
     73            drr.processed = processed;
     74            drr.newIdMap = newIdMap;
     75            InputSource inputSource = new InputSource(new StringReader(source));
     76            try {
     77                SAXParserFactory.newInstance().newSAXParser().parse(inputSource, drr.new Parser());
     78            } catch (ParserConfigurationException e1) {
     79                e1.printStackTrace(); // broken SAXException chaining
     80                throw new SAXException(e1);
     81            }
    8482
    85        for (OsmPrimitive p : osm) {
    86            //System.out.println("old: "+ p);
    87            p.visit(drr);
    88            //System.out.println("new: "+ p);
    89            //System.out.println("");
    90        }
     83            for (OsmPrimitive p : osm) {
     84                //System.out.println("old: "+ p);
     85                p.visit(drr);
     86                //System.out.println("new: "+ p);
     87                //System.out.println("");
     88            }
     89        } finally {
     90            progressMonitor.finishTask();
     91        }
    9192    }
    9293
  • trunk/src/org/openstreetmap/josm/io/MultiFetchServerObjectReader.java

    r1790 r1811  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.IOException;
    76import java.io.InputStream;
    87import java.net.HttpURLConnection;
     
    1413import java.util.logging.Logger;
    1514
    16 import org.openstreetmap.josm.Main;
    1715import org.openstreetmap.josm.data.osm.DataSet;
    1816import org.openstreetmap.josm.data.osm.Node;
     
    2321import org.openstreetmap.josm.data.osm.Way;
    2422import org.openstreetmap.josm.data.osm.visitor.MergeVisitor;
    25 import org.xml.sax.SAXException;
     23import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     24import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2625
    2726/**
    2827 * Retrieves a set of {@see OsmPrimitive}s from an OSM server using the so called
    2928 * Multi Fetch API.
    30  * 
     29 *
    3130 * Usage:
    3231 * <pre>
     
    4241 *    }
    4342 * </pre>
    44  * 
     43 *
    4544 *
    4645 */
     
    5352     * which should be safe according to the
    5453     * <a href="http://www.boutell.com/newfaq/misc/urllength.html">WWW FAQ</a>.
    55      * 
     54     *
    5655     */
    5756    static private int MAX_IDS_PER_REQUEST = 200;
     
    7978     * remembers an {@see OsmPrimitive}'s id and its type. The id will
    8079     * later be fetched as part of a Multi Get request.
    81      * 
     80     *
    8281     * Ignore the id if it id <= 0.
    83      * 
     82     *
    8483     * @param id  the id
    8584     * @param type  the type
     
    10099     * an {@see OsmPrimitive} with id=<code>id</code>. The id will
    101100     * later we fetched as part of a Multi Get request.
    102      * 
     101     *
    103102     * Ignore the id if it id <= 0.
    104      * 
     103     *
    105104     * @param ds  the dataset (must not be null)
    106105     * @param id  the id
     
    123122     * appends a list of  ids to the list of ids which will be fetched from the server. ds must
    124123     * include an {@see OsmPrimitive} for each id in ids.
    125      * 
     124     *
    126125     * id is ignored if id <= 0.
    127      * 
     126     *
    128127     * @param ds  the dataset
    129128     * @param ids  the list of ids
    130129     * @return this
    131      * 
     130     *
    132131     */
    133132    public MultiFetchServerObjectReader append(DataSet ds, long ... ids)  {
     
    142141     * appends a collection of  ids to the list of ids which will be fetched from the server. ds must
    143142     * include an {@see OsmPrimitive} for each id in ids.
    144      * 
     143     *
    145144     * id is ignored if id <= 0.
    146      * 
     145     *
    147146     * @param ds  the dataset
    148147     * @param ids  the collection of ids
    149148     * @return this
    150      * 
     149     *
    151150     */
    152151    public MultiFetchServerObjectReader append(DataSet ds, Collection<Long> ids) {
     
    163162     * @param node  the node (ignored, if null)
    164163     * @return this
    165      * 
     164     *
    166165     */
    167166    public MultiFetchServerObjectReader append(Node node) {
     
    177176     * @param way the way (ignored, if null)
    178177     * @return this
    179      * 
     178     *
    180179     */
    181180    public MultiFetchServerObjectReader append(Way way) {
     
    196195     * @param relation  the relation (ignored, if null)
    197196     * @return this
    198      * 
     197     *
    199198     */
    200199    public MultiFetchServerObjectReader append(Relation relation) {
     
    225224     * @param primitives  the list of primitives (ignored, if null)
    226225     * @return this
    227      * 
     226     *
    228227     * @see #append(Node)
    229228     * @see #append(Way)
    230229     * @see #append(Relation)
    231      * 
     230     *
    232231     */
    233232    public MultiFetchServerObjectReader append(Collection<OsmPrimitive> primitives) {
     
    242241     * extracts a subset of max {@see #MAX_IDS_PER_REQUEST} ids from <code>ids</code> and
    243242     * replies the subset. The extracted subset is removed from <code>ids</code>.
    244      * 
     243     *
    245244     * @param ids a set of ids
    246245     * @return the subset of ids
     
    267266     * builds the Multi Get request string for a set of ids and a given
    268267     * {@see OsmPrimitiveType}.
    269      * 
     268     *
    270269     * @param type the type
    271270     * @param idPackage  the package of ids
     
    290289     * builds the Multi Get request string for a single id and a given
    291290     * {@see OsmPrimitiveType}.
    292      * 
     291     *
    293292     * @param type the type
    294293     * @param id the id
     
    306305     * invokes a Multi Get for a set of ids and a given {@see OsmPrimitiveType}.
    307306     * The retrieved primitives are merged to {@see #outputDataSet}.
    308      * 
     307     *
    309308     * @param type the type
    310309     * @param pkg the package of ids
    311310     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    312      * 
    313      */
    314     protected void multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg) throws OsmTransferException {
     311     *
     312     */
     313    protected void multiGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor) throws OsmTransferException {
    315314        String request = buildRequestString(type, pkg);
    316         final InputStream in = getInputStream(request, Main.pleaseWaitDlg);
     315        final InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE);
    317316        if (in == null) return;
    318         Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
     317        progressMonitor.subTask(tr("Downloading OSM data..."));
    319318        try {
    320             final OsmReader osm = OsmReader.parseDataSetOsm(in, Main.pleaseWaitDlg);
     319            final OsmReader osm = OsmReader.parseDataSetOsm(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    321320            merge(osm.getDs());
    322321        } catch(Exception e) {
     
    328327     * invokes a Multi Get for a single id and a given {@see OsmPrimitiveType}.
    329328     * The retrieved primitive is merged to {@see #outputDataSet}.
    330      * 
     329     *
    331330     * @param type the type
    332331     * @param id the id
    333332     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    334      * 
    335      */
    336     protected void singleGetId(OsmPrimitiveType type, long id) throws OsmTransferException {
     333     *
     334     */
     335    protected void singleGetId(OsmPrimitiveType type, long id, ProgressMonitor progressMonitor) throws OsmTransferException {
    337336        String request = buildRequestString(type, id);
    338         final InputStream in = getInputStream(request, Main.pleaseWaitDlg);
     337        final InputStream in = getInputStream(request, NullProgressMonitor.INSTANCE);
    339338        if (in == null)
    340339            return;
    341         Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
     340        progressMonitor.subTask(tr("Downloading OSM data..."));
    342341        try {
    343             final OsmReader osm = OsmReader.parseDataSetOsm(in,Main.pleaseWaitDlg);
     342            final OsmReader osm = OsmReader.parseDataSetOsm(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    344343            merge(osm.getDs());
    345344        } catch(Exception e) {
     
    351350     * invokes a sequence of Multi Gets for individual ids in a set of ids and a given {@see OsmPrimitiveType}.
    352351     * The retrieved primitives are merged to {@see #outputDataSet}.
    353      * 
     352     *
    354353     * This method is used if one of the ids in pkg doesn't exist (the server replies with return code 404).
    355354     * If the set is fetched with this method it is possible to find out which of the ids doesn't exist.
    356355     * Unfortunatelly, the server does not provide an error header or an error body for a 404 reply.
    357      * 
     356     *
    358357     * @param type the type
    359358     * @param pkg the set of ids
    360359     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    361      * 
    362      */
    363     protected void singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg) throws OsmTransferException {
     360     *
     361     */
     362    protected void singleGetIdPackage(OsmPrimitiveType type, Set<Long> pkg, ProgressMonitor progressMonitor) throws OsmTransferException {
    364363        for (long id : pkg) {
    365364            try {
    366                 singleGetId(type, id);
     365                singleGetId(type, id, progressMonitor);
    367366            } catch(OsmApiException e) {
    368367                if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
     
    378377    /**
    379378     * merges the dataset <code>from</code> to {@see #outputDataSet}.
    380      * 
     379     *
    381380     * @param from the other dataset
    382      * 
     381     *
    383382     */
    384383    protected void merge(DataSet from) {
     
    389388    /**
    390389     * fetches a set of ids of a given {@see OsmPrimitiveType} from the server
    391      * 
     390     *
    392391     * @param ids the set of ids
    393392     * @param type the  type
    394393     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    395394     */
    396     protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type) throws OsmTransferException{
     395    protected void fetchPrimitives(Set<Long> ids, OsmPrimitiveType type, ProgressMonitor progressMonitor) throws OsmTransferException{
    397396        Set<Long> toFetch = new HashSet<Long>(ids);
    398397        toFetch.addAll(ids);
     
    400399            Set<Long> pkg = extractIdPackage(toFetch);
    401400            try {
    402                 multiGetIdPackage(type, pkg);
     401                multiGetIdPackage(type, pkg, progressMonitor);
    403402            } catch(OsmApiException e) {
    404403                if (e.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
    405404                    logger.warning(tr("Server replied with response code 404, retrying with an individual request for each primitive"));
    406                     singleGetIdPackage(type, pkg);
     405                    singleGetIdPackage(type, pkg, progressMonitor);
    407406                } else
    408407                    throw e;
     
    417416     * the latest version of the primitive (if any), even if the primitive is not visible (i.e. if
    418417     * visible==false).
    419      * 
     418     *
    420419     * Invoke {@see #getMissingPrimitives()} to get a list of primitives which have not been
    421420     * found on  the server (the server response code was 404)
    422      * 
     421     *
    423422     * Invoke {@see #getSkippedWay()} to get a list of ways which this reader could not build from
    424423     * the fetched data because the ways refer to nodes which don't exist on the server.
    425      * 
     424     *
    426425     * @return the parsed data
    427426     * @exception OsmTransferException thrown if an error occurs while communicating with the API server
    428427     * @see #getMissingPrimitives()
    429428     * @see #getSkippedWays()
    430      * 
     429     *
    431430
    432431     */
    433432    @Override
    434     public DataSet parseOsm() throws OsmTransferException {
    435         missingPrimitives = new HashSet<Long>();
    436 
    437         fetchPrimitives(nodes,OsmPrimitiveType.NODE);
    438         fetchPrimitives(ways,OsmPrimitiveType.WAY);
    439         fetchPrimitives(relations,OsmPrimitiveType.RELATION);
    440         return outputDataSet;
     433    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
     434        progressMonitor.beginTask("");
     435        try {
     436            missingPrimitives = new HashSet<Long>();
     437
     438            fetchPrimitives(nodes,OsmPrimitiveType.NODE, progressMonitor);
     439            fetchPrimitives(ways,OsmPrimitiveType.WAY, progressMonitor);
     440            fetchPrimitives(relations,OsmPrimitiveType.RELATION, progressMonitor);
     441            return outputDataSet;
     442        } finally {
     443            progressMonitor.finishTask();
     444        }
    441445    }
    442446
     
    445449     * server was submitted but which are not available from the server (the server
    446450     * replied a return code of 404)
    447      * 
     451     *
    448452     * @return the set of ids of missing primitives
    449453     */
  • trunk/src/org/openstreetmap/josm/io/OsmApi.java

    r1750 r1811  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.EventQueue;
    76import java.io.BufferedReader;
    87import java.io.BufferedWriter;
     
    3231import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    3332import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor;
     33import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3434import org.xml.sax.Attributes;
    3535import org.xml.sax.InputSource;
     
    279279     * @throws OsmTransferException signifying a non-200 return code, or connection errors
    280280     */
    281     public void createChangeset(String comment) throws OsmTransferException {
    282         changeset = new Changeset();
    283         notifyStatusMessage(tr("Opening changeset..."));
    284         Properties sysProp = System.getProperties();
    285         Object ua = sysProp.get("http.agent");
    286         changeset.put("created_by", (ua == null) ? "JOSM" : ua.toString());
    287         changeset.put("comment", comment);
    288         createPrimitive(changeset);
     281    public void createChangeset(String comment, ProgressMonitor progressMonitor) throws OsmTransferException {
     282        progressMonitor.beginTask((tr("Opening changeset...")));
     283        try {
     284            changeset = new Changeset();
     285            Properties sysProp = System.getProperties();
     286            Object ua = sysProp.get("http.agent");
     287            changeset.put("created_by", (ua == null) ? "JOSM" : ua.toString());
     288            changeset.put("comment", comment);
     289            createPrimitive(changeset);
     290        } finally {
     291            progressMonitor.finishTask();
     292        }
    289293    }
    290294
     
    294298     * @throws OsmTransferException if something goes wrong.
    295299     */
    296     public void stopChangeset() throws OsmTransferException {
    297         initialize();
    298         notifyStatusMessage(tr("Closing changeset..."));
    299         sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null);
    300         changeset = null;
     300    public void stopChangeset(ProgressMonitor progressMonitor) throws OsmTransferException {
     301        progressMonitor.beginTask(tr("Closing changeset..."));
     302        try {
     303            initialize();
     304            sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null);
     305            changeset = null;
     306        } finally {
     307            progressMonitor.finishTask();
     308        }
    301309    }
    302310
     
    308316     * @throws OsmTransferException if something is wrong
    309317     */
    310     public Collection<OsmPrimitive> uploadDiff(final Collection<OsmPrimitive> list) throws OsmTransferException {
    311 
    312         if (changeset == null)
    313             throw new OsmTransferException(tr("No changeset present for diff upload"));
    314 
    315         initialize();
    316         final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>();
    317 
    318         CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this);
    319 
    320         notifyStatusMessage(tr("Preparing..."));
    321         for (OsmPrimitive osm : list) {
    322             osm.visit(duv);
    323             notifyRelativeProgress(1);
    324         }
    325         notifyStatusMessage(tr("Uploading..."));
    326         setAutoProgressIndication(true);
    327 
    328         String diff = duv.getDocument();
     318    public Collection<OsmPrimitive> uploadDiff(final Collection<OsmPrimitive> list, ProgressMonitor progressMonitor) throws OsmTransferException {
     319
     320        progressMonitor.beginTask("", list.size() * 2);
    329321        try {
    330             String diffresult = sendRequest("POST", "changeset/" + changeset.id + "/upload", diff);
    331             DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(), Main.pleaseWaitDlg);
    332         } catch(OsmTransferException e) {
    333             throw e;
    334         } catch(Exception e) {
    335             throw new OsmTransferException(e);
     322            if (changeset == null)
     323                throw new OsmTransferException(tr("No changeset present for diff upload"));
     324
     325            initialize();
     326            final ArrayList<OsmPrimitive> processed = new ArrayList<OsmPrimitive>();
     327
     328            CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this);
     329
     330            progressMonitor.subTask(tr("Preparing..."));
     331            for (OsmPrimitive osm : list) {
     332                osm.visit(duv);
     333                progressMonitor.worked(1);
     334            }
     335            progressMonitor.indeterminateSubTask(tr("Uploading..."));
     336
     337            String diff = duv.getDocument();
     338            try {
     339                String diffresult = sendRequest("POST", "changeset/" + changeset.id + "/upload", diff);
     340                DiffResultReader.parseDiffResult(diffresult, list, processed, duv.getNewIdMap(),
     341                        progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
     342            } catch(OsmTransferException e) {
     343                throw e;
     344            } catch(Exception e) {
     345                throw new OsmTransferException(e);
     346            }
     347
     348            return processed;
    336349        } finally {
    337             setAutoProgressIndication(false);
    338         }
    339 
    340         return processed;
     350            progressMonitor.finishTask();
     351        }
    341352    }
    342353
     
    468479
    469480    /**
    470      * notifies any listeners about the current state of this API. Currently just
    471      * displays the message in the global progress dialog, see {@see Main#pleaseWaitDlg}
    472      *
    473      * @param message a status message.
    474      */
    475     protected void notifyStatusMessage(String message) {
    476         Main.pleaseWaitDlg.currentAction.setText(message);
    477     }
    478 
    479     /**
    480      * notifies any listeners about the current about a relative progress. Currently just
    481      * increments the progress monitor in the in the global progress dialog, see {@see Main#pleaseWaitDlg}
    482      *
    483      * @param int the delta
    484      */
    485     protected void notifyRelativeProgress(int delta) {
    486         int current= Main.pleaseWaitDlg.progress.getValue();
    487         Main.pleaseWaitDlg.progress.setValue(current + delta);
    488     }
    489 
    490 
    491     protected void setAutoProgressIndication(final boolean enabled) {
    492         EventQueue.invokeLater(
    493                 new Runnable() {
    494                     public void run() {
    495                         Main.pleaseWaitDlg.setIndeterminate(enabled);
    496                     }
    497                 }
    498         );
    499     }
    500 
    501     /**
    502481     * returns the API capabilities; null, if the API is not initialized yet
    503      * 
     482     *
    504483     * @return the API capabilities
    505484     */
  • trunk/src/org/openstreetmap/josm/io/OsmConnection.java

    r1670 r1811  
    1111import java.nio.ByteBuffer;
    1212import java.nio.CharBuffer;
     13import java.nio.charset.CharacterCodingException;
    1314import java.nio.charset.Charset;
    1415import java.nio.charset.CharsetEncoder;
    15 import java.nio.charset.CharacterCodingException;
    1616
    1717import javax.swing.JCheckBox;
     
    126126
    127127    public void cancel() {
    128         Main.pleaseWaitDlg.currentAction.setText(tr("Aborting..."));
     128        //TODO
     129        //Main.pleaseWaitDlg.currentAction.setText(tr("Aborting..."));
    129130        cancel = true;
    130131        if (activeConnection != null) {
  • trunk/src/org/openstreetmap/josm/io/OsmHistoryReader.java

    r1670 r1811  
    1818import org.openstreetmap.josm.data.osm.history.HistoryRelation;
    1919import org.openstreetmap.josm.data.osm.history.HistoryWay;
    20 import org.openstreetmap.josm.gui.PleaseWaitDialog;
     20import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2121import org.openstreetmap.josm.tools.DateUtils;
    2222import org.xml.sax.Attributes;
     
    3232 * {@see OsmPrimitive}s. We use objects derived from {@see HistoryOsmPrimitive} instead and we
    3333 * keep the data in a dedicated {@see HistoryDataSet}.
    34  * 
     34 *
    3535 */
    3636public class OsmHistoryReader {
     
    210210    }
    211211
    212     public HistoryDataSet parse(PleaseWaitDialog dialog) throws SAXException, IOException {
     212    public HistoryDataSet parse(ProgressMonitor progressMonitor) throws SAXException, IOException {
    213213        InputSource inputSource = new InputSource(new InputStreamReader(in, "UTF-8"));
    214         dialog.currentAction.setText("Parsing OSM history data ...");
     214        progressMonitor.beginTask(tr("Parsing OSM history data ..."));
    215215        try {
    216216            SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new Parser());
     
    218218            e1.printStackTrace(); // broken SAXException chaining
    219219            throw new SAXException(e1);
     220        } finally {
     221            progressMonitor.finishTask();
    220222        }
    221223        return data;
  • trunk/src/org/openstreetmap/josm/io/OsmImporter.java

    r1790 r1811  
    1111import java.io.InputStream;
    1212
    13 import javax.swing.JOptionPane;
    14 
    1513import org.openstreetmap.josm.Main;
    1614import org.openstreetmap.josm.actions.ExtensionFileFilter;
    1715import org.openstreetmap.josm.data.osm.DataSet;
    1816import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     17import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    1918import org.xml.sax.SAXException;
    2019
     
    4645
    4746    protected void importData(InputStream in, File associatedFile) throws SAXException, IOException {
    48         OsmReader osm = OsmReader.parseDataSetOsm(in,Main.pleaseWaitDlg);
     47        OsmReader osm = OsmReader.parseDataSetOsm(in, NullProgressMonitor.INSTANCE);
    4948        DataSet dataSet = osm.getDs();
    5049        OsmDataLayer layer = new OsmDataLayer(dataSet, associatedFile.getName(), associatedFile);
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r1806 r1811  
    1616import java.util.logging.Logger;
    1717
    18 import javax.swing.SwingUtilities;
    1918import javax.xml.parsers.ParserConfigurationException;
    2019import javax.xml.parsers.SAXParserFactory;
     
    3130import org.openstreetmap.josm.data.osm.Way;
    3231import org.openstreetmap.josm.data.osm.visitor.AddVisitor;
    33 import org.openstreetmap.josm.gui.PleaseWaitDialog;
     32import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    3433import org.openstreetmap.josm.tools.DateUtils;
    3534import org.xml.sax.Attributes;
     
    7069    /**
    7170     * constructor (for private use only)
    72      * 
    73      * @see #parseDataSet(InputStream, DataSet, PleaseWaitDialog)
    74      * @see #parseDataSetOsm(InputStream, DataSet, PleaseWaitDialog)
     71     *
     72     * @see #parseDataSet(InputStream, DataSet, ProgressMonitor)
     73     * @see #parseDataSetOsm(InputStream, DataSet, ProgressMonitor)
    7574     */
    7675    private OsmReader() {
     
    454453     *  element found there is returned.
    455454     */
    456     public static DataSet parseDataSet(InputStream source, PleaseWaitDialog pleaseWaitDlg) throws SAXException, IOException {
    457         return parseDataSetOsm(source, pleaseWaitDlg).ds;
    458     }
    459 
    460     public static OsmReader parseDataSetOsm(InputStream source, final PleaseWaitDialog pleaseWaitDlg) throws SAXException, IOException {
     455    public static DataSet parseDataSet(InputStream source, ProgressMonitor progressMonitor) throws SAXException, IOException {
     456        return parseDataSetOsm(source, progressMonitor).ds;
     457    }
     458
     459    public static OsmReader parseDataSetOsm(InputStream source, ProgressMonitor progressMonitor) throws SAXException, IOException {
    461460        OsmReader osm = new OsmReader();
    462461
     
    470469        }
    471470
    472         SwingUtilities.invokeLater(
    473                 new Runnable() {
    474                     public void run() {
    475                         pleaseWaitDlg.currentAction.setText(tr("Prepare OSM data..."));
    476                         pleaseWaitDlg.setIndeterminate(true);
    477                     }
     471        progressMonitor.beginTask(tr("Prepare OSM data...", 2));
     472        try {
     473            for (Node n : osm.nodes.values()) {
     474                osm.adder.visit(n);
     475            }
     476
     477            progressMonitor.worked(1);
     478
     479            try {
     480                osm.createWays();
     481                osm.createRelations();
     482            } catch (NumberFormatException e) {
     483                e.printStackTrace();
     484                throw new SAXException(tr("Ill-formed node id"));
     485            }
     486
     487            // clear all negative ids (new to this file)
     488            for (OsmPrimitive o : osm.ds.allPrimitives())
     489                if (o.id < 0) {
     490                    o.id = 0;
    478491                }
    479         );
    480 
    481         for (Node n : osm.nodes.values()) {
    482             osm.adder.visit(n);
    483         }
    484 
    485         try {
    486             osm.createWays();
    487             osm.createRelations();
    488         } catch (NumberFormatException e) {
    489             e.printStackTrace();
    490             throw new SAXException(tr("Ill-formed node id"));
    491         }
    492 
    493         // clear all negative ids (new to this file)
    494         for (OsmPrimitive o : osm.ds.allPrimitives())
    495             if (o.id < 0) {
    496                 o.id = 0;
    497             }
    498 
    499         SwingUtilities.invokeLater(
    500                 new Runnable() {
    501                     public void run() {
    502                         pleaseWaitDlg.setIndeterminate(false);
    503                         pleaseWaitDlg.progress.setValue(0);
    504                     }
    505                 }
    506         );
    507 
    508         return osm;
     492
     493            return osm;
     494        } finally {
     495            progressMonitor.finishTask();
     496        }
    509497    }
    510498}
  • 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}
  • trunk/src/org/openstreetmap/josm/io/OsmServerHistoryReader.java

    r1790 r1811  
    77import java.io.InputStream;
    88
    9 import org.openstreetmap.josm.Main;
    109import org.openstreetmap.josm.data.osm.DataSet;
    1110import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
    1211import org.openstreetmap.josm.data.osm.history.HistoryDataSet;
     12import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1313import org.xml.sax.SAXException;
    1414
     
    1717/**
    1818 * Reads the history of an {@see OsmPrimitive} from the OSM API server.
    19  * 
     19 *
    2020 */
    2121public class OsmServerHistoryReader extends OsmServerReader {
     
    2626    /**
    2727     * constructor
    28      * 
     28     *
    2929     * @param type the type of the primitive whose history is to be fetched from the server.
    3030     *   Must not be null.
    3131     * @param id the id of the primitive
    32      * 
     32     *
    3333     *  @exception IllegalArgumentException thrown, if type is null
    3434     */
     
    4444    /**
    4545     * don't use - not implemented!
    46      * 
     46     *
    4747     * @exception NotImplementedException
    4848     */
    4949    @Override
    50     public DataSet parseOsm() throws OsmTransferException {
     50    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
    5151        throw new NotImplementedException();
    5252    }
     
    5454    /**
    5555     * Fetches the history from the OSM API and parses it
    56      * 
     56     *
    5757     * @return the data set with the parsed history data
    5858     * @throws OsmTransferException thrown, if an exception occurs
    5959     */
    60     public HistoryDataSet parseHistory() throws OsmTransferException {
     60    public HistoryDataSet parseHistory(ProgressMonitor progressMonitor) throws OsmTransferException {
    6161        InputStream in = null;
     62        progressMonitor.beginTask("");
    6263        try {
    63             Main.pleaseWaitDlg.progress.setValue(0);
    64             Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
     64            progressMonitor.indeterminateSubTask(tr("Contacting OSM Server..."));
    6565            StringBuffer sb = new StringBuffer();
    6666            sb.append(primitiveType.getAPIName())
    6767            .append("/").append(id).append("/history");
    6868
    69             in = getInputStream(sb.toString(), Main.pleaseWaitDlg);
     69            in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
    7070            if (in == null)
    7171                return null;
    72             Main.pleaseWaitDlg.currentAction.setText(tr("Downloading history..."));
     72            progressMonitor.indeterminateSubTask(tr("Downloading history..."));
    7373            final OsmHistoryReader reader = new OsmHistoryReader(in);
    74             HistoryDataSet data = reader.parse(Main.pleaseWaitDlg);
     74            HistoryDataSet data = reader.parse(progressMonitor.createSubTaskMonitor(1, true));
    7575            return data;
    7676        } catch(OsmTransferException e) {
     
    8181            throw new OsmTransferException(e);
    8282        } finally {
     83            progressMonitor.finishTask();
    8384            if (in != null) {
    8485                try {
  • trunk/src/org/openstreetmap/josm/io/OsmServerLocationReader.java

    r1790 r1811  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.io.IOException;
    76import java.io.InputStream;
    87
    9 import org.openstreetmap.josm.Main;
    108import org.openstreetmap.josm.data.osm.DataSet;
    11 import org.xml.sax.SAXException;
     9import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1210
    1311public class OsmServerLocationReader extends OsmServerReader {
     
    2321     */
    2422    @Override
    25     public DataSet parseOsm() throws OsmTransferException {
     23    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
    2624        InputStream in = null;
     25        progressMonitor.beginTask(tr("Contacting Server...", 10));
    2726        try {
    28             Main.pleaseWaitDlg.progress.setValue(0);
    29             Main.pleaseWaitDlg.currentAction.setText(tr("Contacting Server..."));
    30 
    31             in = getInputStreamRaw(url, Main.pleaseWaitDlg);
     27            in = getInputStreamRaw(url, progressMonitor.createSubTaskMonitor(9, false));
    3228            if (in == null)
    3329                return null;
    34             Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
    35             return OsmReader.parseDataSet(in, Main.pleaseWaitDlg);
     30            progressMonitor.subTask(tr("Downloading OSM data..."));
     31            return OsmReader.parseDataSet(in, progressMonitor.createSubTaskMonitor(1, false));
    3632        } catch(OsmTransferException e) {
    3733            throw e;
     
    4137            throw new OsmTransferException(e);
    4238        } finally {
     39            progressMonitor.finishTask();
    4340            try {
    4441                if (in != null) {
  • trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java

    r1790 r1811  
    77import java.io.InputStream;
    88
    9 import org.openstreetmap.josm.Main;
    109import org.openstreetmap.josm.data.osm.DataSet;
    1110import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
     11import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1212import org.xml.sax.SAXException;
    1313
     
    3030     */
    3131    @Override
    32     public DataSet parseOsm() throws OsmTransferException {
     32    public DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException {
     33        progressMonitor.beginTask("", 1);
    3334        try {
    34             Main.pleaseWaitDlg.progress.setValue(0);
    35             Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server..."));
     35            progressMonitor.subTask(tr("Downloading OSM data..."));
    3636            StringBuffer sb = new StringBuffer();
    3737            sb.append(type.getAPIName());
     
    4242            }
    4343
    44             final InputStream in = getInputStream(sb.toString(), Main.pleaseWaitDlg);
     44            final InputStream in = getInputStream(sb.toString(), progressMonitor.createSubTaskMonitor(1, true));
    4545            if (in == null)
    4646                return null;
    47             Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data..."));
    48             final OsmReader osm = OsmReader.parseDataSetOsm(in,Main.pleaseWaitDlg);
     47            final OsmReader osm = OsmReader.parseDataSetOsm(in, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false));
    4948            final DataSet data = osm.getDs();
    5049
     
    6463                return null;
    6564            throw new OsmTransferException(e);
     65        } finally {
     66            progressMonitor.finishTask();
    6667        }
    6768    }
  • trunk/src/org/openstreetmap/josm/io/OsmServerReader.java

    r1670 r1811  
    1414import java.util.zip.InflaterInputStream;
    1515
    16 import javax.swing.JOptionPane;
    17 
    1816import org.openstreetmap.josm.Main;
    1917import org.openstreetmap.josm.data.osm.DataSet;
    20 import org.openstreetmap.josm.gui.PleaseWaitDialog;
     18import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    2119
    2220/**
     
    4038     * @return An reader reading the input stream (servers answer) or <code>null</code>.
    4139     */
    42     protected InputStream getInputStream(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws OsmTransferException  {
     40    protected InputStream getInputStream(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException  {
    4341        api.initialize();
    4442        urlStr = api.getBaseUrl() + urlStr;
    45         return getInputStreamRaw(urlStr, pleaseWaitDlg);
     43        return getInputStreamRaw(urlStr, progressMonitor);
    4644    }
    4745
    48     protected InputStream getInputStreamRaw(String urlStr, PleaseWaitDialog pleaseWaitDlg) throws OsmTransferException {
     46    protected InputStream getInputStreamRaw(String urlStr, ProgressMonitor progressMonitor) throws OsmTransferException {
    4947        URL url = null;
    5048        try {
     
    9795
    9896            String encoding = activeConnection.getContentEncoding();
    99             InputStream inputStream = new ProgressInputStream(activeConnection, pleaseWaitDlg);
     97            InputStream inputStream = new ProgressInputStream(activeConnection, progressMonitor);
    10098            if (encoding != null && encoding.equalsIgnoreCase("gzip")) {
    10199                inputStream = new GZIPInputStream(inputStream);
     
    114112    }
    115113
    116     public abstract DataSet parseOsm() throws OsmTransferException;
     114    public abstract DataSet parseOsm(ProgressMonitor progressMonitor) throws OsmTransferException;
    117115
    118116}
  • trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java

    r1750 r1811  
    1313import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1414import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
     15import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1516
    1617/**
     
    8182     * @param primitives list of objects to send
    8283     */
    83     public void uploadOsm(String apiVersion, Collection<OsmPrimitive> primitives) throws OsmTransferException {
     84    public void uploadOsm(String apiVersion, Collection<OsmPrimitive> primitives, ProgressMonitor progressMonitor) throws OsmTransferException {
    8485        processed = new LinkedList<OsmPrimitive>();
    8586
    8687        api.initialize();
    8788
    88         Main.pleaseWaitDlg.progress.setMaximum(primitives.size());
    89         Main.pleaseWaitDlg.progress.setValue(0);
     89        progressMonitor.beginTask("");
    9090
    91         // check whether we can use changeset
    92         //
    93         boolean canUseChangeset = api.hasChangesetSupport();
    94         boolean useChangeset = Main.pref.getBoolean("osm-server.atomic-upload", apiVersion.compareTo("0.6")>=0);
    95         if (useChangeset && ! canUseChangeset) {
    96             System.out.println(tr("WARNING: preference ''{0}'' or api version ''{1}'' of dataset requires to use changesets, but API is not able to handle them. Ignoring changesets.", "osm-server.atomic-upload", apiVersion));
    97             useChangeset = false;
    98         }
     91        try {
    9992
    100         if (useChangeset) {
    101             // upload everything in one changeset
     93            // check whether we can use changeset
    10294            //
    103             try {
    104                 api.createChangeset(getChangesetComment());
    105                 processed.addAll(api.uploadDiff(primitives));
    106             } catch(OsmTransferException e) {
    107                 throw e;
    108             } finally {
     95            boolean canUseChangeset = api.hasChangesetSupport();
     96            boolean useChangeset = Main.pref.getBoolean("osm-server.atomic-upload", apiVersion.compareTo("0.6")>=0);
     97            if (useChangeset && ! canUseChangeset) {
     98                System.out.println(tr("WARNING: preference ''{0}'' or api version ''{1}'' of dataset requires to use changesets, but API is not able to handle them. Ignoring changesets.", "osm-server.atomic-upload", apiVersion));
     99                useChangeset = false;
     100            }
     101
     102            if (useChangeset) {
     103                // upload everything in one changeset
     104                //
    109105                try {
    110                     if (canUseChangeset) {
    111                         api.stopChangeset();
     106                    api.createChangeset(getChangesetComment(), progressMonitor.createSubTaskMonitor(0, false));
     107                    processed.addAll(api.uploadDiff(primitives, progressMonitor.createSubTaskMonitor(ProgressMonitor.ALL_TICKS, false)));
     108                } catch(OsmTransferException e) {
     109                    throw e;
     110                } finally {
     111                    try {
     112                        if (canUseChangeset) {
     113                            api.stopChangeset(progressMonitor.createSubTaskMonitor(0, false));
     114                        }
     115                    } catch (Exception ee) {
     116                        ee.printStackTrace();
     117                        // ignore nested exception
    112118                    }
    113                 } catch (Exception ee) {
    114                     ee.printStackTrace();
    115                     // ignore nested exception
    116119                }
     120            } else {
     121                // upload changes individually (90% of code is for the status display...)
     122                //
     123                progressMonitor.setTicksCount(primitives.size());
     124                api.createChangeset(getChangesetComment(), progressMonitor.createSubTaskMonitor(0, false));
     125                NameVisitor v = new NameVisitor();
     126                uploadStartTime = System.currentTimeMillis();
     127                for (OsmPrimitive osm : primitives) {
     128                    osm.visit(v);
     129                    int progress = progressMonitor.getTicks();
     130                    String time_left_str = timeLeft(progress, primitives.size());
     131                    progressMonitor.subTask(
     132                            tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})",
     133                                    Math.round(100.0*progress/primitives.size()), progress,
     134                                    primitives.size(), time_left_str, tr(v.className), v.name, osm.id));
     135                    makeApiRequest(osm);
     136                    processed.add(osm);
     137                    progressMonitor.worked(1);
     138                }
     139                api.stopChangeset(progressMonitor.createSubTaskMonitor(0, false));
    117140            }
    118         } else {
    119             // upload changes individually (90% of code is for the status display...)
    120             //
    121             api.createChangeset(getChangesetComment());
    122             NameVisitor v = new NameVisitor();
    123             uploadStartTime = System.currentTimeMillis();
    124             for (OsmPrimitive osm : primitives) {
    125                 osm.visit(v);
    126                 int progress = Main.pleaseWaitDlg.progress.getValue();
    127                 String time_left_str = timeLeft(progress, primitives.size());
    128                 Main.pleaseWaitDlg.currentAction.setText(
    129                         tr("{0}% ({1}/{2}), {3} left. Uploading {4}: {5} (id: {6})",
    130                                 Math.round(100.0*progress/primitives.size()), progress,
    131                                 primitives.size(), time_left_str, tr(v.className), v.name, osm.id));
    132                 makeApiRequest(osm);
    133                 processed.add(osm);
    134                 Main.pleaseWaitDlg.progress.setValue(progress+1);
    135             }
    136             api.stopChangeset();
     141        } finally {
     142            progressMonitor.finishTask();
    137143        }
    138144    }
  • trunk/src/org/openstreetmap/josm/io/ProgressInputStream.java

    r1670 r1811  
    88import java.net.URLConnection;
    99
    10 import org.openstreetmap.josm.gui.PleaseWaitDialog;
     10import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
     11import org.openstreetmap.josm.gui.progress.ProgressMonitor;
    1112
    1213/**
     
    1920    private int readSoFar = 0;
    2021    private int lastDialogUpdate = 0;
     22    private boolean sizeKnown;
    2123    private final URLConnection connection;
    22     private PleaseWaitDialog pleaseWaitDlg;
     24    private final ProgressMonitor progressMonitor;
    2325
    24     public ProgressInputStream(URLConnection con, PleaseWaitDialog pleaseWaitDlg) throws OsmTransferException {
     26    public ProgressInputStream(URLConnection con, ProgressMonitor progressMonitor) throws OsmTransferException {
    2527        this.connection = con;
     28        if (progressMonitor == null) {
     29            progressMonitor = NullProgressMonitor.INSTANCE;
     30        }
     31        this.progressMonitor = progressMonitor;
     32        progressMonitor.beginTask(tr("Contacting OSM Server..."), 1);
     33        progressMonitor.indeterminateSubTask(null);
    2634
    2735        try {
     
    3341        }
    3442
    35         int contentLength = con.getContentLength();
    36         this.pleaseWaitDlg = pleaseWaitDlg;
    37         if (pleaseWaitDlg == null)
    38             return;
    39         if (contentLength > 0) {
    40             pleaseWaitDlg.progress.setMaximum(contentLength);
    41         } else {
    42             pleaseWaitDlg.progress.setMaximum(0);
     43        updateSize();
     44        if (!sizeKnown) {
     45            progressMonitor.indeterminateSubTask(tr("Downloading OSM data..."));
    4346        }
    44         pleaseWaitDlg.progress.setValue(0);
    4547    }
    4648
    4749    @Override public void close() throws IOException {
    4850        in.close();
     51        progressMonitor.finishTask();
    4952    }
    5053
     
    5356        if (read != -1) {
    5457            advanceTicker(read);
     58        } else {
     59            progressMonitor.finishTask();
    5560        }
    5661        return read;
     
    6166        if (read != -1) {
    6267            advanceTicker(1);
     68        } else {
     69            progressMonitor.finishTask();
    6370        }
    6471        return read;
     
    7077     */
    7178    private void advanceTicker(int amount) {
    72         if (pleaseWaitDlg == null)
    73             return;
    74 
    75         if (pleaseWaitDlg.progress.getMaximum() == 0 && connection.getContentLength() != -1) {
    76             pleaseWaitDlg.progress.setMaximum(connection.getContentLength());
    77         }
    78 
    7979        readSoFar += amount;
     80        updateSize();
    8081
    8182        if (readSoFar / 1024 != lastDialogUpdate) {
    8283            lastDialogUpdate++;
    83             String progStr = " "+readSoFar/1024+"/";
    84             progStr += (pleaseWaitDlg.progress.getMaximum()==0) ? "??? KB" : (pleaseWaitDlg.progress.getMaximum()/1024)+" KB";
    85             pleaseWaitDlg.progress.setValue(readSoFar);
     84            if (sizeKnown) {
     85                progressMonitor.setExtraText(readSoFar/1024 + " KB");
     86                progressMonitor.setTicks(readSoFar);
     87            } else {
     88                progressMonitor.setExtraText("??? KB");
     89            }
     90        }
     91    }
    8692
    87             String cur = pleaseWaitDlg.currentAction.getText();
    88             int i = cur.indexOf(' ');
    89             if (i != -1) {
    90                 cur = cur.substring(0, i) + progStr;
    91             } else {
    92                 cur += progStr;
    93             }
    94             pleaseWaitDlg.currentAction.setText(cur);
     93    private void updateSize() {
     94        if (!sizeKnown && connection.getContentLength() > 0) {
     95            sizeKnown = true;
     96            progressMonitor.subTask(tr("Downloading OSM data..."));
     97            progressMonitor.setTicksCount(connection.getContentLength());
    9598        }
    9699    }
Note: See TracChangeset for help on using the changeset viewer.