Changeset 11011 in josm
- Timestamp:
- 2016-09-17T16:44:15+02:00 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/layer/LayerManager.java
r10999 r11011 10 10 import java.util.Set; 11 11 import java.util.concurrent.CopyOnWriteArrayList; 12 import java.util.function.Consumer; 12 13 13 14 import org.openstreetmap.josm.Main; … … 170 171 171 172 /** 172 * This is the list of layers we manage. 173 */ 174 private final List<Layer> layers = new ArrayList<>(); 173 * This is the list of layers we manage. The list is unmodifyable It is only changed in the EDT. 174 * @see LayerManager#updateLayers(Consumer) 175 */ 176 private volatile List<Layer> layers = Collections.emptyList(); 175 177 176 178 private final List<LayerChangeListener> layerChangeListeners = new CopyOnWriteArrayList<>(); … … 228 230 229 231 protected Collection<Layer> realRemoveSingleLayer(Layer layerToRemove) { 230 layers.remove(layerToRemove);232 updateLayers(mutableLayers -> mutableLayers.remove(layerToRemove)); 231 233 return fireLayerRemoving(layerToRemove); 232 234 } … … 248 250 checkPosition(position); 249 251 250 int curLayerPos = layers.indexOf(layer);252 int curLayerPos = getLayers().indexOf(layer); 251 253 if (position == curLayerPos) 252 254 return; // already in place. 253 layers.remove(curLayerPos); 254 insertLayerAt(layer, position); 255 // update needs to be done in one run 256 updateLayers(mutableLayers -> { 257 mutableLayers.remove(curLayerPos); 258 insertLayerAt(mutableLayers, layer, position); 259 }); 255 260 fireLayerOrderChanged(); 256 261 } … … 262 267 */ 263 268 private void insertLayerAt(Layer layer, int position) { 269 updateLayers(mutableLayers -> insertLayerAt(mutableLayers, layer, position)); 270 } 271 272 private static void insertLayerAt(List<Layer> layers, Layer layer, int position) { 264 273 if (position == layers.size()) { 265 274 layers.add(layer); … … 275 284 */ 276 285 private void checkPosition(int position) { 277 if (position < 0 || position > layers.size()) {286 if (position < 0 || position > getLayers().size()) { 278 287 throw new IndexOutOfBoundsException("Position " + position + " out of range."); 279 288 } 289 } 290 291 /** 292 * Update the {@link #layers} field. This method should be used instead of a direct field access. 293 * @param mutator A method that gets the writable list of layers and should modify it. 294 */ 295 private void updateLayers(Consumer<List<Layer>> mutator) { 296 GuiHelper.assertCallFromEdt(); 297 ArrayList<Layer> newLayers = new ArrayList<>(getLayers()); 298 mutator.accept(newLayers); 299 layers = Collections.unmodifiableList(newLayers); 280 300 } 281 301 … … 284 304 * @return The list of layers. 285 305 */ 286 public synchronizedList<Layer> getLayers() {287 return Collections.unmodifiableList(new ArrayList<>(layers));306 public List<Layer> getLayers() { 307 return layers; 288 308 } 289 309 … … 299 319 * @return an unmodifiable list of layers of a certain type. 300 320 */ 301 public synchronized<T extends Layer> List<T> getLayersOfType(Class<T> ofType) {321 public <T extends Layer> List<T> getLayersOfType(Class<T> ofType) { 302 322 return new ArrayList<>(Utils.filteredCollection(getLayers(), ofType)); 303 323 } … … 309 329 * @return true if the list of layers managed by this map view contain layer 310 330 */ 311 public synchronizedboolean containsLayer(Layer layer) {312 return layers.contains(layer);331 public boolean containsLayer(Layer layer) { 332 return getLayers().contains(layer); 313 333 } 314 334
Note:
See TracChangeset
for help on using the changeset viewer.