[2912] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.data.osm.event;
|
---|
| 3 |
|
---|
[12048] | 4 | import java.util.Collections;
|
---|
| 5 | import java.util.HashSet;
|
---|
[2912] | 6 | import java.util.List;
|
---|
[9371] | 7 | import java.util.Objects;
|
---|
[2912] | 8 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
[12048] | 9 | import java.util.stream.Stream;
|
---|
[2912] | 10 |
|
---|
| 11 | import javax.swing.SwingUtilities;
|
---|
| 12 |
|
---|
[12048] | 13 | import org.openstreetmap.josm.Main;
|
---|
[2912] | 14 | import org.openstreetmap.josm.data.SelectionChangedListener;
|
---|
[12048] | 15 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
[2912] | 16 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
| 17 | import org.openstreetmap.josm.data.osm.event.DatasetEventManager.FireMode;
|
---|
[12048] | 18 | import org.openstreetmap.josm.gui.layer.MainLayerManager;
|
---|
| 19 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
---|
| 20 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
---|
[2912] | 21 |
|
---|
| 22 | /**
|
---|
[12048] | 23 | * Similar like {@link DatasetEventManager}, just for selection events.
|
---|
| 24 | *
|
---|
| 25 | * It allows to register listeners to global selection events for the selection in the current edit layer.
|
---|
| 26 | *
|
---|
| 27 | * If you want to listen to selections to a specific data layer,
|
---|
| 28 | * you can register a listener to that layer by using {@link DataSet#addSelectionListener(DataSelectionListener)}
|
---|
| 29 | *
|
---|
[11928] | 30 | * @since 2912
|
---|
[2912] | 31 | */
|
---|
[12048] | 32 | public class SelectionEventManager implements DataSelectionListener, ActiveLayerChangeListener {
|
---|
[2912] | 33 |
|
---|
| 34 | private static final SelectionEventManager instance = new SelectionEventManager();
|
---|
| 35 |
|
---|
[11928] | 36 | /**
|
---|
| 37 | * Returns the unique instance.
|
---|
| 38 | * @return the unique instance
|
---|
| 39 | */
|
---|
[2912] | 40 | public static SelectionEventManager getInstance() {
|
---|
| 41 | return instance;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[12098] | 44 | private abstract static class AbstractListenerInfo {
|
---|
| 45 | abstract void fire(SelectionChangeEvent event);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | private static class ListenerInfo extends AbstractListenerInfo {
|
---|
[8285] | 49 | private final SelectionChangedListener listener;
|
---|
[2912] | 50 |
|
---|
[8836] | 51 | ListenerInfo(SelectionChangedListener listener) {
|
---|
[2912] | 52 | this.listener = listener;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | @Override
|
---|
[12098] | 56 | void fire(SelectionChangeEvent event) {
|
---|
| 57 | listener.selectionChanged(event.getSelection());
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | @Override
|
---|
[2912] | 61 | public int hashCode() {
|
---|
[9371] | 62 | return Objects.hash(listener);
|
---|
[2912] | 63 | }
|
---|
| 64 |
|
---|
| 65 | @Override
|
---|
| 66 | public boolean equals(Object o) {
|
---|
[9371] | 67 | if (this == o) return true;
|
---|
| 68 | if (o == null || getClass() != o.getClass()) return false;
|
---|
| 69 | ListenerInfo that = (ListenerInfo) o;
|
---|
| 70 | return Objects.equals(listener, that.listener);
|
---|
[2912] | 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[12098] | 74 | private static class DataListenerInfo extends AbstractListenerInfo {
|
---|
| 75 | private final DataSelectionListener listener;
|
---|
[2912] | 76 |
|
---|
[12098] | 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<>();
|
---|
| 102 |
|
---|
[6361] | 103 | /**
|
---|
| 104 | * Constructs a new {@code SelectionEventManager}.
|
---|
| 105 | */
|
---|
[12048] | 106 | protected SelectionEventManager() {
|
---|
| 107 | MainLayerManager layerManager = Main.getLayerManager();
|
---|
| 108 | // We do not allow for destructing this object.
|
---|
| 109 | // Currently, this is a singleton class, so this is not required.
|
---|
| 110 | layerManager.addAndFireActiveLayerChangeListener(this);
|
---|
[2912] | 111 | }
|
---|
| 112 |
|
---|
[11928] | 113 | /**
|
---|
| 114 | * Registers a new {@code SelectionChangedListener}.
|
---|
[12098] | 115 | *
|
---|
| 116 | * It is preferred to add a DataSelectionListener - that listener will receive more information about the event.
|
---|
[11928] | 117 | * @param listener listener to add
|
---|
[12096] | 118 | * @param fireMode Set this to IN_EDT_CONSOLIDATED if you want the event to be fired in the EDT thread.
|
---|
[12109] | 119 | * Set it to IMMEDIATELY if you want the event to fire in the thread that caused the selection update.
|
---|
[11928] | 120 | */
|
---|
[2912] | 121 | public void addSelectionListener(SelectionChangedListener listener, FireMode fireMode) {
|
---|
[12109] | 122 | if (fireMode == FireMode.IN_EDT) {
|
---|
[2912] | 123 | throw new UnsupportedOperationException("IN_EDT mode not supported, you probably want to use IN_EDT_CONSOLIDATED.");
|
---|
[12096] | 124 | } else if (fireMode == FireMode.IN_EDT_CONSOLIDATED) {
|
---|
[6361] | 125 | inEDTListeners.addIfAbsent(new ListenerInfo(listener));
|
---|
[2912] | 126 | } else {
|
---|
[12098] | 127 | immedatelyListeners.addIfAbsent(new ListenerInfo(listener));
|
---|
[2912] | 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[11928] | 131 | /**
|
---|
[12098] | 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));
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
[11928] | 151 | * Unregisters a {@code SelectionChangedListener}.
|
---|
| 152 | * @param listener listener to remove
|
---|
| 153 | */
|
---|
[2912] | 154 | public void removeSelectionListener(SelectionChangedListener listener) {
|
---|
[12098] | 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) {
|
---|
[2912] | 168 | inEDTListeners.remove(searchListener);
|
---|
[12098] | 169 | immedatelyListeners.remove(searchListener);
|
---|
[2912] | 170 | }
|
---|
| 171 |
|
---|
[6084] | 172 | @Override
|
---|
[12048] | 173 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
---|
| 174 | DataSet oldDataSet = e.getPreviousEditDataSet();
|
---|
| 175 | if (oldDataSet != null) {
|
---|
| 176 | // Fake a selection removal
|
---|
| 177 | // Relying on this allows components to not have to monitor layer changes.
|
---|
| 178 | // If we would not do this, e.g. the move command would have a hard time tracking which layer
|
---|
| 179 | // the last moved selection was in.
|
---|
| 180 | SelectionReplaceEvent event = new SelectionReplaceEvent(oldDataSet,
|
---|
| 181 | new HashSet<>(oldDataSet.getAllSelected()), Stream.empty());
|
---|
| 182 | selectionChanged(event);
|
---|
| 183 | oldDataSet.removeSelectionListener(this);
|
---|
| 184 | }
|
---|
| 185 | DataSet newDataSet = e.getSource().getEditDataSet();
|
---|
| 186 | if (newDataSet != null) {
|
---|
| 187 | newDataSet.addSelectionListener(this);
|
---|
| 188 | // Fake a selection add
|
---|
| 189 | SelectionReplaceEvent event = new SelectionReplaceEvent(newDataSet,
|
---|
| 190 | Collections.emptySet(), newDataSet.getAllSelected().stream());
|
---|
| 191 | selectionChanged(event);
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 |
|
---|
| 195 | @Override
|
---|
[12098] | 196 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
| 197 | fireEvent(immedatelyListeners, event);
|
---|
| 198 | SwingUtilities.invokeLater(() -> fireEvent(inEDTListeners, event));
|
---|
[2912] | 199 | }
|
---|
| 200 |
|
---|
[12098] | 201 | private static void fireEvent(List<AbstractListenerInfo> listeners, SelectionChangeEvent event) {
|
---|
| 202 | for (AbstractListenerInfo listener: listeners) {
|
---|
| 203 | listener.fire(event);
|
---|
[2912] | 204 | }
|
---|
| 205 | }
|
---|
| 206 |
|
---|
[12070] | 207 | /**
|
---|
| 208 | * Only to be used during unit tests, to reset the state. Do not use it in plugins/other code.
|
---|
| 209 | * Called after the layer manager was reset by the test framework.
|
---|
| 210 | */
|
---|
| 211 | public void resetState() {
|
---|
| 212 | inEDTListeners.clear();
|
---|
[12098] | 213 | immedatelyListeners.clear();
|
---|
[12070] | 214 | Main.getLayerManager().addAndFireActiveLayerChangeListener(this);
|
---|
| 215 | }
|
---|
[2912] | 216 | }
|
---|