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

Last change on this file was 18759, checked in by taylor.smock, 11 months ago

Fix #22279: Add new split mode for quick splitting of ways (patch by Woazboat, modified)

Modifications are as follows:

  • Simplify/inline some variables
  • Move an inner class used only in a class that it is not in into the appropriate class
  • Fix a pre-existing bug where InputEvent.META_MASK would not be mapped to InputEvent.META_DOWN_MASK
  • Override some additional methods in SplitMode (layerIsSupported and getModeHelpText)
  • Property svn:eol-style set to native
File size: 5.0 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<>());
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 * Returns an (unmodifiable) set of currently highlighted primitives
112 * @return Currently highlighted primitives
113 *
114 * @since 18759
115 */
116 public Set<OsmPrimitive> getHighlighted() {
117 return Collections.unmodifiableSet(highlightedPrimitives);
118 }
119
120 /**
121 * Clear highlighting of all remembered primitives
122 */
123 public void clear() {
124 for (OsmPrimitive p: highlightedPrimitives) {
125 p.setHighlighted(false);
126 }
127 highlightedPrimitives.clear();
128 }
129
130 /**
131 * Check whether there are any primitives highlighted
132 * @return true when there are highlighted primitives
133 *
134 * @since 18759
135 */
136 public boolean anyHighlighted() {
137 return !highlightedPrimitives.isEmpty();
138 }
139
140 /**
141 * Slow method to import all currently highlighted primitives into this instance
142 */
143 public void findAllHighlighted() {
144 DataSet ds = MainApplication.getLayerManager().getEditDataSet();
145 if (ds != null) {
146 highlightedPrimitives.addAll(ds.allNonDeletedPrimitives());
147 }
148 }
149
150 /**
151 * Slow method to remove highlights from all primitives
152 */
153 public static void clearAllHighlighted() {
154 DataSet ds = MainApplication.getLayerManager().getActiveDataSet();
155 if (ds != null) {
156 for (OsmPrimitive p: ds.allNonDeletedPrimitives()) {
157 p.setHighlighted(false);
158 }
159 }
160 }
161}
Note: See TracBrowser for help on using the repository browser.