[10008] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui.layer;
|
---|
| 3 |
|
---|
| 4 | import java.util.List;
|
---|
[10715] | 5 | import java.util.Objects;
|
---|
[10592] | 6 | import java.util.function.Predicate;
|
---|
[10008] | 7 |
|
---|
| 8 | /**
|
---|
| 9 | * This class defines a position to insert a given layer in the list of layers.
|
---|
| 10 | * @author Michael Zangl
|
---|
| 11 | * @since 10008
|
---|
[10592] | 12 | * @since 10592 functional interface
|
---|
[10008] | 13 | */
|
---|
[10592] | 14 | @FunctionalInterface
|
---|
| 15 | public interface LayerPositionStrategy {
|
---|
[10008] | 16 |
|
---|
| 17 | /**
|
---|
| 18 | * always inserts at the front of the stack.
|
---|
| 19 | */
|
---|
[10592] | 20 | LayerPositionStrategy IN_FRONT = manager -> 0;
|
---|
[10008] | 21 |
|
---|
| 22 | /**
|
---|
| 23 | * A GPX layer is added below the lowest data layer.
|
---|
| 24 | */
|
---|
[10592] | 25 | LayerPositionStrategy AFTER_LAST_DATA_LAYER = afterLast(
|
---|
| 26 | layer -> layer instanceof OsmDataLayer || layer instanceof ValidatorLayer);
|
---|
[10008] | 27 |
|
---|
| 28 | /**
|
---|
| 29 | * A normal layer is added after all validation layers.
|
---|
| 30 | */
|
---|
[10592] | 31 | LayerPositionStrategy AFTER_LAST_VALIDATION_LAYER = afterLast(
|
---|
| 32 | layer -> layer instanceof ValidatorLayer);
|
---|
[10008] | 33 |
|
---|
| 34 | /**
|
---|
| 35 | * The default for background layers: They are added before the first background layer in the list.
|
---|
| 36 | * If there is none, they are added at the end of the list.
|
---|
| 37 | */
|
---|
[10592] | 38 | LayerPositionStrategy BEFORE_FIRST_BACKGROUND_LAYER = inFrontOfFirst(
|
---|
[10717] | 39 | Layer::isBackgroundLayer);
|
---|
[10008] | 40 |
|
---|
| 41 | /**
|
---|
| 42 | * Gets a {@link LayerPositionStrategy} that inserts this layer in front of a given layer
|
---|
| 43 | * @param other The layer before which to insert this layer
|
---|
| 44 | * @return The strategy
|
---|
| 45 | */
|
---|
[10592] | 46 | static LayerPositionStrategy inFrontOf(Layer other) {
|
---|
[10715] | 47 | return inFrontOfFirst(obj -> Objects.equals(obj, other));
|
---|
[10008] | 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * Gets a {@link LayerPositionStrategy} that inserts the layer in front of the first layer that matches a condition.
|
---|
| 52 | * @param what The condition to match.
|
---|
| 53 | * @return The strategy.
|
---|
| 54 | */
|
---|
[10592] | 55 | static LayerPositionStrategy inFrontOfFirst(final Predicate<Layer> what) {
|
---|
| 56 | return manager -> {
|
---|
| 57 | List<Layer> layers = manager.getLayers();
|
---|
| 58 | for (int i = 0; i < layers.size(); i++) {
|
---|
| 59 | if (what.test(layers.get(i))) {
|
---|
| 60 | return i;
|
---|
[10008] | 61 | }
|
---|
| 62 | }
|
---|
[10592] | 63 | return layers.size();
|
---|
[10008] | 64 | };
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Creates a strategy that places the layer after the last layer of a given kind or at the beginning of the list if no such layer exists.
|
---|
| 69 | * @param what what to search for
|
---|
| 70 | * @return The strategy.
|
---|
| 71 | */
|
---|
[10592] | 72 | static LayerPositionStrategy afterLast(final Predicate<Layer> what) {
|
---|
| 73 | return manager -> {
|
---|
| 74 | List<Layer> layers = manager.getLayers();
|
---|
| 75 | for (int i = layers.size() - 1; i >= 0; i--) {
|
---|
| 76 | if (what.test(layers.get(i))) {
|
---|
| 77 | return i + 1;
|
---|
[10008] | 78 | }
|
---|
| 79 | }
|
---|
[10592] | 80 | return 0;
|
---|
[10008] | 81 | };
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * Gets the position where the layer should be inserted
|
---|
| 86 | * @param manager The layer manager to insert the layer in.
|
---|
| 87 | * @return The position in the range 0...layers.size
|
---|
| 88 | */
|
---|
[10592] | 89 | int getPosition(LayerManager manager);
|
---|
[10008] | 90 | }
|
---|