Changeset 30361 in osm
- Timestamp:
- 2014-03-24T22:29:30+01:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/routing/src/com/innovant/josm
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/routing/src/com/innovant/josm/jrt/core/RoutingGraph.java
r29633 r30361 58 58 public class RoutingGraph { 59 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 60 /** 61 * Routing Profile 62 */ 63 private final RoutingProfile routingProfile; 64 65 /** 66 * Diferent algorithms to apply to the graph. 67 */ 68 public enum Algorithm { 69 ROUTING_ALG_DIJKSTRA, ROUTING_ALG_BELLMANFORD 70 }; 71 72 /** 73 * Search criteria for the route. 74 */ 75 public enum RouteType {FASTEST,SHORTEST}; 76 77 /** 78 * 79 */ 80 private RouteType routeType; 81 82 /** 83 * Associated Osm DataSet 84 */ 85 private final DataSet data; 86 87 /** 88 * Logger. 89 */ 90 static Logger logger = Logger.getLogger(RoutingGraph.class); 91 91 92 92 private static Collection<String> excludedHighwayValues = Arrays.asList(new String[]{ … … 94 94 "platform", "give_way", "proposed", "milestone", "speed_camera", "abandoned" 95 95 }); 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 96 97 /** 98 * Graph state 99 * <code>true</code> Graph in memory. 100 * <code>false</code> Graph not created. 101 */ 102 // public boolean graphState; 103 104 /** 105 * OSM Graph. 106 */ 107 // private DirectedWeightedMultigraph<Node, OsmEdge> graph; 108 // private WeightedMultigraph<Node, OsmEdge> graph; 109 private Graph<Node, OsmEdge> graph; 110 private RoutingGraphDelegator rgDelegator=null; 111 112 113 /** 114 * Graph getter 115 */ 116 public Graph<Node, OsmEdge> getGraph(){ 117 return graph; 118 119 } 120 121 122 private void addEdgeBidirectional( Way way, Node from, Node to){ 123 addEdge(way,from,to); 124 addEdge(way,to,from); 125 } 126 127 private void addEdgeReverseOneway( Way way, Node from, Node to){ 128 addEdge(way,to,from); 129 } 130 131 private void addEdgeNormalOneway( Way way, Node from, Node to){ 132 addEdge(way,from,to); 133 } 134 135 /** 136 * Speeds 137 */ 138 private Map<String,Double> waySpeeds; 139 140 /** 141 * Default Constructor. 142 */ 143 public RoutingGraph(DataSet data) { 144 // this.graphState = false; 145 this.graph = null; 146 this.data = data; 147 routeType=RouteType.SHORTEST; 148 routingProfile=new RoutingProfile("default"); 149 routingProfile.setOnewayUse(true); // Don't ignore oneways by default 150 this.setWaySpeeds(routingProfile.getWaySpeeds()); 151 logger.debug("Created RoutingGraph"); 152 } 153 154 /** 155 * Create OSM graph for routing 156 * 157 * @return 158 */ 159 public void createGraph() { 160 161 logger.debug("Creating Graph..."); 162 graph = new DirectedWeightedMultigraph<Node, OsmEdge>(OsmEdge.class); 163 rgDelegator=new RoutingGraphDelegator(graph); 164 rgDelegator.setRouteType(this.routeType); 165 // iterate all ways and segments for all nodes: 166 for (Way way : data.getWays()) { 167 168 // skip way if not suitable for routing. 169 if (way == null || way.isDeleted() || !this.isvalidWay(way) 170 || way.getNodes().size() < 1) continue; 171 172 // INIT 173 Node from = null; 174 Node to = null; 175 List<Node> nodes = way.getNodes(); 176 int nodes_count = nodes.size(); 177 178 /* 179 * Assume node is A B C D E. The procedure should be 180 * 181 * case 1 - bidirectional ways: 182 * 1) Add vertex A B C D E 183 * 2) Link A<->B, B<->C, C<->D, D<->E as Edges 184 * 185 * case 2 - oneway reverse: 186 * 1) Add vertex A B C D E 187 * 2) Link B->A,C->B,D->C,E->D as Edges. result: A<-B<-C<-D<-E 188 * 189 * case 3 - oneway normal: 190 * 1) Add vertex A B C D E 191 * 2) Link A->B, B->C, C->D, D->E as Edges. result: A->B->C->D->E 192 * 193 * 194 */ 195 196 String oneway_val = way.get("oneway"); /* get (oneway=?) tag for this way. */ 197 String junction_val = way.get("junction"); /* get (junction=?) tag for this way. */ 198 199 from = nodes.get(0); /* 1st node A */ 200 graph.addVertex(from); /* add vertex A */ 201 202 for (int i = 1; i < nodes_count; i++) { /* loop from B until E */ 203 204 to = nodes.get(i); /* 2nd node B */ 205 206 if (to != null && !to.isDeleted()) { 207 graph.addVertex(to); /* add vertex B */ 208 209 210 //this is where we link the vertices 211 if (!routingProfile.isOnewayUsed()) { 212 //"Ignore oneways" is selected 213 addEdgeBidirectional(way, from, to); 214 215 } else if (oneway_val == null && junction_val == "roundabout") { 216 //Case (roundabout): oneway=implicit yes 217 addEdgeNormalOneway(way, from, to); 218 219 } else if (oneway_val == null || oneway_val == "false" || oneway_val == "no" || oneway_val == "0") { 220 //Case (bi-way): oneway=false OR oneway=unset OR oneway=0 OR oneway=no 221 addEdgeBidirectional(way, from, to); 222 223 } else if (oneway_val == "-1") { 224 //Case (oneway reverse): oneway=-1 225 addEdgeReverseOneway(way, from, to); 226 227 } else if (oneway_val == "1" || oneway_val == "yes" || oneway_val == "true") { 228 //Case (oneway normal): oneway=yes OR 1 OR true 229 addEdgeNormalOneway(way, from, to); 230 231 } 232 233 from = to; /* we did A<->B, next loop we will do B<->C, so from=B,to=C for next loop. */ 234 } 235 236 } // end of looping thru nodes 237 } // end of looping thru ways 238 239 logger.debug("End Create Graph"); 240 logger.debug("Vertex: "+graph.vertexSet().size()); 241 logger.debug("Edges: "+graph.edgeSet().size()); 242 } 243 244 /** 245 * Compute weight and add edge to the graph 246 * @param way 247 * @param from 248 * @param to 249 */ 250 private void addEdge(Way way,Node from, Node to) { 251 LatLon fromLL = from.getCoor(); 252 LatLon toLL = from.getCoor(); 253 if (fromLL == null || toLL == null) { 254 return; 255 } 256 double length = fromLL.greatCircleDistance(toLL); 257 258 OsmEdge edge = new OsmEdge(way, from, to); 259 edge.setSpeed(12.1); 260 graph.addEdge(from, to, edge); 261 // weight = getWeight(way); 262 double weight = getWeight(way, length); 263 setWeight(edge, length); 264 logger.debug("edge for way " + way.getId() 265 + "(from node " + from.getId() + " to node " 266 + to.getId() + ") has weight: " + weight); 267 ((DirectedWeightedMultigraph<Node,OsmEdge>)graph).setEdgeWeight(edge, weight); 268 } 269 270 /** 271 * Set the weight for the given segment depending on the highway type 272 * and the length of the segment. The higher the value, the less it is used 273 * in routing. 274 * 275 * @param way 276 * the way. 277 * @return 278 */ 279 private void setWeight(OsmEdge osmedge, double length) { 280 281 osmedge.setLength(length); 282 if (this.waySpeeds.containsKey(osmedge.getWay().get("highway"))) 283 osmedge.setSpeed(this.waySpeeds.get(osmedge.getWay().get("highway"))); 284 285 } 286 287 /** 288 * Returns the weight for the given segment depending on the highway type 289 * and the length of the segment. The higher the value, the less it is used 290 * in routing. 291 * 292 * @param way 293 * the way. 294 * @return 295 */ 296 private double getWeight(Way way, double length) { 297 // Default speed if no setting is found 298 double speed = 1; 299 300 switch (routeType) { 301 case SHORTEST: 302 // Same speed for all types of ways 303 if (this.waySpeeds.containsKey("residential")) 304 speed=this.waySpeeds.get("residential"); 305 break; 306 case FASTEST: 307 // Each type of way may have a different speed 308 if (this.waySpeeds.containsKey(way.get("highway"))) 309 speed=this.waySpeeds.get(way.get("highway")); 310 logger.debug("Speed="+speed); 311 break; 312 default: 313 break; 314 } 315 // Return the time spent to traverse the way 316 return length / speed; 317 } 318 319 /** 320 * Check if a Way is correct. 321 * 322 * @param way 323 * The way. 324 * @return <code>true</code> is valid. <code>false</code> is not valid. 325 */ 326 public boolean isvalidWay(Way way) { 327 //if (!way.isTagged()) <---not needed me thinks 328 // return false; 329 330 String highway = way.get("highway"); 331 332 return (highway != null && !excludedHighwayValues.contains(highway)) || way.get("junction") != null 333 || way.get("service") != null; 334 335 } 336 337 /** 338 * Apply selected routing algorithm to the graph. 339 * 340 * @param nodes 341 * Nodes used to calculate path. 342 * @param algorithm 343 * Algorithm used to compute the path, 344 * RoutingGraph.Algorithm.ROUTING_ALG_DIJKSTRA or 345 * RoutingGraph.Algorithm.ROUTING_ALG_BELLMANFORD 346 * @return new path. 347 */ 348 public List<OsmEdge> applyAlgorithm(List<Node> nodes, Algorithm algorithm) { 349 List<OsmEdge> path = new ArrayList<OsmEdge>(); 350 Graph<Node,OsmEdge> g; 351 double totalWeight = 0; 352 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 353 RoutingModel routingModel = layer.getRoutingModel(); 354 355 if (graph == null || routingModel.getOnewayChanged()) 356 this.createGraph(); 357 logger.debug("apply algorithm between nodes "); 358 359 for (Node node : nodes) { 360 logger.debug(node.getId()); 361 } 362 logger.debug("-----------------------------------"); 363 364 // Assign the graph to g 365 g = graph; 366 367 switch (algorithm) { 368 case ROUTING_ALG_DIJKSTRA: 369 logger.debug("Using Dijkstra algorithm"); 370 DijkstraShortestPath<Node, OsmEdge> routingk = null; 371 for (int index = 1; index < nodes.size(); ++index) { 372 routingk = new DijkstraShortestPath<Node, OsmEdge>(g, nodes 373 .get(index - 1), nodes.get(index)); 374 if (routingk.getPathEdgeList() == null) { 375 logger.debug("no path found!"); 376 break; 377 } 378 path.addAll(routingk.getPathEdgeList()); 379 totalWeight += routingk.getPathLength(); 380 } 381 break; 382 case ROUTING_ALG_BELLMANFORD: 383 logger.debug("Using Bellman Ford algorithm"); 384 for (int index = 1; index < nodes.size(); ++index) { 385 path = BellmanFordShortestPath.findPathBetween(rgDelegator, nodes 386 .get(index - 1), nodes.get(index)); 387 if (path == null) { 388 logger.debug("no path found!"); 389 return null; 390 } 391 } 392 break; 393 default: 394 logger.debug("Wrong algorithm"); 395 break; 396 } 397 398 logger.debug("shortest path found: " + path + "\nweight: " 399 + totalWeight); 400 return path; 401 } 402 403 /** 404 * Return the number of vertices. 405 * @return the number of vertices. 406 */ 407 public int getVertexCount(){ 408 int value=0; 409 if (graph!=null) value=graph.vertexSet().size(); 410 return value; 411 } 412 413 /** 414 * Return the number of edges. 415 * @return the number of edges. 416 */ 417 public int getEdgeCount(){ 418 int value=0; 419 if (graph!=null) value=graph.edgeSet().size(); 420 return value; 421 } 422 423 /** 424 * @param routeType the routeType to set 425 */ 426 public void setTypeRoute(RouteType routetype) { 427 this.routeType = routetype; 428 this.rgDelegator.setRouteType(routetype); 429 } 430 431 /** 432 * @return the routeType 433 */ 434 public RouteType getTypeRoute() { 435 return routeType; 436 } 437 438 public Map<String, Double> getWaySpeeds() { 439 return waySpeeds; 440 } 441 442 public void setWaySpeeds(Map<String, Double> waySpeeds) { 443 this.waySpeeds = waySpeeds; 444 } 445 446 public void resetGraph() { 447 graph=null; 448 } 449 450 public RoutingProfile getRoutingProfile() { 451 return routingProfile; 452 } 453 453 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingLayer.java
r29586 r30361 125 125 126 126 127 this.routingModel.routingGraph.createGraph(); 128 Main.map.repaint(); 127 this.routingModel.routingGraph.createGraph(); /* construct the graph right after we we create the layer */ 128 Main.map.repaint(); /* update MapView */ 129 129 } 130 130 … … 172 172 } 173 173 174 /*175 * (non-Javadoc)176 * @see org.openstreetmap.josm.gui.layer.Layer#getIcon()177 */178 174 @Override 179 175 public Icon getIcon() { … … 182 178 } 183 179 184 /*185 * (non-Javadoc)186 * @see org.openstreetmap.josm.gui.layer.Layer#getInfoComponent()187 */188 180 @Override 189 181 public Object getInfoComponent() { … … 197 189 } 198 190 199 /*200 * (non-Javadoc)201 * @see org.openstreetmap.josm.gui.layer.Layer#getMenuEntries()202 */203 191 @Override 204 192 public Action[] getMenuEntries() { … … 214 202 } 215 203 216 /*217 * (non-Javadoc)218 * @see org.openstreetmap.josm.gui.layer.Layer#getToolTipText()219 */220 204 @Override 221 205 public String getToolTipText() { … … 225 209 } 226 210 227 /*228 * (non-Javadoc)229 * @see org.openstreetmap.josm.gui.layer.Layer#isMergable(org.openstreetmap.josm.gui.layer.Layer)230 */231 211 @Override 232 212 public boolean isMergable(Layer other) { … … 234 214 } 235 215 236 /*237 * (non-Javadoc)238 * @see org.openstreetmap.josm.gui.layer.Layer#mergeFrom(org.openstreetmap.josm.gui.layer.Layer)239 */240 216 @Override 241 217 public void mergeFrom(Layer from) { … … 243 219 } 244 220 245 /*246 * (non-Javadoc)247 * @see org.openstreetmap.josm.gui.layer.Layer#paint(java.awt.Graphics, org.openstreetmap.josm.gui.MapView)248 */249 221 @Override 250 222 public void paint(Graphics2D g, MapView mv, Bounds bounds) { … … 265 237 String widthString = Main.pref.get(PreferencesKeys.KEY_ROUTE_WIDTH.key); 266 238 if (widthString.length() == 0) { 267 widthString = "2"; 239 widthString = "2"; /* I think 2 is better */ 268 240 // FIXME add after good width is found: Main.pref.put(KEY_ROUTE_WIDTH, widthString); 269 241 } … … 273 245 // draw our graph 274 246 if (isActiveLayer) { 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 } 290 247 if(routingModel != null) { 248 if(routingModel.routingGraph != null && routingModel.routingGraph.getGraph() != null) { 249 Set<OsmEdge> graphEdges = routingModel.routingGraph.getGraph().edgeSet(); 250 if (!graphEdges.isEmpty()) { 251 Color color2 = ColorHelper.html2color("#00ff00"); /* just green for now */ 252 OsmEdge firstedge = (OsmEdge) graphEdges.toArray()[0]; 253 Point from = mv.getPoint(firstedge.fromEastNorth()); 254 g.drawRect(from.x-4, from.y+4, from.x+4, from.y-4); 255 for(OsmEdge edge : graphEdges) { 256 drawGraph(g, mv, edge, color2, width); 257 } 258 } 259 } 260 } 261 } 262 291 263 292 264 if(nodes == null || nodes.size() == 0) return; … … 322 294 } 323 295 324 /*325 * (non-Javadoc)326 * @see org.openstreetmap.josm.gui.layer.Layer#visitBoundingBox(org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor)327 */328 296 @Override 329 297 public void visitBoundingBox(BoundingXYVisitor v) { … … 333 301 } 334 302 335 /*336 * (non-Javadoc)337 * @see org.openstreetmap.josm.gui.layer.Layer#destroy()338 */339 303 @Override 340 304 public void destroy() { … … 381 345 382 346 g2d.setStroke(oldStroke); 383 384 347 } 348 385 349 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingModel.java
r28138 r30361 48 48 public class RoutingModel { 49 49 50 51 52 53 50 /** 51 * Logger 52 */ 53 static Logger logger = Logger.getLogger(RoutingModel.class); 54 54 55 56 57 58 55 /** 56 * Graph to calculate route 57 */ 58 public RoutingGraph routingGraph=null; 59 59 60 61 62 63 60 /** 61 * List of nodes that the route has to traverse 62 */ 63 private List<Node> nodes=null; 64 64 65 65 private List<OsmEdge> path=null; 66 66 67 68 69 70 67 /** 68 * Flag to advise about changes in the selected nodes. 69 */ 70 private boolean changeNodes=false; 71 71 72 73 74 75 72 /** 73 * Flag to advise about changes in ways. 74 */ 75 private boolean changeOneway=false; 76 76 77 78 79 80 81 82 83 84 77 /** 78 * Default Constructor. 79 */ 80 public RoutingModel(DataSet data) { 81 nodes = new ArrayList<Node>(); 82 System.out.println("gr " + data); 83 routingGraph = new RoutingGraph(data); 84 } 85 85 86 87 88 89 90 91 92 86 /** 87 * Method that returns the selected nodes to calculate route. 88 * @return the selectedNodes 89 */ 90 public List<Node> getSelectedNodes() { 91 return nodes; 92 } 93 93 94 95 96 97 98 99 100 101 94 /** 95 * Adds a node to the route node list. 96 * @param node the node to add. 97 */ 98 public void addNode(Node node) { 99 nodes.add(node); 100 this.changeNodes=true; 101 } 102 102 103 104 105 106 107 108 109 110 111 112 103 /** 104 * Removes a node from the route node list. 105 * @param index the index of the node to remove. 106 */ 107 public void removeNode(int index) { 108 if (nodes.size()>index) { 109 nodes.remove(index); 110 this.changeNodes=true; 111 } 112 } 113 113 114 115 116 117 118 119 120 121 122 123 124 114 /** 115 * Inserts a node in the route node list. 116 * @param index the index where the node will be inserted 117 * @param node the node to be inserted 118 */ 119 public void insertNode(int index, Node node) { 120 if (nodes.size()>=index) { 121 nodes.add(index, node); 122 this.changeNodes=true; 123 } 124 } 125 125 126 127 128 129 130 131 132 133 134 135 136 126 /** 127 * Reverse list of nodes 128 */ 129 public void reverseNodes() { 130 List<Node> aux = new ArrayList<Node>(); 131 for (Node n : nodes) { 132 aux.add(0,n); 133 } 134 nodes = aux; 135 this.changeNodes=true; 136 } 137 137 138 139 140 141 142 143 144 145 146 147 148 149 150 138 /** 139 * Get the edges of the route. 140 * @return A list of edges forming the shortest path 141 */ 142 public List<OsmEdge> getRouteEdges() { 143 if (this.changeNodes || path==null) 144 { 145 path=this.routingGraph.applyAlgorithm(nodes, Algorithm.ROUTING_ALG_DIJKSTRA); 146 this.changeNodes=false; 147 this.changeOneway=false; 148 } 149 return path; 150 } 151 151 152 153 154 155 156 157 152 /** 153 * Marks that some node or the node order has changed so the path should be computed again 154 */ 155 public void setNodesChanged() { 156 this.changeNodes = true; 157 } 158 158 159 160 161 162 163 164 159 /** 160 * Marks that "Ignore oneway" option has changed so the path should be computed again 161 */ 162 public void setOnewayChanged() { 163 this.changeOneway = true; 164 } 165 165 166 167 168 169 170 171 166 /** 167 * Marks that "Ignore oneway" option has changed so the path should be computed again 168 */ 169 public boolean getOnewayChanged() { 170 return this.changeOneway; 171 } 172 172 173 174 175 176 177 178 179 173 /** 174 * Resets all data. 175 */ 176 public void reset() { 177 nodes.clear(); 178 this.changeNodes=true; 179 } 180 180 181 181 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/RoutingPlugin.java
r30145 r30361 66 66 */ 67 67 public class RoutingPlugin extends Plugin implements LayerChangeListener,DataSetListenerAdapter.Listener { 68 /** 69 * Logger 70 */ 71 static Logger logger = Logger.getLogger(RoutingPlugin.class); 72 73 /** 74 * The list of routing layers 75 */ 76 private final ArrayList<RoutingLayer> layers; 77 78 /** 79 * The side dialog where nodes are listed 80 */ 81 private RoutingDialog routingDialog; 82 83 /** 84 * Preferences Settings Dialog. 85 */ 86 private final PreferenceSetting preferenceSettings; 87 88 /** 89 * MapMode for adding route nodes. 90 * We use this field to enable or disable the mode automatically. 91 */ 92 private AddRouteNodeAction addRouteNodeAction; 93 94 /** 95 * MapMode for removing route nodes. 96 * We use this field to enable or disable the mode automatically. 97 */ 98 private RemoveRouteNodeAction removeRouteNodeAction; 99 100 /** 101 * MapMode for moving route nodes. 102 * We use this field to enable or disable the mode automatically. 103 */ 104 private MoveRouteNodeAction moveRouteNodeAction; 105 106 /** 107 * IconToggleButton for adding route nodes, we use this field to show or hide the button. 108 */ 109 private IconToggleButton addRouteNodeButton; 110 111 /** 112 * IconToggleButton for removing route nodes, we use this field to show or hide the button. 113 */ 114 private IconToggleButton removeRouteNodeButton; 115 116 /** 117 * IconToggleButton for moving route nodes, we use this field to show or hide the button. 118 */ 119 private IconToggleButton moveRouteNodeButton; 120 121 /** 122 * IconToggleButton for moving route nodes, we use this field to show or hide the button. 123 */ 124 private final RoutingMenu menu; 125 126 /** 127 * Reference for the plugin class (as if it were a singleton) 128 */ 129 private static RoutingPlugin plugin; 130 131 private final DataSetListenerAdapter datasetAdapter; 132 133 /** 134 * Default Constructor 135 */ 136 public RoutingPlugin(PluginInformation info) { 137 super(info); 138 139 datasetAdapter = new DataSetListenerAdapter(this); 140 plugin = this; // Assign reference to the plugin class 141 File log4jConfigFile = new java.io.File("log4j.xml"); 142 if (log4jConfigFile.exists()) { 143 DOMConfigurator.configure(log4jConfigFile.getPath()); 144 } else { 145 System.err.println("Routing plugin warning: log4j configuration not found"); 146 } 147 logger.debug("Loading routing plugin..."); 148 preferenceSettings=new RoutingPreferenceDialog(); 149 // Initialize layers list 150 layers = new ArrayList<RoutingLayer>(); 151 // Add menu 152 menu = new RoutingMenu(); 153 // Register this class as LayerChangeListener 154 MapView.addLayerChangeListener(this); 155 DatasetEventManager.getInstance().addDatasetListener(datasetAdapter, FireMode.IN_EDT_CONSOLIDATED); 156 logger.debug("Finished loading plugin"); 157 } 158 159 /** 160 * Provides static access to the plugin instance, to enable access to the plugin methods 161 * @return the instance of the plugin 162 */ 163 public static RoutingPlugin getInstance() { 164 return plugin; 165 } 166 167 /** 168 * Get the routing side dialog 169 * @return The instance of the routing side dialog 170 */ 171 public RoutingDialog getRoutingDialog() { 172 return routingDialog; 173 } 174 175 public void addLayer() { 176 OsmDataLayer osmLayer = Main.main.getEditLayer(); 177 if (osmLayer != null) { 178 RoutingLayer layer = new RoutingLayer(tr("Routing") + " [" + osmLayer.getName() + "]", osmLayer); 179 layers.add(layer); 180 Main.main.addLayer(layer); 181 } 182 } 183 184 /* 185 * (non-Javadoc) 186 * @see org.openstreetmap.josm.plugins.Plugin#mapFrameInitialized(org.openstreetmap.josm.gui.MapFrame, org.openstreetmap.josm.gui.MapFrame) 187 */ 188 @Override 189 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 190 if (newFrame != null) { 191 // Create plugin map modes 192 addRouteNodeAction = new AddRouteNodeAction(newFrame); 193 removeRouteNodeAction = new RemoveRouteNodeAction(newFrame); 194 moveRouteNodeAction = new MoveRouteNodeAction(newFrame); 195 // Create plugin buttons and add them to the toolbar 196 addRouteNodeButton = new IconToggleButton(addRouteNodeAction); 197 removeRouteNodeButton = new IconToggleButton(removeRouteNodeAction); 198 moveRouteNodeButton = new IconToggleButton(moveRouteNodeAction); 199 addRouteNodeButton.setAutoHideDisabledButton(true); 200 removeRouteNodeButton.setAutoHideDisabledButton(true); 201 moveRouteNodeButton.setAutoHideDisabledButton(true); 202 newFrame.addMapMode(addRouteNodeButton); 203 newFrame.addMapMode(removeRouteNodeButton); 204 newFrame.addMapMode(moveRouteNodeButton); 205 // Enable menu 206 menu.enableStartItem(); 207 newFrame.addToggleDialog(routingDialog = new RoutingDialog()); 208 } else { 209 addRouteNodeAction = null; 210 removeRouteNodeAction = null; 211 moveRouteNodeAction = null; 212 addRouteNodeButton = null; 213 removeRouteNodeButton = null; 214 moveRouteNodeButton = null; 215 routingDialog = null; 216 } 217 } 218 219 /* 220 * (non-Javadoc) 221 * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#activeLayerChange(org.openstreetmap.josm.gui.layer.Layer, org.openstreetmap.josm.gui.layer.Layer) 222 */ 223 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 224 if (newLayer instanceof RoutingLayer) { /* show Routing toolbar and dialog window */ 225 menu.enableRestOfItems(); 226 if (routingDialog != null) { 227 routingDialog.showDialog(); 228 routingDialog.refresh(); 229 } 230 }else{ /* hide Routing toolbar and dialog window */ 231 menu.disableRestOfItems(); 232 if (routingDialog != null) { 233 routingDialog.hideDialog(); 234 } 235 } 236 } 237 238 /* 239 * (non-Javadoc) 240 * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerAdded(org.openstreetmap.josm.gui.layer.Layer) 241 */ 242 public void layerAdded(Layer newLayer) { 243 // Add button(s) to the tool bar when the routing layer is added 244 if (newLayer instanceof RoutingLayer) { 245 menu.enableRestOfItems(); 246 // Set layer on top and select layer, also refresh toggleDialog to reflect selection 247 Main.map.mapView.moveLayer(newLayer, 0); 248 logger.debug("Added routing layer."); 249 } 250 } 251 252 /* 253 * (non-Javadoc) 254 * @see org.openstreetmap.josm.gui.layer.Layer.LayerChangeListener#layerRemoved(org.openstreetmap.josm.gui.layer.Layer) 255 */ 256 public void layerRemoved(Layer oldLayer) { 257 if ((oldLayer instanceof RoutingLayer) & (layers.size()==1)) { 258 // Remove button(s) from the tool bar when the last routing layer is removed 259 addRouteNodeButton.setVisible(false); 260 removeRouteNodeButton.setVisible(false); 261 moveRouteNodeButton.setVisible(false); 262 menu.disableRestOfItems(); 263 layers.remove(oldLayer); 264 logger.debug("Removed routing layer."); 265 } else if (oldLayer instanceof OsmDataLayer) { 266 // Remove all associated routing layers 267 // Convert to Array to prevent ConcurrentModificationException when removing layers from ArrayList 268 // FIXME: can't remove associated routing layers without triggering exceptions in some cases 269 RoutingLayer[] layersArray = layers.toArray(new RoutingLayer[0]); 270 for (int i=0;i<layersArray.length;i++) { 271 if (layersArray[i].getDataLayer().equals(oldLayer)) { 272 try { 273 // Remove layer 274 Main.main.removeLayer(layersArray[i]); 275 } catch (IllegalArgumentException e) { 276 } 277 } 278 } 279 } 280 // Reload RoutingDialog table model 281 if (routingDialog != null) { 282 routingDialog.refresh(); 283 } 284 } 285 286 public void processDatasetEvent(AbstractDatasetChangedEvent event){ 287 288 289 } 290 /* (non-Javadoc) 291 * @see org.openstreetmap.josm.plugins.Plugin#getPreferenceSetting() 292 */ 293 @Override 294 public PreferenceSetting getPreferenceSetting() { 295 return preferenceSettings; 296 } 68 /** 69 * Logger 70 */ 71 static Logger logger = Logger.getLogger(RoutingPlugin.class); 72 73 /** 74 * The list of routing layers 75 */ 76 private final ArrayList<RoutingLayer> layers; 77 78 /** 79 * The side dialog where nodes are listed 80 */ 81 private RoutingDialog routingDialog; 82 83 /** 84 * Preferences Settings Dialog. 85 */ 86 private final PreferenceSetting preferenceSettings; 87 88 /** 89 * MapMode for adding route nodes. 90 * We use this field to enable or disable the mode automatically. 91 */ 92 private AddRouteNodeAction addRouteNodeAction; 93 94 /** 95 * MapMode for removing route nodes. 96 * We use this field to enable or disable the mode automatically. 97 */ 98 private RemoveRouteNodeAction removeRouteNodeAction; 99 100 /** 101 * MapMode for moving route nodes. 102 * We use this field to enable or disable the mode automatically. 103 */ 104 private MoveRouteNodeAction moveRouteNodeAction; 105 106 /** 107 * IconToggleButton for adding route nodes, we use this field to show or hide the button. 108 */ 109 private IconToggleButton addRouteNodeButton; 110 111 /** 112 * IconToggleButton for removing route nodes, we use this field to show or hide the button. 113 */ 114 private IconToggleButton removeRouteNodeButton; 115 116 /** 117 * IconToggleButton for moving route nodes, we use this field to show or hide the button. 118 */ 119 private IconToggleButton moveRouteNodeButton; 120 121 /** 122 * IconToggleButton for moving route nodes, we use this field to show or hide the button. 123 */ 124 private final RoutingMenu menu; 125 126 /** 127 * Reference for the plugin class (as if it were a singleton) 128 */ 129 private static RoutingPlugin plugin; 130 131 private final DataSetListenerAdapter datasetAdapter; 132 133 /** 134 * Default Constructor 135 */ 136 public RoutingPlugin(PluginInformation info) { 137 super(info); 138 139 datasetAdapter = new DataSetListenerAdapter(this); 140 plugin = this; // Assign reference to the plugin class 141 File log4jConfigFile = new java.io.File("log4j.xml"); 142 if (log4jConfigFile.exists()) { 143 DOMConfigurator.configure(log4jConfigFile.getPath()); 144 } else { 145 System.err.println("Routing plugin warning: log4j configuration not found"); 146 } 147 logger.debug("Loading routing plugin..."); 148 preferenceSettings=new RoutingPreferenceDialog(); 149 // Initialize layers list 150 layers = new ArrayList<RoutingLayer>(); 151 // Add menu 152 menu = new RoutingMenu(); 153 // Register this class as LayerChangeListener 154 MapView.addLayerChangeListener(this); 155 DatasetEventManager.getInstance().addDatasetListener(datasetAdapter, FireMode.IN_EDT_CONSOLIDATED); 156 logger.debug("Finished loading plugin"); 157 } 158 159 /** 160 * Provides static access to the plugin instance, to enable access to the plugin methods 161 * @return the instance of the plugin 162 */ 163 public static RoutingPlugin getInstance() { 164 return plugin; 165 } 166 167 /** 168 * Get the routing side dialog 169 * @return The instance of the routing side dialog 170 */ 171 public RoutingDialog getRoutingDialog() { 172 return routingDialog; 173 } 174 175 public void addLayer() { 176 OsmDataLayer osmLayer = Main.main.getEditLayer(); 177 if (osmLayer != null) { 178 RoutingLayer layer = new RoutingLayer(tr("Routing") + " [" + osmLayer.getName() + "]", osmLayer); 179 layers.add(layer); 180 Main.main.addLayer(layer); 181 } 182 } 183 184 @Override 185 public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) { 186 if (newFrame != null) { 187 // Create plugin map modes 188 addRouteNodeAction = new AddRouteNodeAction(newFrame); 189 removeRouteNodeAction = new RemoveRouteNodeAction(newFrame); 190 moveRouteNodeAction = new MoveRouteNodeAction(newFrame); 191 // Create plugin buttons and add them to the toolbar 192 addRouteNodeButton = new IconToggleButton(addRouteNodeAction); 193 removeRouteNodeButton = new IconToggleButton(removeRouteNodeAction); 194 moveRouteNodeButton = new IconToggleButton(moveRouteNodeAction); 195 addRouteNodeButton.setAutoHideDisabledButton(true); 196 removeRouteNodeButton.setAutoHideDisabledButton(true); 197 moveRouteNodeButton.setAutoHideDisabledButton(true); 198 newFrame.addMapMode(addRouteNodeButton); 199 newFrame.addMapMode(removeRouteNodeButton); 200 newFrame.addMapMode(moveRouteNodeButton); 201 // Enable menu 202 menu.enableStartItem(); 203 newFrame.addToggleDialog(routingDialog = new RoutingDialog()); 204 } else { 205 addRouteNodeAction = null; 206 removeRouteNodeAction = null; 207 moveRouteNodeAction = null; 208 addRouteNodeButton = null; 209 removeRouteNodeButton = null; 210 moveRouteNodeButton = null; 211 routingDialog = null; 212 } 213 } 214 215 public void activeLayerChange(Layer oldLayer, Layer newLayer) { 216 if (newLayer instanceof RoutingLayer) { /* show Routing toolbar and dialog window */ 217 menu.enableRestOfItems(); 218 if (routingDialog != null) { 219 routingDialog.showDialog(); 220 routingDialog.refresh(); 221 } 222 }else{ /* hide Routing toolbar and dialog window */ 223 menu.disableRestOfItems(); 224 if (routingDialog != null) { 225 routingDialog.hideDialog(); 226 } 227 } 228 } 229 230 public void layerAdded(Layer newLayer) { 231 // Add button(s) to the tool bar when the routing layer is added 232 if (newLayer instanceof RoutingLayer) { 233 menu.enableRestOfItems(); 234 // Set layer on top and select layer, also refresh toggleDialog to reflect selection 235 Main.map.mapView.moveLayer(newLayer, 0); 236 logger.debug("Added routing layer."); 237 } 238 } 239 240 public void layerRemoved(Layer oldLayer) { 241 if ((oldLayer instanceof RoutingLayer) & (layers.size()==1)) { 242 // Remove button(s) from the tool bar when the last routing layer is removed 243 addRouteNodeButton.setVisible(false); 244 removeRouteNodeButton.setVisible(false); 245 moveRouteNodeButton.setVisible(false); 246 menu.disableRestOfItems(); 247 layers.remove(oldLayer); 248 logger.debug("Removed routing layer."); 249 } else if (oldLayer instanceof OsmDataLayer) { 250 // Remove all associated routing layers 251 // Convert to Array to prevent ConcurrentModificationException when removing layers from ArrayList 252 // FIXME: can't remove associated routing layers without triggering exceptions in some cases 253 RoutingLayer[] layersArray = layers.toArray(new RoutingLayer[0]); 254 for (int i=0;i<layersArray.length;i++) { 255 if (layersArray[i].getDataLayer().equals(oldLayer)) { 256 try { 257 // Remove layer 258 Main.main.removeLayer(layersArray[i]); 259 } catch (IllegalArgumentException e) { 260 } 261 } 262 } 263 } 264 // Reload RoutingDialog table model 265 if (routingDialog != null) { 266 routingDialog.refresh(); 267 } 268 } 269 270 public void processDatasetEvent(AbstractDatasetChangedEvent event){ 271 272 } 273 274 @Override 275 public PreferenceSetting getPreferenceSetting() { 276 return preferenceSettings; 277 } 297 278 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingDialog.java
r28288 r30361 52 52 public class RoutingDialog extends ToggleDialog { 53 53 54 55 56 54 private final DefaultListModel model; 55 private JList jList = null; 56 private JScrollPane jScrollPane = null; 57 57 58 59 60 61 58 /** 59 * Serial UID 60 */ 61 private static final long serialVersionUID = 8625615652900341987L; 62 62 63 64 65 66 67 68 63 public RoutingDialog() { 64 super(tr("Routing"), "routing", tr("Open a list of routing nodes"), 65 Shortcut.registerShortcut("subwindow:routing", tr("Toggle: {0}", tr("Routing")), KeyEvent.VK_R, Shortcut.ALT_CTRL_SHIFT), 150); 66 model = new DefaultListModel(); 67 createLayout(getJScrollPane(), false, null); 68 } 69 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 70 /** 71 * This method initializes jScrollPane 72 * 73 * @return javax.swing.JScrollPane 74 */ 75 private JScrollPane getJScrollPane() { 76 if (jScrollPane == null) { 77 jScrollPane = new JScrollPane(); 78 jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); 79 jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); 80 jScrollPane.setViewportView(getJList()); 81 } 82 return jScrollPane; 83 } 84 84 85 86 87 88 89 90 91 92 93 94 95 96 97 85 /** 86 * This method initializes jList 87 * 88 * @return javax.swing.JList 89 */ 90 private JList getJList() { 91 if (jList == null) { 92 jList = new JList(); 93 jList.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 94 jList.setModel(model); 95 } 96 return jList; 97 } 98 98 99 100 101 102 103 104 105 99 /** 100 * Remove item from the list of nodes 101 * @param index 102 */ 103 public void removeNode(int index) { 104 model.remove(index); 105 } 106 106 107 108 109 110 111 112 113 107 /** 108 * Add item to the list of nodes 109 * @param obj 110 */ 111 public void addNode(Node n) { 112 model.addElement(n.getId()+" ["+n.getCoor().toDisplayString()+"]"); 113 } 114 114 115 116 117 118 119 120 121 122 115 /** 116 * Insert item to the list of nodes 117 * @param index 118 * @param obj 119 */ 120 public void insertNode(int index, Node n) { 121 model.insertElementAt(n.getId()+" ["+n.getCoor().toDisplayString()+"]", index); 122 } 123 123 124 125 126 127 128 129 124 /** 125 * Clear list of nodes 126 */ 127 public void clearNodes() { 128 model.clear(); 129 } 130 130 131 132 133 134 135 136 137 138 139 140 131 public void refresh() { 132 clearNodes(); 133 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 134 RoutingLayer routingLayer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 135 RoutingModel routingModel = routingLayer.getRoutingModel(); 136 for (Node n : routingModel.getSelectedNodes()) { 137 addNode(n); 138 } 139 } 140 } 141 141 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingMenu.java
r29784 r30361 59 59 public class RoutingMenu extends JMenu { 60 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 61 /** 62 * Default serial version UID 63 */ 64 private static final long serialVersionUID = 3559922048225708480L; 65 66 private final JMenuItem startMI; 67 private final JMenuItem reverseMI; 68 private final JMenuItem clearMI; 69 private final JMenuItem regraphMI; 70 private final JMenu criteriaM; 71 private final JMenu menu; 72 73 /** 74 */ 75 public RoutingMenu() { 76 MainMenu mm = Main.main.menu; 77 menu = mm.addMenu(marktr("Routing"), KeyEvent.VK_O, mm.getDefaultMenuPos(), ht("/Plugin/Routing")); 78 79 startMI = new JMenuItem(tr("Add routing layer")); 80 startMI.addActionListener(new ActionListener() { 81 public void actionPerformed(ActionEvent e) { 82 RoutingPlugin.getInstance().addLayer(); 83 } 84 }); 85 menu.add(startMI); 86 87 menu.addSeparator(); 88 ButtonGroup group = new ButtonGroup(); 89 90 criteriaM = new JMenu(tr("Criteria")); 91 92 JRadioButtonMenuItem rshorter = new JRadioButtonMenuItem(tr("Shortest")); 93 rshorter.setSelected(true); 94 rshorter.addItemListener(new ItemListener() { 95 public void itemStateChanged(ItemEvent e) { 96 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 97 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 98 RoutingModel routingModel = layer.getRoutingModel(); 99 if (e.getStateChange()==ItemEvent.SELECTED) { 100 routingModel.routingGraph.setTypeRoute(RouteType.SHORTEST); 101 } else { 102 routingModel.routingGraph.setTypeRoute(RouteType.FASTEST); 103 } 104 // routingModel.routingGraph.resetGraph(); 105 // routingModel.routingGraph.createGraph(); 106 //TODO: Change this way 107 //FIXME: do not change node but recalculate routing. 108 routingModel.setNodesChanged(); 109 Main.map.repaint(); 110 } 111 } 112 113 }); 114 115 JRadioButtonMenuItem rfaster = new JRadioButtonMenuItem(tr("Fastest")); 116 group.add(rshorter); 117 group.add(rfaster); 118 criteriaM.add(rshorter); 119 criteriaM.add(rfaster); 120 121 criteriaM.addSeparator(); 122 JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(tr("Ignore oneways")); 123 cbmi.addItemListener(new ItemListener() { 124 public void itemStateChanged(ItemEvent e) { 125 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 126 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 127 RoutingModel routingModel = layer.getRoutingModel(); 128 if (e.getStateChange()==ItemEvent.SELECTED) 129 routingModel.routingGraph.getRoutingProfile().setOnewayUse(false); 130 else 131 routingModel.routingGraph.getRoutingProfile().setOnewayUse(true); 132 routingModel.setNodesChanged(); 133 routingModel.setOnewayChanged(); 134 Main.map.repaint(); 135 } 136 } 137 }); 138 criteriaM.add(cbmi); 139 menu.add(criteriaM); 140 141 menu.addSeparator(); 142 reverseMI = new JMenuItem(tr("Reverse route")); 143 reverseMI.addActionListener(new ActionListener() { 144 public void actionPerformed(ActionEvent e) { 145 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 146 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 147 RoutingModel routingModel = layer.getRoutingModel(); 148 routingModel.reverseNodes(); 149 Main.map.repaint(); 150 } 151 } 152 }); 153 menu.add(reverseMI); 154 155 clearMI = new JMenuItem(tr("Clear route")); 156 clearMI.addActionListener(new ActionListener() { 157 public void actionPerformed(ActionEvent e) { 158 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 159 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 160 RoutingModel routingModel = layer.getRoutingModel(); 161 // Reset routing nodes and paths 162 routingModel.reset(); 163 RoutingPlugin.getInstance().getRoutingDialog().clearNodes(); 164 Main.map.repaint(); 165 } 166 } 167 }); 168 menu.add(clearMI); 169 170 regraphMI = new JMenuItem(tr("Reconstruct Graph")); 171 regraphMI.addActionListener(new ActionListener() { 172 public void actionPerformed(ActionEvent e) { 173 174 if (Main.map.mapView.getActiveLayer() instanceof RoutingLayer) { 175 RoutingLayer layer = (RoutingLayer)Main.map.mapView.getActiveLayer(); 176 RoutingModel routingModel = layer.getRoutingModel(); 177 routingModel.routingGraph.resetGraph(); 178 routingModel.routingGraph.createGraph(); 179 } 180 181 } 182 }); 183 menu.add(regraphMI); 184 185 186 // Initially disabled 187 disableAllItems(); 188 } 189 190 public void disableAllItems() { 191 startMI.setEnabled(false); 192 reverseMI.setEnabled(false); 193 clearMI.setEnabled(false); 194 criteriaM.setEnabled(false); 195 regraphMI.setEnabled(false); 196 } 197 198 public void enableStartItem() { 199 startMI.setEnabled(true); 200 } 201 202 public void enableRestOfItems() { 203 reverseMI.setEnabled(true); 204 clearMI.setEnabled(true); 205 criteriaM.setEnabled(true); 206 regraphMI.setEnabled(true); 207 } 208 209 public void disableRestOfItems() { 210 reverseMI.setEnabled(false); 211 clearMI.setEnabled(false); 212 criteriaM.setEnabled(false); 213 regraphMI.setEnabled(false); 214 } 215 215 } -
applications/editors/josm/plugins/routing/src/com/innovant/josm/plugin/routing/gui/RoutingPreferenceDialog.java
r28288 r30361 63 63 public class RoutingPreferenceDialog extends DefaultTabPreferenceSetting { 64 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 65 /** 66 * Logger 67 */ 68 static Logger logger = Logger.getLogger(RoutingPreferenceDialog.class); 69 70 private Map<String, String> orig; 71 private DefaultTableModel model; 72 73 /** 74 * Constructor 75 */ 76 public RoutingPreferenceDialog() { 77 super("routing", tr("Routing Plugin Preferences"), tr("Configure routing preferences.")); 78 readPreferences(); 79 } 80 81 public void addGui(final PreferenceTabbedPane gui) { 82 83 JPanel principal = gui.createPreferenceTab(this); 84 85 JPanel p = new JPanel(); 86 p.setLayout(new GridBagLayout()); 87 88 model = new DefaultTableModel(new String[] { tr("Highway type"), 89 tr("Speed (Km/h)") }, 0) { 90 private static final long serialVersionUID = 4253339034781567453L; 91 92 @Override 93 public boolean isCellEditable(int row, int column) { 94 return column != 0; 95 } 96 }; 97 final JTable list = new JTable(model); 98 loadSpeeds(model); 99 100 JScrollPane scroll = new JScrollPane(list); 101 102 p.add(scroll, GBC.eol().fill(GBC.BOTH)); 103 scroll.setPreferredSize(new Dimension(200, 200)); 104 105 JButton add = new JButton(tr("Add")); 106 p.add(Box.createHorizontalGlue(), GBC.std().fill(GBC.HORIZONTAL)); 107 p.add(add, GBC.std().insets(0, 5, 0, 0)); 108 add.addActionListener(new ActionListener() { 109 public void actionPerformed(ActionEvent e) { 110 JPanel p = new JPanel(new GridBagLayout()); 111 p.add(new JLabel(tr("Weight")), GBC.std().insets(0, 0, 5, 0)); 112 JComboBox key = new JComboBox(); 113 for (OsmWayTypes pk : OsmWayTypes.values()) 114 key.addItem(pk.getTag()); 115 JTextField value = new JTextField(10); 116 p.add(key, GBC.eop().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL)); 117 p.add(new JLabel(tr("Value")), GBC.std().insets(0, 0, 5, 0)); 118 p.add(value, GBC.eol().insets(5, 0, 0, 0).fill(GBC.HORIZONTAL)); 119 int answer = JOptionPane.showConfirmDialog(gui, p, 120 tr("Enter weight values"), 121 JOptionPane.OK_CANCEL_OPTION); 122 if (answer == JOptionPane.OK_OPTION) { 123 model 124 .addRow(new String[] { 125 key.getSelectedItem().toString(), 126 value.getText() }); 127 } 128 } 129 }); 130 131 JButton delete = new JButton(tr("Delete")); 132 p.add(delete, GBC.std().insets(0, 5, 0, 0)); 133 delete.addActionListener(new ActionListener() { 134 public void actionPerformed(ActionEvent e) { 135 if (list.getSelectedRow() == -1) 136 JOptionPane.showMessageDialog(gui, 137 tr("Please select the row to delete.")); 138 else { 139 Integer i; 140 while ((i = list.getSelectedRow()) != -1) 141 model.removeRow(i); 142 } 143 } 144 }); 145 146 JButton edit = new JButton(tr("Edit")); 147 p.add(edit, GBC.std().insets(5, 5, 5, 0)); 148 edit.addActionListener(new ActionListener() { 149 public void actionPerformed(ActionEvent e) { 150 edit(gui, list); 151 } 152 }); 153 154 JTabbedPane Opciones = new JTabbedPane(); 155 Opciones.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT); 156 157 Opciones.addTab("Profile", null, p, null); 158 // Opciones.addTab("Preferences", new JPanel()); 159 160 list.addMouseListener(new MouseAdapter(){ 161 @Override public void mouseClicked(MouseEvent e) { 162 if (e.getClickCount() == 2) 163 edit(gui, list); 164 } 165 }); 166 167 principal.add(Opciones, GBC.eol().fill(GBC.BOTH)); 168 169 } 170 171 public boolean ok() { 172 for (int i = 0; i < model.getRowCount(); ++i) { 173 String value = model.getValueAt(i, 1).toString(); 174 if (value.length() != 0) { 175 String key = model.getValueAt(i, 0).toString(); 176 String origValue = orig.get(key); 177 if (origValue == null || !origValue.equals(value)) 178 Main.pref.put(key, value); 179 orig.remove(key); // processed. 180 } 181 } 182 for (Entry<String, String> e : orig.entrySet()) 183 Main.pref.put(e.getKey(), null); 184 return false; 185 } 186 187 private void edit(final PreferenceTabbedPane gui, final JTable list) { 188 if (list.getSelectedRowCount() != 1) { 189 JOptionPane.showMessageDialog(gui, 190 tr("Please select the row to edit.")); 191 return; 192 } 193 String v = JOptionPane.showInputDialog(tr("New value for {0}", model 194 .getValueAt(list.getSelectedRow(), 0)), model.getValueAt(list 195 .getSelectedRow(), 1)); 196 if (v != null) 197 model.setValueAt(v, list.getSelectedRow(), 1); 198 } 199 200 private void loadSpeeds(DefaultTableModel model) { 201 // Read dialog values from preferences 202 readPreferences(); 203 // Put these values in the model 204 for (String tag : orig.keySet()) { 205 model.addRow(new String[] { tag, orig.get(tag) }); 206 } 207 } 208 209 private void readPreferences() { 210 orig = Main.pref.getAllPrefix("routing.profile.default.speed"); 211 if (orig.size() == 0) { // defaults 212 logger.debug("Loading Default Preferences."); 213 for (OsmWayTypes owt : OsmWayTypes.values()) { 214 Main.pref.putInteger("routing.profile.default.speed." 215 + owt.getTag(), owt.getSpeed()); 216 } 217 orig = Main.pref.getAllPrefix("routing.profile.default.speed"); 218 } 219 else logger.debug("Default preferences already exist."); 220 } 221 /* 222 private String getKeyTag(String tag) { 223 return tag.split(".", 5)[4]; 224 } 225 226 private String getTypeTag(String tag) { 227 return tag.split(".", 5)[3]; 228 } 229 230 private String getNameTag(String tag) { 231 return tag.split(".", 5)[2]; 232 } 233 */ 234 234 }
Note:
See TracChangeset
for help on using the changeset viewer.