1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Graphics2D;
|
---|
7 | import java.awt.Point;
|
---|
8 | import java.awt.geom.Point2D;
|
---|
9 |
|
---|
10 | import javax.swing.Action;
|
---|
11 | import javax.swing.Icon;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.Main;
|
---|
14 | import org.openstreetmap.josm.actions.RenameLayerAction;
|
---|
15 | import org.openstreetmap.josm.data.Bounds;
|
---|
16 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import org.openstreetmap.josm.data.osm.visitor.AbstractVisitor;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
|
---|
23 | import org.openstreetmap.josm.data.validation.util.ValUtil;
|
---|
24 | import org.openstreetmap.josm.gui.MapView;
|
---|
25 | import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
|
---|
26 | import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
|
---|
27 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
28 | import org.openstreetmap.josm.gui.preferences.ValidatorPreference;
|
---|
29 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * A debug layer for testing the grid cells a way crosses.
|
---|
33 | *
|
---|
34 | * @author frsantos
|
---|
35 | */
|
---|
36 | public class GridLayer extends Layer
|
---|
37 | {
|
---|
38 | /**
|
---|
39 | * Constructor
|
---|
40 | * @param name
|
---|
41 | */
|
---|
42 | public GridLayer(String name)
|
---|
43 | {
|
---|
44 | super(name);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Return a static icon.
|
---|
49 | */
|
---|
50 | @Override public Icon getIcon() {
|
---|
51 | return ImageProvider.get("layer", "validator");
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Draw the grid and highlight all cells acuppied by any selected primitive.
|
---|
56 | */
|
---|
57 | @Override
|
---|
58 | public void paint(final Graphics2D g, final MapView mv, Bounds bounds)
|
---|
59 | {
|
---|
60 | if( !Main.pref.hasKey(ValidatorPreference.PREF_DEBUG + ".grid") )
|
---|
61 | return;
|
---|
62 |
|
---|
63 | int gridWidth = Integer.parseInt(Main.pref.get(ValidatorPreference.PREF_DEBUG + ".grid") );
|
---|
64 | int width = mv.getWidth();
|
---|
65 | int height = mv.getHeight();
|
---|
66 |
|
---|
67 | EastNorth origin = mv.getEastNorth(0, 0);
|
---|
68 | EastNorth border = mv.getEastNorth(width, height);
|
---|
69 |
|
---|
70 | if( border.east() * gridWidth > 50 )
|
---|
71 | return;
|
---|
72 |
|
---|
73 | g.setColor(Color.RED.darker().darker());
|
---|
74 | HighlightCellVisitor visitor = new HighlightCellVisitor(g, mv, gridWidth);
|
---|
75 | for(OsmPrimitive p : Main.main.getCurrentDataSet().getSelected() )
|
---|
76 | p.visit(visitor);
|
---|
77 |
|
---|
78 | long x0 = (long)Math.floor(origin.east() * gridWidth);
|
---|
79 | long x1 = (long)Math.floor(border.east() * gridWidth);
|
---|
80 | long y0 = (long)Math.floor(origin.north() * gridWidth) + 1;
|
---|
81 | long y1 = (long)Math.floor(border.north() * gridWidth) + 1;
|
---|
82 | long aux;
|
---|
83 | if( x0 > x1 ) { aux = x0; x0 = x1; x1 = aux; }
|
---|
84 | if( y0 > y1 ) { aux = y0; y0 = y1; y1 = aux; }
|
---|
85 |
|
---|
86 | g.setColor(Color.RED.brighter().brighter());
|
---|
87 | for( double x = x0; x <= x1; x++)
|
---|
88 | {
|
---|
89 | Point point = mv.getPoint( new EastNorth(x/gridWidth, 0));
|
---|
90 | g.drawLine(point.x, 0, point.x, height);
|
---|
91 | }
|
---|
92 |
|
---|
93 | for( double y = y0; y <= y1; y++)
|
---|
94 | {
|
---|
95 | Point point = mv.getPoint( new EastNorth(0, y/gridWidth));
|
---|
96 | g.drawLine(0, point.y, width, point.y);
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | @Override
|
---|
101 | public String getToolTipText()
|
---|
102 | {
|
---|
103 | return null;
|
---|
104 | }
|
---|
105 |
|
---|
106 | @Override
|
---|
107 | public void mergeFrom(Layer from) {}
|
---|
108 |
|
---|
109 | @Override
|
---|
110 | public boolean isMergable(Layer other) {
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 |
|
---|
114 | @Override
|
---|
115 | public void visitBoundingBox(BoundingXYVisitor v) {}
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public Object getInfoComponent()
|
---|
119 | {
|
---|
120 | return getToolTipText();
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public Action[] getMenuEntries()
|
---|
125 | {
|
---|
126 | return new Action[] {
|
---|
127 | LayerListDialog.getInstance().createShowHideLayerAction(),
|
---|
128 | LayerListDialog.getInstance().createDeleteLayerAction(),
|
---|
129 | SeparatorLayerAction.INSTANCE,
|
---|
130 | new RenameLayerAction(null, this),
|
---|
131 | SeparatorLayerAction.INSTANCE,
|
---|
132 | new LayerListPopup.InfoAction(this)};
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override public void destroy() { }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Visitor that highlights all cells the selected primitives go through
|
---|
139 | */
|
---|
140 | static class HighlightCellVisitor extends AbstractVisitor
|
---|
141 | {
|
---|
142 | /** The MapView */
|
---|
143 | private final MapView mv;
|
---|
144 | /** The graphics */
|
---|
145 | private final Graphics g;
|
---|
146 | /** The grid width */
|
---|
147 | private final int gridDetail;
|
---|
148 | /** The width of a cell */
|
---|
149 | private int cellWidth;
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Constructor
|
---|
153 | * @param g the graphics
|
---|
154 | * @param mv The MapView
|
---|
155 | * @param gridDetail The grid detail
|
---|
156 | */
|
---|
157 | public HighlightCellVisitor(final Graphics g, final MapView mv, int gridDetail)
|
---|
158 | {
|
---|
159 | this.g = g;
|
---|
160 | this.mv = mv;
|
---|
161 | this.gridDetail = gridDetail;
|
---|
162 |
|
---|
163 | Point p = mv.getPoint( new EastNorth(0, 0) );
|
---|
164 | Point p2 = mv.getPoint( new EastNorth(1d/gridDetail, 1d/gridDetail) );
|
---|
165 | cellWidth = Math.abs(p2.x - p.x);
|
---|
166 | }
|
---|
167 |
|
---|
168 | public void visit(Node n)
|
---|
169 | {
|
---|
170 | double x = n.getEastNorth().east() * gridDetail;
|
---|
171 | double y = n.getEastNorth().north()* gridDetail + 1;
|
---|
172 |
|
---|
173 | drawCell( Math.floor(x), Math.floor(y) );
|
---|
174 | }
|
---|
175 |
|
---|
176 | public void visit(Way w)
|
---|
177 | {
|
---|
178 | Node lastN = null;
|
---|
179 | for (Node n : w.getNodes()) {
|
---|
180 | if (lastN == null) {
|
---|
181 | lastN = n;
|
---|
182 | continue;
|
---|
183 | }
|
---|
184 | for (Point2D p : ValUtil.getSegmentCells(lastN, n, gridDetail)) {
|
---|
185 | drawCell( p.getX(), p.getY() );
|
---|
186 | }
|
---|
187 | lastN = n;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | public void visit(Relation r) {}
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Draws a solid cell at the (x,y) location
|
---|
195 | * @param x
|
---|
196 | * @param y
|
---|
197 | */
|
---|
198 | protected void drawCell(double x, double y)
|
---|
199 | {
|
---|
200 | Point p = mv.getPoint( new EastNorth(x/gridDetail, y/gridDetail) );
|
---|
201 | g.fillRect(p.x, p.y, cellWidth, cellWidth);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|