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

Last change on this file since 29531 was 29531, checked in by donvip, 11 years ago

[josm_plugins] Do not use Main.map.mapView instead of mv parameter in paint() methods

File size: 6.4 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( newLayer == 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 OsmDataLayer dataLayer = mv.getEditLayer();
117 float opacity = dataLayer == null ? 0.0f : !dataLayer.isVisible() ? 0.0f : (float)dataLayer.getOpacity();
118 if( opacity < 0.01 )
119 return;
120
121 Composite oldComposite = g.getComposite();
122 Stroke oldStroke = g.getStroke();
123 g.setStroke(new BasicStroke(9, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
124 g.setColor(Color.yellow);
125 g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f * opacity));
126
127 drawRelations(g, mv, bbox, chosenRelation);
128
129 g.setComposite(oldComposite);
130 g.setStroke(oldStroke);
131
132 }
133 private void drawRelations(Graphics2D g, MapView mv, Bounds bbox, Relation rel) {
134 for( OsmPrimitive element : rel.getMemberPrimitives() ) {
135 if( element.getType() == OsmPrimitiveType.NODE ) {
136 Node node = (Node)element;
137 Point center = mv.getPoint(node);
138 g.drawOval(center.x - 4, center.y - 4, 9, 9);
139 } else if( element.getType() == OsmPrimitiveType.WAY ) {
140 Way way = (Way)element;
141 if( way.getNodesCount() >= 2 ) {
142 GeneralPath b = new GeneralPath();
143 Point p = mv.getPoint(way.getNode(0));
144 b.moveTo(p.x, p.y);
145 for( int i = 1; i < way.getNodesCount(); i++ ) {
146 p = mv.getPoint(way.getNode(i));
147 b.lineTo(p.x, p.y);
148 }
149 g.draw(b);
150 }
151 } else if( element.getType() == OsmPrimitiveType.RELATION ) {
152 Color oldColor = g.getColor();
153 g.setColor(Color.magenta);
154 drawRelations(g, mv, bbox, (Relation)element);
155 g.setColor(oldColor);
156 }
157 // todo: closedway, multipolygon - ?
158 }
159 }
160
161 public void relationMembersChanged( RelationMembersChangedEvent event ) {
162 if( chosenRelation != null && event.getRelation().equals(chosenRelation) )
163 fireRelationChanged(chosenRelation);
164 }
165
166 public void tagsChanged( TagsChangedEvent event ) {
167 if( chosenRelation != null && event.getPrimitive().equals(chosenRelation) )
168 fireRelationChanged(chosenRelation);
169 }
170
171 public void dataChanged( DataChangedEvent event ) {
172 if( chosenRelation != null )
173 fireRelationChanged(chosenRelation);
174 }
175
176 public void primitivesRemoved( PrimitivesRemovedEvent event ) {
177 if( chosenRelation != null && event.getPrimitives().contains(chosenRelation) )
178 clear();
179 }
180
181 public void wayNodesChanged( WayNodesChangedEvent event ) {
182 if( chosenRelation != null )
183 fireRelationChanged(chosenRelation); // download incomplete primitives doesn't cause dataChanged event
184 }
185
186 public void primitivesAdded( PrimitivesAddedEvent event ) {}
187 public void nodeMoved( NodeMovedEvent event ) {}
188 public void otherDatasetChange( AbstractDatasetChangedEvent event ) {}
189}
Note: See TracBrowser for help on using the repository browser.