source: josm/trunk/src/org/openstreetmap/josm/gui/util/HighlightHelper.java@ 12636

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

see #15182 - deprecate Main.getLayerManager(). Replacement: gui.MainApplication.getLayerManager()

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