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

Last change on this file since 26290 was 26290, checked in by zverik, 15 years ago

implemented most TODO requests, fixed some bugs

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