Changeset 26481 in osm for applications/editors/josm
- Timestamp:
- 2011-08-07T19:46:22+02:00 (13 years ago)
- Location:
- applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/plugin
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/plugin/GraphViewPlugin.java
r26345 r26481 119 119 graphViewLayer.setWayGraph(graph); 120 120 graphViewLayer.setColorScheme(preferences.getCurrentColorScheme()); 121 graphViewLayer.setArrowheadPlacement(preferences.getArrowheadPlacement()); 121 122 graphViewLayer.setNodePositioner(new DefaultNodePositioner()); 122 123 … … 269 270 if (graphViewLayer != null) { 270 271 graphViewLayer.setColorScheme(preferences.getCurrentColorScheme()); 272 graphViewLayer.setArrowheadPlacement(preferences.getArrowheadPlacement()); 271 273 } 272 274 } -
applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/plugin/dialogs/GraphViewPreferenceEditor.java
r26466 r26481 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.awt.BasicStroke; 5 6 import java.awt.Color; 7 import java.awt.Dimension; 8 import java.awt.Graphics; 9 import java.awt.Graphics2D; 6 10 import java.awt.GridBagConstraints; 7 11 import java.awt.GridBagLayout; 8 12 import java.awt.GridLayout; 13 import java.awt.Point; 9 14 import java.awt.event.ActionEvent; 10 15 import java.awt.event.ActionListener; 16 import java.awt.geom.Line2D; 11 17 import java.io.File; 12 18 import java.util.Collection; … … 25 31 import javax.swing.JOptionPane; 26 32 import javax.swing.JPanel; 33 import javax.swing.JSlider; 27 34 import javax.swing.JTextField; 35 import javax.swing.event.ChangeEvent; 36 import javax.swing.event.ChangeListener; 28 37 29 38 import org.openstreetmap.josm.gui.preferences.PreferenceSetting; 30 39 import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane; 31 40 import org.openstreetmap.josm.plugins.graphview.plugin.dialogs.AccessParameterDialog.BookmarkAction; 41 import org.openstreetmap.josm.plugins.graphview.plugin.layer.GraphViewLayer; 32 42 import org.openstreetmap.josm.plugins.graphview.plugin.preferences.GraphViewPreferenceDefaults; 33 43 import org.openstreetmap.josm.plugins.graphview.plugin.preferences.GraphViewPreferences; … … 56 66 private JButton nodeColorButton; 57 67 private JPanel nodeColorField; 58 68 private JButton arrowheadFillColorButton; 69 private JPanel arrowheadFillColorField; 70 private JSlider arrowheadPlacementSlider; 71 private JPanel arrowPreviewPanel; 72 59 73 public void addGui(PreferenceTabbedPane gui) { 60 74 … … 182 196 return vehiclePanel; 183 197 } 184 198 185 199 private JPanel createVisualizationPanel() { 186 200 187 201 JPanel visualizationPanel = new JPanel(); 188 202 visualizationPanel.setBorder(BorderFactory.createTitledBorder(tr("Visualization"))); 189 203 visualizationPanel.setLayout(new BoxLayout(visualizationPanel, BoxLayout.Y_AXIS)); 190 204 191 205 separateDirectionsCheckBox = new JCheckBox(tr("Draw directions separately")); 192 206 separateDirectionsCheckBox.setSelected(GraphViewPreferences.getInstance().getSeparateDirections()); 193 207 visualizationPanel.add(separateDirectionsCheckBox); 194 208 195 209 { // create color chooser panel 196 210 197 211 JPanel colorPanel = new JPanel(); 198 colorPanel.setLayout(new GridLayout( 2, 2));199 212 colorPanel.setLayout(new GridLayout(3, 2)); 213 200 214 Color nodeColor = GraphViewPreferences.getInstance().getNodeColor(); 201 215 202 216 nodeColorButton = new JButton(tr("Node color")); 203 217 nodeColorButton.addActionListener(chooseNodeColorActionListener); … … 206 220 nodeColorField.setBackground(nodeColor); 207 221 colorPanel.add(nodeColorField); 208 222 209 223 Color segmentColor = GraphViewPreferences.getInstance().getSegmentColor(); 210 224 211 225 segmentColorButton = new JButton(tr("Arrow color")); 212 226 segmentColorButton.addActionListener(chooseSegmentColorActionListener); … … 215 229 segmentColorField.setBackground(segmentColor); 216 230 colorPanel.add(segmentColorField); 217 231 232 Color arrowheadFillColor = GraphViewPreferences.getInstance().getArrowheadFillColor(); 233 234 arrowheadFillColorButton = new JButton(tr("Arrowhead fill color")); 235 arrowheadFillColorButton.addActionListener(chooseArrowheadFillColorActionListener); 236 colorPanel.add(arrowheadFillColorButton); 237 arrowheadFillColorField = new JPanel(); 238 arrowheadFillColorField.setBackground(arrowheadFillColor); 239 colorPanel.add(arrowheadFillColorField); 240 218 241 visualizationPanel.add(colorPanel); 219 242 220 243 } 221 244 245 arrowheadPlacementSlider = new JSlider(0, 100); 246 arrowheadPlacementSlider.setToolTipText(tr("Arrowhead placement")); 247 arrowheadPlacementSlider.setMajorTickSpacing(10); 248 arrowheadPlacementSlider.setPaintTicks(true); 249 arrowheadPlacementSlider.setName("name"); 250 arrowheadPlacementSlider.setLabelTable(null); 251 arrowheadPlacementSlider.setValue((int)Math.round( 252 100 * GraphViewPreferences.getInstance().getArrowheadPlacement())); 253 arrowheadPlacementSlider.addChangeListener(arrowheadPlacementChangeListener); 254 visualizationPanel.add(arrowheadPlacementSlider); 255 256 arrowPreviewPanel = new ArrowPreviewPanel(); 257 visualizationPanel.add(arrowPreviewPanel); 258 222 259 return visualizationPanel; 223 260 } … … 239 276 preferences.setNodeColor(nodeColorField.getBackground()); 240 277 preferences.setSegmentColor(segmentColorField.getBackground()); 241 278 preferences.setArrowheadFillColor(arrowheadFillColorField.getBackground()); 279 280 preferences.setArrowheadPlacement( 281 arrowheadPlacementSlider.getValue() / 100f); 282 242 283 preferences.distributeChanges(); 243 284 … … 371 412 } 372 413 }; 373 414 374 415 private final ActionListener chooseNodeColorActionListener = new ActionListener() { 375 416 public void actionPerformed(ActionEvent e) { 376 417 377 418 Color selectedColor = JColorChooser.showDialog( 378 419 preferencePanel, tr("Choose node color"), nodeColorField.getBackground()); 379 420 380 421 if (selectedColor != null) { 381 422 nodeColorField.setBackground(selectedColor); 382 423 } 383 384 } 385 }; 386 424 425 arrowPreviewPanel.repaint(); 426 427 } 428 }; 429 387 430 private final ActionListener chooseSegmentColorActionListener = new ActionListener() { 388 431 public void actionPerformed(ActionEvent e) { 389 432 390 433 Color selectedColor = JColorChooser.showDialog( 391 434 preferencePanel, tr("Choose arrow color"), segmentColorField.getBackground()); 392 435 393 436 if (selectedColor != null) { 394 437 segmentColorField.setBackground(selectedColor); 395 438 } 396 439 440 arrowPreviewPanel.repaint(); 441 442 } 443 }; 444 445 private final ActionListener chooseArrowheadFillColorActionListener = new ActionListener() { 446 public void actionPerformed(ActionEvent e) { 447 448 Color selectedColor = JColorChooser.showDialog( 449 preferencePanel, tr("Choose arrowhead fill color"), segmentColorField.getBackground()); 450 451 if (selectedColor != null) { 452 arrowheadFillColorField.setBackground(selectedColor); 453 } 454 455 arrowPreviewPanel.repaint(); 456 457 } 458 }; 459 460 private final ChangeListener arrowheadPlacementChangeListener = new ChangeListener() { 461 public void stateChanged(ChangeEvent e) { 462 arrowPreviewPanel.repaint(); 397 463 } 398 464 }; … … 426 492 } 427 493 494 private class ArrowPreviewPanel extends JPanel { 495 496 public ArrowPreviewPanel() { 497 setPreferredSize(new Dimension(100, 50)); 498 setBackground(Color.DARK_GRAY); 499 } 500 501 @Override 502 public void paint(Graphics g) { 503 504 super.paint(g); 505 506 Graphics2D g2D = (Graphics2D)g; 507 508 Point p1 = new Point(15, this.getHeight() / 2); 509 Point p2 = new Point(this.getWidth()-15, this.getHeight() / 2); 510 511 g2D.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND)); 512 g2D.setColor(segmentColorField.getBackground()); 513 g2D.draw(new Line2D.Float(p1.x, p1.y, p2.x, p2.y)); 514 515 GraphViewLayer.paintNode(g, p1, nodeColorField.getBackground()); 516 GraphViewLayer.paintNode(g, p2, nodeColorField.getBackground()); 517 518 GraphViewLayer.paintArrowhead(g2D, p1, p2, 519 arrowheadPlacementSlider.getValue() / 100.0, 520 segmentColorField.getBackground(), 521 arrowheadFillColorField.getBackground()); 522 523 } 524 525 } 526 428 527 } -
applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/plugin/layer/GraphViewLayer.java
r26174 r26481 66 66 private static final Shape ARROW_HEAD; 67 67 68 /** arrow head core, can be colored differently and is rendered on top */ 69 private static final Shape ARROW_HEAD_CORE; 70 68 71 static { 69 72 … … 71 74 72 75 head.addPoint( 0, 0); 73 head.addPoint(- 15, +4);74 head.addPoint(- 15, -4);76 head.addPoint(-22, +6); 77 head.addPoint(-22, -6); 75 78 76 79 ARROW_HEAD = head; 80 81 Polygon headCore = new Polygon(); 82 83 headCore.addPoint(-12, 0); 84 headCore.addPoint(-19, +2); 85 headCore.addPoint(-19, -2); 86 87 ARROW_HEAD_CORE = headCore; 88 89 77 90 } 78 91 … … 80 93 81 94 private ColorScheme colorScheme = null; 95 private Double arrowheadPlacement = null; 82 96 private NodePositioner nodePositioner = new NonMovingNodePositioner(); 83 97 … … 101 115 public void setColorScheme(ColorScheme colorScheme) { 102 116 this.colorScheme = colorScheme; 117 Main.panel.repaint(); 118 } 119 120 /** sets the arrowhead placement (relative offset from edge start) */ 121 public void setArrowheadPlacement(double arrowheadPlacement) { 122 assert arrowheadPlacement >= 0 && arrowheadPlacement <= 1; 123 this.arrowheadPlacement = arrowheadPlacement; 103 124 Main.panel.repaint(); 104 125 } … … 124 145 125 146 Color color = colorScheme != null ? colorScheme.getNodeColor(node) : Color.LIGHT_GRAY; 126 127 147 Point p = getNodePoint(node, mv); 128 g.setColor(color); 148 149 paintNode(g, p, color); 150 151 } 152 153 public static void paintNode(final Graphics g, Point p, Color color) { 154 g.setColor(color); 129 155 g.fillOval(p.x - NODE_RADIUS, p.y - NODE_RADIUS, 2 * NODE_RADIUS, 2 * NODE_RADIUS); 130 131 } 132 133 private void paintGraphEdge(final GraphEdge e, final Graphics2D g2D, final MapView mv) {156 } 157 158 private void paintGraphEdge(final GraphEdge e, final Graphics2D g2D, final MapView mv, 159 boolean drawLine, boolean drawDirectionIndicator) { 134 160 135 161 if (!CONNECT_ALL_NODE_PAIRS && GraphViewPreferences.getInstance().getSeparateDirections()) { … … 175 201 } 176 202 177 g2D.draw(new Line2D.Float(p1.x, p1.y, p2.x, p2.y)); 203 if (drawLine) { 204 g2D.draw(new Line2D.Float(p1.x, p1.y, p2.x, p2.y)); 205 } 178 206 179 207 } … … 183 211 Color color = GraphViewPreferences.getInstance().getSegmentColor(); 184 212 g2D.setColor(color); 185 213 186 214 Point p1 = getNodePoint(e.getStartNode(), mv); 187 215 Point p2 = getNodePoint(e.getTargetNode(), mv); 188 216 189 g2D.draw(new Line2D.Float(p1.x, p1.y, p2.x, p2.y)); 217 if (drawLine) { 218 g2D.draw(new Line2D.Float(p1.x, p1.y, p2.x, p2.y)); 219 } 190 220 191 221 } … … 197 227 Point p2 = getNodePoint(e.getTargetNode(), mv); 198 228 199 if (edgeSegments.size() > 0) {229 if (edgeSegments.size() > 1) { 200 230 Segment lastSegment = edgeSegments.get(edgeSegments.size() - 1); 201 231 p1 = getNodePoint(lastSegment.getNode1(), mv); 202 232 } 203 233 204 double angle = angleFromXAxis(p1, p2); // angle between x-axis and [p1,p2] 205 Shape head = ARROW_HEAD; 206 head = AffineTransform.getRotateInstance(angle).createTransformedShape(head); 207 head = AffineTransform.getTranslateInstance(p2.x, p2.y).createTransformedShape(head); 208 209 g2D.fill(head); 210 211 } 212 } 234 if (drawDirectionIndicator) { 235 236 paintArrowhead(g2D, p1, p2, arrowheadPlacement, null, 237 GraphViewPreferences.getInstance().getArrowheadFillColor()); 238 239 } 240 241 } 242 } 243 244 public static void paintArrowhead(Graphics2D g2D, 245 Point p1, Point p2, Double arrowheadPlacement2, 246 Color casingColor, Color fillColor) { 247 248 Point pTip = new Point( 249 (int)(p1.x + arrowheadPlacement2 * (p2.x - p1.x)), 250 (int)(p1.y + arrowheadPlacement2 * (p2.y - p1.y))); 251 252 double angle = angleFromXAxis(p1, p2); // angle between x-axis and [p1,p2] 253 254 { //draw head shape 255 256 if (casingColor != null) { 257 g2D.setColor(casingColor); 258 } 259 260 Shape head = ARROW_HEAD; 261 head = AffineTransform.getRotateInstance(angle).createTransformedShape(head); 262 head = AffineTransform.getTranslateInstance(pTip.x, pTip.y).createTransformedShape(head); 263 g2D.fill(head); 264 } 265 266 { //draw head core shape 267 268 if (fillColor != null) { 269 g2D.setColor(fillColor); 270 } 271 272 Shape headCore = ARROW_HEAD_CORE; 273 headCore = AffineTransform.getRotateInstance(angle).createTransformedShape(headCore); 274 headCore = AffineTransform.getTranslateInstance(pTip.x, pTip.y).createTransformedShape(headCore); 275 g2D.fill(headCore); 276 } 277 278 } 213 279 214 280 private Point getNodePoint(GraphNode node, MapView mv) { … … 247 313 return nodePoint; 248 314 } 249 private Point getNodePoint(SegmentNode node, MapView mv) { 315 private static Point getNodePoint(SegmentNode node, MapView mv) { 250 316 LatLonCoords coords = new LatLonCoords(node.getLat(), node.getLon()); 251 317 return getNodePoint(coords, mv); 252 318 } 253 private Point getNodePoint(LatLonCoords coords, MapView mv) { 319 private static Point getNodePoint(LatLonCoords coords, MapView mv) { 254 320 LatLon latLon = new LatLon(coords.getLat(), coords.getLon()); 255 321 EastNorth eastNorth = Main.getProjection().latlon2eastNorth(latLon); … … 263 329 * @return angle in radians, in range [-Pi .. +Pi] 264 330 */ 265 private double angleFromXAxis(Point p1, Point p2) { 331 private static double angleFromXAxis(Point p1, Point p2) { 266 332 assert p1 != null && p2 != null; 267 333 … … 293 359 294 360 for (GraphEdge e : wayGraph.getEdges()) { 295 paintGraphEdge(e, g, mv); 361 paintGraphEdge(e, g, mv, true, false); 362 } 363 364 for (GraphEdge e : wayGraph.getEdges()) { 365 //draw arrowheads last to make sure they end up on top 366 paintGraphEdge(e, g, mv, false, true); 296 367 } 297 368 -
applications/editors/josm/plugins/graphview/src/org/openstreetmap/josm/plugins/graphview/plugin/preferences/GraphViewPreferences.java
r26174 r26481 1 1 package org.openstreetmap.josm.plugins.graphview.plugin.preferences; 2 3 import static org.openstreetmap.josm.tools.I18n.marktr;4 2 5 3 import static org.openstreetmap.josm.plugins.graphview.core.property.VehiclePropertyTypes.AXLELOAD; … … 38 36 * changes will also be synchronized (two-way) with JOSM's preference storage. 39 37 * This is a singleton class. 40 * 38 * 41 39 * Note: Currently, manual updates in the "advanced preferences" will not have any effect 42 40 * because this class isn't registered as a preference listener. … … 69 67 private Color nodeColor; 70 68 private Color segmentColor; 69 private Color arrowheadFillColor; 71 70 72 71 private boolean separateDirections; 73 72 73 private double arrowheadPlacement; 74 74 75 75 public synchronized boolean getUseInternalRulesets() { … … 168 168 } 169 169 170 public synchronized boolean getSeparateDirections() { 170 public synchronized Color getArrowheadFillColor() { 171 return arrowheadFillColor; 172 } 173 public synchronized void setArrowheadFillColor(Color arrowheadFillColor) { 174 this.arrowheadFillColor = arrowheadFillColor; 175 } 176 177 public synchronized boolean getSeparateDirections() { 171 178 return separateDirections; 172 179 } 173 180 public synchronized void setSeparateDirections(boolean separateDirections) { 174 181 this.separateDirections = separateDirections; 182 } 183 184 public synchronized double getArrowheadPlacement() { 185 return arrowheadPlacement; 186 } 187 public synchronized void setArrowheadPlacement(double arrowheadPlacement) { 188 this.arrowheadPlacement = arrowheadPlacement; 175 189 } 176 190 … … 220 234 currentColorScheme = new PreferencesColorScheme(this); 221 235 236 nodeColor = Color.WHITE; 237 segmentColor = Color.WHITE; 238 arrowheadFillColor = Color.BLACK; 239 222 240 separateDirections = false; 223 241 … … 244 262 } 245 263 246 Main.pref.putColor(marktr("GraphView default node"), nodeColor); 247 Main.pref.putColor(marktr("Graphview default segment"), segmentColor); 264 Main.pref.put("graphview.defaultNodeColor", createColorString(nodeColor)); 265 Main.pref.put("graphview.defaultSegmentColor", createColorString(segmentColor)); 266 Main.pref.put("graphview.defaultArrowheadCoreColor", createColorString(arrowheadFillColor)); 248 267 249 268 Main.pref.put("graphview.separateDirections", separateDirections); 269 270 Main.pref.putDouble("graphview.arrowheadPlacement", arrowheadPlacement); 250 271 251 272 } … … 289 310 } 290 311 291 nodeColor = Main.pref.getColor(marktr("GraphView default node"), Color.white); 292 segmentColor = Main.pref.getColor(marktr("Graphview default segment"), Color.white); 312 if (Main.pref.hasKey("graphview.defaultNodeColor")) { 313 Color color = parseColorString(Main.pref.get("graphview.defaultNodeColor")); 314 if (color != null) { 315 nodeColor = color; 316 } 317 } 318 if (Main.pref.hasKey("graphview.defaultSegmentColor")) { 319 Color color = parseColorString(Main.pref.get("graphview.defaultSegmentColor")); 320 if (color != null) { 321 segmentColor = color; 322 } 323 } 324 if (Main.pref.hasKey("graphview.defaultArrowheadCoreColor")) { 325 Color color = parseColorString(Main.pref.get("graphview.defaultArrowheadCoreColor")); 326 if (color != null) { 327 arrowheadFillColor = color; 328 } 329 } 330 293 331 separateDirections = Main.pref.getBoolean("graphview.separateDirections", false); 332 333 arrowheadPlacement = Main.pref.getDouble("graphview.arrowheadPlacement", 1.0); 334 if (arrowheadPlacement < 0.0 || arrowheadPlacement >= 1.0) { 335 arrowheadPlacement = 1.0; 336 } 294 337 295 338 } … … 460 503 } 461 504 } 505 506 private static final Pattern COLOR_PATTERN = 507 Pattern.compile("^(\\d{1,3}),\\s*(\\d{1,3}),\\s*(\\d{1,3})$"); 508 509 private String createColorString(Color color) { 510 return color.getRed() + ", " + color.getGreen() + ", " + color.getBlue(); 511 } 512 513 private Color parseColorString(String string) { 514 Matcher matcher = COLOR_PATTERN.matcher(string); 515 if (!matcher.matches()) { 516 return null; 517 } else { 518 int r = Integer.parseInt(matcher.group(1)); 519 int g = Integer.parseInt(matcher.group(2)); 520 int b = Integer.parseInt(matcher.group(3)); 521 return new Color(r, g, b); 522 } 523 } 524 462 525 }
Note:
See TracChangeset
for help on using the changeset viewer.