diff --git a/src/org/openstreetmap/josm/gui/layer/LayerPositionStrategy.java b/src/org/openstreetmap/josm/gui/layer/LayerPositionStrategy.java
index d577e30..3c1c754 100644
|
a
|
b
|
|
| 2 | 2 | package org.openstreetmap.josm.gui.layer; |
| 3 | 3 | |
| 4 | 4 | import java.util.List; |
| | 5 | import java.util.function.Predicate; |
| 5 | 6 | |
| 6 | | import org.openstreetmap.josm.tools.Predicate; |
| 7 | 7 | import org.openstreetmap.josm.tools.Predicates; |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * This class defines a position to insert a given layer in the list of layers. |
| 11 | 11 | * @author Michael Zangl |
| 12 | 12 | * @since 10008 |
| | 13 | * @since xxx functional interface |
| 13 | 14 | */ |
| 14 | | public abstract class LayerPositionStrategy { |
| | 15 | @FunctionalInterface |
| | 16 | public interface LayerPositionStrategy { |
| 15 | 17 | |
| 16 | 18 | /** |
| 17 | 19 | * always inserts at the front of the stack. |
| 18 | 20 | */ |
| 19 | | public static final LayerPositionStrategy IN_FRONT = new LayerPositionStrategy() { |
| 20 | | @Override |
| 21 | | public int getPosition(LayerManager manager) { |
| 22 | | return 0; |
| 23 | | } |
| 24 | | }; |
| | 21 | public static final LayerPositionStrategy IN_FRONT = manager -> 0; |
| 25 | 22 | |
| 26 | 23 | /** |
| 27 | 24 | * A GPX layer is added below the lowest data layer. |
| 28 | 25 | */ |
| 29 | | public static final LayerPositionStrategy AFTER_LAST_DATA_LAYER = afterLast(new Predicate<Layer>() { |
| 30 | | @Override |
| 31 | | public boolean evaluate(Layer object) { |
| 32 | | return object instanceof OsmDataLayer || object instanceof ValidatorLayer; |
| 33 | | } |
| 34 | | }); |
| | 26 | public static final LayerPositionStrategy AFTER_LAST_DATA_LAYER = afterLast( |
| | 27 | layer -> layer instanceof OsmDataLayer || layer instanceof ValidatorLayer); |
| 35 | 28 | |
| 36 | 29 | /** |
| 37 | 30 | * A normal layer is added after all validation layers. |
| 38 | 31 | */ |
| 39 | | public static final LayerPositionStrategy AFTER_LAST_VALIDATION_LAYER = afterLast(new Predicate<Layer>() { |
| 40 | | @Override |
| 41 | | public boolean evaluate(Layer object) { |
| 42 | | return object instanceof ValidatorLayer; |
| 43 | | } |
| 44 | | }); |
| | 32 | public static final LayerPositionStrategy AFTER_LAST_VALIDATION_LAYER = afterLast( |
| | 33 | layer -> layer instanceof ValidatorLayer); |
| 45 | 34 | |
| 46 | 35 | /** |
| 47 | 36 | * The default for background layers: They are added before the first background layer in the list. |
| 48 | 37 | * If there is none, they are added at the end of the list. |
| 49 | 38 | */ |
| 50 | | public static final LayerPositionStrategy BEFORE_FIRST_BACKGROUND_LAYER = inFrontOfFirst(new Predicate<Layer>() { |
| 51 | | @Override |
| 52 | | public boolean evaluate(Layer object) { |
| 53 | | return object.isBackgroundLayer(); |
| 54 | | } |
| 55 | | }); |
| | 39 | public static final LayerPositionStrategy BEFORE_FIRST_BACKGROUND_LAYER = inFrontOfFirst( |
| | 40 | layer -> layer.isBackgroundLayer()); |
| 56 | 41 | |
| 57 | 42 | /** |
| 58 | 43 | * Gets a {@link LayerPositionStrategy} that inserts this layer in front of a given layer |
| … |
… |
public abstract class LayerPositionStrategy {
|
| 69 | 54 | * @return The strategy. |
| 70 | 55 | */ |
| 71 | 56 | public static LayerPositionStrategy inFrontOfFirst(final Predicate<Layer> what) { |
| 72 | | return new LayerPositionStrategy() { |
| 73 | | @Override |
| 74 | | public int getPosition(LayerManager manager) { |
| 75 | | List<Layer> layers = manager.getLayers(); |
| 76 | | for (int i = 0; i < layers.size(); i++) { |
| 77 | | if (what.evaluate(layers.get(i))) { |
| 78 | | return i; |
| 79 | | } |
| | 57 | return manager -> { |
| | 58 | List<Layer> layers = manager.getLayers(); |
| | 59 | for (int i = 0; i < layers.size(); i++) { |
| | 60 | if (what.test(layers.get(i))) { |
| | 61 | return i; |
| 80 | 62 | } |
| 81 | | return layers.size(); |
| 82 | 63 | } |
| | 64 | return layers.size(); |
| 83 | 65 | }; |
| 84 | 66 | } |
| 85 | 67 | |
| … |
… |
public abstract class LayerPositionStrategy {
|
| 89 | 71 | * @return The strategy. |
| 90 | 72 | */ |
| 91 | 73 | public static LayerPositionStrategy afterLast(final Predicate<Layer> what) { |
| 92 | | return new LayerPositionStrategy() { |
| 93 | | @Override |
| 94 | | public int getPosition(LayerManager manager) { |
| 95 | | List<Layer> layers = manager.getLayers(); |
| 96 | | for (int i = layers.size() - 1; i >= 0; i--) { |
| 97 | | if (what.evaluate(layers.get(i))) { |
| 98 | | return i + 1; |
| 99 | | } |
| | 74 | return manager -> { |
| | 75 | List<Layer> layers = manager.getLayers(); |
| | 76 | for (int i = layers.size() - 1; i >= 0; i--) { |
| | 77 | if (what.test(layers.get(i))) { |
| | 78 | return i + 1; |
| 100 | 79 | } |
| 101 | | return 0; |
| 102 | 80 | } |
| | 81 | return 0; |
| 103 | 82 | }; |
| 104 | 83 | } |
| 105 | 84 | |
| … |
… |
public abstract class LayerPositionStrategy {
|
| 108 | 87 | * @param manager The layer manager to insert the layer in. |
| 109 | 88 | * @return The position in the range 0...layers.size |
| 110 | 89 | */ |
| 111 | | public abstract int getPosition(LayerManager manager); |
| | 90 | int getPosition(LayerManager manager); |
| 112 | 91 | } |
diff --git a/test/unit/org/openstreetmap/josm/gui/layer/LayerManagerTest.java b/test/unit/org/openstreetmap/josm/gui/layer/LayerManagerTest.java
index 5fe8dde..6d4a9b9 100644
|
a
|
b
|
public class LayerManagerTest {
|
| 238 | 238 | TestLayer layer1 = new TestLayer() { |
| 239 | 239 | @Override |
| 240 | 240 | public LayerPositionStrategy getDefaultLayerPosition() { |
| 241 | | return new LayerPositionStrategy() { |
| 242 | | @Override |
| 243 | | public int getPosition(LayerManager manager) { |
| 244 | | return 42; |
| 245 | | } |
| 246 | | }; |
| | 241 | return manager -> 42; |
| 247 | 242 | } |
| 248 | 243 | }; |
| 249 | 244 | layerManager.addLayer(layer1); |