source: josm/trunk/src/org/openstreetmap/josm/gui/layer/LayerPositionStrategy.java@ 10715

Last change on this file since 10715 was 10715, checked in by simon04, 8 years ago

see #11390, see #12890 - Deprecate Predicates class

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.util.List;
5import java.util.Objects;
6import 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
15public 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 -> 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 List<Layer> layers = manager.getLayers();
58 for (int i = 0; i < layers.size(); i++) {
59 if (what.test(layers.get(i))) {
60 return i;
61 }
62 }
63 return layers.size();
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 */
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;
78 }
79 }
80 return 0;
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 */
89 int getPosition(LayerManager manager);
90}
Note: See TracBrowser for help on using the repository browser.