Changeset 10540 in osm for applications/editors


Ignore:
Timestamp:
2008-09-07T18:44:33+02:00 (16 years ago)
Author:
stotz
Message:

Double clicking an tree entry now zooms to way(s)/node(s) (highlighted parts only)

Location:
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/TestError.java

    r10490 r10540  
    11package org.openstreetmap.josm.plugins.validator;
    22
    3 import java.awt.*;
     3import java.awt.Color;
     4import java.awt.Graphics;
     5import java.awt.Point;
    46import java.util.Collection;
    57import java.util.Collections;
     
    810
    911import org.openstreetmap.josm.command.Command;
    10 import org.openstreetmap.josm.data.osm.*;
     12import org.openstreetmap.josm.data.osm.Node;
     13import org.openstreetmap.josm.data.osm.OsmPrimitive;
     14import org.openstreetmap.josm.data.osm.Relation;
     15import org.openstreetmap.josm.data.osm.Way;
     16import org.openstreetmap.josm.data.osm.WaySegment;
    1117import org.openstreetmap.josm.data.osm.visitor.Visitor;
    1218import org.openstreetmap.josm.gui.MapView;
     
    1622 * @author frsantos
    1723 */
    18 public class TestError
    19 {
    20         /** is this error on the ignore list */
    21         private Boolean ignored = false;
    22         /** Severity */
    23         private Severity severity;
    24         /** The error message */
    25         private String message;
    26         /** Deeper error description */
    27         private String description;
    28         private String description_en;
    29         /** The affected primitives */
    30         private List<? extends OsmPrimitive> primitives;
    31         /** The primitives to be highlighted */
    32         private List<?> highlighted;
    33         /** The tester that raised this error */
    34         private Test tester;
    35         /** Internal code used by testers to classify errors */
    36         private int code;
    37         /** If this error is selected */
    38         private boolean selected;
    39 
    40         /**
    41          * Constructors
    42          * @param tester The tester
    43          * @param severity The severity of this error
    44          * @param message The error message
    45          * @param primitive The affected primitive
    46          * @param primitives The affected primitives
    47          * @param code The test error reference code
    48          */
    49         public TestError(Test tester, Severity severity, String message, String description, String description_en, int code,
    50                         List<? extends OsmPrimitive> primitives, List<?> highlighted) {
    51                 this.tester = tester;
    52                 this.severity = severity;
    53                 this.message = message;
    54                 this.description = description;
    55                 this.description_en = description_en;
    56                 this.primitives = primitives;
    57                 this.highlighted = highlighted;
    58                 this.code = code;
    59         }
    60         public TestError(Test tester, Severity severity, String message, int code, List<? extends OsmPrimitive> primitives, List<?> highlighted)
    61         {
    62                 this(tester, severity, message, null, null, code, primitives, highlighted);
    63         }
    64         public TestError(Test tester, Severity severity, String message, String description, String description_en,
    65         int code, List<? extends OsmPrimitive> primitives)
    66         {
    67                 this(tester, severity, message, description, description_en, code, primitives, primitives);
    68         }
    69         public TestError(Test tester, Severity severity, String message, int code, List<? extends OsmPrimitive> primitives)
    70         {
    71                 this(tester, severity, message, null, null, code, primitives, primitives);
    72         }
    73         public TestError(Test tester, Severity severity, String message, int code, OsmPrimitive primitive)
    74         {
    75                 this(tester, severity, message, null, null, code, Collections.singletonList(primitive), Collections.singletonList(primitive));
    76         }
    77         public TestError(Test tester, Severity severity, String message, String description,
    78         String description_en, int code, OsmPrimitive primitive)
    79         {
    80                 this(tester, severity, message, description, description_en, code, Collections.singletonList(primitive));
    81         }
    82 
    83         /**
    84          * Gets the error message
    85          * @return the error message
    86          */
    87         public String getMessage()
    88         {
    89                 return message;
    90         }
    91 
    92         /**
    93          * Gets the error message
    94          * @return the error description
    95          */
    96         public String getDescription()
    97         {
    98                 return description;
    99         }
    100 
    101         /**
    102          * Sets the error message
    103          * @param message The error message
    104          */
    105         public void setMessage(String message)
    106         {
    107                 this.message = message;
    108         }
    109 
    110         /**
    111          * Gets the list of primitives affected by this error
    112          * @return the list of primitives affected by this error
    113          */
    114         public List<? extends OsmPrimitive> getPrimitives()
    115         {
    116                 return primitives;
    117         }
    118 
    119         /**
    120          * Sets the list of primitives affected by this error
    121          * @param primitives the list of primitives affected by this error
    122          */
    123 
    124         public void setPrimitives(List<OsmPrimitive> primitives)
    125         {
    126                 this.primitives = primitives;
    127         }
    128 
    129         /**
    130          * Gets the severity of this error
    131          * @return the severity of this error
    132          */
    133         public Severity getSeverity()
    134         {
    135                 return severity;
    136         }
    137 
    138         /**
    139          * Sets the severity of this error
    140          * @param severity the severity of this error
    141          */
    142         public void setSeverity(Severity severity)
    143         {
    144                 this.severity = severity;
    145         }
    146 
    147         /**
    148          * Sets the ignore state for this error
    149          */
    150         public String getIgnoreState()
    151         {
    152                 Collection<String> strings = new TreeSet<String>();
    153                 String ignorestring = getIgnoreSubGroup();
    154                 for (OsmPrimitive o : primitives)
    155                 {
    156                         // ignore data not yet uploaded
    157                         if(o.id == 0)
    158                                 return null;
    159                         String type = "u";
    160                         if (o instanceof Way) type = "w";
    161                         else if (o instanceof Relation) type = "r";
    162                         else if (o instanceof Node) type = "n";
    163                         strings.add(type + "_" + o.id);
    164                 }
    165                 for (String o : strings)
    166                 {
    167                         ignorestring += ":" + o;
    168                 }
    169                 return ignorestring;
    170         }
    171 
    172         public String getIgnoreSubGroup()
    173         {
    174                 String ignorestring = getIgnoreGroup();
    175                 if(description_en != null)
    176                         ignorestring += "_"+description_en;
    177                 return ignorestring;
    178         }
    179 
    180         public String getIgnoreGroup()
    181         {
    182                 return Integer.toString(code);
    183         }
    184 
    185         public void setIgnored(boolean state)
    186         {
    187                 ignored = state;
    188         }
    189 
    190         public Boolean getIgnored()
    191         {
    192                 return ignored;
    193         }
    194 
    195         /**
    196          * Gets the tester that raised this error
    197          * @return the tester that raised this error
    198          */
    199         public Test getTester()
    200         {
    201                 return tester;
    202         }
    203 
    204         /**
    205          * Gets the code
    206          * @return the code
    207          */
    208         public int getCode()
    209         {
    210                 return code;
    211         }
    212 
    213         /**
    214          * Returns true if the error can be fixed automatically
    215          *
    216          * @return true if the error can be fixed
    217          */
    218         public boolean isFixable()
    219         {
    220                 return tester != null && tester.isFixable(this);
    221         }
    222 
    223         /**
    224          * Fixes the error with the appropiate command
    225          *
    226          * @return The command to fix the error
    227          */
    228         public Command getFix()
    229         {
    230                 if( tester == null )
    231                         return null;
    232 
    233                 return tester.fixError(this);
    234         }
    235 
    236         /**
    237          * Paints the error on affected primitives
    238          *
    239          * @param g The graphics
    240          * @param mv The MapView
    241          */
    242         public void paint(Graphics g, MapView mv)
    243         {
    244                 if(!ignored)
    245                 {
    246                         PaintVisitor v = new PaintVisitor(g, mv);
    247                         for (Object o : highlighted) {
    248                                 if (o instanceof OsmPrimitive)
    249                                         v.visit((OsmPrimitive) o);
    250                                 else if (o instanceof WaySegment)
    251                                         v.visit((WaySegment) o);
    252                         }
    253                 }
    254         }
    255 
    256         /**
    257          * Visitor that highlights the primitives affected by this error
    258          * @author frsantos
    259          */
    260         class PaintVisitor implements Visitor
    261         {
    262                 /** The graphics */
    263                 private final Graphics g;
    264                 /** The MapView */
    265                 private final MapView mv;
    266 
    267                 /**
    268                  * Constructor
    269                  * @param g The graphics
    270                  * @param mv The Mapview
    271                  */
    272                 public PaintVisitor(Graphics g, MapView mv)
    273                 {
    274                         this.g = g;
    275                         this.mv = mv;
    276                 }
    277 
    278                 public void visit(OsmPrimitive p) {
    279                         if (!p.deleted && !p.incomplete) {
    280                                 p.visit(this);
    281                         }
    282                 }
    283 
    284                 /**
    285                  * Draws a circle around the node
    286                  * @param n The node
    287                  * @param color The circle color
    288                  */
    289                 public void drawNode(Node n, Color color)
    290                 {
    291                         Point p = mv.getPoint(n.eastNorth);
    292                         g.setColor(color);
    293                         if( selected )
    294                         {
    295                                 g.fillOval(p.x-5, p.y-5, 10, 10);
    296                         }
    297                         else
    298                                 g.drawOval(p.x-5, p.y-5, 10, 10);
    299                 }
    300 
    301                 /**
    302                  * Draws a line around the segment
    303                  *
    304                  * @param s The segment
    305                  * @param color The color
    306                  */
    307                 public void drawSegment(Node n1, Node n2, Color color)
    308                 {
    309                         Point p1 = mv.getPoint(n1.eastNorth);
    310                         Point p2 = mv.getPoint(n2.eastNorth);
    311                         g.setColor(color);
    312 
    313                         double t = Math.atan2(p2.x-p1.x, p2.y-p1.y);
    314                         double cosT = Math.cos(t);
    315                         double sinT = Math.sin(t);
    316                         int deg = (int)Math.toDegrees(t);
    317                         if( selected )
    318                         {
    319                                 int[] x = new int[] {(int)(p1.x + 5*cosT), (int)(p2.x + 5*cosT), (int)(p2.x - 5*cosT), (int)(p1.x - 5*cosT)};
    320                                 int[] y = new int[] {(int)(p1.y - 5*sinT), (int)(p2.y - 5*sinT), (int)(p2.y + 5*sinT), (int)(p1.y + 5*sinT)};
    321                                 g.fillPolygon(x, y, 4);
    322                                 g.fillArc(p1.x-5, p1.y-5, 10, 10, deg, 180 );
    323                                 g.fillArc(p2.x-5, p2.y-5, 10, 10, deg, -180);
    324                         }
    325                         else
    326                         {
    327                                 g.drawLine((int)(p1.x + 5*cosT), (int)(p1.y - 5*sinT), (int)(p2.x + 5*cosT), (int)(p2.y - 5*sinT));
    328                                 g.drawLine((int)(p1.x - 5*cosT), (int)(p1.y + 5*sinT), (int)(p2.x - 5*cosT), (int)(p2.y + 5*sinT));
    329                                 g.drawArc(p1.x-5, p1.y-5, 10, 10, deg, 180 );
    330                                 g.drawArc(p2.x-5, p2.y-5, 10, 10, deg, -180);
    331                         }
    332                 }
    333 
    334 
    335                 /**
    336                  * Draw a small rectangle.
    337                  * White if selected (as always) or red otherwise.
    338                  *
    339                  * @param n The node to draw.
    340                  */
    341                 public void visit(Node n)
    342                 {
    343                         if( isNodeVisible(n) )
    344                                 drawNode(n, severity.getColor());
    345                 }
    346 
    347                 public void visit(Way w)
    348                 {
    349                         Node lastN = null;
    350                         for (Node n : w.nodes) {
    351                                 if (lastN == null) {
    352                                         lastN = n;
    353                                         continue;
    354                                 }
    355                                 if (isSegmentVisible(lastN, n))
    356                                 {
    357                                         drawSegment(lastN, n, severity.getColor());
    358                                 }
    359                                 lastN = n;
    360                         }
    361                 }
    362 
    363                 public void visit(WaySegment ws) {
    364                         if (ws.lowerIndex < 0 || ws.lowerIndex + 1 >= ws.way.nodes.size()) return;
    365                         Node a = ws.way.nodes.get(ws.lowerIndex),
    366                                  b = ws.way.nodes.get(ws.lowerIndex + 1);
    367                         if (isSegmentVisible(a, b)) {
    368                                 drawSegment(a, b, severity.getColor());
    369                         }
    370                 }
    371 
    372                 public void visit(Relation r)
    373                 {
    374                         /* No idea how to draw a relation. */
    375                 }
    376 
    377                 /**
    378                  * Checks if the given node is in the visible area.
    379                  * @param n The node to check for visibility
    380                  * @return true if the node is visible
    381                  */
    382                 protected boolean isNodeVisible(Node n) {
    383                         Point p = mv.getPoint(n.eastNorth);
    384                         return !((p.x < 0) || (p.y < 0) || (p.x > mv.getWidth()) || (p.y > mv.getHeight()));
    385                 }
    386 
    387                 /**
    388                  * Checks if the given segment is in the visible area.
    389                  * NOTE: This will return true for a small number of non-visible
    390                  *               segments.
    391                  * @param ls The segment to check
    392                  * @return true if the segment is visible
    393                  */
    394                 protected boolean isSegmentVisible(Node n1, Node n2) {
    395                         Point p1 = mv.getPoint(n1.eastNorth);
    396                         Point p2 = mv.getPoint(n2.eastNorth);
    397                         if ((p1.x < 0) && (p2.x < 0)) return false;
    398                         if ((p1.y < 0) && (p2.y < 0)) return false;
    399                         if ((p1.x > mv.getWidth()) && (p2.x > mv.getWidth())) return false;
    400                         if ((p1.y > mv.getHeight()) && (p2.y > mv.getHeight())) return false;
    401                         return true;
    402                 }
    403         }
    404 
    405         /**
    406          * Sets the selection flag of this error
    407          * @param selected if this error is selected
    408          */
    409         public void setSelected(boolean selected)
    410         {
    411                 this.selected = selected;
    412         }
     24public class TestError {
     25    /** is this error on the ignore list */
     26    private Boolean ignored = false;
     27    /** Severity */
     28    private Severity severity;
     29    /** The error message */
     30    private String message;
     31    /** Deeper error description */
     32    private String description;
     33    private String description_en;
     34    /** The affected primitives */
     35    private List<? extends OsmPrimitive> primitives;
     36    /** The primitives to be highlighted */
     37    private List<?> highlighted;
     38    /** The tester that raised this error */
     39    private Test tester;
     40    /** Internal code used by testers to classify errors */
     41    private int code;
     42    /** If this error is selected */
     43    private boolean selected;
     44
     45    /**
     46     * Constructors
     47     * @param tester The tester
     48     * @param severity The severity of this error
     49     * @param message The error message
     50     * @param primitive The affected primitive
     51     * @param primitives The affected primitives
     52     * @param code The test error reference code
     53     */
     54    public TestError(Test tester, Severity severity, String message, String description, String description_en,
     55            int code, List<? extends OsmPrimitive> primitives, List<?> highlighted) {
     56        this.tester = tester;
     57        this.severity = severity;
     58        this.message = message;
     59        this.description = description;
     60        this.description_en = description_en;
     61        this.primitives = primitives;
     62        this.highlighted = highlighted;
     63        this.code = code;
     64    }
     65
     66    public TestError(Test tester, Severity severity, String message, int code, List<? extends OsmPrimitive> primitives,
     67            List<?> highlighted) {
     68        this(tester, severity, message, null, null, code, primitives, highlighted);
     69    }
     70
     71    public TestError(Test tester, Severity severity, String message, String description, String description_en,
     72            int code, List<? extends OsmPrimitive> primitives) {
     73        this(tester, severity, message, description, description_en, code, primitives, primitives);
     74    }
     75
     76    public TestError(Test tester, Severity severity, String message, int code, List<? extends OsmPrimitive> primitives) {
     77        this(tester, severity, message, null, null, code, primitives, primitives);
     78    }
     79
     80    public TestError(Test tester, Severity severity, String message, int code, OsmPrimitive primitive) {
     81        this(tester, severity, message, null, null, code, Collections.singletonList(primitive), Collections
     82                .singletonList(primitive));
     83    }
     84
     85    public TestError(Test tester, Severity severity, String message, String description, String description_en,
     86            int code, OsmPrimitive primitive) {
     87        this(tester, severity, message, description, description_en, code, Collections.singletonList(primitive));
     88    }
     89
     90    /**
     91     * Gets the error message
     92     * @return the error message
     93     */
     94    public String getMessage() {
     95        return message;
     96    }
     97
     98    /**
     99     * Gets the error message
     100     * @return the error description
     101     */
     102    public String getDescription() {
     103        return description;
     104    }
     105
     106    /**
     107     * Sets the error message
     108     * @param message The error message
     109     */
     110    public void setMessage(String message) {
     111        this.message = message;
     112    }
     113
     114    /**
     115     * Gets the list of primitives affected by this error
     116     * @return the list of primitives affected by this error
     117     */
     118    public List<? extends OsmPrimitive> getPrimitives() {
     119        return primitives;
     120    }
     121
     122    /**
     123     * Sets the list of primitives affected by this error
     124     * @param primitives the list of primitives affected by this error
     125     */
     126
     127    public void setPrimitives(List<OsmPrimitive> primitives) {
     128        this.primitives = primitives;
     129    }
     130
     131    /**
     132     * Gets the severity of this error
     133     * @return the severity of this error
     134     */
     135    public Severity getSeverity() {
     136        return severity;
     137    }
     138
     139    /**
     140     * Sets the severity of this error
     141     * @param severity the severity of this error
     142     */
     143    public void setSeverity(Severity severity) {
     144        this.severity = severity;
     145    }
     146
     147    /**
     148     * Sets the ignore state for this error
     149     */
     150    public String getIgnoreState() {
     151        Collection<String> strings = new TreeSet<String>();
     152        String ignorestring = getIgnoreSubGroup();
     153        for (OsmPrimitive o : primitives) {
     154            // ignore data not yet uploaded
     155            if (o.id == 0)
     156                return null;
     157            String type = "u";
     158            if (o instanceof Way)
     159                type = "w";
     160            else if (o instanceof Relation)
     161                type = "r";
     162            else if (o instanceof Node)
     163                type = "n";
     164            strings.add(type + "_" + o.id);
     165        }
     166        for (String o : strings) {
     167            ignorestring += ":" + o;
     168        }
     169        return ignorestring;
     170    }
     171
     172    public String getIgnoreSubGroup() {
     173        String ignorestring = getIgnoreGroup();
     174        if (description_en != null)
     175            ignorestring += "_" + description_en;
     176        return ignorestring;
     177    }
     178
     179    public String getIgnoreGroup() {
     180        return Integer.toString(code);
     181    }
     182
     183    public void setIgnored(boolean state) {
     184        ignored = state;
     185    }
     186
     187    public Boolean getIgnored() {
     188        return ignored;
     189    }
     190
     191    /**
     192     * Gets the tester that raised this error
     193     * @return the tester that raised this error
     194     */
     195    public Test getTester() {
     196        return tester;
     197    }
     198
     199    /**
     200     * Gets the code
     201     * @return the code
     202     */
     203    public int getCode() {
     204        return code;
     205    }
     206
     207    /**
     208     * Returns true if the error can be fixed automatically
     209     *
     210     * @return true if the error can be fixed
     211     */
     212    public boolean isFixable() {
     213        return tester != null && tester.isFixable(this);
     214    }
     215
     216    /**
     217     * Fixes the error with the appropiate command
     218     *
     219     * @return The command to fix the error
     220     */
     221    public Command getFix() {
     222        if (tester == null)
     223            return null;
     224
     225        return tester.fixError(this);
     226    }
     227
     228    /**
     229     * Paints the error on affected primitives
     230     *
     231     * @param g The graphics
     232     * @param mv The MapView
     233     */
     234    public void paint(Graphics g, MapView mv) {
     235        if (!ignored) {
     236            PaintVisitor v = new PaintVisitor(g, mv);
     237            visitHighlighted(v);
     238        }
     239    }
     240
     241    public void visitHighlighted(ValidatorVisitor v) {
     242        for (Object o : highlighted) {
     243            if (o instanceof OsmPrimitive)
     244                v.visit((OsmPrimitive) o);
     245            else if (o instanceof WaySegment)
     246                v.visit((WaySegment) o);
     247        }
     248    }
     249
     250    /**
     251     * Visitor that highlights the primitives affected by this error
     252     * @author frsantos
     253     */
     254    class PaintVisitor implements ValidatorVisitor, Visitor {
     255        /** The graphics */
     256        private final Graphics g;
     257        /** The MapView */
     258        private final MapView mv;
     259
     260        /**
     261         * Constructor
     262         * @param g The graphics
     263         * @param mv The Mapview
     264         */
     265        public PaintVisitor(Graphics g, MapView mv) {
     266            this.g = g;
     267            this.mv = mv;
     268        }
     269
     270        public void visit(OsmPrimitive p) {
     271            if (!p.deleted && !p.incomplete) {
     272                p.visit(this);
     273            }
     274        }
     275
     276        /**
     277         * Draws a circle around the node
     278         * @param n The node
     279         * @param color The circle color
     280         */
     281        public void drawNode(Node n, Color color) {
     282            Point p = mv.getPoint(n.eastNorth);
     283            g.setColor(color);
     284            if (selected) {
     285                g.fillOval(p.x - 5, p.y - 5, 10, 10);
     286            } else
     287                g.drawOval(p.x - 5, p.y - 5, 10, 10);
     288        }
     289
     290        /**
     291         * Draws a line around the segment
     292         *
     293         * @param s The segment
     294         * @param color The color
     295         */
     296        public void drawSegment(Node n1, Node n2, Color color) {
     297            Point p1 = mv.getPoint(n1.eastNorth);
     298            Point p2 = mv.getPoint(n2.eastNorth);
     299            g.setColor(color);
     300
     301            double t = Math.atan2(p2.x - p1.x, p2.y - p1.y);
     302            double cosT = Math.cos(t);
     303            double sinT = Math.sin(t);
     304            int deg = (int) Math.toDegrees(t);
     305            if (selected) {
     306                int[] x = new int[] { (int) (p1.x + 5 * cosT), (int) (p2.x + 5 * cosT), (int) (p2.x - 5 * cosT),
     307                        (int) (p1.x - 5 * cosT) };
     308                int[] y = new int[] { (int) (p1.y - 5 * sinT), (int) (p2.y - 5 * sinT), (int) (p2.y + 5 * sinT),
     309                        (int) (p1.y + 5 * sinT) };
     310                g.fillPolygon(x, y, 4);
     311                g.fillArc(p1.x - 5, p1.y - 5, 10, 10, deg, 180);
     312                g.fillArc(p2.x - 5, p2.y - 5, 10, 10, deg, -180);
     313            } else {
     314                g.drawLine((int) (p1.x + 5 * cosT), (int) (p1.y - 5 * sinT), (int) (p2.x + 5 * cosT),
     315                        (int) (p2.y - 5 * sinT));
     316                g.drawLine((int) (p1.x - 5 * cosT), (int) (p1.y + 5 * sinT), (int) (p2.x - 5 * cosT),
     317                        (int) (p2.y + 5 * sinT));
     318                g.drawArc(p1.x - 5, p1.y - 5, 10, 10, deg, 180);
     319                g.drawArc(p2.x - 5, p2.y - 5, 10, 10, deg, -180);
     320            }
     321        }
     322
     323        /**
     324         * Draw a small rectangle.
     325         * White if selected (as always) or red otherwise.
     326         *
     327         * @param n The node to draw.
     328         */
     329        public void visit(Node n) {
     330            if (isNodeVisible(n))
     331                drawNode(n, severity.getColor());
     332        }
     333
     334        public void visit(Way w) {
     335            Node lastN = null;
     336            for (Node n : w.nodes) {
     337                if (lastN == null) {
     338                    lastN = n;
     339                    continue;
     340                }
     341                if (isSegmentVisible(lastN, n)) {
     342                    drawSegment(lastN, n, severity.getColor());
     343                }
     344                lastN = n;
     345            }
     346        }
     347
     348        public void visit(WaySegment ws) {
     349            if (ws.lowerIndex < 0 || ws.lowerIndex + 1 >= ws.way.nodes.size())
     350                return;
     351            Node a = ws.way.nodes.get(ws.lowerIndex), b = ws.way.nodes.get(ws.lowerIndex + 1);
     352            if (isSegmentVisible(a, b)) {
     353                drawSegment(a, b, severity.getColor());
     354            }
     355        }
     356
     357        public void visit(Relation r) {
     358            /* No idea how to draw a relation. */
     359        }
     360
     361        /**
     362         * Checks if the given node is in the visible area.
     363         * @param n The node to check for visibility
     364         * @return true if the node is visible
     365         */
     366        protected boolean isNodeVisible(Node n) {
     367            Point p = mv.getPoint(n.eastNorth);
     368            return !((p.x < 0) || (p.y < 0) || (p.x > mv.getWidth()) || (p.y > mv.getHeight()));
     369        }
     370
     371        /**
     372         * Checks if the given segment is in the visible area.
     373         * NOTE: This will return true for a small number of non-visible
     374         *               segments.
     375         * @param ls The segment to check
     376         * @return true if the segment is visible
     377         */
     378        protected boolean isSegmentVisible(Node n1, Node n2) {
     379            Point p1 = mv.getPoint(n1.eastNorth);
     380            Point p2 = mv.getPoint(n2.eastNorth);
     381            if ((p1.x < 0) && (p2.x < 0))
     382                return false;
     383            if ((p1.y < 0) && (p2.y < 0))
     384                return false;
     385            if ((p1.x > mv.getWidth()) && (p2.x > mv.getWidth()))
     386                return false;
     387            if ((p1.y > mv.getHeight()) && (p2.y > mv.getHeight()))
     388                return false;
     389            return true;
     390        }
     391    }
     392
     393    /**
     394     * Sets the selection flag of this error
     395     * @param selected if this error is selected
     396     */
     397    public void setSelected(boolean selected) {
     398        this.selected = selected;
     399    }
    413400}
  • applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/ValidatorDialog.java

    r10538 r10540  
    2626import org.openstreetmap.josm.Main;
    2727import org.openstreetmap.josm.command.Command;
    28 import org.openstreetmap.josm.data.coor.EastNorth;
    2928import org.openstreetmap.josm.data.osm.DataSet;
    30 import org.openstreetmap.josm.data.osm.Node;
    3129import org.openstreetmap.josm.data.osm.OsmPrimitive;
    32 import org.openstreetmap.josm.data.osm.Way;
     30import org.openstreetmap.josm.data.osm.WaySegment;
    3331import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
    3432import org.openstreetmap.josm.gui.SideButton;
     
    340338            if (isDblClick) {
    341339                Main.ds.setSelected(sel);
    342                 if (sel.size() == 1) {
    343                     // Center the view on the selected item
    344                     double scale = Main.map.mapView.getScale();
    345                     OsmPrimitive prim = sel.iterator().next();
    346                     BoundingXYVisitor box = new BoundingXYVisitor();
    347                     if (prim instanceof Way) {
    348                         Way way = (Way) prim;
    349                         way.visitNodes(box);
    350                         EastNorth center = new EastNorth(box.min.east() / 2 + box.max.east() / 2, box.min.north() / 2
    351                                 + box.max.north() / 2);
    352                         Main.map.mapView.zoomTo(center, scale);
    353                     } else if (prim instanceof Node) {
    354                         Node node = (Node) prim;
    355                         Main.map.mapView.zoomTo(node.eastNorth, scale);
    356                     }
     340            }
     341            TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
     342            if ((selPath != null) && (e.getClickCount() == 2)) {
     343                DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath
     344                        .getPathComponent(selPath.getPathCount() - 1);
     345                if (node.getUserObject() instanceof TestError) {
     346                    TestError testError = (TestError) node.getUserObject();
     347                    ValidatorBoundingXYVisitor box = new ValidatorBoundingXYVisitor();
     348                    testError.visitHighlighted(box);
     349                    if (box.max.equals(box.min))
     350                        Main.map.mapView.zoomTo(box.max, 0.00001);
     351                    else
     352                        Main.map.mapView.recalculateCenterScale(box);
    357353                }
    358354            }
     
    380376        }
    381377    }
     378
     379    public static class ValidatorBoundingXYVisitor extends BoundingXYVisitor implements ValidatorVisitor {
     380
     381        public void visit(OsmPrimitive p) {
     382            if (!p.deleted && !p.incomplete) {
     383                p.visit(this);
     384            }
     385        }
     386
     387        public void visit(WaySegment ws) {
     388            if (ws.lowerIndex < 0 || ws.lowerIndex + 1 >= ws.way.nodes.size())
     389                return;
     390            visit(ws.way.nodes.get(ws.lowerIndex));
     391            visit(ws.way.nodes.get(ws.lowerIndex + 1));
     392        }
     393    }
    382394}
Note: See TracChangeset for help on using the changeset viewer.