| 1 | package org.openstreetmap.josm.data;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.Collection;
|
|---|
| 4 | import java.util.LinkedList;
|
|---|
| 5 |
|
|---|
| 6 | import javax.swing.SwingUtilities;
|
|---|
| 7 |
|
|---|
| 8 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * This class is to help the DataSet collecting and fire selectionChanged events.
|
|---|
| 12 | * For more, @see org.openstreetmap.josm.data.SelectionChangedListener
|
|---|
| 13 | *
|
|---|
| 14 | * @author imi
|
|---|
| 15 | */
|
|---|
| 16 | abstract public class SelectionTracker {
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Collects the selction changed events. The first collector that runs in
|
|---|
| 20 | * one queue, starts the purger.
|
|---|
| 21 | * @author imi
|
|---|
| 22 | */
|
|---|
| 23 | private final class Collector implements Runnable {
|
|---|
| 24 | public void run() {
|
|---|
| 25 | switch (state) {
|
|---|
| 26 | case WAITING:
|
|---|
| 27 | throw new IllegalStateException();
|
|---|
| 28 | case COLLECTING:
|
|---|
| 29 | state = SelectionEventState.PURGING;
|
|---|
| 30 | SwingUtilities.invokeLater(new Purger());
|
|---|
| 31 | break;
|
|---|
| 32 | case PURGING:
|
|---|
| 33 | break; // still purging events.
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Informs the listener clients and go back to waiting state.
|
|---|
| 40 | * @author imi
|
|---|
| 41 | */
|
|---|
| 42 | private final class Purger implements Runnable {
|
|---|
| 43 | public void run() {
|
|---|
| 44 | if (state != SelectionEventState.PURGING)
|
|---|
| 45 | throw new IllegalStateException();
|
|---|
| 46 | state = SelectionEventState.WAITING;
|
|---|
| 47 | Collection<OsmPrimitive> sel = getSelected();
|
|---|
| 48 | for (SelectionChangedListener l : listeners)
|
|---|
| 49 | l.selectionChanged(sel);
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | /**
|
|---|
| 54 | * The event state for the selection dispatching. WAITING means we are
|
|---|
| 55 | * waiting for any fireSelectionChanged event. COLLECTING means, we have
|
|---|
| 56 | * already some events in the EventQueue and are now collecting more events.
|
|---|
| 57 | * PURGING means, the collecting phase is over and we wait now for the finish
|
|---|
| 58 | * event to just contact the listeners.
|
|---|
| 59 | * @author imi
|
|---|
| 60 | */
|
|---|
| 61 | private enum SelectionEventState {WAITING, COLLECTING, PURGING}
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * The state, regarding to the selection changing that we are in.
|
|---|
| 65 | */
|
|---|
| 66 | transient SelectionEventState state = SelectionEventState.WAITING;
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * A list of listeners to selection changed events.
|
|---|
| 70 | */
|
|---|
| 71 | transient Collection<SelectionChangedListener> listeners = new LinkedList<SelectionChangedListener>();
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * Add a listener to the selection changed listener list. If <code>null</code>
|
|---|
| 76 | * is passed, nothing happens.
|
|---|
| 77 | * @param listener The listener to add to the list.
|
|---|
| 78 | */
|
|---|
| 79 | public void addSelectionChangedListener(SelectionChangedListener listener) {
|
|---|
| 80 | if (listener != null)
|
|---|
| 81 | listeners.add(listener);
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | /**
|
|---|
| 85 | * Remove a listener from the selection changed listener list.
|
|---|
| 86 | * If <code>null</code> is passed, nothing happens.
|
|---|
| 87 | * @param listener The listener to remove from the list.
|
|---|
| 88 | */
|
|---|
| 89 | public void removeSelectionChangedListener(SelectionChangedListener listener) {
|
|---|
| 90 | if (listener != null)
|
|---|
| 91 | listeners.remove(listener);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * Remember to fire an selection changed event. A call to this will not fire
|
|---|
| 96 | * the event immediately. For more, @see SelectionChangedListener
|
|---|
| 97 | */
|
|---|
| 98 | public void fireSelectionChanged() {
|
|---|
| 99 | if (state == SelectionEventState.WAITING) {
|
|---|
| 100 | state = SelectionEventState.COLLECTING;
|
|---|
| 101 | SwingUtilities.invokeLater(new Collector());
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * This function is needed by the Purger to get the actual selection.
|
|---|
| 107 | * @return The selected primitives.
|
|---|
| 108 | */
|
|---|
| 109 | abstract public Collection<OsmPrimitive> getSelected();
|
|---|
| 110 | }
|
|---|