Ignore:
Timestamp:
2009-06-06T22:25:31+02:00 (16 years ago)
Author:
stoecker
Message:

lots of updates due to josm changes

Location:
applications/editors/josm/plugins/routing/src/com/innovant/josm
Files:
18 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/EdgeIterator.java

    r15106 r15707  
    33public interface EdgeIterator {
    44
    5         public boolean hasNext();
    6        
    7         public RoutingEdge next();
     5    public boolean hasNext();
     6   
     7    public RoutingEdge next();
    88
    99}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/PreferencesKeys.java

    r14423 r15707  
    2828
    2929public enum PreferencesKeys {
    30         KEY_ACTIVE_ROUTE_COLOR ("routing.active.route.color"),
    31         KEY_INACTIVE_ROUTE_COLOR ("routing.inactive.route.color"),
    32         KEY_ROUTE_WIDTH ("routing.route.width"),
    33         KEY_ROUTE_SELECT ("routing.route.select");
     30    KEY_ACTIVE_ROUTE_COLOR ("routing.active.route.color"),
     31    KEY_INACTIVE_ROUTE_COLOR ("routing.inactive.route.color"),
     32    KEY_ROUTE_WIDTH ("routing.route.width"),
     33    KEY_ROUTE_SELECT ("routing.route.select");
    3434
    35         public final String key;
    36         PreferencesKeys (String key) {
    37                 this.key=key;
    38         }
     35    public final String key;
     36    PreferencesKeys (String key) {
     37        this.key=key;
     38    }
    3939
    40         public String getKey() {return key;};
     40    public String getKey() {return key;};
    4141}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingEdge.java

    r15106 r15707  
    55public interface RoutingEdge {
    66
    7           public LatLon fromLatLon();
     7      public LatLon fromLatLon();
    88
    9           public LatLon toLatLon();
    10          
    11           public Object fromV();
     9      public LatLon toLatLon();
     10     
     11      public Object fromV();
    1212
    13           public Object toV();
     13      public Object toV();
    1414
    15           public double getLength();
    16          
    17           public void setLength(double length);
    18          
    19           public double getSpeed();
     15      public double getLength();
     16     
     17      public void setLength(double length);
     18     
     19      public double getSpeed();
    2020
    21           public void setSpeed(double speed);
    22          
    23           public boolean isOneway();
    24          
    25           public void setOneway(boolean isOneway);
     21      public void setSpeed(double speed);
     22     
     23      public boolean isOneway();
     24     
     25      public void setOneway(boolean isOneway);
    2626
    2727}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingGraph.java

    r15106 r15707  
    5252public class RoutingGraph {
    5353
    54         /**
     54    /**
    5555     * Routing Profile
    5656     */
    5757    private RoutingProfile routingProfile;
    5858
    59         /**
    60         * Diferent algorithms to apply to the graph.
    61         */
    62         public enum Algorithm {
    63                 ROUTING_ALG_DIJKSTRA, ROUTING_ALG_BELLMANFORD
    64         };
    65 
    66         /**
    67         * Search criteria for the route.
    68         */
    69         public enum RouteType {FASTEST,SHORTEST};
    70 
    71         /**
    72         *
    73         */
    74         private RouteType routeType;
     59    /**
     60    * Diferent algorithms to apply to the graph.
     61    */
     62    public enum Algorithm {
     63        ROUTING_ALG_DIJKSTRA, ROUTING_ALG_BELLMANFORD
     64    };
     65
     66    /**
     67    * Search criteria for the route.
     68    */
     69    public enum RouteType {FASTEST,SHORTEST};
     70
     71    /**
     72    *
     73    */
     74    private RouteType routeType;
    7575
    7676    /**
    7777     * Associated Osm DataSet
    7878     */
    79         private DataSet data;
    80 
    81         /**
    82         * Logger.
    83         */
    84         static Logger logger = Logger.getLogger(RoutingGraph.class);
    85 
    86         /**
    87         * Graph state
    88         * <code>true</code> Graph in memory.
    89         * <code>false</code> Graph not created.
    90         */
    91 //      public boolean graphState;
    92 
    93         /**
    94         * OSM Graph.
    95         */
    96 //      private DirectedWeightedMultigraph<Node, OsmEdge> graph;
    97 //      private WeightedMultigraph<Node, OsmEdge> graph;
    98         private Graph<Node, OsmEdge> graph;
    99         private RoutingGraphDelegator rgDelegator=null;
    100 
    101         /**
    102         * Speeds
    103         */
    104         private Map<String,Double> waySpeeds;
    105 
    106         /**
    107         * Default Constructor.
    108         */
    109         public RoutingGraph(DataSet data) {
    110 //              this.graphState = false;
    111                 this.graph = null;
    112 //              this.data = data;
    113                 routeType=RouteType.SHORTEST;
    114                 routingProfile=new RoutingProfile("default");
    115                 routingProfile.setOnewayUse(true); // Don't ignore oneways by default
    116                 this.setWaySpeeds(routingProfile.getWaySpeeds());
    117                 logger.debug("Created RoutingGraph");
    118         }
    119 
    120         /**
    121         * Create OSM graph for routing
    122         *
    123         * @return
    124         */
    125         public void createGraph() {
    126 
    127                 logger.debug("Creating Graph...");
    128                 graph = new DirectedWeightedMultigraph<Node, OsmEdge>(OsmEdge.class);
    129                 rgDelegator=new RoutingGraphDelegator(graph);
    130                 rgDelegator.setRouteType(this.routeType);
    131                 // iterate all ways and segments for all nodes:
    132                 for (Way way : data.ways) {
    133                         if (way != null && !way.deleted && this.isvalidWay(way)) {
    134                                 Node from = null;
    135                                 for (Node to : way.nodes) {
    136                                         // Ignore the node if deleted
    137                                         if (!to.deleted) {
    138                                                 graph.addVertex(to);
    139                                                 if (from != null) {
    140                                                         addEdge(way, from, to);
    141                                                         if (!isOneWay(way)){
    142                                                                 addEdge(way, to, from);}
    143                                                 }
    144                                                 from = to;
    145                                         }
    146                                 }
    147                         }
    148                 }
    149 //              graph.vertexSet().size();
    150                 logger.debug("End Create Graph");
    151                 logger.debug("Vertex: "+graph.vertexSet().size());
    152                 logger.debug("Edges: "+graph.edgeSet().size());
    153         }
    154 
    155         /**
    156         * Compute weight and add edge to the graph
    157         * @param way
    158         * @param from
    159         * @param to
    160         */
    161         private void addEdge(Way way,Node from, Node to) {
    162                 double length = from.coor.greatCircleDistance(to.coor);
    163                
    164                 OsmEdge edge = new OsmEdge(way, from, to);
    165                 edge.setSpeed(12.1);
    166                 graph.addEdge(from, to, edge);
    167                 // weight = getWeight(way);
    168                 double weight = getWeight(way, length);
    169                 setWeight(edge, length);
    170                 logger.debug("edge for way " + way.id
    171                                      + "(from node " + from.id + " to node "
    172                                      + to.id + ") has weight: " + weight);
    173                 //((GraphDelegator<Node,OsmEdge>) graph).setEdgeWeight(edge, weight);
    174                 ((DirectedWeightedMultigraph<Node,OsmEdge>)graph).setEdgeWeight(edge, weight);
    175         }
    176 
    177         /**
    178         * Set the weight for the given segment depending on the highway type
    179         * and the length of the segment. The higher the value, the less it is used
    180         * in routing.
    181         *
    182         * @param way
    183         *            the way.
    184         * @return
    185         */
    186         private void setWeight(OsmEdge osmedge, double length) {
    187                
    188                 osmedge.setLength(length);
    189                 if (this.waySpeeds.containsKey(osmedge.getWay().get("highway")))
    190                         osmedge.setSpeed(this.waySpeeds.get(osmedge.getWay().get("highway")));
    191                                        
    192         }
    193 
    194         /**
    195         * Returns the weight for the given segment depending on the highway type
    196         * and the length of the segment. The higher the value, the less it is used
    197         * in routing.
    198         *
    199         * @param way
    200         *            the way.
    201         * @return
    202         */
    203         private double getWeight(Way way, double length) {
    204                 // Default speed if no setting is found
    205                 double speed = 1;
    206 
    207                 switch (routeType) {
    208                 case SHORTEST:
    209                         // Same speed for all types of ways
    210                         if (this.waySpeeds.containsKey("residential"))
    211                                 speed=this.waySpeeds.get("residential");
    212                         break;
    213                 case FASTEST:
    214                         // Each type of way may have a different speed
    215                         if (this.waySpeeds.containsKey(way.get("highway")))
    216                                 speed=this.waySpeeds.get(way.get("highway"));
    217                         logger.debug("Speed="+speed);
    218                         break;
    219                 default:
    220                         break;
    221                 }
    222                 // Return the time spent to traverse the way
    223                 return length / speed;
    224         }
    225        
    226         /**
    227         * Check is One Way.
    228         *
    229         * @param way
    230         *            the way.
    231         * @return <code>true</code> is a one way. <code>false</code> is not a one
    232         *         way.
    233         */
    234         private boolean isOneWay(Way way) {
    235                 // FIXME: oneway=-1 is ignored for the moment!
    236                 return way.get("oneway") != null
    237                                 || "motorway".equals(way.get("highway"));
    238         }
    239 
    240         /**
    241         * Check if a Way is correct.
    242         *
    243         * @param way
    244         *            The way.
    245         * @return <code>true</code> is valid. <code>false</code> is not valid.
    246         */
    247         public boolean isvalidWay(Way way) {
    248                 if (!way.isTagged())
    249                         return false;
    250 
    251                 return way.get("highway") != null || way.get("junction") != null
    252                                 || way.get("service") != null;
    253 
    254         }
    255 
    256         public boolean isvalidNode(Node node) {
    257                 return true;
    258         }
    259 
    260         /**
    261         * Apply selected routing algorithm to the graph.
    262         *
    263         * @param nodes
    264         *            Nodes used to calculate path.
    265         * @param algorithm
    266         *            Algorithm used to compute the path,
    267         *            RoutingGraph.Algorithm.ROUTING_ALG_DIJKSTRA or
    268         *            RoutingGraph.Algorithm.ROUTING_ALG_BELLMANFORD
    269         * @return new path.
    270         */
    271         public List<OsmEdge> applyAlgorithm(List<Node> nodes, Algorithm algorithm) {
    272                 List<OsmEdge> path = new ArrayList<OsmEdge>();
    273                 Graph<Node,OsmEdge> g;
    274                 double totalWeight = 0;
    275 
    276                 if (graph == null)
    277                         this.createGraph();
    278                 logger.debug("apply algorithm between nodes ");
    279 
    280                 for (Node node : nodes) {
    281                         logger.debug(node.id);
    282                 }
    283                 logger.debug("-----------------------------------");
    284 
    285                 // Assign the graph or an undirected view of the graph to g,
    286                 // depending on whether oneway tags are used or not
    287                 if (routingProfile.isOnewayUsed())
    288                         g = graph;
    289                 else
    290                         g = new AsUndirectedGraph<Node, OsmEdge>((DirectedWeightedMultigraph<Node,OsmEdge>)graph);
    291                 //TODO: Problemas no tiene encuenta el tema de oneway.
    292                 switch (algorithm) {
    293                 case ROUTING_ALG_DIJKSTRA:
    294                         logger.debug("Using Dijkstra algorithm");
    295                         DijkstraShortestPath<Node, OsmEdge> routingk = null;
    296                         for (int index = 1; index < nodes.size(); ++index) {
    297                                 routingk = new DijkstraShortestPath<Node, OsmEdge>(rgDelegator, nodes
    298                                                 .get(index - 1), nodes.get(index));
    299                                 if (routingk.getPathEdgeList() == null) {
    300                                         logger.debug("no path found!");
    301                                         break;
    302                                 }
    303                                 path.addAll(routingk.getPathEdgeList());
    304                                 totalWeight += routingk.getPathLength();
    305                         }
    306                         break;
    307                 case ROUTING_ALG_BELLMANFORD:
    308                         logger.debug("Using Bellman Ford algorithm");
    309                         for (int index = 1; index < nodes.size(); ++index) {
    310                                 path = BellmanFordShortestPath.findPathBetween(rgDelegator, nodes
    311                                                 .get(index - 1), nodes.get(index));
    312                                 if (path == null) {
    313                                         logger.debug("no path found!");
    314                                         return null;
    315                                 }
    316                         }
    317                         break;
    318                 default:
    319                         logger.debug("Wrong algorithm");
    320                         break;
    321                 }
    322 
    323                 logger.debug("shortest path found: " + path + "\nweight: "
    324                                                 + totalWeight);
    325                 return path;
    326         }
    327 
    328         /**
    329         * Return the number of vertices.
    330         * @return the number of vertices.
    331         */
    332         public int getVertexCount(){
    333                 int value=0;
    334                 if (graph!=null) value=graph.vertexSet().size();
    335                 return value;
    336         }
    337 
    338         /**
    339         * Return the number of edges.
    340         * @return the number of edges.
    341         */
    342         public int getEdgeCount(){
    343                 int value=0;
    344                 if (graph!=null) value=graph.edgeSet().size();
    345                 return value;
    346         }
    347 
    348         /**
    349         * @param routeType the routeType to set
    350         */
    351         public void setTypeRoute(RouteType routetype) {
    352                 this.routeType = routetype;
    353                 this.rgDelegator.setRouteType(routetype);
    354         }
    355 
    356         /**
    357         * @return the routeType
    358         */
    359         public RouteType getTypeRoute() {
    360                 return routeType;
    361         }
    362 
    363         public Map<String, Double> getWaySpeeds() {
    364                 return waySpeeds;
    365         }
    366 
    367         public void setWaySpeeds(Map<String, Double> waySpeeds) {
    368                 this.waySpeeds = waySpeeds;
    369         }
    370 
    371         public void resetGraph() {
    372                 graph=null;
    373         }
    374 
    375         public RoutingProfile getRoutingProfile() {
    376                 return routingProfile;
    377         }
     79    private DataSet data;
     80
     81    /**
     82    * Logger.
     83    */
     84    static Logger logger = Logger.getLogger(RoutingGraph.class);
     85
     86    /**
     87    * Graph state
     88    * <code>true</code> Graph in memory.
     89    * <code>false</code> Graph not created.
     90    */
     91//  public boolean graphState;
     92
     93    /**
     94    * OSM Graph.
     95    */
     96//  private DirectedWeightedMultigraph<Node, OsmEdge> graph;
     97//  private WeightedMultigraph<Node, OsmEdge> graph;
     98    private Graph<Node, OsmEdge> graph;
     99    private RoutingGraphDelegator rgDelegator=null;
     100
     101    /**
     102    * Speeds
     103    */
     104    private Map<String,Double> waySpeeds;
     105
     106    /**
     107    * Default Constructor.
     108    */
     109    public RoutingGraph(DataSet data) {
     110//      this.graphState = false;
     111        this.graph = null;
     112        this.data = data;
     113        routeType=RouteType.SHORTEST;
     114        routingProfile=new RoutingProfile("default");
     115        routingProfile.setOnewayUse(true); // Don't ignore oneways by default
     116        this.setWaySpeeds(routingProfile.getWaySpeeds());
     117        logger.debug("Created RoutingGraph");
     118    }
     119
     120    /**
     121    * Create OSM graph for routing
     122    *
     123    * @return
     124    */
     125    public void createGraph() {
     126
     127        logger.debug("Creating Graph...");
     128        graph = new DirectedWeightedMultigraph<Node, OsmEdge>(OsmEdge.class);
     129        rgDelegator=new RoutingGraphDelegator(graph);
     130        rgDelegator.setRouteType(this.routeType);
     131        // iterate all ways and segments for all nodes:
     132        for (Way way : data.ways) {
     133            if (way != null && !way.deleted && this.isvalidWay(way)) {
     134                Node from = null;
     135                for (Node to : way.nodes) {
     136                    // Ignore the node if deleted
     137                    if (!to.deleted) {
     138                        graph.addVertex(to);
     139                        if (from != null) {
     140                            addEdge(way, from, to);
     141                            if (!isOneWay(way)){
     142                                addEdge(way, to, from);}
     143                        }
     144                        from = to;
     145                    }
     146                }
     147            }
     148        }
     149//      graph.vertexSet().size();
     150        logger.debug("End Create Graph");
     151        logger.debug("Vertex: "+graph.vertexSet().size());
     152        logger.debug("Edges: "+graph.edgeSet().size());
     153    }
     154
     155    /**
     156    * Compute weight and add edge to the graph
     157    * @param way
     158    * @param from
     159    * @param to
     160    */
     161    private void addEdge(Way way,Node from, Node to) {
     162        double length = from.coor.greatCircleDistance(to.coor);
     163
     164        OsmEdge edge = new OsmEdge(way, from, to);
     165        edge.setSpeed(12.1);
     166        graph.addEdge(from, to, edge);
     167        // weight = getWeight(way);
     168        double weight = getWeight(way, length);
     169        setWeight(edge, length);
     170        logger.debug("edge for way " + way.id
     171                     + "(from node " + from.id + " to node "
     172                     + to.id + ") has weight: " + weight);
     173        //((GraphDelegator<Node,OsmEdge>) graph).setEdgeWeight(edge, weight);
     174        ((DirectedWeightedMultigraph<Node,OsmEdge>)graph).setEdgeWeight(edge, weight);
     175    }
     176
     177    /**
     178    * Set the weight for the given segment depending on the highway type
     179    * and the length of the segment. The higher the value, the less it is used
     180    * in routing.
     181    *
     182    * @param way
     183    *            the way.
     184    * @return
     185    */
     186    private void setWeight(OsmEdge osmedge, double length) {
     187
     188        osmedge.setLength(length);
     189        if (this.waySpeeds.containsKey(osmedge.getWay().get("highway")))
     190            osmedge.setSpeed(this.waySpeeds.get(osmedge.getWay().get("highway")));
     191
     192    }
     193
     194    /**
     195    * Returns the weight for the given segment depending on the highway type
     196    * and the length of the segment. The higher the value, the less it is used
     197    * in routing.
     198    *
     199    * @param way
     200    *            the way.
     201    * @return
     202    */
     203    private double getWeight(Way way, double length) {
     204        // Default speed if no setting is found
     205        double speed = 1;
     206
     207        switch (routeType) {
     208        case SHORTEST:
     209            // Same speed for all types of ways
     210            if (this.waySpeeds.containsKey("residential"))
     211                speed=this.waySpeeds.get("residential");
     212            break;
     213        case FASTEST:
     214            // Each type of way may have a different speed
     215            if (this.waySpeeds.containsKey(way.get("highway")))
     216                speed=this.waySpeeds.get(way.get("highway"));
     217            logger.debug("Speed="+speed);
     218            break;
     219        default:
     220            break;
     221        }
     222        // Return the time spent to traverse the way
     223        return length / speed;
     224    }
     225
     226    /**
     227    * Check is One Way.
     228    *
     229    * @param way
     230    *            the way.
     231    * @return <code>true</code> is a one way. <code>false</code> is not a one
     232    *         way.
     233    */
     234    private boolean isOneWay(Way way) {
     235        // FIXME: oneway=-1 is ignored for the moment!
     236        return way.get("oneway") != null
     237                || "motorway".equals(way.get("highway"));
     238    }
     239
     240    /**
     241    * Check if a Way is correct.
     242    *
     243    * @param way
     244    *            The way.
     245    * @return <code>true</code> is valid. <code>false</code> is not valid.
     246    */
     247    public boolean isvalidWay(Way way) {
     248        if (!way.isTagged())
     249            return false;
     250
     251        return way.get("highway") != null || way.get("junction") != null
     252                || way.get("service") != null;
     253
     254    }
     255
     256    public boolean isvalidNode(Node node) {
     257        return true;
     258    }
     259
     260    /**
     261    * Apply selected routing algorithm to the graph.
     262    *
     263    * @param nodes
     264    *            Nodes used to calculate path.
     265    * @param algorithm
     266    *            Algorithm used to compute the path,
     267    *            RoutingGraph.Algorithm.ROUTING_ALG_DIJKSTRA or
     268    *            RoutingGraph.Algorithm.ROUTING_ALG_BELLMANFORD
     269    * @return new path.
     270    */
     271    public List<OsmEdge> applyAlgorithm(List<Node> nodes, Algorithm algorithm) {
     272        List<OsmEdge> path = new ArrayList<OsmEdge>();
     273        Graph<Node,OsmEdge> g;
     274        double totalWeight = 0;
     275
     276        if (graph == null)
     277            this.createGraph();
     278        logger.debug("apply algorithm between nodes ");
     279
     280        for (Node node : nodes) {
     281            logger.debug(node.id);
     282        }
     283        logger.debug("-----------------------------------");
     284
     285        // Assign the graph or an undirected view of the graph to g,
     286        // depending on whether oneway tags are used or not
     287        if (routingProfile.isOnewayUsed())
     288            g = graph;
     289        else
     290            g = new AsUndirectedGraph<Node, OsmEdge>((DirectedWeightedMultigraph<Node,OsmEdge>)graph);
     291        //TODO: Problemas no tiene encuenta el tema de oneway.
     292        switch (algorithm) {
     293        case ROUTING_ALG_DIJKSTRA:
     294            logger.debug("Using Dijkstra algorithm");
     295            DijkstraShortestPath<Node, OsmEdge> routingk = null;
     296            for (int index = 1; index < nodes.size(); ++index) {
     297                routingk = new DijkstraShortestPath<Node, OsmEdge>(rgDelegator, nodes
     298                        .get(index - 1), nodes.get(index));
     299                if (routingk.getPathEdgeList() == null) {
     300                    logger.debug("no path found!");
     301                    break;
     302                }
     303                path.addAll(routingk.getPathEdgeList());
     304                totalWeight += routingk.getPathLength();
     305            }
     306            break;
     307        case ROUTING_ALG_BELLMANFORD:
     308            logger.debug("Using Bellman Ford algorithm");
     309            for (int index = 1; index < nodes.size(); ++index) {
     310                path = BellmanFordShortestPath.findPathBetween(rgDelegator, nodes
     311                        .get(index - 1), nodes.get(index));
     312                if (path == null) {
     313                    logger.debug("no path found!");
     314                    return null;
     315                }
     316            }
     317            break;
     318        default:
     319            logger.debug("Wrong algorithm");
     320            break;
     321        }
     322
     323        logger.debug("shortest path found: " + path + "\nweight: "
     324                        + totalWeight);
     325        return path;
     326    }
     327
     328    /**
     329    * Return the number of vertices.
     330    * @return the number of vertices.
     331    */
     332    public int getVertexCount(){
     333        int value=0;
     334        if (graph!=null) value=graph.vertexSet().size();
     335        return value;
     336    }
     337
     338    /**
     339    * Return the number of edges.
     340    * @return the number of edges.
     341    */
     342    public int getEdgeCount(){
     343        int value=0;
     344        if (graph!=null) value=graph.edgeSet().size();
     345        return value;
     346    }
     347
     348    /**
     349    * @param routeType the routeType to set
     350    */
     351    public void setTypeRoute(RouteType routetype) {
     352        this.routeType = routetype;
     353        this.rgDelegator.setRouteType(routetype);
     354    }
     355
     356    /**
     357    * @return the routeType
     358    */
     359    public RouteType getTypeRoute() {
     360        return routeType;
     361    }
     362
     363    public Map<String, Double> getWaySpeeds() {
     364        return waySpeeds;
     365    }
     366
     367    public void setWaySpeeds(Map<String, Double> waySpeeds) {
     368        this.waySpeeds = waySpeeds;
     369    }
     370
     371    public void resetGraph() {
     372        graph=null;
     373    }
     374
     375    public RoutingProfile getRoutingProfile() {
     376        return routingProfile;
     377    }
    378378}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingGraphDelegator.java

    r14760 r15707  
    1919public class RoutingGraphDelegator extends GraphDelegator<Node, OsmEdge> {
    2020
    21         /**
    22         * Logger.
    23         */
    24         static Logger logger = Logger.getLogger(RoutingGraphDelegator.class);
    25        
    26         /**
    27         *
    28         */
    29         private RouteType routeType;
    30        
    31         public RoutingGraphDelegator(Graph<Node, OsmEdge> arg0) {
    32                 super(arg0);
    33         }
    34        
     21    /**
     22    * Logger.
     23    */
     24    static Logger logger = Logger.getLogger(RoutingGraphDelegator.class);
     25   
     26    /**
     27    *
     28    */
     29    private RouteType routeType;
     30   
     31    public RoutingGraphDelegator(Graph<Node, OsmEdge> arg0) {
     32        super(arg0);
     33    }
     34   
    3535
    36         public RouteType getRouteType() {
    37                 return routeType;
    38         }
     36    public RouteType getRouteType() {
     37        return routeType;
     38    }
    3939
    40         public void setRouteType(RouteType routeType) {
    41                 this.routeType = routeType;
    42         }
     40    public void setRouteType(RouteType routeType) {
     41        this.routeType = routeType;
     42    }
    4343
    4444
    45         /**
    46         *
    47         */
    48         private static final long serialVersionUID = 1L;
     45    /**
     46    *
     47    */
     48    private static final long serialVersionUID = 1L;
    4949
    50         @Override
    51         public double getEdgeWeight(OsmEdge edge) {
    52                 double weight=Double.MAX_VALUE;
    53                
    54                 if (routeType==RouteType.SHORTEST) weight=edge.getLength();
    55                 if (routeType==RouteType.FASTEST) weight=edge.getLength() / edge.getSpeed();
    56                 // Return the time spent to traverse the way
    57                 return weight;
    58         }
     50    @Override
     51    public double getEdgeWeight(OsmEdge edge) {
     52        double weight=Double.MAX_VALUE;
     53       
     54        if (routeType==RouteType.SHORTEST) weight=edge.getLength();
     55        if (routeType==RouteType.FASTEST) weight=edge.getLength() / edge.getSpeed();
     56        // Return the time spent to traverse the way
     57        return weight;
     58    }
    5959
    6060}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingProfile.java

    r14287 r15707  
    2929 */
    3030public class RoutingProfile {
    31         /**
    32         * logger
    33         */
    34         static Logger logger = Logger.getLogger(RoutingProfile.class);
    35         /**
    36         * True if oneway is used for routing (i.e. for cars).
    37         */
    38         private boolean useOneway;
     31    /**
     32    * logger
     33    */
     34    static Logger logger = Logger.getLogger(RoutingProfile.class);
     35    /**
     36    * True if oneway is used for routing (i.e. for cars).
     37    */
     38    private boolean useOneway;
    3939
    40         /**
    41         * True if turn restrictions are used for routing (i.e. for cars).
    42         */
    43         private boolean useRestrictions;
     40    /**
     41    * True if turn restrictions are used for routing (i.e. for cars).
     42    */
     43    private boolean useRestrictions;
    4444
    45         /**
    46         * True if maximum allowed speed of ways is considered for routing (i.e. for cars).
    47         */
    48         private boolean useMaxAllowedSpeed;
     45    /**
     46    * True if maximum allowed speed of ways is considered for routing (i.e. for cars).
     47    */
     48    private boolean useMaxAllowedSpeed;
    4949
    50         /**
    51         * Name of the routing profile, for identification issues (i.e. "pedestrian").
    52         */
    53         private String name;
     50    /**
     51    * Name of the routing profile, for identification issues (i.e. "pedestrian").
     52    */
     53    private String name;
    5454
    55         /**
    56         * Holds traverse speed for each type of way, using the type as key.
    57         * A speed of zero means that this type of way cannot be traversed.
    58         */
    59         private Map<String,Double> waySpeeds;
     55    /**
     56    * Holds traverse speed for each type of way, using the type as key.
     57    * A speed of zero means that this type of way cannot be traversed.
     58    */
     59    private Map<String,Double> waySpeeds;
    6060
    6161
    6262
    63         /**
    64         * Holds permission of use for each type of transport mode, using the mode as key.
    65         */
    66         private Map<String,Boolean> allowedModes;
     63    /**
     64    * Holds permission of use for each type of transport mode, using the mode as key.
     65    */
     66    private Map<String,Boolean> allowedModes;
    6767
    68         /**
    69         * Constructor
    70         * @param name The name for the routing profile. Please use a name that is
    71         * self descriptive, i.e., something that an application user would
    72         * understand (like "pedestrian", "motorbike", "bicycle", etc.)
    73         */
    74         public RoutingProfile(String name) {
    75                 logger.debug("Init RoutingProfile with name: "+name);
    76                 this.name = name;
    77                 waySpeeds=new HashMap<String,Double>();
    78                 Map<String,String> prefs=Main.pref.getAllPrefix("routing.profile."+name+".speed");
    79                 for(String key:prefs.keySet()){
    80                         waySpeeds.put((key.split("\\.")[4]), Double.valueOf(prefs.get(key)));
    81                 }
    82                 for (String key:waySpeeds.keySet())
    83                         logger.debug(key+ "-- speed: "+waySpeeds.get(key));
    84                 logger.debug("End init RoutingProfile with name: "+name);
    85         }
     68    /**
     69    * Constructor
     70    * @param name The name for the routing profile. Please use a name that is
     71    * self descriptive, i.e., something that an application user would
     72    * understand (like "pedestrian", "motorbike", "bicycle", etc.)
     73    */
     74    public RoutingProfile(String name) {
     75        logger.debug("Init RoutingProfile with name: "+name);
     76        this.name = name;
     77        waySpeeds=new HashMap<String,Double>();
     78        Map<String,String> prefs=Main.pref.getAllPrefix("routing.profile."+name+".speed");
     79        for(String key:prefs.keySet()){
     80            waySpeeds.put((key.split("\\.")[4]), Double.valueOf(prefs.get(key)));
     81        }
     82        for (String key:waySpeeds.keySet())
     83            logger.debug(key+ "-- speed: "+waySpeeds.get(key));
     84        logger.debug("End init RoutingProfile with name: "+name);
     85    }
    8686
    87         public void setName(String name) {
    88                 this.name = name;
    89         }
     87    public void setName(String name) {
     88        this.name = name;
     89    }
    9090
    91         public String getName() {
    92                 return name;
    93         }
     91    public String getName() {
     92        return name;
     93    }
    9494
    95         public void setOnewayUse(boolean useOneway) {
    96                 this.useOneway = useOneway;
    97         }
     95    public void setOnewayUse(boolean useOneway) {
     96        this.useOneway = useOneway;
     97    }
    9898
    99         public boolean isOnewayUsed() {
    100                 return useOneway;
    101         }
     99    public boolean isOnewayUsed() {
     100        return useOneway;
     101    }
    102102
    103         public void setRestrictionsUse(boolean useRestrictions) {
    104                 this.useRestrictions = useRestrictions;
    105         }
     103    public void setRestrictionsUse(boolean useRestrictions) {
     104        this.useRestrictions = useRestrictions;
     105    }
    106106
    107         public boolean isRestrictionsUsed() {
    108                 return useRestrictions;
    109         }
     107    public boolean isRestrictionsUsed() {
     108        return useRestrictions;
     109    }
    110110
    111         public void setMaxAllowedSpeedUse(boolean useMaxAllowedSpeed) {
    112                 this.useMaxAllowedSpeed = useMaxAllowedSpeed;
    113         }
     111    public void setMaxAllowedSpeedUse(boolean useMaxAllowedSpeed) {
     112        this.useMaxAllowedSpeed = useMaxAllowedSpeed;
     113    }
    114114
    115         public boolean isMaxAllowedSpeedUsed() {
    116                 return useMaxAllowedSpeed;
    117         }
     115    public boolean isMaxAllowedSpeedUsed() {
     116        return useMaxAllowedSpeed;
     117    }
    118118
    119         public void setWayTypeSpeed(String type, double speed) {
    120                 waySpeeds.put(type, speed);
    121         }
     119    public void setWayTypeSpeed(String type, double speed) {
     120        waySpeeds.put(type, speed);
     121    }
    122122
    123         public void setTransportModePermission(String mode, boolean permission) {
    124                 allowedModes.put(mode, permission);
    125         }
     123    public void setTransportModePermission(String mode, boolean permission) {
     124        allowedModes.put(mode, permission);
     125    }
    126126
    127         /**
    128         * Return whether the driving profile specifies that a particular type of way
    129         * can be traversed
    130         * @param type Key for the way type
    131         * @return True if the way type can be traversed
    132         */
    133         public boolean isWayTypeAllowed(String type) {
    134                 if (waySpeeds.get(type) != 0.0)
    135                         return true;
    136                 return false;
    137         }
     127    /**
     128    * Return whether the driving profile specifies that a particular type of way
     129    * can be traversed
     130    * @param type Key for the way type
     131    * @return True if the way type can be traversed
     132    */
     133    public boolean isWayTypeAllowed(String type) {
     134        if (waySpeeds.get(type) != 0.0)
     135            return true;
     136        return false;
     137    }
    138138
    139         /**
    140         * Return whether the driving profile specifies that a particular type of transport
    141         * mode can be used
    142         * @param mode Key for the way type
    143         * @return True if the way type can be traversed
    144         */
    145         public boolean isTransportModeAllowed(String mode) {
    146                 return allowedModes.get(mode);
    147         }
     139    /**
     140    * Return whether the driving profile specifies that a particular type of transport
     141    * mode can be used
     142    * @param mode Key for the way type
     143    * @return True if the way type can be traversed
     144    */
     145    public boolean isTransportModeAllowed(String mode) {
     146        return allowedModes.get(mode);
     147    }
    148148
    149         public double getSpeed(String key){
    150                 if(!waySpeeds.containsKey(key)) return 0.0;
    151                 return waySpeeds.get(key);
    152         }
     149    public double getSpeed(String key){
     150        if(!waySpeeds.containsKey(key)) return 0.0;
     151        return waySpeeds.get(key);
     152    }
    153153
    154         public Map<String, Double> getWaySpeeds() {
    155                 return waySpeeds;
    156         }
     154    public Map<String, Double> getWaySpeeds() {
     155        return waySpeeds;
     156    }
    157157
    158         public void setWaySpeeds(Map<String, Double> waySpeeds) {
    159                 this.waySpeeds = waySpeeds;
    160         }
     158    public void setWaySpeeds(Map<String, Double> waySpeeds) {
     159        this.waySpeeds = waySpeeds;
     160    }
    161161}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/gtfs/GTFSTransportModes.java

    r14287 r15707  
    99public class GTFSTransportModes {
    1010
    11         /**
    12         * 0 - Tram, Streetcar, Light rail. Any light rail or street level system within
    13         *     a metropolitan area.
    14         */
    15         public static final int TRAM = 0;
    16         public static final int STREETCAR = 0;
    17         public static final int LIGHT_RAIL = 0;
     11    /**
     12    * 0 - Tram, Streetcar, Light rail. Any light rail or street level system within
     13    *     a metropolitan area.
     14    */
     15    public static final int TRAM = 0;
     16    public static final int STREETCAR = 0;
     17    public static final int LIGHT_RAIL = 0;
    1818
    19         /**
    20         * 1 - Subway, Metro. Any underground rail system within a metropolitan area.
    21         */
    22         public static final int SUBWAY = 1;
    23         public static final int METRO = 1;
     19    /**
     20    * 1 - Subway, Metro. Any underground rail system within a metropolitan area.
     21    */
     22    public static final int SUBWAY = 1;
     23    public static final int METRO = 1;
    2424
    2525    /**
    2626     * 2 - Rail. Used for intercity or long-distance travel.
    2727     */
    28         public static final int RAIL = 2;
     28    public static final int RAIL = 2;
    2929
    3030    /**
    3131     * 3 - Bus. Used for short- and long-distance bus routes.
    3232     */
    33         public static final int BUS = 3;
     33    public static final int BUS = 3;
    3434
    3535    /**
    3636     * 4 - Ferry. Used for short- and long-distance boat service.
    3737     */
    38         public static final int FERRY = 4;
     38    public static final int FERRY = 4;
    3939
    40         /**
    41         * 5 - Cable car. Used for street-level cable cars where the cable runs beneath the car.
    42         */
    43         public static final int CABLE_CAR = 5;
     40    /**
     41    * 5 - Cable car. Used for street-level cable cars where the cable runs beneath the car.
     42    */
     43    public static final int CABLE_CAR = 5;
    4444
    45         /**
    46         * 6 - Gondola, Suspended cable car. Typically used for aerial cable cars where
    47         *     the car is suspended from the cable.
    48         */
    49         public static final int GONDOLA = 6;
    50         public static final int SUSPENDED_CABLE_CAR = 6;
     45    /**
     46    * 6 - Gondola, Suspended cable car. Typically used for aerial cable cars where
     47    *     the car is suspended from the cable.
     48    */
     49    public static final int GONDOLA = 6;
     50    public static final int SUSPENDED_CABLE_CAR = 6;
    5151
    5252    /**
    5353     * 7 - Funicular. Any rail system designed for steep inclines.
    5454     */
    55         public static final int FUNICULAR = 7;
     55    public static final int FUNICULAR = 7;
    5656
    5757}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/osm/OsmEdge.java

    r14760 r15707  
    6565   */
    6666  public OsmEdge(Way way, Node from, Node to) {
    67                 super();
    68                 this.way = way;
    69                 this.from = from;
    70                 this.to = to;
    71                 this.length = from.coor.greatCircleDistance(to.coor);
    72           }
     67        super();
     68        this.way = way;
     69        this.from = from;
     70        this.to = to;
     71        this.length = from.coor.greatCircleDistance(to.coor);
     72      }
    7373
    7474  /**
     
    9292   */
    9393  public double getLength() {
    94         return length;
     94    return length;
    9595  }
    9696 
    9797  public void setLength(double length) {
    98         this.length = length;
     98    this.length = length;
    9999}
    100100
    101101public double getSpeed() {
    102                 return speed;
     102        return speed;
    103103  }
    104104
    105105  public void setSpeed(double speed) {
    106                 this.speed = speed;
     106        this.speed = speed;
    107107  }
    108108}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/osm/OsmWayTypes.java

    r14287 r15707  
    3434 */
    3535public enum OsmWayTypes {
    36         MOTORWAY ("motorway",120),
    37         MOTORWAY_LINK ("motorway_link",120),
    38         TRUNK ("trunk",120),
    39         TRUNK_LINK ("trunk_link",120),
    40         PRIMARY  ("primary",100),
    41         PRIMARY_LINK ("primary_link",100),
    42         SECONDARY ("secondary",90),
    43         TERTIARY ("tertiary",90),
    44         UNCLASSIFIED ("unclassified",50),
    45         ROAD ("road",100),
     36    MOTORWAY ("motorway",120),
     37    MOTORWAY_LINK ("motorway_link",120),
     38    TRUNK ("trunk",120),
     39    TRUNK_LINK ("trunk_link",120),
     40    PRIMARY  ("primary",100),
     41    PRIMARY_LINK ("primary_link",100),
     42    SECONDARY ("secondary",90),
     43    TERTIARY ("tertiary",90),
     44    UNCLASSIFIED ("unclassified",50),
     45    ROAD ("road",100),
    4646    RESIDENTIAL ("residential",50),
    47         LIVING_STREET ("living_street",30),
    48         SERVICE ("service",30),
    49         TRACK ("track",50),
    50         PEDESTRIAN ("pedestrian",30),
    51         BUS_GUIDEWAY ("bus_guideway",50),
    52         PATH ("path",40),
    53         CYCLEWAY ("cycleway",40),
    54         FOOTWAY ("footway",20),
    55         BRIDLEWAY ("bridleway",40),
    56         BYWAY ("byway",50),
    57         STEPS ("steps",10);
     47    LIVING_STREET ("living_street",30),
     48    SERVICE ("service",30),
     49    TRACK ("track",50),
     50    PEDESTRIAN ("pedestrian",30),
     51    BUS_GUIDEWAY ("bus_guideway",50),
     52    PATH ("path",40),
     53    CYCLEWAY ("cycleway",40),
     54    FOOTWAY ("footway",20),
     55    BRIDLEWAY ("bridleway",40),
     56    BYWAY ("byway",50),
     57    STEPS ("steps",10);
    5858
    59         /**
    60         * Default Constructor
    61         * @param tag
    62         */
    63         OsmWayTypes(String tag,int speed) {
    64                 this.tag = tag;
    65                 this.speed = speed;
    66         }
     59    /**
     60    * Default Constructor
     61    * @param tag
     62    */
     63    OsmWayTypes(String tag,int speed) {
     64        this.tag = tag;
     65        this.speed = speed;
     66    }
    6767
    68         /**
    69         * Tag
    70         */
    71         private final String tag;
    72         private final int speed;
     68    /**
     69    * Tag
     70    */
     71    private final String tag;
     72    private final int speed;
    7373
    74         /**
    75         * @return
    76         */
    77         public String getTag() {return tag;};
    78         public int getSpeed() {return speed;};
     74    /**
     75    * @return
     76    */
     77    public String getTag() {return tag;};
     78    public int getSpeed() {return speed;};
    7979}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingLayer.java

    r14884 r15707  
    7070public class RoutingLayer extends Layer {
    7171
    72         /**
    73         * Logger
    74         */
    75         static Logger logger = Logger.getLogger(RoutingLayer.class);
     72    /**
     73    * Logger
     74    */
     75    static Logger logger = Logger.getLogger(RoutingLayer.class);
    7676
    7777    /**
     
    9999     * @param name Layer name.
    100100     */
    101         public RoutingLayer(String name, OsmDataLayer dataLayer) {
    102                 super(name);
    103                 logger.debug("Creating Routing Layer...");
     101    public RoutingLayer(String name, OsmDataLayer dataLayer) {
     102        super(name);
     103        logger.debug("Creating Routing Layer...");
    104104        if(startIcon == null) startIcon = ImageProvider.get("routing", "startflag");
    105105        if(middleIcon == null) middleIcon = ImageProvider.get("routing", "middleflag");
     
    108108        this.routingModel = new RoutingModel(dataLayer.data);
    109109        logger.debug("Routing Layer created.");
    110         }
    111 
    112         /**
    113         * Getter Routing Model.
    114         * @return the routingModel
    115         */
    116         public RoutingModel getRoutingModel() {
    117                 return this.routingModel;
    118         }
    119 
    120         /**
    121         * Gets associated data layer
    122         * @return OsmDataLayer associated to the RoutingLayer
    123         */
    124         public OsmDataLayer getDataLayer() {
    125                 return dataLayer;
    126         }
    127 
    128         /**
    129         * Gets nearest node belonging to a highway tagged way
    130         * @param p Point on the screen
    131         * @return The nearest highway node, in the range of the snap distance
    132         */
     110    }
     111
     112    /**
     113    * Getter Routing Model.
     114    * @return the routingModel
     115    */
     116    public RoutingModel getRoutingModel() {
     117        return this.routingModel;
     118    }
     119
     120    /**
     121    * Gets associated data layer
     122    * @return OsmDataLayer associated to the RoutingLayer
     123    */
     124    public OsmDataLayer getDataLayer() {
     125        return dataLayer;
     126    }
     127
     128    /**
     129    * Gets nearest node belonging to a highway tagged way
     130    * @param p Point on the screen
     131    * @return The nearest highway node, in the range of the snap distance
     132    */
    133133    public final Node getNearestHighwayNode(Point p) {
    134         Node nearest = null;
    135         double minDist = 0;
     134        Node nearest = null;
     135        double minDist = 0;
    136136        for (Way w : dataLayer.data.ways) {
    137137            if (w.deleted || w.incomplete || w.get("highway")==null) continue;
     
    142142                double dist = p.distanceSq(P);
    143143                if (dist < NavigatableComponent.snapDistance) {
    144                         if ((nearest == null) || (dist < minDist)) {
    145                                 nearest = n;
    146                                 minDist = dist;
    147                         }
     144                    if ((nearest == null) || (dist < minDist)) {
     145                        nearest = n;
     146                        minDist = dist;
     147                    }
    148148                }
    149149            }
     
    152152    }
    153153
    154         /*
    155         * (non-Javadoc)
    156         * @see org.openstreetmap.josm.gui.layer.Layer#getIcon()
    157         */
    158         @Override
    159         public Icon getIcon() {
     154    /*
     155    * (non-Javadoc)
     156    * @see org.openstreetmap.josm.gui.layer.Layer#getIcon()
     157    */
     158    @Override
     159    public Icon getIcon() {
    160160        Icon icon = ImageProvider.get("layer", "routing_small");
    161161        return icon;
    162         }
    163 
    164         /*
    165         * (non-Javadoc)
    166         * @see org.openstreetmap.josm.gui.layer.Layer#getInfoComponent()
    167         */
    168         @Override
    169         public Object getInfoComponent() {
    170                 String info = "<html>"
    171                                                 + "<body>"
    172                                                         +"Graph Vertex: "+this.routingModel.routingGraph.getVertexCount()+"<br/>"
    173                                                         +"Graph Edges: "+this.routingModel.routingGraph.getEdgeCount()+"<br/>"
    174                                                 + "</body>"
    175                                         + "</html>";
     162    }
     163
     164    /*
     165    * (non-Javadoc)
     166    * @see org.openstreetmap.josm.gui.layer.Layer#getInfoComponent()
     167    */
     168    @Override
     169    public Object getInfoComponent() {
     170        String info = "<html>"
     171                        + "<body>"
     172                            +"Graph Vertex: "+this.routingModel.routingGraph.getVertexCount()+"<br/>"
     173                            +"Graph Edges: "+this.routingModel.routingGraph.getEdgeCount()+"<br/>"
     174                        + "</body>"
     175                    + "</html>";
    176176        return info;
    177         }
    178 
    179         /*
    180         * (non-Javadoc)
    181         * @see org.openstreetmap.josm.gui.layer.Layer#getMenuEntries()
    182         */
    183         @Override
    184         public Component[] getMenuEntries() {
     177    }
     178
     179    /*
     180    * (non-Javadoc)
     181    * @see org.openstreetmap.josm.gui.layer.Layer#getMenuEntries()
     182    */
     183    @Override
     184    public Component[] getMenuEntries() {
    185185        Collection<Component> components = new ArrayList<Component>();
    186186        components.add(new JMenuItem(new LayerListDialog.ShowHideLayerAction(this)));
     
    188188        components.add(new JMenuItem(new LayerListDialog.DeleteLayerAction(this)));
    189189        components.add(new JSeparator());
    190         components.add(new JMenuItem(new RenameLayerAction(associatedFile, this)));
     190        components.add(new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)));
    191191        components.add(new JSeparator());
    192192        components.add(new JMenuItem(new LayerListPopup.InfoAction(this)));
    193193        return components.toArray(new Component[0]);
    194         }
    195 
    196         /*
    197         * (non-Javadoc)
    198         * @see org.openstreetmap.josm.gui.layer.Layer#getToolTipText()
    199         */
    200         @Override
    201         public String getToolTipText() {
    202                 String tooltip = this.routingModel.routingGraph.getVertexCount() + " vertices, "
    203                                 + this.routingModel.routingGraph.getEdgeCount() + " edges";
    204                 return tooltip;
    205         }
    206 
    207         /*
    208         * (non-Javadoc)
    209         * @see org.openstreetmap.josm.gui.layer.Layer#isMergable(org.openstreetmap.josm.gui.layer.Layer)
    210         */
    211         @Override
    212         public boolean isMergable(Layer other) {
    213                 return false;
    214         }
    215 
    216         /*
    217         * (non-Javadoc)
    218         * @see org.openstreetmap.josm.gui.layer.Layer#mergeFrom(org.openstreetmap.josm.gui.layer.Layer)
    219         */
    220         @Override
    221         public void mergeFrom(Layer from) {
    222                 // This layer is not mergable, so do nothing
    223         }
    224 
    225         /*
    226         * (non-Javadoc)
    227         * @see org.openstreetmap.josm.gui.layer.Layer#paint(java.awt.Graphics, org.openstreetmap.josm.gui.MapView)
    228         */
    229         @Override
    230         public void paint(Graphics g, MapView mv) {
    231                 boolean isActiveLayer = (mv.getActiveLayer().equals(this));
    232                 // Get routing nodes (start, middle, end)
     194    }
     195
     196    /*
     197    * (non-Javadoc)
     198    * @see org.openstreetmap.josm.gui.layer.Layer#getToolTipText()
     199    */
     200    @Override
     201    public String getToolTipText() {
     202        String tooltip = this.routingModel.routingGraph.getVertexCount() + " vertices, "
     203                + this.routingModel.routingGraph.getEdgeCount() + " edges";
     204        return tooltip;
     205    }
     206
     207    /*
     208    * (non-Javadoc)
     209    * @see org.openstreetmap.josm.gui.layer.Layer#isMergable(org.openstreetmap.josm.gui.layer.Layer)
     210    */
     211    @Override
     212    public boolean isMergable(Layer other) {
     213        return false;
     214    }
     215
     216    /*
     217    * (non-Javadoc)
     218    * @see org.openstreetmap.josm.gui.layer.Layer#mergeFrom(org.openstreetmap.josm.gui.layer.Layer)
     219    */
     220    @Override
     221    public void mergeFrom(Layer from) {
     222        // This layer is not mergable, so do nothing
     223    }
     224
     225    /*
     226    * (non-Javadoc)
     227    * @see org.openstreetmap.josm.gui.layer.Layer#paint(java.awt.Graphics, org.openstreetmap.josm.gui.MapView)
     228    */
     229    @Override
     230    public void paint(Graphics g, MapView mv) {
     231        boolean isActiveLayer = (mv.getActiveLayer().equals(this));
     232        // Get routing nodes (start, middle, end)
    233233        List<Node> nodes = routingModel.getSelectedNodes();
    234234        if(nodes == null || nodes.size() == 0) {
    235                 logger.debug("no nodes selected");
     235            logger.debug("no nodes selected");
    236236            return;
    237237        }
     
    241241        String colorString;
    242242        if (isActiveLayer) {
    243                 if (Main.pref.hasKey(PreferencesKeys.KEY_ACTIVE_ROUTE_COLOR.key))
    244                                 colorString = Main.pref.get(PreferencesKeys.KEY_ACTIVE_ROUTE_COLOR.key);
    245                 else {
    246                         colorString = ColorHelper.color2html(Color.RED);
    247                         Main.pref.put(PreferencesKeys.KEY_ACTIVE_ROUTE_COLOR.key, colorString);
    248                 }
     243            if (Main.pref.hasKey(PreferencesKeys.KEY_ACTIVE_ROUTE_COLOR.key))
     244                    colorString = Main.pref.get(PreferencesKeys.KEY_ACTIVE_ROUTE_COLOR.key);
     245            else {
     246                colorString = ColorHelper.color2html(Color.RED);
     247                Main.pref.put(PreferencesKeys.KEY_ACTIVE_ROUTE_COLOR.key, colorString);
     248            }
    249249        } else {
    250                 if (Main.pref.hasKey(PreferencesKeys.KEY_INACTIVE_ROUTE_COLOR.key))
    251                         colorString = Main.pref.get(PreferencesKeys.KEY_INACTIVE_ROUTE_COLOR.key);
    252                 else {
    253                         colorString = ColorHelper.color2html(Color.decode("#dd2222"));
    254                         Main.pref.put(PreferencesKeys.KEY_INACTIVE_ROUTE_COLOR.key, colorString);
    255                 }
     250            if (Main.pref.hasKey(PreferencesKeys.KEY_INACTIVE_ROUTE_COLOR.key))
     251                colorString = Main.pref.get(PreferencesKeys.KEY_INACTIVE_ROUTE_COLOR.key);
     252            else {
     253                colorString = ColorHelper.color2html(Color.decode("#dd2222"));
     254                Main.pref.put(PreferencesKeys.KEY_INACTIVE_ROUTE_COLOR.key, colorString);
     255            }
    256256        }
    257257        Color color = ColorHelper.html2color(colorString);
     
    277277        Point screen = mv.getPoint(node.eastNorth);
    278278        startIcon.paintIcon(mv, g, screen.x - startIcon.getIconWidth()/2,
    279                         screen.y - startIcon.getIconHeight());
     279                screen.y - startIcon.getIconHeight());
    280280
    281281        // paint middle icons
     
    284284            screen = mv.getPoint(node.eastNorth);
    285285            middleIcon.paintIcon(mv, g, screen.x - startIcon.getIconWidth()/2,
    286                         screen.y - middleIcon.getIconHeight());
     286                    screen.y - middleIcon.getIconHeight());
    287287        }
    288288        // paint end icon
     
    291291            screen = mv.getPoint(node.eastNorth);
    292292            endIcon.paintIcon(mv, g, screen.x - startIcon.getIconWidth()/2,
    293                         screen.y - endIcon.getIconHeight());
    294         }
    295         }
    296 
    297         /*
    298         * (non-Javadoc)
    299         * @see org.openstreetmap.josm.gui.layer.Layer#visitBoundingBox(org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor)
    300         */
    301         @Override
    302         public void visitBoundingBox(BoundingXYVisitor v) {
     293                    screen.y - endIcon.getIconHeight());
     294        }
     295    }
     296
     297    /*
     298    * (non-Javadoc)
     299    * @see org.openstreetmap.josm.gui.layer.Layer#visitBoundingBox(org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor)
     300    */
     301    @Override
     302    public void visitBoundingBox(BoundingXYVisitor v) {
    303303        for (Node node : routingModel.getSelectedNodes()) {
    304304            v.visit(node);
    305305        }
    306         }
    307 
    308         /*
    309         * (non-Javadoc)
    310         * @see org.openstreetmap.josm.gui.layer.Layer#destroy()
    311         */
    312         @Override
     306    }
     307
     308    /*
     309    * (non-Javadoc)
     310    * @see org.openstreetmap.josm.gui.layer.Layer#destroy()
     311    */
     312    @Override
    313313    public void destroy() {
    314                 routingModel.reset();
    315 //              layerAdded = false;
    316         }
     314        routingModel.reset();
     315//      layerAdded = false;
     316    }
    317317
    318318    /**
     
    320320     */
    321321    private void drawEdge(Graphics g, MapView mv, OsmEdge edge, Color col, int width,
    322                 boolean showDirection) {
     322            boolean showDirection) {
    323323        g.setColor(col);
    324324        Point from;
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingModel.java

    r14884 r15707  
    4848public class RoutingModel {
    4949
    50         /**
    51         * Logger
    52         */
    53         static Logger logger = Logger.getLogger(RoutingModel.class);
     50    /**
     51    * Logger
     52    */
     53    static Logger logger = Logger.getLogger(RoutingModel.class);
    5454
    5555    /**
    5656     * Graph to calculate route
    5757     */
    58         public RoutingGraph routingGraph=null;
     58    public RoutingGraph routingGraph=null;
    5959
    6060    /**
     
    7171     * Default Constructor.
    7272     */
    73         public RoutingModel(DataSet data) {
     73    public RoutingModel(DataSet data) {
    7474        nodes = new ArrayList<Node>();
     75System.out.println("gr " + data);
    7576        routingGraph = new RoutingGraph(data);
    76         }
     77    }
    7778
    7879    /**
     
    9899     */
    99100    public void removeNode(int index) {
    100         if (nodes.size()>index) {
    101                 nodes.remove(index);
    102                 this.changeNodes=true;
    103         }
     101        if (nodes.size()>index) {
     102            nodes.remove(index);
     103            this.changeNodes=true;
     104        }
    104105    }
    105106
     
    110111     */
    111112    public void insertNode(int index, Node node) {
    112         if (nodes.size()>=index) {
    113                 nodes.add(index, node);
    114                 this.changeNodes=true;
    115         }
     113        if (nodes.size()>=index) {
     114            nodes.add(index, node);
     115            this.changeNodes=true;
     116        }
    116117    }
    117118
     
    120121     */
    121122    public void reverseNodes() {
    122         List<Node> aux = new ArrayList<Node>();
    123         for (Node n : nodes) {
    124                 aux.add(0,n);
    125         }
    126         nodes = aux;
    127         this.changeNodes=true;
     123        List<Node> aux = new ArrayList<Node>();
     124        for (Node n : nodes) {
     125            aux.add(0,n);
     126        }
     127        nodes = aux;
     128        this.changeNodes=true;
    128129    }
    129130
     
    133134     */
    134135    public List<OsmEdge> getRouteEdges() {
    135         if (this.changeNodes || path==null)
    136         {
    137                 path=this.routingGraph.applyAlgorithm(nodes, Algorithm.ROUTING_ALG_DIJKSTRA);
    138                 this.changeNodes=false;
    139         }
    140         return path;
     136        if (this.changeNodes || path==null)
     137        {
     138            path=this.routingGraph.applyAlgorithm(nodes, Algorithm.ROUTING_ALG_DIJKSTRA);
     139            this.changeNodes=false;
     140        }
     141        return path;
    141142    }
    142143
     
    145146     */
    146147    public void setNodesChanged() {
    147         this.changeNodes = true;
     148        this.changeNodes = true;
    148149    }
    149150
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingPlugin.java

    r14884 r15707  
    2626 */
    2727
    28 
    2928package com.innovant.josm.plugin.routing;
    3029
     30import static org.openstreetmap.josm.tools.I18n.marktr;
    3131import static org.openstreetmap.josm.tools.I18n.tr;
    3232
     
    6060 */
    6161public class RoutingPlugin extends Plugin implements LayerChangeListener {
    62         /**
    63         * Logger
    64         */
    65         static Logger logger = Logger.getLogger(RoutingPlugin.class);
     62    /**
     63    * Logger
     64    */
     65    static Logger logger = Logger.getLogger(RoutingPlugin.class);
    6666
    6767    /**
     
    126126     * Default Constructor
    127127     */
    128         public RoutingPlugin() {
    129                 super();
    130                 plugin = this; // Assign reference to the plugin class
    131                 DOMConfigurator.configure("log4j.xml");
    132                 logger.debug("Loading routing plugin...");
    133                 preferenceSettings=new RoutingPreferenceDialog();
    134                 // Create side dialog
    135                 routingDialog = new RoutingDialog();
    136                 // Initialize layers list
    137                 layers = new ArrayList<RoutingLayer>();
     128    public RoutingPlugin() {
     129        super();
     130        plugin = this; // Assign reference to the plugin class
     131        DOMConfigurator.configure("log4j.xml");
     132        logger.debug("Loading routing plugin...");
     133        preferenceSettings=new RoutingPreferenceDialog();
     134        // Create side dialog
     135        routingDialog = new RoutingDialog();
     136        // Initialize layers list
     137        layers = new ArrayList<RoutingLayer>();
    138138        // Add menu
    139         menu = new RoutingMenu(tr("Routing"));
    140         Main.main.menu.add(menu);
     139        menu = new RoutingMenu(marktr("Routing"));
    141140        // Register this class as LayerChangeListener
    142141        Layer.listeners.add(this);
    143142        logger.debug("Finished loading plugin");
    144         }
    145 
    146         /**
    147         * Provides static access to the plugin instance, to enable access to the plugin methods
    148         * @return the instance of the plugin
    149         */
    150         public static RoutingPlugin getInstance() {
    151                 return plugin;
    152         }
    153 
    154         /**
    155         * Get the routing side dialog
    156         * @return The instance of the routing side dialog
    157         */
    158         public RoutingDialog getRoutingDialog() {
    159                 return routingDialog;
    160         }
    161 
    162         public void addLayer() {
    163                 OsmDataLayer osmLayer = Main.main.editLayer();
    164                 RoutingLayer layer = new RoutingLayer(tr("Routing") + " [" + osmLayer.name + "]", osmLayer);
    165                 layers.add(layer);
    166                 Main.main.addLayer(layer);
    167         }
    168 
    169         /*
    170         * (non-Javadoc)
    171         * @see org.openstreetmap.josm.plugins.Plugin#mapFrameInitialized(org.openstreetmap.josm.gui.MapFrame, org.openstreetmap.josm.gui.MapFrame)
    172         */
     143    }
     144
     145    /**
     146    * Provides static access to the plugin instance, to enable access to the plugin methods
     147    * @return the instance of the plugin
     148    */
     149    public static RoutingPlugin getInstance() {
     150        return plugin;
     151    }
     152
     153    /**
     154    * Get the routing side dialog
     155    * @return The instance of the routing side dialog
     156    */
     157    public RoutingDialog getRoutingDialog() {
     158        return routingDialog;
     159    }
     160
     161    public void addLayer() {
     162        OsmDataLayer osmLayer = Main.main.editLayer();
     163        RoutingLayer layer = new RoutingLayer(tr("Routing") + " [" + osmLayer.name + "]", osmLayer);
     164        layers.add(layer);
     165        Main.main.addLayer(layer);
     166    }
     167
     168    /*
     169    * (non-Javadoc)
     170    * @see org.openstreetmap.josm.plugins.Plugin#mapFrameInitialized(org.openstreetmap.josm.gui.MapFrame, org.openstreetmap.josm.gui.MapFrame)
     171    */
    173172    @Override
    174173    public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
    175174        if(newFrame != null) {
    176                 // Create plugin map modes
    177                 addRouteNodeAction = new AddRouteNodeAction(newFrame);
    178                 removeRouteNodeAction = new RemoveRouteNodeAction(newFrame);
    179                 moveRouteNodeAction = new MoveRouteNodeAction(newFrame);
    180                 // Create plugin buttons and add them to the toolbar
    181                 addRouteNodeButton = new IconToggleButton(addRouteNodeAction);
    182                 removeRouteNodeButton = new IconToggleButton(removeRouteNodeAction);
    183                 moveRouteNodeButton = new IconToggleButton(moveRouteNodeAction);
     175            // Create plugin map modes
     176            addRouteNodeAction = new AddRouteNodeAction(newFrame);
     177            removeRouteNodeAction = new RemoveRouteNodeAction(newFrame);
     178            moveRouteNodeAction = new MoveRouteNodeAction(newFrame);
     179            // Create plugin buttons and add them to the toolbar
     180            addRouteNodeButton = new IconToggleButton(addRouteNodeAction);
     181            removeRouteNodeButton = new IconToggleButton(removeRouteNodeAction);
     182            moveRouteNodeButton = new IconToggleButton(moveRouteNodeAction);
    184183            newFrame.addMapMode(addRouteNodeButton);
    185184            newFrame.addMapMode(removeRouteNodeButton);
     
    189188            newFrame.toolGroup.add(moveRouteNodeButton);
    190189            // Hide them by default
    191                         addRouteNodeButton.setVisible(false);
    192                         removeRouteNodeButton.setVisible(false);
    193                         moveRouteNodeButton.setVisible(false);
    194                         // Enable menu
    195                         menu.enableStartItem();
     190            addRouteNodeButton.setVisible(false);
     191            removeRouteNodeButton.setVisible(false);
     192            moveRouteNodeButton.setVisible(false);
     193            // Enable menu
     194            menu.enableStartItem();
    196195            newFrame.addToggleDialog(routingDialog);
    197196        }
     
    202201     * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#activeLayerChange(org.openstreetmap.josm.gui.layer.Layer, org.openstreetmap.josm.gui.layer.Layer)
    203202     */
    204         public void activeLayerChange(Layer oldLayer, Layer newLayer) {
    205                 routingDialog.refresh();
    206         }
    207 
    208         /*
    209         * (non-Javadoc)
    210         * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerAdded(org.openstreetmap.josm.gui.layer.Layer)
    211         */
    212         public void layerAdded(Layer newLayer) {
    213                 // Add button(s) to the tool bar when the routing layer is added
    214                 if (newLayer instanceof RoutingLayer) {
    215                         addRouteNodeButton.setVisible(true);
    216                         removeRouteNodeButton.setVisible(true);
    217                         moveRouteNodeButton.setVisible(true);
    218                         menu.enableRestOfItems();
    219                         // Set layer on top and select layer, also refresh toggleDialog to reflect selection
    220                         Main.map.mapView.moveLayer(newLayer, 0);
    221                         logger.debug("Added routing layer.");
    222                 }
    223         }
    224 
    225         /*
    226         * (non-Javadoc)
    227         * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerRemoved(org.openstreetmap.josm.gui.layer.Layer)
    228         */
    229         public void layerRemoved(Layer oldLayer) {
    230                 if ((oldLayer instanceof RoutingLayer) & (layers.size()==1)) {
    231                         // Remove button(s) from the tool bar when the last routing layer is removed
    232                         addRouteNodeButton.setVisible(false);
    233                         removeRouteNodeButton.setVisible(false);
    234                         moveRouteNodeButton.setVisible(false);
    235                         menu.disableRestOfItems();
    236                         layers.remove(oldLayer);
    237                 logger.debug("Removed routing layer.");
    238                 } else if (oldLayer instanceof OsmDataLayer) {
    239                         // Remove all associated routing layers
    240                         // Convert to Array to prevent ConcurrentModificationException when removing layers from ArrayList
    241                         // FIXME: can't remove associated routing layers without triggering exceptions in some cases
    242                         RoutingLayer[] layersArray = layers.toArray(new RoutingLayer[0]);
    243                         for (int i=0;i<layersArray.length;i++) {
    244                                 if (layersArray[i].getDataLayer().equals(oldLayer)) {
    245                                         try {
    246                                                 // Remove layer
    247                                                 Main.map.mapView.removeLayer(layersArray[i]);
    248                                         } catch (IllegalArgumentException e) {
    249                                         }
    250                                 }
    251                         }
    252                 }
    253                 // Reload RoutingDialog table model
    254                 routingDialog.refresh();
    255         }
    256 
    257         /* (non-Javadoc)
    258         * @see org.openstreetmap.josm.plugins.Plugin#getPreferenceSetting()
    259         */
    260         @Override
    261         public PreferenceSetting getPreferenceSetting() {
    262                 return preferenceSettings;
    263         }
     203    public void activeLayerChange(Layer oldLayer, Layer newLayer) {
     204        routingDialog.refresh();
     205    }
     206
     207    /*
     208    * (non-Javadoc)
     209    * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerAdded(org.openstreetmap.josm.gui.layer.Layer)
     210    */
     211    public void layerAdded(Layer newLayer) {
     212        // Add button(s) to the tool bar when the routing layer is added
     213        if (newLayer instanceof RoutingLayer) {
     214            addRouteNodeButton.setVisible(true);
     215            removeRouteNodeButton.setVisible(true);
     216            moveRouteNodeButton.setVisible(true);
     217            menu.enableRestOfItems();
     218            // Set layer on top and select layer, also refresh toggleDialog to reflect selection
     219            Main.map.mapView.moveLayer(newLayer, 0);
     220            logger.debug("Added routing layer.");
     221        }
     222    }
     223
     224    /*
     225    * (non-Javadoc)
     226    * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerRemoved(org.openstreetmap.josm.gui.layer.Layer)
     227    */
     228    public void layerRemoved(Layer oldLayer) {
     229        if ((oldLayer instanceof RoutingLayer) & (layers.size()==1)) {
     230            // Remove button(s) from the tool bar when the last routing layer is removed
     231            addRouteNodeButton.setVisible(false);
     232            removeRouteNodeButton.setVisible(false);
     233            moveRouteNodeButton.setVisible(false);
     234            menu.disableRestOfItems();
     235            layers.remove(oldLayer);
     236            logger.debug("Removed routing layer.");
     237        } else if (oldLayer instanceof OsmDataLayer) {
     238            // Remove all associated routing layers
     239            // Convert to Array to prevent ConcurrentModificationException when removing layers from ArrayList
     240            // FIXME: can't remove associated routing layers without triggering exceptions in some cases
     241            RoutingLayer[] layersArray = layers.toArray(new RoutingLayer[0]);
     242            for (int i=0;i<layersArray.length;i++) {
     243                if (layersArray[i].getDataLayer().equals(oldLayer)) {
     244                    try {
     245                        // Remove layer
     246                        Main.map.mapView.removeLayer(layersArray[i]);
     247                    } catch (IllegalArgumentException e) {
     248                    }
     249                }
     250            }
     251        }
     252        // Reload RoutingDialog table model
     253        routingDialog.refresh();
     254    }
     255
     256    /* (non-Javadoc)
     257    * @see org.openstreetmap.josm.plugins.Plugin#getPreferenceSetting()
     258    */
     259    @Override
     260    public PreferenceSetting getPreferenceSetting() {
     261        return preferenceSettings;
     262    }
    264263}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/actions/AddRouteNodeAction.java

    r14423 r15707  
    5151 */
    5252public class AddRouteNodeAction extends MapMode {
    53         /**
    54         * Serial.
    55         */
    56         private static final long serialVersionUID = 1L;
    57         /**
    58         * Logger.
    59         */
    60         static Logger logger = Logger.getLogger(AddRouteNodeAction.class);
    61         /**
    62         * Routing Dialog.
    63         */
     53    /**
     54    * Serial.
     55    */
     56    private static final long serialVersionUID = 1L;
     57    /**
     58    * Logger.
     59    */
     60    static Logger logger = Logger.getLogger(AddRouteNodeAction.class);
     61    /**
     62    * Routing Dialog.
     63    */
    6464    private RoutingDialog routingDialog;
    6565
     
    6868     * @param mapFrame
    6969     */
    70         public AddRouteNodeAction(MapFrame mapFrame) {
    71                 // TODO Use constructor with shortcut
    72                 super(tr("Routing"), "add",
    73                                 tr("Click to add destination."),
    74                                 mapFrame, ImageProvider.getCursor("crosshair", null));
     70    public AddRouteNodeAction(MapFrame mapFrame) {
     71        // TODO Use constructor with shortcut
     72        super(tr("Routing"), "add",
     73                tr("Click to add destination."),
     74                mapFrame, ImageProvider.getCursor("crosshair", null));
    7575        this.routingDialog = RoutingPlugin.getInstance().getRoutingDialog();
    76         }
     76    }
    7777
    7878    @Override public void enterMode() {
     
    8989        // If left button is clicked
    9090        if (e.getButton() == MouseEvent.BUTTON1) {
    91                 // Search for nearest highway node
    92                 Node node = null;
    93                 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    94                         RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    95                         node = layer.getNearestHighwayNode(e.getPoint());
     91            // Search for nearest highway node
     92            Node node = null;
     93            if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     94                RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     95                node = layer.getNearestHighwayNode(e.getPoint());
    9696                if(node == null) {
    97                         logger.debug("no selected node");
     97                    logger.debug("no selected node");
    9898                    return;
    9999                }
     
    101101                layer.getRoutingModel().addNode(node);
    102102                routingDialog.addNode(node);
    103                 }
     103            }
    104104        }
    105105        Main.map.repaint();
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/actions/MoveRouteNodeAction.java

    r14423 r15707  
    5454 */
    5555public class MoveRouteNodeAction extends MapMode {
    56         /**
    57         * Serial.
    58         */
    59         private static final long serialVersionUID = 1L;
     56    /**
     57    * Serial.
     58    */
     59    private static final long serialVersionUID = 1L;
    6060
    61         /**
    62         * Square of the distance radius where route nodes can be selected for dragging
    63         */
    64         private static final int DRAG_SQR_RADIUS = 100;
     61    /**
     62    * Square of the distance radius where route nodes can be selected for dragging
     63    */
     64    private static final int DRAG_SQR_RADIUS = 100;
    6565
    66         /**
    67         * Logger.
    68         */
    69         static Logger logger = Logger.getLogger(RoutingLayer.class);
     66    /**
     67    * Logger.
     68    */
     69    static Logger logger = Logger.getLogger(RoutingLayer.class);
    7070
    71         /**
    72         * Routing Dialog.
    73         */
     71    /**
     72    * Routing Dialog.
     73    */
    7474    private RoutingDialog routingDialog;
    7575
     
    8383     * @param mapFrame
    8484     */
    85         public MoveRouteNodeAction(MapFrame mapFrame) {
    86                 // TODO Use constructor with shortcut
    87                 super(tr("Routing"), "move",
    88                                 tr("Click and drag to move destination"),
    89                                 mapFrame, ImageProvider.getCursor("normal", "move"));
     85    public MoveRouteNodeAction(MapFrame mapFrame) {
     86        // TODO Use constructor with shortcut
     87        super(tr("Routing"), "move",
     88                tr("Click and drag to move destination"),
     89                mapFrame, ImageProvider.getCursor("normal", "move"));
    9090        this.routingDialog = RoutingPlugin.getInstance().getRoutingDialog();
    91         }
     91    }
    9292
    9393    @Override public void enterMode() {
     
    104104        // If left button is pressed
    105105        if (e.getButton() == MouseEvent.BUTTON1) {
    106                 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    107                         RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    108                         RoutingModel routingModel = layer.getRoutingModel();
    109                 // Search for the nearest node in the list
    110                 List<Node> nl = routingModel.getSelectedNodes();
    111                 index = -1;
    112                 double dmax = DRAG_SQR_RADIUS; // maximum distance, in pixels
    113                 for (int i=0;i<nl.size();i++) {
    114                         Node node = nl.get(i);
    115                         double d = Main.map.mapView.getPoint(node.eastNorth).distanceSq(e.getPoint());
    116                         if (d < dmax) {
    117                                 dmax = d;
    118                                 index = i;
    119                         }
    120                 }
    121                 if (index>=0)
     106            if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     107                RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     108                RoutingModel routingModel = layer.getRoutingModel();
     109                // Search for the nearest node in the list
     110                List<Node> nl = routingModel.getSelectedNodes();
     111                index = -1;
     112                double dmax = DRAG_SQR_RADIUS; // maximum distance, in pixels
     113                for (int i=0;i<nl.size();i++) {
     114                    Node node = nl.get(i);
     115                    double d = Main.map.mapView.getPoint(node.eastNorth).distanceSq(e.getPoint());
     116                    if (d < dmax) {
     117                        dmax = d;
     118                        index = i;
     119                    }
     120                }
     121                if (index>=0)
    122122                    logger.debug("Moved from node " + nl.get(index));
    123                 }
     123            }
    124124        }
    125125    }
    126126
    127127    @Override public void mouseReleased(MouseEvent e) {
    128         // If left button is released and a route node is being dragged
    129         if ((e.getButton() == MouseEvent.BUTTON1) && (index>=0)) {
    130                 searchAndReplaceNode(e.getPoint());
     128        // If left button is released and a route node is being dragged
     129        if ((e.getButton() == MouseEvent.BUTTON1) && (index>=0)) {
     130            searchAndReplaceNode(e.getPoint());
    131131        }
    132132    }
     
    136136
    137137    private void searchAndReplaceNode(Point point) {
    138         if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    139                 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    140                 RoutingModel routingModel = layer.getRoutingModel();
    141                 // Search for nearest highway node
    142                 Node node = null;
    143                 node = layer.getNearestHighwayNode(point);
     138        if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     139            RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     140            RoutingModel routingModel = layer.getRoutingModel();
     141            // Search for nearest highway node
     142            Node node = null;
     143            node = layer.getNearestHighwayNode(point);
    144144            if (node == null) {
    145                 logger.debug("Didn't found a close node to move to.");
     145                logger.debug("Didn't found a close node to move to.");
    146146                return;
    147147            }
    148148            logger.debug("Moved to node " + node);
    149149            routingModel.removeNode(index);
    150                 routingDialog.removeNode(index);
     150            routingDialog.removeNode(index);
    151151            routingModel.insertNode(index, node);
    152                 routingDialog.insertNode(index, node);
     152            routingDialog.insertNode(index, node);
    153153            Main.map.repaint();
    154         }
     154        }
    155155    }
    156156}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/actions/RemoveRouteNodeAction.java

    r14423 r15707  
    5353 */
    5454public class RemoveRouteNodeAction extends MapMode {
    55         /**
    56         * Serial.
    57         */
    58         private static final long serialVersionUID = 1L;
     55    /**
     56    * Serial.
     57    */
     58    private static final long serialVersionUID = 1L;
    5959
    60         /**
    61         * Square of the distance radius where route nodes can be removed
    62         */
    63         private static final int REMOVE_SQR_RADIUS = 100;
     60    /**
     61    * Square of the distance radius where route nodes can be removed
     62    */
     63    private static final int REMOVE_SQR_RADIUS = 100;
    6464
    65         /**
    66         * Logger.
    67         */
    68         static Logger logger = Logger.getLogger(RoutingLayer.class);
    69         /**
    70         * Routing Dialog.
    71         */
     65    /**
     66    * Logger.
     67    */
     68    static Logger logger = Logger.getLogger(RoutingLayer.class);
     69    /**
     70    * Routing Dialog.
     71    */
    7272    private RoutingDialog routingDialog;
    7373
    74         public RemoveRouteNodeAction(MapFrame mapFrame) {
    75                 // TODO Use constructor with shortcut
    76                 super(tr("Routing"), "remove",
    77                                 tr("Click to remove destination"),
    78                                 mapFrame, ImageProvider.getCursor("normal", "delete"));
     74    public RemoveRouteNodeAction(MapFrame mapFrame) {
     75        // TODO Use constructor with shortcut
     76        super(tr("Routing"), "remove",
     77                tr("Click to remove destination"),
     78                mapFrame, ImageProvider.getCursor("normal", "delete"));
    7979        this.routingDialog = RoutingPlugin.getInstance().getRoutingDialog();
    80         }
     80    }
    8181
    8282    @Override public void enterMode() {
     
    9393        // If left button is clicked
    9494        if (e.getButton() == MouseEvent.BUTTON1) {
    95                 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    96                         RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    97                         RoutingModel routingModel = layer.getRoutingModel();
    98                 // Search for the nearest node in the list
    99                 List<Node> nl = routingModel.getSelectedNodes();
    100                 int index = -1;
    101                 double dmax = REMOVE_SQR_RADIUS; // maximum distance, in pixels
    102                 for (int i=0;i<nl.size();i++) {
    103                         Node node = nl.get(i);
    104                         double d = Main.map.mapView.getPoint(node.eastNorth).distanceSq(e.getPoint());
    105                         if (d < dmax) {
    106                                 dmax = d;
    107                                 index = i;
    108                         }
    109                 }
    110                 // If found a close node, remove it and recalculate route
     95            if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     96                RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     97                RoutingModel routingModel = layer.getRoutingModel();
     98                // Search for the nearest node in the list
     99                List<Node> nl = routingModel.getSelectedNodes();
     100                int index = -1;
     101                double dmax = REMOVE_SQR_RADIUS; // maximum distance, in pixels
     102                for (int i=0;i<nl.size();i++) {
     103                    Node node = nl.get(i);
     104                    double d = Main.map.mapView.getPoint(node.eastNorth).distanceSq(e.getPoint());
     105                    if (d < dmax) {
     106                        dmax = d;
     107                        index = i;
     108                    }
     109                }
     110                // If found a close node, remove it and recalculate route
    111111                if (index >= 0) {
    112                         // Remove node
    113                         logger.debug("Removing node " + nl.get(index));
     112                    // Remove node
     113                    logger.debug("Removing node " + nl.get(index));
    114114                    routingModel.removeNode(index);
    115                         routingDialog.removeNode(index);
     115                    routingDialog.removeNode(index);
    116116                    Main.map.repaint();
    117117                } else {
    118                         logger.debug("Can't find a node to remove.");
     118                    logger.debug("Can't find a node to remove.");
    119119                }
    120                 }
     120            }
    121121        }
    122122    }
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingDialog.java

    r14404 r15707  
    5656public class RoutingDialog extends ToggleDialog {
    5757
    58         private DefaultListModel model;
    59         private JList jList = null;
    60         private JScrollPane jScrollPane = null;
     58    private DefaultListModel model;
     59    private JList jList = null;
     60    private JScrollPane jScrollPane = null;
    6161
    62         /**
    63         * Serial UID
    64         */
    65         private static final long serialVersionUID = 8625615652900341987L;
     62    /**
     63    * Serial UID
     64    */
     65    private static final long serialVersionUID = 8625615652900341987L;
    6666
    67         public RoutingDialog() {
    68                 super(tr("Routing"), "routing", tr("Open a list of routing nodes"),
    69                                 Shortcut.registerShortcut("subwindow:relations", tr("Toggle: {0}", tr("Routing")), KeyEvent.VK_R, Shortcut.GROUP_LAYER), 150);
    70                 model= new DefaultListModel();
    71                 this.setSize(456, 292);
    72                 this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    73                 this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
    74                 this.setName("PrincipalDialog");
    75                 this.setFont(new Font("Dialog", Font.PLAIN, 12));
    76                 this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    77                 this.add(getJScrollPane(), null);
     67    public RoutingDialog() {
     68        super(tr("Routing"), "routing", tr("Open a list of routing nodes"),
     69                Shortcut.registerShortcut("subwindow:relations", tr("Toggle: {0}", tr("Routing")), KeyEvent.VK_R, Shortcut.GROUP_LAYER), 150);
     70        model= new DefaultListModel();
     71        this.setSize(456, 292);
     72        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
     73        this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
     74        this.setName("PrincipalDialog");
     75        this.setFont(new Font("Dialog", Font.PLAIN, 12));
     76        this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
     77        this.add(getJScrollPane(), null);
    7878
    79         }
     79    }
    8080
    81         /**
    82         * This method initializes jScrollPane
    83         *
    84         * @return javax.swing.JScrollPane
    85         */
    86         private JScrollPane getJScrollPane() {
    87                 if (jScrollPane == null) {
    88                         jScrollPane = new JScrollPane();
    89                         jScrollPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
    90                         jScrollPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    91                         jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    92                         jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    93                         jScrollPane.setName("nList");
    94                         jScrollPane.setViewportBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
    95                         jScrollPane.setViewportView(getJList());
    96                 }
    97                 return jScrollPane;
    98         }
     81    /**
     82    * This method initializes jScrollPane
     83    *
     84    * @return javax.swing.JScrollPane
     85    */
     86    private JScrollPane getJScrollPane() {
     87        if (jScrollPane == null) {
     88            jScrollPane = new JScrollPane();
     89            jScrollPane.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
     90            jScrollPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
     91            jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
     92            jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
     93            jScrollPane.setName("nList");
     94            jScrollPane.setViewportBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
     95            jScrollPane.setViewportView(getJList());
     96        }
     97        return jScrollPane;
     98    }
    9999
    100         /**
    101         * This method initializes jList
    102         *
    103         * @return javax.swing.JList
    104         */
    105         private JList getJList() {
    106                 if (jList == null) {
    107                         jList = new JList();
    108                         jList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    109                         jList.setModel(model);
    110                 }
    111                 return jList;
    112         }
     100    /**
     101    * This method initializes jList
     102    *
     103    * @return javax.swing.JList
     104    */
     105    private JList getJList() {
     106        if (jList == null) {
     107            jList = new JList();
     108            jList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
     109            jList.setModel(model);
     110        }
     111        return jList;
     112    }
    113113
    114         /**
    115         * Remove item from the list of nodes
    116         * @param index
    117         */
    118         public void removeNode(int index) {
    119                 model.remove(index);
    120         }
     114    /**
     115    * Remove item from the list of nodes
     116    * @param index
     117    */
     118    public void removeNode(int index) {
     119        model.remove(index);
     120    }
    121121
    122         /**
    123         * Add item to the list of nodes
    124         * @param obj
    125         */
    126         public void addNode(Node n) {
    127                 model.addElement(n.id+" ["+n.coor.toDisplayString()+"]");
    128         }
     122    /**
     123    * Add item to the list of nodes
     124    * @param obj
     125    */
     126    public void addNode(Node n) {
     127        model.addElement(n.id+" ["+n.coor.toDisplayString()+"]");
     128    }
    129129
    130         /**
    131         * Insert item to the list of nodes
    132         * @param index
    133         * @param obj
    134         */
    135         public void insertNode(int index, Node n) {
    136                 model.insertElementAt(n.id+" ["+n.coor.toDisplayString()+"]", index);
    137         }
     130    /**
     131    * Insert item to the list of nodes
     132    * @param index
     133    * @param obj
     134    */
     135    public void insertNode(int index, Node n) {
     136        model.insertElementAt(n.id+" ["+n.coor.toDisplayString()+"]", index);
     137    }
    138138
    139         /**
    140         * Clear list of nodes
    141         */
    142         public void clearNodes() {
    143                 model.clear();
    144         }
     139    /**
     140    * Clear list of nodes
     141    */
     142    public void clearNodes() {
     143        model.clear();
     144    }
    145145
    146         public void refresh() {
    147                 clearNodes();
    148         if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    149                 RoutingLayer routingLayer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    150                 RoutingModel routingModel = routingLayer.getRoutingModel();
    151                 for (Node n : routingModel.getSelectedNodes()) {
    152                         addNode(n);
    153                 }
    154         }
    155         }
     146    public void refresh() {
     147        clearNodes();
     148        if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     149            RoutingLayer routingLayer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     150            RoutingModel routingModel = routingLayer.getRoutingModel();
     151            for (Node n : routingModel.getSelectedNodes()) {
     152                addNode(n);
     153            }
     154        }
     155    }
    156156}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingMenu.java

    r14760 r15707  
    3434import java.awt.event.ItemEvent;
    3535import java.awt.event.ItemListener;
     36import java.awt.event.KeyEvent;
    3637
    3738import javax.swing.ButtonGroup;
     
    4243
    4344import org.openstreetmap.josm.Main;
     45import org.openstreetmap.josm.gui.MainMenu;
    4446
    4547import com.innovant.josm.jrt.core.RoutingGraph.RouteType;
     
    5557public class RoutingMenu extends JMenu {
    5658
    57         /**
    58         * Default serial version UID
    59         */
    60         private static final long serialVersionUID = 3559922048225708480L;
     59    /**
     60    * Default serial version UID
     61    */
     62    private static final long serialVersionUID = 3559922048225708480L;
    6163
    62         private JMenuItem startMI;
    63         private JMenuItem reverseMI;
    64         private JMenuItem clearMI;
    65         private JMenu criteriaM;
     64    private JMenuItem startMI;
     65    private JMenuItem reverseMI;
     66    private JMenuItem clearMI;
     67    private JMenu criteriaM;
     68    private JMenu menu;
    6669
    67         /**
    68          * @param s
    69          */
    70         public RoutingMenu(final String name) {
    71                 super(name);
     70    /**
     71     * @param s
     72     */
     73    public RoutingMenu(final String name) {
     74        MainMenu mm = Main.main.menu;
     75        menu = mm.addMenu(name, KeyEvent.VK_R, mm.defaultMenuPos);
    7276
    73                 JMenuItem mi;
    74                 JMenu m;
     77        startMI = new JMenuItem(tr("Add routing layer"));
     78        startMI.addActionListener(new ActionListener() {
     79            public void actionPerformed(ActionEvent e) {
     80                RoutingPlugin.getInstance().addLayer();
     81            }
     82        });
     83        menu.add(startMI);
    7584
    76                 startMI = new JMenuItem(tr("Add routing layer"));
    77                 startMI.addActionListener(new ActionListener() {
    78                         public void actionPerformed(ActionEvent e) {
    79                                 RoutingPlugin.getInstance().addLayer();
    80                         }
    81                 });
    82                 this.add(startMI);
     85        menu.addSeparator();
     86        ButtonGroup group = new ButtonGroup();
    8387
    84                 this.addSeparator();
    85                 ButtonGroup group = new ButtonGroup();
     88        criteriaM = new JMenu(tr("Criteria"));
    8689
    87                 criteriaM = new JMenu(tr("Criteria"));
     90        JRadioButtonMenuItem rshorter = new JRadioButtonMenuItem(tr("Shortest"));
     91        rshorter.setSelected(true);
     92        rshorter.addItemListener(new ItemListener() {
     93            public void itemStateChanged(ItemEvent e) {
     94                if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     95                    RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     96                    RoutingModel routingModel = layer.getRoutingModel();
     97                    if (e.getStateChange()==ItemEvent.SELECTED) {
     98                        routingModel.routingGraph.setTypeRoute(RouteType.SHORTEST);
     99                    } else {
     100                        routingModel.routingGraph.setTypeRoute(RouteType.FASTEST);
     101                    }
     102                //  routingModel.routingGraph.resetGraph();
     103                //  routingModel.routingGraph.createGraph();
     104                    //TODO: Change this way
     105                    //FIXME: do not change node but recalculate routing.
     106                    routingModel.setNodesChanged();
     107                    Main.map.repaint();
     108                }
     109            }
    88110
    89                 JRadioButtonMenuItem rshorter = new JRadioButtonMenuItem(tr("Shortest"));
    90                 rshorter.setSelected(true);
    91                 rshorter.addItemListener(new ItemListener() {
    92                         public void itemStateChanged(ItemEvent e) {
    93                         if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    94                                 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    95                                 RoutingModel routingModel = layer.getRoutingModel();
    96                                         if (e.getStateChange()==ItemEvent.SELECTED) {
    97                                                 routingModel.routingGraph.setTypeRoute(RouteType.SHORTEST);
    98                                         } else {
    99                                                 routingModel.routingGraph.setTypeRoute(RouteType.FASTEST);
    100                                         }
    101                                 //      routingModel.routingGraph.resetGraph();
    102                                 //      routingModel.routingGraph.createGraph();
    103                                         //TODO: Change this way
    104                                         //FIXME: do not change node but recalculate routing.
    105                                         routingModel.setNodesChanged();
    106                                         Main.map.repaint();
    107                         }
    108                         }
     111        });
    109112
    110                 });
     113        JRadioButtonMenuItem rfaster = new JRadioButtonMenuItem(tr("Fastest"));
     114        group.add(rshorter);
     115        group.add(rfaster);
     116        criteriaM.add(rshorter);
     117        criteriaM.add(rfaster);
    111118
    112                 JRadioButtonMenuItem rfaster = new JRadioButtonMenuItem(tr("Fastest"));
    113                 group.add(rshorter);
    114                 group.add(rfaster);
    115                 criteriaM.add(rshorter);
    116                 criteriaM.add(rfaster);
     119        criteriaM.addSeparator();
     120        JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Ignore oneways");
     121        cbmi.addItemListener(new ItemListener() {
     122            public void itemStateChanged(ItemEvent e) {
     123                if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     124                    RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     125                    RoutingModel routingModel = layer.getRoutingModel();
     126                    if (e.getStateChange()==ItemEvent.SELECTED)
     127                        routingModel.routingGraph.getRoutingProfile().setOnewayUse(false);
     128                    else
     129                        routingModel.routingGraph.getRoutingProfile().setOnewayUse(true);
     130                    routingModel.setNodesChanged();
     131                    Main.map.repaint();
     132                }
     133            }
     134        });
     135        criteriaM.add(cbmi);
     136        menu.add(criteriaM);
    117137
    118                 criteriaM.addSeparator();
    119                 JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem("Ignore oneways");
    120                 cbmi.addItemListener(new ItemListener() {
    121                         public void itemStateChanged(ItemEvent e) {
    122                         if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    123                                 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    124                                 RoutingModel routingModel = layer.getRoutingModel();
    125                                         if (e.getStateChange()==ItemEvent.SELECTED)
    126                                                 routingModel.routingGraph.getRoutingProfile().setOnewayUse(false);
    127                                         else
    128                                                 routingModel.routingGraph.getRoutingProfile().setOnewayUse(true);
    129                                         routingModel.setNodesChanged();
    130                                         Main.map.repaint();
    131                         }
    132                         }
    133                 });
    134                 criteriaM.add(cbmi);
    135                 this.add(criteriaM);
     138        menu.addSeparator();
     139        reverseMI = new JMenuItem(tr("Reverse route"));
     140        reverseMI.addActionListener(new ActionListener() {
     141            public void actionPerformed(ActionEvent e) {
     142                if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     143                    RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     144                    RoutingModel routingModel = layer.getRoutingModel();
     145                    routingModel.reverseNodes();
     146                    Main.map.repaint();
     147                }
     148            }
     149        });
     150        menu.add(reverseMI);
    136151
    137                 this.addSeparator();
    138                 reverseMI = new JMenuItem(tr("Reverse route"));
    139                 reverseMI.addActionListener(new ActionListener() {
    140                         public void actionPerformed(ActionEvent e) {
    141                         if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    142                                 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    143                                 RoutingModel routingModel = layer.getRoutingModel();
    144                                         routingModel.reverseNodes();
    145                                         Main.map.repaint();
    146                         }
    147                         }
    148                 });
    149                 this.add(reverseMI);
     152        clearMI = new JMenuItem(tr("Clear route"));
     153        clearMI.addActionListener(new ActionListener() {
     154            public void actionPerformed(ActionEvent e) {
     155                if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
     156                    RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
     157                    RoutingModel routingModel = layer.getRoutingModel();
     158                    // Reset routing nodes and paths
     159                    routingModel.reset();
     160                    RoutingPlugin.getInstance().getRoutingDialog().clearNodes();
     161                    Main.map.repaint();
     162                }
     163            }
     164        });
     165        menu.add(clearMI);
    150166
    151                 clearMI = new JMenuItem(tr("Clear route"));
    152                 clearMI.addActionListener(new ActionListener() {
    153                         public void actionPerformed(ActionEvent e) {
    154                         if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) {
    155                                 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer();
    156                                 RoutingModel routingModel = layer.getRoutingModel();
    157                                         // Reset routing nodes and paths
    158                                         routingModel.reset();
    159                                         RoutingPlugin.getInstance().getRoutingDialog().clearNodes();
    160                                         Main.map.repaint();
    161                         }
    162                         }
    163                 });
    164                 this.add(clearMI);
     167        // Initially disabled
     168        disableAllItems();
     169    }
    165170
    166                 // Initially disabled
    167                 disableAllItems();
    168         }
     171    public void disableAllItems() {
     172        startMI.setEnabled(false);
     173        reverseMI.setEnabled(false);
     174        clearMI.setEnabled(false);
     175        criteriaM.setEnabled(false);
     176    }
    169177
    170         public void disableAllItems() {
    171                 startMI.setEnabled(false);
    172                 reverseMI.setEnabled(false);
    173                 clearMI.setEnabled(false);
    174                 criteriaM.setEnabled(false);
    175         }
     178    public void enableStartItem() {
     179        startMI.setEnabled(true);
     180    }
    176181
    177         public void enableStartItem() {
    178                 startMI.setEnabled(true);
    179         }
     182    public void enableRestOfItems() {
     183        reverseMI.setEnabled(true);
     184        clearMI.setEnabled(true);
     185        criteriaM.setEnabled(true);
     186    }
    180187
    181         public void enableRestOfItems() {
    182                 reverseMI.setEnabled(true);
    183                 clearMI.setEnabled(true);
    184                 criteriaM.setEnabled(true);
    185         }
    186 
    187         public void disableRestOfItems() {
    188                 reverseMI.setEnabled(false);
    189                 clearMI.setEnabled(false);
    190                 criteriaM.setEnabled(false);
    191         }
     188    public void disableRestOfItems() {
     189        reverseMI.setEnabled(false);
     190        clearMI.setEnabled(false);
     191        criteriaM.setEnabled(false);
     192    }
    192193}
  • applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingPreferenceDialog.java

    r14287 r15707  
    6464public class RoutingPreferenceDialog implements PreferenceSetting {
    6565
    66         /**
    67         * Logger
    68         */
    69         static Logger logger = Logger.getLogger(RoutingPreferenceDialog.class);
    70 
    71         private Map<String, String> orig;
    72         private DefaultTableModel model;
    73 
    74         /**
    75         * Constructor
    76         */
    77         public RoutingPreferenceDialog() {
    78                 super();
    79                 readPreferences();
    80         }
    81 
    82         public void addGui(final PreferenceDialog gui) {
    83 
    84                 JPanel principal = gui.createPreferenceTab("routing",
    85                                 tr("Routing Plugin Preferences"), tr("Configure routing preferences."));
    86 
    87                 JPanel p = new JPanel();
    88                 p.setLayout(new GridBagLayout());
    89 
    90                 model = new DefaultTableModel(new String[] { tr("Highway type"),
    91                                 tr("Speed (Km/h)") }, 0) {
    92                         private static final long serialVersionUID = 4253339034781567453L;
    93 
    94                         @Override
    95                         public boolean isCellEditable(int row, int column) {
    96                                 return column != 0;
    97                         }
    98                 };
    99                 final JTable list = new JTable(model);
    100                 loadSpeeds(model);
    101 
    102                 JScrollPane scroll = new JScrollPane(list);
    103 
    104                 p.add(scroll, GBC.eol().fill(GBC.BOTH));
    105                 scroll.setPreferredSize(new Dimension(200, 200));
    106 
    107                 JButton add = new JButton(tr("Add"));
    108                 p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
    109                 p.add(add, GBC.std().insets(0, 5, 0, 0));
    110                 add.addActionListener(new ActionListener() {
    111                         public void actionPerformed(ActionEvent e) {
    112                                 JPanel p = new JPanel(new GridBagLayout());
    113                                 p.add(new JLabel(tr("Weight")), GBC.std().insets(0, 0, 5, 0));
    114                                 JComboBox key = new JComboBox();
    115                                 for (OsmWayTypes pk : OsmWayTypes.values())
    116                                         key.addItem(pk.getTag());
    117                                 JTextField value = new JTextField(10);
    118                                 p.add(key, GBC.eop().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL));
    119                                 p.add(new JLabel(tr("Value")), GBC.std().insets(0, 0, 5, 0));
    120                                 p.add(value, GBC.eol().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL));
    121                                 int answer = JOptionPane.showConfirmDialog(gui, p,
    122                                                 tr("Enter weight values"),
    123                                                 JOptionPane.OK_CANCEL_OPTION);
    124                                 if (answer == JOptionPane.OK_OPTION) {
    125                                         model
    126                                         .addRow(new String[] {
    127                                                         key.getSelectedItem().toString(),
    128                                                         value.getText() });
    129                                 }
    130                         }
    131                 });
    132 
    133                 JButton delete = new JButton(tr("Delete"));
    134                 p.add(delete, GBC.std().insets(0, 5, 0, 0));
    135                 delete.addActionListener(new ActionListener() {
    136                         public void actionPerformed(ActionEvent e) {
    137                                 if (list.getSelectedRow() == -1)
    138                                         JOptionPane.showMessageDialog(gui,
    139                                                         tr("Please select the row to delete."));
    140                                 else {
    141                                         Integer i;
    142                                         while ((i = list.getSelectedRow()) != -1)
    143                                                 model.removeRow(i);
    144                                 }
    145                         }
    146                 });
    147 
    148                 JButton edit = new JButton(tr("Edit"));
    149                 p.add(edit, GBC.std().insets(5, 5, 5, 0));
    150                 edit.addActionListener(new ActionListener() {
    151                         public void actionPerformed(ActionEvent e) {
    152                                 edit(gui, list);
    153                         }
    154                 });
    155 
    156                 JTabbedPane Opciones = new JTabbedPane();
    157                 Opciones.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    158 
    159                 Opciones.addTab("Profile", null, p, null);
    160 //              Opciones.addTab("Preferences", new JPanel());
    161 
    162                 list.addMouseListener(new MouseAdapter(){
    163                         @Override public void mouseClicked(MouseEvent e) {
    164                                 if (e.getClickCount() == 2)
    165                                         edit(gui, list);
    166                         }
    167                 });
    168 
    169                 principal.add(Opciones, GBC.eol().fill(GBC.BOTH));
    170 
    171         }
    172 
    173         public boolean ok() {
    174                 for (int i = 0; i < model.getRowCount(); ++i) {
    175                         String value = model.getValueAt(i, 1).toString();
    176                         if (value.length() != 0) {
    177                                 String key = model.getValueAt(i, 0).toString();
    178                                 String origValue = orig.get(key);
    179                                 if (origValue == null || !origValue.equals(value))
    180                                         Main.pref.put(key, value);
    181                                 orig.remove(key); // processed.
    182                         }
    183                 }
    184                 for (Entry<String, String> e : orig.entrySet())
    185                         Main.pref.put(e.getKey(), null);
    186                 return false;
    187         }
    188 
    189         private void edit(final PreferenceDialog gui, final JTable list) {
    190                 if (list.getSelectedRowCount() != 1) {
    191                         JOptionPane.showMessageDialog(gui,
    192                                         tr("Please select the row to edit."));
    193                         return;
    194                 }
    195                 String v = JOptionPane.showInputDialog(tr("New value for {0}", model
    196                                 .getValueAt(list.getSelectedRow(), 0)), model.getValueAt(list
    197                                                 .getSelectedRow(), 1));
    198                 if (v != null)
    199                         model.setValueAt(v, list.getSelectedRow(), 1);
    200         }
    201 
    202         private void loadSpeeds(DefaultTableModel model) {
    203                 // Read dialog values from preferences
    204                 readPreferences();
    205                 // Put these values in the model
    206                 for (String tag : orig.keySet()) {
    207                         model.addRow(new String[] { tag, orig.get(tag) });
    208                 }
    209         }
    210 
    211         private void readPreferences() {
    212                 orig = Main.pref.getAllPrefix("routing.profile.default.speed");
    213                 if (orig.size() == 0) { // defaults
    214                         logger.debug("Loading Default Preferences.");
    215                         for (OsmWayTypes owt : OsmWayTypes.values()) {
    216                                 Main.pref.putInteger("routing.profile.default.speed."
    217                                                 + owt.getTag(), owt.getSpeed());
    218                         }
    219                         orig = Main.pref.getAllPrefix("routing.profile.default.speed");
    220                 }
    221                 else logger.debug("Default preferences already exist.");
    222         }
    223 
    224         private String getKeyTag(String tag) {
    225                 return tag.split(".", 5)[4];
    226         }
    227 
    228         private String getTypeTag(String tag) {
    229                 return tag.split(".", 5)[3];
    230         }
    231 
    232         private String getNameTag(String tag) {
    233                 return tag.split(".", 5)[2];
    234         }
     66    /**
     67    * Logger
     68    */
     69    static Logger logger = Logger.getLogger(RoutingPreferenceDialog.class);
     70
     71    private Map<String, String> orig;
     72    private DefaultTableModel model;
     73
     74    /**
     75    * Constructor
     76    */
     77    public RoutingPreferenceDialog() {
     78        super();
     79        readPreferences();
     80    }
     81
     82    public void addGui(final PreferenceDialog gui) {
     83
     84        JPanel principal = gui.createPreferenceTab("routing",
     85                tr("Routing Plugin Preferences"), tr("Configure routing preferences."));
     86
     87        JPanel p = new JPanel();
     88        p.setLayout(new GridBagLayout());
     89
     90        model = new DefaultTableModel(new String[] { tr("Highway type"),
     91                tr("Speed (Km/h)") }, 0) {
     92            private static final long serialVersionUID = 4253339034781567453L;
     93
     94            @Override
     95            public boolean isCellEditable(int row, int column) {
     96                return column != 0;
     97            }
     98        };
     99        final JTable list = new JTable(model);
     100        loadSpeeds(model);
     101
     102        JScrollPane scroll = new JScrollPane(list);
     103
     104        p.add(scroll, GBC.eol().fill(GBC.BOTH));
     105        scroll.setPreferredSize(new Dimension(200, 200));
     106
     107        JButton add = new JButton(tr("Add"));
     108        p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL));
     109        p.add(add, GBC.std().insets(0, 5, 0, 0));
     110        add.addActionListener(new ActionListener() {
     111            public void actionPerformed(ActionEvent e) {
     112                JPanel p = new JPanel(new GridBagLayout());
     113                p.add(new JLabel(tr("Weight")), GBC.std().insets(0, 0, 5, 0));
     114                JComboBox key = new JComboBox();
     115                for (OsmWayTypes pk : OsmWayTypes.values())
     116                    key.addItem(pk.getTag());
     117                JTextField value = new JTextField(10);
     118                p.add(key, GBC.eop().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL));
     119                p.add(new JLabel(tr("Value")), GBC.std().insets(0, 0, 5, 0));
     120                p.add(value, GBC.eol().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL));
     121                int answer = JOptionPane.showConfirmDialog(gui, p,
     122                        tr("Enter weight values"),
     123                        JOptionPane.OK_CANCEL_OPTION);
     124                if (answer == JOptionPane.OK_OPTION) {
     125                    model
     126                    .addRow(new String[] {
     127                            key.getSelectedItem().toString(),
     128                            value.getText() });
     129                }
     130            }
     131        });
     132
     133        JButton delete = new JButton(tr("Delete"));
     134        p.add(delete, GBC.std().insets(0, 5, 0, 0));
     135        delete.addActionListener(new ActionListener() {
     136            public void actionPerformed(ActionEvent e) {
     137                if (list.getSelectedRow() == -1)
     138                    JOptionPane.showMessageDialog(gui,
     139                            tr("Please select the row to delete."));
     140                else {
     141                    Integer i;
     142                    while ((i = list.getSelectedRow()) != -1)
     143                        model.removeRow(i);
     144                }
     145            }
     146        });
     147
     148        JButton edit = new JButton(tr("Edit"));
     149        p.add(edit, GBC.std().insets(5, 5, 5, 0));
     150        edit.addActionListener(new ActionListener() {
     151            public void actionPerformed(ActionEvent e) {
     152                edit(gui, list);
     153            }
     154        });
     155
     156        JTabbedPane Opciones = new JTabbedPane();
     157        Opciones.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
     158
     159        Opciones.addTab("Profile", null, p, null);
     160//      Opciones.addTab("Preferences", new JPanel());
     161
     162        list.addMouseListener(new MouseAdapter(){
     163            @Override public void mouseClicked(MouseEvent e) {
     164                if (e.getClickCount() == 2)
     165                    edit(gui, list);
     166            }
     167        });
     168
     169        principal.add(Opciones, GBC.eol().fill(GBC.BOTH));
     170
     171    }
     172
     173    public boolean ok() {
     174        for (int i = 0; i < model.getRowCount(); ++i) {
     175            String value = model.getValueAt(i, 1).toString();
     176            if (value.length() != 0) {
     177                String key = model.getValueAt(i, 0).toString();
     178                String origValue = orig.get(key);
     179                if (origValue == null || !origValue.equals(value))
     180                    Main.pref.put(key, value);
     181                orig.remove(key); // processed.
     182            }
     183        }
     184        for (Entry<String, String> e : orig.entrySet())
     185            Main.pref.put(e.getKey(), null);
     186        return false;
     187    }
     188
     189    private void edit(final PreferenceDialog gui, final JTable list) {
     190        if (list.getSelectedRowCount() != 1) {
     191            JOptionPane.showMessageDialog(gui,
     192                    tr("Please select the row to edit."));
     193            return;
     194        }
     195        String v = JOptionPane.showInputDialog(tr("New value for {0}", model
     196                .getValueAt(list.getSelectedRow(), 0)), model.getValueAt(list
     197                        .getSelectedRow(), 1));
     198        if (v != null)
     199            model.setValueAt(v, list.getSelectedRow(), 1);
     200    }
     201
     202    private void loadSpeeds(DefaultTableModel model) {
     203        // Read dialog values from preferences
     204        readPreferences();
     205        // Put these values in the model
     206        for (String tag : orig.keySet()) {
     207            model.addRow(new String[] { tag, orig.get(tag) });
     208        }
     209    }
     210
     211    private void readPreferences() {
     212        orig = Main.pref.getAllPrefix("routing.profile.default.speed");
     213        if (orig.size() == 0) { // defaults
     214            logger.debug("Loading Default Preferences.");
     215            for (OsmWayTypes owt : OsmWayTypes.values()) {
     216                Main.pref.putInteger("routing.profile.default.speed."
     217                        + owt.getTag(), owt.getSpeed());
     218            }
     219            orig = Main.pref.getAllPrefix("routing.profile.default.speed");
     220        }
     221        else logger.debug("Default preferences already exist.");
     222    }
     223
     224    private String getKeyTag(String tag) {
     225        return tag.split(".", 5)[4];
     226    }
     227
     228    private String getTypeTag(String tag) {
     229        return tag.split(".", 5)[3];
     230    }
     231
     232    private String getNameTag(String tag) {
     233        return tag.split(".", 5)[2];
     234    }
    235235}
Note: See TracChangeset for help on using the changeset viewer.