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

Last change on this file since 10592 was 10592, checked in by Don-vip, 8 years ago

see #11390, fix #13192 - Update LayerPositionStrategy to be a java 8 functional interface (patch by michael2402, modified) - gsoc-core

  • Property svn:eol-style set to native
File size: 3.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import java.util.List;
5import java.util.function.Predicate;
6
7import org.openstreetmap.josm.tools.Predicates;
8
9/**
10 * This class defines a position to insert a given layer in the list of layers.
11 * @author Michael Zangl
12 * @since 10008
13 * @since 10592 functional interface
14 */
15@FunctionalInterface
16public interface LayerPositionStrategy {
17
18 /**
19 * always inserts at the front of the stack.
20 */
21 LayerPositionStrategy IN_FRONT = manager -> 0;
22
23 /**
24 * A GPX layer is added below the lowest data layer.
25 */
26 LayerPositionStrategy AFTER_LAST_DATA_LAYER = afterLast(
27 layer -> layer instanceof OsmDataLayer || layer instanceof ValidatorLayer);
28
29 /**
30 * A normal layer is added after all validation layers.
31 */
32 LayerPositionStrategy AFTER_LAST_VALIDATION_LAYER = afterLast(
33 layer -> layer instanceof ValidatorLayer);
34
35 /**
36 * The default for background layers: They are added before the first background layer in the list.
37 * If there is none, they are added at the end of the list.
38 */
39 LayerPositionStrategy BEFORE_FIRST_BACKGROUND_LAYER = inFrontOfFirst(
40 layer -> layer.isBackgroundLayer());
41
42 /**
43 * Gets a {@link LayerPositionStrategy} that inserts this layer in front of a given layer
44 * @param other The layer before which to insert this layer
45 * @return The strategy
46 */
47 static LayerPositionStrategy inFrontOf(Layer other) {
48 return inFrontOfFirst(Predicates.equalTo(other));
49 }
50
51 /**
52 * Gets a {@link LayerPositionStrategy} that inserts the layer in front of the first layer that matches a condition.
53 * @param what The condition to match.
54 * @return The strategy.
55 */
56 static LayerPositionStrategy inFrontOfFirst(final Predicate<Layer> what) {
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;
62 }
63 }
64 return layers.size();
65 };
66 }
67
68 /**
69 * 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.
70 * @param what what to search for
71 * @return The strategy.
72 */
73 static LayerPositionStrategy afterLast(final Predicate<Layer> what) {
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;
79 }
80 }
81 return 0;
82 };
83 }
84
85 /**
86 * Gets the position where the layer should be inserted
87 * @param manager The layer manager to insert the layer in.
88 * @return The position in the range 0...layers.size
89 */
90 int getPosition(LayerManager manager);
91}
Note: See TracBrowser for help on using the repository browser.