Ignore:
Timestamp:
2009-03-07T13:40:54+01:00 (17 years ago)
Author:
stoecker
Message:

fix #1967. patch by xeen. This will break plugins using ProgressDialog until recompiled

File:
1 edited

Legend:

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

    r1169 r1465  
    55
    66import java.io.IOException;
     7import java.util.concurrent.Future;
    78
    89import javax.swing.JCheckBox;
     
    1011import org.openstreetmap.josm.Main;
    1112import org.openstreetmap.josm.actions.DownloadAction;
     13import org.openstreetmap.josm.data.Bounds;
     14import org.openstreetmap.josm.data.coor.LatLon;
    1215import org.openstreetmap.josm.data.osm.DataSet;
    1316import org.openstreetmap.josm.data.osm.DataSource;
    1417import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    1518import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask;
     19import org.openstreetmap.josm.gui.layer.Layer;
    1620import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    1721import org.openstreetmap.josm.io.BoundingBoxDownloader;
    1822import org.openstreetmap.josm.io.OsmServerLocationReader;
    1923import org.openstreetmap.josm.io.OsmServerReader;
    20 import org.openstreetmap.josm.data.Bounds;
    21 import org.openstreetmap.josm.data.coor.LatLon;
    2224import org.xml.sax.SAXException;
    2325
     
    2830 */
    2931public class DownloadOsmTask implements DownloadTask {
    30 
    3132    private static Bounds currentBounds;
     33    private Future<Task> task = null;
    3234
    3335    private static class Task extends PleaseWaitRunnable {
     
    3537        private DataSet dataSet;
    3638        private boolean newLayer;
    37 
    38         public Task(boolean newLayer, OsmServerReader reader) {
     39        private int num = 1;
     40        private String msg = "";
     41       
     42        public Task(boolean newLayer, OsmServerReader reader, boolean silent,
     43                int numLayers, String msg) {
    3944            super(tr("Downloading data"));
     45            this.msg = msg;
    4046            this.reader = reader;
    4147            this.newLayer = newLayer;
     48            this.silent = silent;
    4249        }
    4350
    4451        @Override public void realRun() throws IOException, SAXException {
     52            Main.pleaseWaitDlg.setCustomText(msg);
    4553            dataSet = reader.parseOsm();
    4654        }
     
    4856        @Override protected void finish() {
    4957            if (dataSet == null)
    50                 return; // user cancelled download or error occoured
     58                return; // user canceled download or error occurred
    5159            if (dataSet.allPrimitives().isEmpty()) {
    52                 errorMessage = tr("No data imported.");
     60                // If silent is set to true, we don't want to see information messages
     61                if(!silent)
     62                    errorMessage = tr("No data imported.");
    5363                // need to synthesize a download bounds lest the visual indication of downloaded
    5464                // area doesn't work
    5565                dataSet.dataSources.add(new DataSource(currentBounds, "OpenStreetMap server"));
    5666            }
    57 
    58             OsmDataLayer layer = new OsmDataLayer(dataSet, tr("Data Layer"), null);
     67           
     68            OsmDataLayer layer = new OsmDataLayer(dataSet, tr("Data Layer {0}", num), null);
    5969            if (newLayer)
    6070                Main.main.addLayer(layer);
    6171            else
    6272                Main.main.editLayer().mergeFrom(layer);
     73           
     74            Main.pleaseWaitDlg.setCustomText("");
    6375        }
    6476
     
    6678            if (reader != null)
    6779                reader.cancel();
     80            Main.pleaseWaitDlg.cancel.setEnabled(false);
    6881        }
    6982    }
    7083    private JCheckBox checkBox = new JCheckBox(tr("OpenStreetMap data"), true);
    7184
    72     public void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon) {
     85    public void download(DownloadAction action, double minlat, double minlon,
     86            double maxlat, double maxlon, boolean silent, String message) {
    7387        // Swap min and max if user has specified them the wrong way round
    7488        // (easy to do if you are crossing 0, for example)
     
    8094            double t = minlon; minlon = maxlon; maxlon = t;
    8195        }
     96       
     97        boolean newLayer = action != null
     98                                && (action.dialog == null || action.dialog.newLayer.isSelected());
    8299
    83         Task task = new Task(action != null && (action.dialog == null || action.dialog.newLayer.isSelected()), new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon));
     100        Task t = new Task(newLayer,
     101                new BoundingBoxDownloader(minlat, minlon, maxlat, maxlon),
     102                silent,
     103                getDataLayersCount(),
     104                message);
    84105        currentBounds = new Bounds(new LatLon(minlat, minlon), new LatLon(maxlat, maxlon));
    85         Main.worker.execute(task);
     106        // We need submit instead of execute so we can wait for it to finish and get the error
     107        // message if necessary. If no one calls getErrorMessage() it just behaves like execute.
     108        task = Main.worker.submit(t, t);       
     109    }
     110   
     111    public void download(DownloadAction action, double minlat, double minlon,
     112            double maxlat, double maxlon) {
     113        download(action, minlat, minlon, maxlat, maxlon, false, "");
    86114    }
    87115
     116    /**
     117     * Loads a given URL from the OSM Server
     118     * @param True if the data should be saved to a new layer
     119     * @param The URL as String
     120     */
    88121    public void loadUrl(boolean new_layer, String url) {
    89         Task task = new Task(new_layer, new OsmServerLocationReader(url));
    90         Main.worker.execute(task);
     122        Task t = new Task(new_layer,
     123                new OsmServerLocationReader(url),
     124                false,
     125                getDataLayersCount(),
     126                "");
     127        task = Main.worker.submit(t, t);
    91128    }
    92 
    93 
    94 
    95129
    96130    public JCheckBox getCheckBox() {
     
    101135        return "osm";
    102136    }
     137   
     138    /**
     139     * Finds the number of data layers currently opened
     140     * @return Number of data layers
     141     */
     142    private int getDataLayersCount() {
     143        if(Main.map == null || Main.map.mapView == null)
     144            return 0;
     145        int num = 0;
     146        for(Layer l : Main.map.mapView.getAllLayers())
     147            if(l instanceof OsmDataLayer)
     148                num++;
     149        return num;
     150    }
     151   
     152   /*
     153    * (non-Javadoc)
     154    * @see org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask#getErrorMessage()
     155    */
     156    public String getErrorMessage() {
     157        if(task == null)
     158            return "";       
     159
     160        try {
     161            Task t = task.get();
     162            return t.errorMessage == null
     163                ? ""
     164                : t.errorMessage;
     165        } catch (Exception e) {
     166            return "";
     167        }
     168    }
    103169}
Note: See TracChangeset for help on using the changeset viewer.