1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer;
|
---|
3 |
|
---|
4 | import java.util.List;
|
---|
5 | import java.util.Objects;
|
---|
6 | import java.util.function.Predicate;
|
---|
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
|
---|
12 | * @since 10592 functional interface
|
---|
13 | */
|
---|
14 | @FunctionalInterface
|
---|
15 | public interface LayerPositionStrategy {
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * always inserts at the front of the stack.
|
---|
19 | */
|
---|
20 | LayerPositionStrategy IN_FRONT = manager -> 0;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * A GPX layer is added below the lowest data layer.
|
---|
24 | */
|
---|
25 | LayerPositionStrategy AFTER_LAST_DATA_LAYER = afterLast(
|
---|
26 | layer -> layer instanceof OsmDataLayer || layer instanceof ValidatorLayer);
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * A normal layer is added after all validation layers.
|
---|
30 | */
|
---|
31 | LayerPositionStrategy AFTER_LAST_VALIDATION_LAYER = afterLast(
|
---|
32 | layer -> layer instanceof ValidatorLayer);
|
---|
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 | */
|
---|
38 | LayerPositionStrategy BEFORE_FIRST_BACKGROUND_LAYER = inFrontOfFirst(
|
---|
39 | Layer::isBackgroundLayer);
|
---|
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 | */
|
---|
46 | static LayerPositionStrategy inFrontOf(Layer other) {
|
---|
47 | return inFrontOfFirst(obj -> Objects.equals(obj, other));
|
---|
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 | */
|
---|
55 | static LayerPositionStrategy inFrontOfFirst(final Predicate<Layer> what) {
|
---|
56 | return manager -> {
|
---|
57 | if (manager != null) {
|
---|
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;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | return layers.size();
|
---|
65 | }
|
---|
66 | return 0;
|
---|
67 | };
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * 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.
|
---|
72 | * @param what what to search for
|
---|
73 | * @return The strategy.
|
---|
74 | */
|
---|
75 | static LayerPositionStrategy afterLast(final Predicate<Layer> what) {
|
---|
76 | return manager -> {
|
---|
77 | if (manager != null) {
|
---|
78 | List<Layer> layers = manager.getLayers();
|
---|
79 | for (int i = layers.size() - 1; i >= 0; i--) {
|
---|
80 | if (what.test(layers.get(i))) {
|
---|
81 | return i + 1;
|
---|
82 | }
|
---|
83 | }
|
---|
84 | }
|
---|
85 | return 0;
|
---|
86 | };
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Gets the position where the layer should be inserted
|
---|
91 | * @param manager The layer manager to insert the layer in.
|
---|
92 | * @return The position in the range 0...layers.size
|
---|
93 | */
|
---|
94 | int getPosition(LayerManager manager);
|
---|
95 | }
|
---|