Changeset 1465 in josm for trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java
- Timestamp:
- 2009-03-07T13:40:54+01:00 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/actions/downloadtasks/DownloadOsmTask.java
r1169 r1465 5 5 6 6 import java.io.IOException; 7 import java.util.concurrent.Future; 7 8 8 9 import javax.swing.JCheckBox; … … 10 11 import org.openstreetmap.josm.Main; 11 12 import org.openstreetmap.josm.actions.DownloadAction; 13 import org.openstreetmap.josm.data.Bounds; 14 import org.openstreetmap.josm.data.coor.LatLon; 12 15 import org.openstreetmap.josm.data.osm.DataSet; 13 16 import org.openstreetmap.josm.data.osm.DataSource; 14 17 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 15 18 import org.openstreetmap.josm.gui.download.DownloadDialog.DownloadTask; 19 import org.openstreetmap.josm.gui.layer.Layer; 16 20 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 17 21 import org.openstreetmap.josm.io.BoundingBoxDownloader; 18 22 import org.openstreetmap.josm.io.OsmServerLocationReader; 19 23 import org.openstreetmap.josm.io.OsmServerReader; 20 import org.openstreetmap.josm.data.Bounds;21 import org.openstreetmap.josm.data.coor.LatLon;22 24 import org.xml.sax.SAXException; 23 25 … … 28 30 */ 29 31 public class DownloadOsmTask implements DownloadTask { 30 31 32 private static Bounds currentBounds; 33 private Future<Task> task = null; 32 34 33 35 private static class Task extends PleaseWaitRunnable { … … 35 37 private DataSet dataSet; 36 38 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) { 39 44 super(tr("Downloading data")); 45 this.msg = msg; 40 46 this.reader = reader; 41 47 this.newLayer = newLayer; 48 this.silent = silent; 42 49 } 43 50 44 51 @Override public void realRun() throws IOException, SAXException { 52 Main.pleaseWaitDlg.setCustomText(msg); 45 53 dataSet = reader.parseOsm(); 46 54 } … … 48 56 @Override protected void finish() { 49 57 if (dataSet == null) 50 return; // user cancel led download or error occoured58 return; // user canceled download or error occurred 51 59 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."); 53 63 // need to synthesize a download bounds lest the visual indication of downloaded 54 64 // area doesn't work 55 65 dataSet.dataSources.add(new DataSource(currentBounds, "OpenStreetMap server")); 56 66 } 57 58 OsmDataLayer layer = new OsmDataLayer(dataSet, tr("Data Layer "), null);67 68 OsmDataLayer layer = new OsmDataLayer(dataSet, tr("Data Layer {0}", num), null); 59 69 if (newLayer) 60 70 Main.main.addLayer(layer); 61 71 else 62 72 Main.main.editLayer().mergeFrom(layer); 73 74 Main.pleaseWaitDlg.setCustomText(""); 63 75 } 64 76 … … 66 78 if (reader != null) 67 79 reader.cancel(); 80 Main.pleaseWaitDlg.cancel.setEnabled(false); 68 81 } 69 82 } 70 83 private JCheckBox checkBox = new JCheckBox(tr("OpenStreetMap data"), true); 71 84 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) { 73 87 // Swap min and max if user has specified them the wrong way round 74 88 // (easy to do if you are crossing 0, for example) … … 80 94 double t = minlon; minlon = maxlon; maxlon = t; 81 95 } 96 97 boolean newLayer = action != null 98 && (action.dialog == null || action.dialog.newLayer.isSelected()); 82 99 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); 84 105 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, ""); 86 114 } 87 115 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 */ 88 121 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); 91 128 } 92 93 94 95 129 96 130 public JCheckBox getCheckBox() { … … 101 135 return "osm"; 102 136 } 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 } 103 169 }
Note:
See TracChangeset
for help on using the changeset viewer.
