1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 | import java.util.Collections;
|
---|
6 | import java.util.HashSet;
|
---|
7 | import java.util.Iterator;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * This class stores the set of highlited primitives and
|
---|
17 | * allows easy and fast change of highlighting
|
---|
18 | */
|
---|
19 | public class HighlightHelper {
|
---|
20 | private final Set<OsmPrimitive> highlightedPrimitives = new HashSet<>();
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Highlight and remember given primitives
|
---|
24 | * @param prims - primitives to highlight/unhighlight
|
---|
25 | */
|
---|
26 | public boolean highlight(Collection<? extends OsmPrimitive> prims) {
|
---|
27 | return highlight(prims, false);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Highlight and remember given primitives
|
---|
32 | * @param prims - primitives to highlight/unhighlight
|
---|
33 | * @param only - remove previous highlighting
|
---|
34 | */
|
---|
35 | public boolean highlight(Collection<? extends OsmPrimitive> prims, boolean only) {
|
---|
36 | boolean needsRepaint = false;
|
---|
37 | if (only) {
|
---|
38 | Iterator<OsmPrimitive> it = highlightedPrimitives.iterator();
|
---|
39 | while (it.hasNext()) {
|
---|
40 | OsmPrimitive p = it.next();
|
---|
41 | if (!prims.contains(p)) {
|
---|
42 | p.setHighlighted(false);
|
---|
43 | it.remove();
|
---|
44 | needsRepaint = true;
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 | for (OsmPrimitive p: prims) {
|
---|
49 | needsRepaint |= setHighlight(p, true);
|
---|
50 | }
|
---|
51 |
|
---|
52 | return needsRepaint;
|
---|
53 | }
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * Highlight and remember given primitives, forgetting previously highlighted by this instance
|
---|
57 | * @param prims - primitives to highlight/unhighlight
|
---|
58 | */
|
---|
59 | public boolean highlightOnly(Collection<? extends OsmPrimitive> prims) {
|
---|
60 | return highlight(prims, true);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Highlight and remember given primitive, forgetting previously highlighted by this instance
|
---|
65 | * @param p - primitives to highlight/unhighlight
|
---|
66 | */
|
---|
67 | public boolean highlightOnly(OsmPrimitive p) {
|
---|
68 | return highlight(Collections.singleton(p), true);
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Highlight and remember given primitive
|
---|
73 | * @param p - primitive to highlight/unhighlight
|
---|
74 | * @param flag - true to highlight
|
---|
75 | */
|
---|
76 | public boolean setHighlight(OsmPrimitive p, boolean flag) {
|
---|
77 | return setHighlight(p, flag, new HashSet<Relation>());
|
---|
78 | }
|
---|
79 |
|
---|
80 | private boolean setHighlight(OsmPrimitive p, boolean flag, Set<Relation> seenRelations) {
|
---|
81 | if (p instanceof Relation) {
|
---|
82 | Relation r = (Relation) p;
|
---|
83 | seenRelations.add(r);
|
---|
84 | boolean needRepaint = false;
|
---|
85 | for (OsmPrimitive m : r.getMemberPrimitives()) {
|
---|
86 | if (!(m instanceof Relation) || !seenRelations.contains(m)) {
|
---|
87 | needRepaint |= setHighlight(m, flag, seenRelations);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | return needRepaint;
|
---|
91 | } else if (flag) {
|
---|
92 | if (highlightedPrimitives.add(p)) {
|
---|
93 | p.setHighlighted(true);
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 | } else {
|
---|
97 | if (highlightedPrimitives.remove(p)) {
|
---|
98 | p.setHighlighted(false);
|
---|
99 | return true;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return false;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Clear highlighting of all remembered primitives
|
---|
107 | */
|
---|
108 | public void clear() {
|
---|
109 | for (OsmPrimitive p: highlightedPrimitives) {
|
---|
110 | p.setHighlighted(false);
|
---|
111 | }
|
---|
112 | highlightedPrimitives.clear();
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Slow method to import all currently highlighted primitives into this instance
|
---|
117 | */
|
---|
118 | public void findAllHighlighted() {
|
---|
119 | DataSet ds = Main.main.getCurrentDataSet();
|
---|
120 | if (ds != null) {
|
---|
121 | highlightedPrimitives.addAll(ds.allNonDeletedPrimitives());
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Slow method to remove highlights from all primitives
|
---|
127 | */
|
---|
128 | public static void clearAllHighlighted() {
|
---|
129 | DataSet ds = Main.main.getCurrentDataSet();
|
---|
130 | if (ds != null) {
|
---|
131 | for (OsmPrimitive p: ds.allNonDeletedPrimitives()) {
|
---|
132 | p.setHighlighted(false);
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|