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

Last change on this file since 26834 was 26299, checked in by stoecker, 13 years ago

i18n update, update to josm core cleanup

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