Changeset 35047 in osm for applications/editors/josm/plugins/measurement/src/org
- Timestamp:
- 2019-07-07T16:09:02+02:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/measurement/src/org/openstreetmap/josm/plugins/measurement/MeasurementDialog.java
r34529 r35047 11 11 import java.util.Arrays; 12 12 import java.util.Collection; 13 import java.util.Objects; 13 14 14 15 import javax.swing.AbstractAction; … … 22 23 import org.openstreetmap.josm.data.osm.Node; 23 24 import org.openstreetmap.josm.data.osm.OsmPrimitive; 25 import org.openstreetmap.josm.data.osm.Relation; 24 26 import org.openstreetmap.josm.data.osm.Way; 25 27 import org.openstreetmap.josm.data.osm.event.AbstractDatasetChangedEvent; … … 37 39 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; 38 40 import org.openstreetmap.josm.gui.help.HelpUtil; 41 import org.openstreetmap.josm.gui.layer.LayerManager.LayerAddEvent; 42 import org.openstreetmap.josm.gui.layer.LayerManager.LayerChangeListener; 43 import org.openstreetmap.josm.gui.layer.LayerManager.LayerOrderChangeEvent; 44 import org.openstreetmap.josm.gui.layer.LayerManager.LayerRemoveEvent; 45 import org.openstreetmap.josm.gui.layer.OsmDataLayer; 39 46 import org.openstreetmap.josm.gui.util.GuiHelper; 47 import org.openstreetmap.josm.tools.Geometry; 40 48 import org.openstreetmap.josm.tools.ImageProvider; 41 49 import org.openstreetmap.josm.tools.Shortcut; … … 47 55 * @author ramack 48 56 */ 49 public class MeasurementDialog extends ToggleDialog implements DataSelectionListener, DataSetListener, SoMChangeListener {57 public class MeasurementDialog extends ToggleDialog implements DataSelectionListener, DataSetListener, SoMChangeListener, LayerChangeListener { 50 58 private static final long serialVersionUID = 4708541586297950021L; 51 59 … … 82 90 private DataSet ds; 83 91 92 private Collection<Relation> relations; 84 93 private Collection<Way> ways; 85 94 private Collection<Node> nodes; … … 143 152 })); 144 153 154 MainApplication.getLayerManager().addLayerChangeListener(this); 145 155 SelectionEventManager.getInstance().addSelectionListener(this); 146 156 SystemOfMeasurement.addSoMChangeListener(this); … … 185 195 if (ways.isEmpty()) { 186 196 nodes = new SubclassFilteredCollection<>(selection, Node.class::isInstance); 187 for (Node n : nodes) { 188 if (n.getCoor() != null) { 189 if (lastNode == null) { 190 lastNode = n; 191 } else { 192 length += lastNode.getCoor().greatCircleDistance(n.getCoor()); 193 segAngle = MeasurementLayer.angleBetween(lastNode.getCoor(), n.getCoor()); 194 lastNode = n; 197 if (nodes.isEmpty()) { 198 relations = new SubclassFilteredCollection<>(selection, Relation.class::isInstance); 199 for (Relation r : relations) { 200 if (r.isMultipolygon()) { 201 area += Geometry.multipolygonArea(r); 202 } 203 } 204 } else { 205 for (Node n : nodes) { 206 if (n.getCoor() != null) { 207 if (lastNode == null) { 208 lastNode = n; 209 } else { 210 length += lastNode.getCoor().greatCircleDistance(n.getCoor()); 211 segAngle = MeasurementLayer.angleBetween(lastNode.getCoor(), n.getCoor()); 212 lastNode = n; 213 } 195 214 } 196 215 } … … 264 283 SystemOfMeasurement.removeSoMChangeListener(this); 265 284 SelectionEventManager.getInstance().removeSelectionListener(this); 285 MainApplication.getLayerManager().removeLayerChangeListener(this); 286 clear(); 287 } 288 289 private void clear() { 266 290 if (ds != null) { 267 291 ds.removeDataSetListener(this); 268 292 ds = null; 269 293 } 270 } 271 272 private boolean waysContain(Node n) { 273 if (ways != null) { 274 for (Way w : ways) { 275 if (w.containsNode(n)) { 276 return true; 277 } 278 } 279 } 280 return false; 294 clear(relations); 295 clear(ways); 296 clear(nodes); 297 } 298 299 private static void clear(Collection<?> collection) { 300 if (collection != null) { 301 collection.clear(); 302 } 303 } 304 305 private boolean parentsContain(Way w) { 306 return w.getReferrers().stream() 307 .anyMatch(ref -> ref instanceof Relation && relations != null && relations.contains(ref)); 308 } 309 310 private boolean parentsContain(Node n) { 311 return n.getReferrers().stream() 312 .anyMatch(ref 313 -> (ref instanceof Way && ((ways != null && ways.contains(ref)) || parentsContain((Way) ref))) 314 || (ref instanceof Relation && relations != null && relations.contains(ref))); 281 315 } 282 316 … … 284 318 Node n = event.getNode(); 285 319 // Refresh selection if a node belonging to a selected member has moved (example: scale action) 286 if ((nodes != null && nodes.contains(n)) || waysContain(n)) {320 if ((nodes != null && nodes.contains(n)) || parentsContain(n)) { 287 321 refresh(event.getDataset().getSelected()); 288 322 } … … 290 324 291 325 @Override public void wayNodesChanged(WayNodesChangedEvent event) { 292 if (ways.contains(event.getChangedWay())) { 326 Way w = event.getChangedWay(); 327 if ((ways != null && ways.contains(w)) || parentsContain(w)) { 328 refresh(event.getDataset().getSelected()); 329 } 330 } 331 332 @Override public void relationMembersChanged(RelationMembersChangedEvent event) { 333 if (relations != null && relations.contains(event.getRelation())) { 293 334 refresh(event.getDataset().getSelected()); 294 335 } … … 298 339 @Override public void primitivesRemoved(PrimitivesRemovedEvent event) {} 299 340 @Override public void tagsChanged(TagsChangedEvent event) {} 300 @Override public void relationMembersChanged(RelationMembersChangedEvent event) {}301 341 @Override public void otherDatasetChange(AbstractDatasetChangedEvent event) {} 302 342 @Override public void dataChanged(DataChangedEvent event) {} … … 310 350 } 311 351 } 352 353 @Override public void layerOrderChanged(LayerOrderChangeEvent e) {} 354 @Override public void layerAdded(LayerAddEvent e) {} 355 356 @Override 357 public void layerRemoving(LayerRemoveEvent e) { 358 if (e.getRemovedLayer() instanceof OsmDataLayer && Objects.equals(ds, ((OsmDataLayer) e.getRemovedLayer()).getDataSet())) { 359 clear(); 360 } 361 } 312 362 }
Note:
See TracChangeset
for help on using the changeset viewer.