Changeset 12098 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2017-05-10T23:00:16+02:00 (8 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/SelectionChangedListener.java
r10600 r12098 4 4 import java.util.Collection; 5 5 6 import org.openstreetmap.josm.data.osm.DataSelectionListener; 6 7 import org.openstreetmap.josm.data.osm.OsmPrimitive; 8 import org.openstreetmap.josm.data.osm.event.SelectionEventManager; 7 9 8 10 /** … … 14 16 * Swing event queue and packed together. So only one selection changed event 15 17 * is issued within a one message dispatch routine. 18 * 19 * @see DataSelectionListener For a more advanced listener class. 20 * @see SelectionEventManager For managing your selection events. 16 21 * 17 22 * @author imi -
trunk/src/org/openstreetmap/josm/data/osm/event/SelectionEventManager.java
r12096 r12098 2 2 package org.openstreetmap.josm.data.osm.event; 3 3 4 import java.util.Collection;5 4 import java.util.Collections; 6 5 import java.util.HashSet; 7 6 import java.util.List; 8 7 import java.util.Objects; 9 import java.util.Set;10 8 import java.util.concurrent.CopyOnWriteArrayList; 11 9 import java.util.stream.Stream; … … 17 15 import org.openstreetmap.josm.data.osm.DataSelectionListener; 18 16 import org.openstreetmap.josm.data.osm.DataSet; 19 import org.openstreetmap.josm.data.osm.OsmPrimitive;20 17 import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode; 21 18 import org.openstreetmap.josm.gui.layer.MainLayerManager; … … 45 42 } 46 43 47 private static class ListenerInfo { 44 private abstract static class AbstractListenerInfo { 45 abstract void fire(SelectionChangeEvent event); 46 } 47 48 private static class ListenerInfo extends AbstractListenerInfo { 48 49 private final SelectionChangedListener listener; 49 50 50 51 ListenerInfo(SelectionChangedListener listener) { 51 52 this.listener = listener; 53 } 54 55 @Override 56 void fire(SelectionChangeEvent event) { 57 listener.selectionChanged(event.getSelection()); 52 58 } 53 59 … … 66 72 } 67 73 68 private Collection<? extends OsmPrimitive> selection; 69 private final CopyOnWriteArrayList<ListenerInfo> inEDTListeners = new CopyOnWriteArrayList<>(); 70 private final CopyOnWriteArrayList<ListenerInfo> normalListeners = new CopyOnWriteArrayList<>(); 74 private static class DataListenerInfo extends AbstractListenerInfo { 75 private final DataSelectionListener listener; 76 77 DataListenerInfo(DataSelectionListener listener) { 78 this.listener = listener; 79 } 80 81 @Override 82 void fire(SelectionChangeEvent event) { 83 listener.selectionChanged(event); 84 } 85 86 @Override 87 public int hashCode() { 88 return Objects.hash(listener); 89 } 90 91 @Override 92 public boolean equals(Object o) { 93 if (this == o) return true; 94 if (o == null || getClass() != o.getClass()) return false; 95 DataListenerInfo that = (DataListenerInfo) o; 96 return Objects.equals(listener, that.listener); 97 } 98 } 99 100 private final CopyOnWriteArrayList<AbstractListenerInfo> inEDTListeners = new CopyOnWriteArrayList<>(); 101 private final CopyOnWriteArrayList<AbstractListenerInfo> immedatelyListeners = new CopyOnWriteArrayList<>(); 71 102 72 103 /** … … 82 113 /** 83 114 * Registers a new {@code SelectionChangedListener}. 115 * 116 * It is preferred to add a DataSelectionListener - that listener will receive more information about the event. 84 117 * @param listener listener to add 85 118 * @param fireMode Set this to IN_EDT_CONSOLIDATED if you want the event to be fired in the EDT thread. … … 92 125 inEDTListeners.addIfAbsent(new ListenerInfo(listener)); 93 126 } else { 94 normalListeners.addIfAbsent(new ListenerInfo(listener)); 95 } 127 immedatelyListeners.addIfAbsent(new ListenerInfo(listener)); 128 } 129 } 130 131 /** 132 * Adds a selection listener that gets notified for selections immediately. 133 * @param listener The listener to add. 134 * @since 12098 135 */ 136 public void addSelectionListener(DataSelectionListener listener) { 137 immedatelyListeners.addIfAbsent(new DataListenerInfo(listener)); 138 } 139 140 /** 141 * Adds a selection listener that gets notified for selections later in the EDT thread. 142 * Events are sent in the right order but may be delayed. 143 * @param listener The listener to add. 144 * @since 12098 145 */ 146 public void addSelectionListenerForEdt(DataSelectionListener listener) { 147 inEDTListeners.addIfAbsent(new DataListenerInfo(listener)); 96 148 } 97 149 … … 101 153 */ 102 154 public void removeSelectionListener(SelectionChangedListener listener) { 103 ListenerInfo searchListener = new ListenerInfo(listener); 155 remove(new ListenerInfo(listener)); 156 } 157 158 /** 159 * Unregisters a {@code DataSelectionListener}. 160 * @param listener listener to remove 161 * @since 12098 162 */ 163 public void removeSelectionListener(DataSelectionListener listener) { 164 remove(new DataListenerInfo(listener)); 165 } 166 167 private void remove(AbstractListenerInfo searchListener) { 104 168 inEDTListeners.remove(searchListener); 105 normalListeners.remove(searchListener);169 immedatelyListeners.remove(searchListener); 106 170 } 107 171 … … 130 194 131 195 @Override 132 public void selectionChanged(SelectionChangeEvent e) { 133 Set<OsmPrimitive> newSelection = e.getSelection(); 134 fireEvents(normalListeners, newSelection); 135 selection = newSelection; 136 SwingUtilities.invokeLater(edtRunnable); 137 } 138 139 private static void fireEvents(List<ListenerInfo> listeners, Collection<? extends OsmPrimitive> newSelection) { 140 for (ListenerInfo listener: listeners) { 141 listener.listener.selectionChanged(newSelection); 142 } 143 } 144 145 private final Runnable edtRunnable = () -> { 146 if (selection != null) { 147 fireEvents(inEDTListeners, selection); 148 } 149 }; 196 public void selectionChanged(SelectionChangeEvent event) { 197 fireEvent(immedatelyListeners, event); 198 SwingUtilities.invokeLater(() -> fireEvent(inEDTListeners, event)); 199 } 200 201 private static void fireEvent(List<AbstractListenerInfo> listeners, SelectionChangeEvent event) { 202 for (AbstractListenerInfo listener: listeners) { 203 listener.fire(event); 204 } 205 } 150 206 151 207 /** … … 155 211 public void resetState() { 156 212 inEDTListeners.clear(); 157 normalListeners.clear();213 immedatelyListeners.clear(); 158 214 Main.getLayerManager().addAndFireActiveLayerChangeListener(this); 159 215 }
Note:
See TracChangeset
for help on using the changeset viewer.