| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package relcontext;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.AlphaComposite;
|
|---|
| 5 | import java.awt.BasicStroke;
|
|---|
| 6 | import java.awt.Color;
|
|---|
| 7 | import java.awt.Composite;
|
|---|
| 8 | import java.awt.Graphics2D;
|
|---|
| 9 | import java.awt.Point;
|
|---|
| 10 | import java.awt.Stroke;
|
|---|
| 11 | import java.awt.geom.GeneralPath;
|
|---|
| 12 | import java.util.HashSet;
|
|---|
| 13 | import java.util.Set;
|
|---|
| 14 |
|
|---|
| 15 | import org.openstreetmap.josm.Main;
|
|---|
| 16 | import org.openstreetmap.josm.data.Bounds;
|
|---|
| 17 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 19 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
|---|
| 20 | import org.openstreetmap.josm.data.osm.Relation;
|
|---|
| 21 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent;
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.event.DataChangedEvent;
|
|---|
| 24 | import org.openstreetmap.josm.data.osm.event.DataSetListener;
|
|---|
| 25 | import org.openstreetmap.josm.data.osm.event.NodeMovedEvent;
|
|---|
| 26 | import org.openstreetmap.josm.data.osm.event.PrimitivesAddedEvent;
|
|---|
| 27 | import org.openstreetmap.josm.data.osm.event.PrimitivesRemovedEvent;
|
|---|
| 28 | import org.openstreetmap.josm.data.osm.event.RelationMembersChangedEvent;
|
|---|
| 29 | import org.openstreetmap.josm.data.osm.event.TagsChangedEvent;
|
|---|
| 30 | import org.openstreetmap.josm.data.osm.event.WayNodesChangedEvent;
|
|---|
| 31 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 32 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeEvent;
|
|---|
| 33 | import org.openstreetmap.josm.gui.layer.MainLayerManager.ActiveLayerChangeListener;
|
|---|
| 34 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
|---|
| 35 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Chosen relation; is used for all actions and is highlighted on the map.
|
|---|
| 39 | *
|
|---|
| 40 | * @author Zverik
|
|---|
| 41 | */
|
|---|
| 42 | public class ChosenRelation implements ActiveLayerChangeListener, MapViewPaintable, DataSetListener {
|
|---|
| 43 | protected Relation chosenRelation = null;
|
|---|
| 44 | private Set<ChosenRelationListener> chosenRelationListeners = new HashSet<>();
|
|---|
| 45 |
|
|---|
| 46 | public void set(Relation rel) {
|
|---|
| 47 | if (rel == chosenRelation || (rel != null && chosenRelation != null && rel.equals(chosenRelation)))
|
|---|
| 48 | return; // new is the same as old
|
|---|
| 49 | Relation oldRel = chosenRelation;
|
|---|
| 50 | chosenRelation = rel;
|
|---|
| 51 | analyse();
|
|---|
| 52 | Main.map.mapView.repaint();
|
|---|
| 53 | fireRelationChanged(oldRel);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | protected void fireRelationChanged(Relation oldRel) {
|
|---|
| 57 | for (ChosenRelationListener listener : chosenRelationListeners) {
|
|---|
| 58 | listener.chosenRelationChanged(oldRel, chosenRelation);
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public Relation get() {
|
|---|
| 63 | return chosenRelation;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | public void clear() {
|
|---|
| 67 | set(null);
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | public boolean isSame(Object r) {
|
|---|
| 71 | if (r == null)
|
|---|
| 72 | return chosenRelation == null;
|
|---|
| 73 | else if (!(r instanceof Relation))
|
|---|
| 74 | return false;
|
|---|
| 75 | else
|
|---|
| 76 | return chosenRelation != null && r.equals(chosenRelation);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | private static final String[] MULTIPOLYGON_TYPES = new String[] {
|
|---|
| 80 | "multipolygon", "boundary"
|
|---|
| 81 | };
|
|---|
| 82 |
|
|---|
| 83 | /**
|
|---|
| 84 | * Check if the relation type assumes all ways inside it form a multipolygon.
|
|---|
| 85 | */
|
|---|
| 86 | public boolean isMultipolygon() {
|
|---|
| 87 | return isMultipolygon(chosenRelation);
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | public static boolean isMultipolygon(Relation r) {
|
|---|
| 91 | if (r == null)
|
|---|
| 92 | return false;
|
|---|
| 93 | String type = r.get("type");
|
|---|
| 94 | if (type == null)
|
|---|
| 95 | return false;
|
|---|
| 96 | for (String t : MULTIPOLYGON_TYPES) {
|
|---|
| 97 | if (t.equals(type))
|
|---|
| 98 | return true;
|
|---|
| 99 | }
|
|---|
| 100 | return false;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | public int getSegmentsCount() {
|
|---|
| 104 | return 0;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | public int getCirclesCount() {
|
|---|
| 108 | return 0;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | protected void analyse() {
|
|---|
| 112 | // todo
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | public void addChosenRelationListener(ChosenRelationListener listener) {
|
|---|
| 116 | chosenRelationListeners.add(listener);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | public void removeChosenRelationListener(ChosenRelationListener listener) {
|
|---|
| 120 | chosenRelationListeners.remove(listener);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | @Override
|
|---|
| 124 | public void activeOrEditLayerChanged(ActiveLayerChangeEvent e) {
|
|---|
| 125 | // todo: dim chosen relation when changing layer
|
|---|
| 126 | // todo: check this WTF!
|
|---|
| 127 | OsmDataLayer newLayer = Main.getLayerManager().getEditLayer();
|
|---|
| 128 | clear();
|
|---|
| 129 | if (newLayer != null && e.getPreviousEditLayer() == null) {
|
|---|
| 130 | Main.map.mapView.addTemporaryLayer(this);
|
|---|
| 131 | } else if (newLayer == null) {
|
|---|
| 132 | Main.map.mapView.removeTemporaryLayer(this);
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | @Override
|
|---|
| 137 | public void paint(Graphics2D g, MapView mv, Bounds bbox) {
|
|---|
| 138 | if (chosenRelation == null)
|
|---|
| 139 | return;
|
|---|
| 140 |
|
|---|
| 141 | OsmDataLayer dataLayer = mv.getLayerManager().getEditLayer();
|
|---|
| 142 | float opacity = dataLayer == null ? 0.0f : !dataLayer.isVisible() ? 0.0f : (float) dataLayer.getOpacity();
|
|---|
| 143 | if (opacity < 0.01)
|
|---|
| 144 | return;
|
|---|
| 145 |
|
|---|
| 146 | Composite oldComposite = g.getComposite();
|
|---|
| 147 | Stroke oldStroke = g.getStroke();
|
|---|
| 148 | g.setStroke(new BasicStroke(9, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
|---|
| 149 | g.setColor(Color.yellow);
|
|---|
| 150 | g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f * opacity));
|
|---|
| 151 |
|
|---|
| 152 | drawRelations(g, mv, bbox, chosenRelation);
|
|---|
| 153 |
|
|---|
| 154 | g.setComposite(oldComposite);
|
|---|
| 155 | g.setStroke(oldStroke);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | private void drawRelations(Graphics2D g, MapView mv, Bounds bbox, Relation rel) {
|
|---|
| 159 | for (OsmPrimitive element : rel.getMemberPrimitives()) {
|
|---|
| 160 | if (element.getType() == OsmPrimitiveType.NODE) {
|
|---|
| 161 | Node node = (Node) element;
|
|---|
| 162 | Point center = mv.getPoint(node);
|
|---|
| 163 | g.drawOval(center.x - 4, center.y - 4, 9, 9);
|
|---|
| 164 | } else if (element.getType() == OsmPrimitiveType.WAY) {
|
|---|
| 165 | Way way = (Way) element;
|
|---|
| 166 | if (way.getNodesCount() >= 2) {
|
|---|
| 167 | GeneralPath b = new GeneralPath();
|
|---|
| 168 | Point p = mv.getPoint(way.getNode(0));
|
|---|
| 169 | b.moveTo(p.x, p.y);
|
|---|
| 170 | for (int i = 1; i < way.getNodesCount(); i++) {
|
|---|
| 171 | p = mv.getPoint(way.getNode(i));
|
|---|
| 172 | b.lineTo(p.x, p.y);
|
|---|
| 173 | }
|
|---|
| 174 | g.draw(b);
|
|---|
| 175 | }
|
|---|
| 176 | } else if (element.getType() == OsmPrimitiveType.RELATION) {
|
|---|
| 177 | Color oldColor = g.getColor();
|
|---|
| 178 | g.setColor(Color.magenta);
|
|---|
| 179 | drawRelations(g, mv, bbox, (Relation) element);
|
|---|
| 180 | g.setColor(oldColor);
|
|---|
| 181 | }
|
|---|
| 182 | // todo: closedway, multipolygon - ?
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | @Override
|
|---|
| 187 | public void relationMembersChanged(RelationMembersChangedEvent event) {
|
|---|
| 188 | if (chosenRelation != null && event.getRelation().equals(chosenRelation)) {
|
|---|
| 189 | fireRelationChanged(chosenRelation);
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | @Override
|
|---|
| 194 | public void tagsChanged(TagsChangedEvent event) {
|
|---|
| 195 | if (chosenRelation != null && event.getPrimitive().equals(chosenRelation)) {
|
|---|
| 196 | fireRelationChanged(chosenRelation);
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | @Override
|
|---|
| 201 | public void dataChanged(DataChangedEvent event) {
|
|---|
| 202 | if (chosenRelation != null) {
|
|---|
| 203 | fireRelationChanged(chosenRelation);
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | @Override
|
|---|
| 208 | public void primitivesRemoved(PrimitivesRemovedEvent event) {
|
|---|
| 209 | if (chosenRelation != null && event.getPrimitives().contains(chosenRelation)) {
|
|---|
| 210 | clear();
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | @Override
|
|---|
| 215 | public void wayNodesChanged(WayNodesChangedEvent event) {
|
|---|
| 216 | if (chosenRelation != null) {
|
|---|
| 217 | fireRelationChanged(chosenRelation); // download incomplete primitives doesn't cause dataChanged event
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | @Override
|
|---|
| 222 | public void primitivesAdded(PrimitivesAddedEvent event) {}
|
|---|
| 223 |
|
|---|
| 224 | @Override
|
|---|
| 225 | public void nodeMoved(NodeMovedEvent event) {}
|
|---|
| 226 |
|
|---|
| 227 | @Override
|
|---|
| 228 | public void otherDatasetChange(AbstractDatasetChangedEvent event) {}
|
|---|
| 229 | }
|
|---|