source: osm/applications/editors/josm/plugins/reltoolbox/src/relcontext/ChosenRelation.java@ 26174

Last change on this file since 26174 was 25751, checked in by zverik, 14 years ago

execute sort&fix on add/remove; also static refactoring (reltoolbox plugin)

File size: 5.9 KB
Line 
1package relcontext;
2
3import java.awt.*;
4import java.awt.geom.GeneralPath;
5import java.util.*;
6import org.openstreetmap.josm.Main;
7import org.openstreetmap.josm.data.Bounds;
8import org.openstreetmap.josm.data.osm.*;
9import org.openstreetmap.josm.data.osm.event.*;
10import org.openstreetmap.josm.gui.MapView;
11import org.openstreetmap.josm.gui.MapView.EditLayerChangeListener;
12import org.openstreetmap.josm.gui.layer.MapViewPaintable;
13import org.openstreetmap.josm.gui.layer.OsmDataLayer;
14
15/**
16 * Chosen relation; is used for all actions and is highlighted on the map.
17 *
18 * @author Zverik
19 */
20public class ChosenRelation implements EditLayerChangeListener, MapViewPaintable, DataSetListener {
21 protected Relation chosenRelation = null;
22 private Set<ChosenRelationListener> chosenRelationListeners = new HashSet<ChosenRelationListener>();
23
24 public void set( Relation rel ) {
25 if( rel == chosenRelation || (rel != null && chosenRelation != null && rel.equals(chosenRelation)) ) {
26 return; // new is the same as old
27 }
28 Relation oldRel = chosenRelation;
29 chosenRelation = rel;
30 analyse();
31 Main.map.mapView.repaint();
32 fireRelationChanged(oldRel);
33 }
34
35 protected void fireRelationChanged( Relation oldRel ) {
36 for( ChosenRelationListener listener : chosenRelationListeners )
37 listener.chosenRelationChanged(oldRel, chosenRelation);
38 }
39
40 public Relation get() {
41 return chosenRelation;
42 }
43
44 public void clear() {
45 set(null);
46 }
47
48 public boolean isSame( Object r ) {
49 if( r == null )
50 return chosenRelation == null;
51 else if( !(r instanceof Relation) )
52 return false;
53 else
54 return chosenRelation != null && r.equals(chosenRelation);
55 }
56
57 private final static String[] MULTIPOLYGON_TYPES = new String[] {
58 "multipolygon", "boundary"
59 };
60
61 /**
62 * Check if the relation type assumes all ways inside it form a multipolygon.
63 */
64 public boolean isMultipolygon() {
65 return isMultipolygon(chosenRelation);
66 }
67
68 public static boolean isMultipolygon( Relation r ) {
69 if( r == null )
70 return false;
71 String type = r.get("type");
72 if( type == null )
73 return false;
74 for( String t : MULTIPOLYGON_TYPES )
75 if( t.equals(type) )
76 return true;
77 return false;
78 }
79
80 public int getSegmentsCount() {
81 return 0;
82 }
83
84 public int getCirclesCount() {
85 return 0;
86 }
87
88 protected void analyse() {
89 // todo
90 }
91
92 public void addChosenRelationListener( ChosenRelationListener listener ) {
93 chosenRelationListeners.add(listener);
94 }
95
96 public void removeChosenRelationListener( ChosenRelationListener listener ) {
97 chosenRelationListeners.remove(listener);
98 }
99
100 public void editLayerChanged( OsmDataLayer oldLayer, OsmDataLayer newLayer ) {
101 // todo: dim chosen relation when changing layer
102 // todo: check this WTF!
103 clear();
104 if( newLayer != null && oldLayer == null ) {
105 Main.map.mapView.addTemporaryLayer(this);
106 } else if( oldLayer != null ) {
107 Main.map.mapView.removeTemporaryLayer(this);
108 }
109 }
110
111 public void paint( Graphics2D g, MapView mv, Bounds bbox ) {
112 if( chosenRelation == null ) {
113 return;
114 }
115
116 Stroke oldStroke = g.getStroke();
117 Composite oldComposite = g.getComposite();
118 g.setColor(Color.yellow);
119 g.setStroke(new BasicStroke(9, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
120 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f));
121 for( OsmPrimitive element : chosenRelation.getMemberPrimitives() ) {
122 if( element.getType() == OsmPrimitiveType.NODE ) {
123 Node node = (Node)element;
124 Point center = mv.getPoint(node);
125 g.drawOval(center.x - 4, center.y - 4, 9, 9);
126 } else if( element.getType() == OsmPrimitiveType.WAY ) {
127 Way way = (Way)element;
128 if( way.getNodesCount() >= 2 ) {
129 GeneralPath b = new GeneralPath();
130 Point p = mv.getPoint(way.getNode(0));
131 b.moveTo(p.x, p.y);
132 for( int i = 1; i < way.getNodesCount(); i++ ) {
133 p = mv.getPoint(way.getNode(i));
134 b.lineTo(p.x, p.y);
135 }
136 g.draw(b);
137 }
138 } else if( element.getType() == OsmPrimitiveType.RELATION ) {
139 // todo: draw all relation members (recursion?)
140 }
141 // todo: closedway, multipolygon - ?
142 }
143 g.setStroke(oldStroke);
144 g.setComposite(oldComposite);
145 }
146
147 public void relationMembersChanged( RelationMembersChangedEvent event ) {
148 if( chosenRelation != null && event.getRelation().equals(chosenRelation) )
149 fireRelationChanged(chosenRelation);
150 }
151
152 public void tagsChanged( TagsChangedEvent event ) {
153 if( chosenRelation != null && event.getPrimitive().equals(chosenRelation) )
154 fireRelationChanged(chosenRelation);
155 }
156
157 public void dataChanged( DataChangedEvent event ) {
158 if( chosenRelation != null )
159 fireRelationChanged(chosenRelation);
160 }
161
162 public void primtivesRemoved( PrimitivesRemovedEvent event ) {
163 if( chosenRelation != null && event.getPrimitives().contains(chosenRelation) )
164 clear();
165 }
166
167 public void wayNodesChanged( WayNodesChangedEvent event ) {
168 if( chosenRelation != null )
169 fireRelationChanged(chosenRelation); // download incomplete primitives doesn't cause dataChanged event
170 }
171
172 public void primtivesAdded( PrimitivesAddedEvent event ) {}
173 public void nodeMoved( NodeMovedEvent event ) {}
174 public void otherDatasetChange( AbstractDatasetChangedEvent event ) {}
175}
Note: See TracBrowser for help on using the repository browser.