Changeset 9412 in josm for trunk/src/org
- Timestamp:
- 2016-01-11T18:49:59+01:00 (9 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java
r8870 r9412 17 17 import org.openstreetmap.josm.Main; 18 18 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry; 19 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 19 20 import org.openstreetmap.josm.io.CachedFile; 20 21 import org.openstreetmap.josm.io.OfflineAccessException; 21 22 import org.openstreetmap.josm.io.OnlineResource; 22 23 import org.openstreetmap.josm.io.imagery.ImageryReader; 24 import org.openstreetmap.josm.tools.Utils; 23 25 import org.xml.sax.SAXException; 26 27 import static org.openstreetmap.josm.tools.I18n.tr; 24 28 25 29 /** … … 73 77 Collections.sort(layers); 74 78 } 75 loadDefaults(false );79 loadDefaults(false, true); 76 80 } 77 81 … … 84 88 * 85 89 * @param clearCache if true, clear the cache and start a fresh download. 86 */ 87 public void loadDefaults(boolean clearCache) { 88 defaultLayers.clear(); 89 defaultLayerIds.clear(); 90 for (String source : getImageryLayersSites()) { 90 * @param quiet whether not the loading should be performed using a {@link PleaseWaitRunnable} in the background 91 */ 92 public void loadDefaults(boolean clearCache, boolean quiet) { 93 final DefaultEntryLoader loader = new DefaultEntryLoader(clearCache); 94 if (quiet) { 95 loader.realRun(); 96 loader.finish(); 97 } else { 98 Main.worker.execute(new DefaultEntryLoader(clearCache)); 99 } 100 } 101 102 /** 103 * Loader/updater of the available imagery entries 104 */ 105 class DefaultEntryLoader extends PleaseWaitRunnable { 106 107 private final boolean clearCache; 108 private final List<ImageryInfo> newLayers = new ArrayList<>(); 109 private transient ImageryReader reader; 110 private transient boolean canceled; 111 112 DefaultEntryLoader(boolean clearCache) { 113 super(tr("Update default entries")); 114 this.clearCache = clearCache; 115 } 116 117 @Override 118 protected void cancel() { 119 canceled = true; 120 Utils.close(reader); 121 } 122 123 @Override 124 protected void realRun() { 125 for (String source : getImageryLayersSites()) { 126 if (canceled) { 127 return; 128 } 129 loadSource(source); 130 } 131 } 132 133 protected void loadSource(String source) { 91 134 boolean online = true; 92 135 try { … … 100 143 } 101 144 try { 102 ImageryReaderreader = new ImageryReader(source);145 reader = new ImageryReader(source); 103 146 Collection<ImageryInfo> result = reader.parse(); 104 defaultLayers.addAll(result);147 newLayers.addAll(result); 105 148 } catch (IOException ex) { 106 149 Main.error(ex, false); … … 109 152 } 110 153 } 111 while (defaultLayers.remove(null)) { 112 // Do nothing 113 } 114 Collections.sort(defaultLayers); 115 buildIdMap(defaultLayers, defaultLayerIds); 116 updateEntriesFromDefaults(); 117 buildIdMap(layers, layerIds); 154 155 @Override 156 protected void finish() { 157 defaultLayers.clear(); 158 defaultLayers.addAll(newLayers); 159 defaultLayerIds.clear(); 160 Collections.sort(defaultLayers); 161 buildIdMap(defaultLayers, defaultLayerIds); 162 updateEntriesFromDefaults(); 163 buildIdMap(layers, layerIds); 164 } 118 165 } 119 166 -
trunk/src/org/openstreetmap/josm/gui/preferences/imagery/ImageryPreference.java
r9231 r9412 636 636 */ 637 637 ReloadAction() { 638 putValue(SHORT_DESCRIPTION, tr(" reload defaults"));638 putValue(SHORT_DESCRIPTION, tr("Update default entries")); 639 639 putValue(SMALL_ICON, ImageProvider.get("dialogs", "refresh")); 640 640 } … … 642 642 @Override 643 643 public void actionPerformed(ActionEvent evt) { 644 layerInfo.loadDefaults(true );644 layerInfo.loadDefaults(true, false); 645 645 defaultModel.fireTableDataChanged(); 646 646 defaultTable.getSelectionModel().clearSelection(); -
trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java
r9406 r9412 2 2 package org.openstreetmap.josm.io.imagery; 3 3 4 import java.io.BufferedReader; 5 import java.io.Closeable; 4 6 import java.io.IOException; 5 import java.io.InputStream;6 7 import java.util.ArrayList; 7 8 import java.util.Arrays; … … 28 29 import org.xml.sax.helpers.DefaultHandler; 29 30 30 public class ImageryReader {31 public class ImageryReader implements Closeable { 31 32 32 33 private final String source; 34 private transient CachedFile cachedFile; 33 35 34 36 private enum State { … … 53 55 Parser parser = new Parser(); 54 56 try { 55 try (InputStream in = new CachedFile(source) 56 .setMaxAge(1*CachedFile.DAYS) 57 cachedFile = new CachedFile(source); 58 try (BufferedReader in = cachedFile 59 .setMaxAge(CachedFile.DAYS) 57 60 .setCachingStrategy(CachedFile.CachingStrategy.IfModifiedSince) 58 .get InputStream()) {59 InputSource is = new InputSource( UTFInputStreamReader.create(in));61 .getContentReader()) { 62 InputSource is = new InputSource(in); 60 63 Utils.parseSafeSAX(is, parser); 61 64 return parser.entries; … … 355 358 } 356 359 } 360 361 @Override 362 public void close() throws IOException { 363 Utils.close(cachedFile); 364 } 357 365 }
Note:
See TracChangeset
for help on using the changeset viewer.