| 1 | package org.openstreetmap.josm.data.projection;
|
|---|
| 2 |
|
|---|
| 3 | import java.util.LinkedList;
|
|---|
| 4 | import java.util.List;
|
|---|
| 5 |
|
|---|
| 6 | import javax.swing.JComponent;
|
|---|
| 7 | import javax.swing.event.ChangeEvent;
|
|---|
| 8 | import javax.swing.event.ChangeListener;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.data.GeoPoint;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 12 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 13 |
|
|---|
| 14 | /**
|
|---|
| 15 | * Classes subclass this are able to convert lat/lon values to
|
|---|
| 16 | * planear screen coordinates.
|
|---|
| 17 | *
|
|---|
| 18 | * @author imi
|
|---|
| 19 | */
|
|---|
| 20 | abstract public class Projection implements Cloneable {
|
|---|
| 21 |
|
|---|
| 22 | /**
|
|---|
| 23 | * The event list with all state chaned listener
|
|---|
| 24 | */
|
|---|
| 25 | List<ChangeListener> listener = new LinkedList<ChangeListener>();
|
|---|
| 26 |
|
|---|
| 27 | /**
|
|---|
| 28 | * Convert from lat/lon to northing/easting.
|
|---|
| 29 | *
|
|---|
| 30 | * @param p The geo point to convert. x/y members of the point are filled.
|
|---|
| 31 | */
|
|---|
| 32 | abstract public void latlon2xy(GeoPoint p);
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Convert from norting/easting to lat/lon.
|
|---|
| 36 | *
|
|---|
| 37 | * @param p The geo point to convert. lat/lon members of the point are filled.
|
|---|
| 38 | */
|
|---|
| 39 | abstract public void xy2latlon(GeoPoint p);
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | // description functions
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Describe the projection converter in one or two words.
|
|---|
| 46 | */
|
|---|
| 47 | abstract public String toString();
|
|---|
| 48 |
|
|---|
| 49 | /**
|
|---|
| 50 | * Describe the projection converter. Give examples, where it is best to use
|
|---|
| 51 | * and maybe a reference link to more information about the converter.
|
|---|
| 52 | */
|
|---|
| 53 | abstract public String description();
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | // miscellous functions
|
|---|
| 58 |
|
|---|
| 59 | /**
|
|---|
| 60 | * If the projection supports any configuration, this function return
|
|---|
| 61 | * the configuration panel. If no configuration needed, return null.
|
|---|
| 62 | *
|
|---|
| 63 | * The items on the configuration panel should update the configuration
|
|---|
| 64 | * directly, so the changes are instantly visible on screen.
|
|---|
| 65 | */
|
|---|
| 66 | abstract public JComponent getConfigurationPanel();
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * Initialize itself with the given dataSet.
|
|---|
| 70 | *
|
|---|
| 71 | * This function should initialize own parameters needed to do the
|
|---|
| 72 | * projection and then initialize every Node of the dataset with x/y
|
|---|
| 73 | * projection values.
|
|---|
| 74 | *
|
|---|
| 75 | * This implementation loops through the dataset's nodes and call latlon2xy
|
|---|
| 76 | * for each member. Subclasses can call this to initialize the dataset.
|
|---|
| 77 | *
|
|---|
| 78 | * init must not fire an state changed event, since it is usually called
|
|---|
| 79 | * from the event handler.
|
|---|
| 80 | *
|
|---|
| 81 | * @param dataSet
|
|---|
| 82 | * The dataset, which will be displayed on screen. Later, all
|
|---|
| 83 | * projections should be relative to the given dataset. Any
|
|---|
| 84 | * reverse projections (xy2latlon) can be assumed to be in near
|
|---|
| 85 | * distance to nodes of this dataset (that means, it is ok, if
|
|---|
| 86 | * there is a conversion error, if the requested x/y to xy2latlon
|
|---|
| 87 | * is far away from any coordinate in the dataset)
|
|---|
| 88 | */
|
|---|
| 89 | public void init(DataSet dataSet) {
|
|---|
| 90 | for (Node w : dataSet.allNodes)
|
|---|
| 91 | latlon2xy(w.coor);
|
|---|
| 92 | }
|
|---|
| 93 |
|
|---|
| 94 | /**
|
|---|
| 95 | * All projections support cloning.
|
|---|
| 96 | */
|
|---|
| 97 | @Override
|
|---|
| 98 | public Projection clone() {
|
|---|
| 99 | try {return (Projection)super.clone();} catch (CloneNotSupportedException e) {}
|
|---|
| 100 | return null;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Add an event listener to the state changed event queue. If passed
|
|---|
| 105 | * <code>null</code>, nothing happens.
|
|---|
| 106 | */
|
|---|
| 107 | public final void addChangeListener(ChangeListener l) {
|
|---|
| 108 | if (l != null)
|
|---|
| 109 | listener.add(l);
|
|---|
| 110 | }
|
|---|
| 111 | /**
|
|---|
| 112 | * Remove an event listener from the event queue. If passed
|
|---|
| 113 | * <code>null</code>, nothing happens.
|
|---|
| 114 | */
|
|---|
| 115 | public final void removeChangeListener(ChangeListener l) {
|
|---|
| 116 | listener.remove(l);
|
|---|
| 117 | }
|
|---|
| 118 | /**
|
|---|
| 119 | * Fire an ChangeEvent to every listener on the queue.
|
|---|
| 120 | */
|
|---|
| 121 | public final void fireStateChanged() {
|
|---|
| 122 | ChangeEvent e = null;
|
|---|
| 123 | for(ChangeListener l : listener) {
|
|---|
| 124 | if (e == null)
|
|---|
| 125 | e = new ChangeEvent(this);
|
|---|
| 126 | l.stateChanged(e);
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|