[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[115] | 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
[3754] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[804] | 5 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
| 6 |
|
---|
| 7 | import java.awt.Color;
|
---|
[10078] | 8 | import java.awt.Dimension;
|
---|
[115] | 9 | import java.awt.Graphics;
|
---|
| 10 | import java.awt.geom.Rectangle2D;
|
---|
| 11 |
|
---|
[9954] | 12 | import javax.accessibility.Accessible;
|
---|
| 13 | import javax.accessibility.AccessibleContext;
|
---|
| 14 | import javax.accessibility.AccessibleValue;
|
---|
[115] | 15 | import javax.swing.JComponent;
|
---|
| 16 |
|
---|
[12987] | 17 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
[2252] | 18 | import org.openstreetmap.josm.gui.help.Helpful;
|
---|
[116] | 19 |
|
---|
[9954] | 20 | /**
|
---|
| 21 | * Map scale bar, displaying the distance in meter that correspond to 100 px on screen.
|
---|
| 22 | * @since 115
|
---|
| 23 | */
|
---|
| 24 | public class MapScaler extends JComponent implements Helpful, Accessible {
|
---|
[115] | 25 |
|
---|
[1169] | 26 | private final NavigatableComponent mv;
|
---|
[3530] | 27 |
|
---|
[10079] | 28 | private static final int PADDING_LEFT = 5;
|
---|
| 29 | private static final int PADDING_RIGHT = 50;
|
---|
[3530] | 30 |
|
---|
[12987] | 31 | private static final NamedColorProperty SCALER_COLOR = new NamedColorProperty(marktr("scale"), Color.WHITE);
|
---|
[10853] | 32 |
|
---|
[9954] | 33 | /**
|
---|
| 34 | * Constructs a new {@code MapScaler}.
|
---|
| 35 | * @param mv map view
|
---|
| 36 | */
|
---|
[1722] | 37 | public MapScaler(NavigatableComponent mv) {
|
---|
[1169] | 38 | this.mv = mv;
|
---|
[10078] | 39 | setPreferredLineLength(100);
|
---|
[1169] | 40 | setOpaque(false);
|
---|
[115] | 41 | }
|
---|
| 42 |
|
---|
[10078] | 43 | /**
|
---|
| 44 | * Sets the preferred length the distance line should have.
|
---|
| 45 | * @param pixel The length.
|
---|
| 46 | */
|
---|
| 47 | public void setPreferredLineLength(int pixel) {
|
---|
| 48 | setPreferredSize(new Dimension(pixel + PADDING_LEFT + PADDING_RIGHT, 30));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[6792] | 51 | @Override
|
---|
| 52 | public void paint(Graphics g) {
|
---|
[1221] | 53 | g.setColor(getColor());
|
---|
[10078] | 54 |
|
---|
| 55 | double dist100Pixel = mv.getDist100Pixel(true);
|
---|
| 56 | TickMarks tickMarks = new TickMarks(dist100Pixel, getWidth() - PADDING_LEFT - PADDING_RIGHT);
|
---|
| 57 | tickMarks.paintTicks(g);
|
---|
[115] | 58 | }
|
---|
[155] | 59 |
|
---|
[9954] | 60 | /**
|
---|
| 61 | * Returns the color of map scaler.
|
---|
| 62 | * @return the color of map scaler
|
---|
| 63 | */
|
---|
[6889] | 64 | public static Color getColor() {
|
---|
[10853] | 65 | return SCALER_COLOR.get();
|
---|
[1221] | 66 | }
|
---|
| 67 |
|
---|
[6084] | 68 | @Override
|
---|
[1169] | 69 | public String helpTopic() {
|
---|
[3754] | 70 | return ht("/MapView/Scaler");
|
---|
[155] | 71 | }
|
---|
[9954] | 72 |
|
---|
| 73 | @Override
|
---|
| 74 | public AccessibleContext getAccessibleContext() {
|
---|
| 75 | if (accessibleContext == null) {
|
---|
| 76 | accessibleContext = new AccessibleMapScaler();
|
---|
| 77 | }
|
---|
| 78 | return accessibleContext;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | class AccessibleMapScaler extends AccessibleJComponent implements AccessibleValue {
|
---|
| 82 |
|
---|
| 83 | @Override
|
---|
| 84 | public Number getCurrentAccessibleValue() {
|
---|
| 85 | return mv.getDist100Pixel();
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | @Override
|
---|
| 89 | public boolean setCurrentAccessibleValue(Number n) {
|
---|
| 90 | return false;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | @Override
|
---|
| 94 | public Number getMinimumAccessibleValue() {
|
---|
| 95 | return null;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | @Override
|
---|
| 99 | public Number getMaximumAccessibleValue() {
|
---|
| 100 | return null;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[10078] | 103 |
|
---|
| 104 | /**
|
---|
| 105 | * This class finds the best possible tick mark positions.
|
---|
| 106 | * <p>
|
---|
| 107 | * It will attempt to use steps of 1m, 2.5m, 10m, 25m, ...
|
---|
| 108 | */
|
---|
| 109 | private static final class TickMarks {
|
---|
| 110 |
|
---|
| 111 | private final double dist100Pixel;
|
---|
| 112 | /**
|
---|
| 113 | * Distance in meters between two ticks.
|
---|
| 114 | */
|
---|
| 115 | private final double spacingMeter;
|
---|
| 116 | private final int steps;
|
---|
[10504] | 117 | private final int minorStepsPerMajor;
|
---|
[10078] | 118 |
|
---|
| 119 | /**
|
---|
| 120 | * Creates a new tick mark helper.
|
---|
| 121 | * @param dist100Pixel The distance of 100 pixel on the map.
|
---|
| 122 | * @param width The width of the mark.
|
---|
| 123 | */
|
---|
[10079] | 124 | TickMarks(double dist100Pixel, int width) {
|
---|
[10078] | 125 | this.dist100Pixel = dist100Pixel;
|
---|
[11534] | 126 | double lineDistance = dist100Pixel * width / 100;
|
---|
[10078] | 127 |
|
---|
| 128 | double log10 = Math.log(lineDistance) / Math.log(10);
|
---|
| 129 | double spacingLog10 = Math.pow(10, Math.floor(log10));
|
---|
[10504] | 130 | int minorStepsPerMajor;
|
---|
| 131 | double distanceBetweenMinor;
|
---|
[10078] | 132 | if (log10 - Math.floor(log10) < .75) {
|
---|
[10504] | 133 | // Add 2 ticks for every full unit
|
---|
| 134 | distanceBetweenMinor = spacingLog10 / 2;
|
---|
| 135 | minorStepsPerMajor = 2;
|
---|
[10078] | 136 | } else {
|
---|
[10504] | 137 | // Add 10 ticks for every full unit
|
---|
| 138 | distanceBetweenMinor = spacingLog10;
|
---|
| 139 | minorStepsPerMajor = 5;
|
---|
[10078] | 140 | }
|
---|
[10504] | 141 | // round down to the last major step.
|
---|
| 142 | int majorSteps = (int) Math.floor(lineDistance / distanceBetweenMinor / minorStepsPerMajor);
|
---|
| 143 | if (majorSteps >= 4) {
|
---|
| 144 | // we have many major steps, do not paint the minor now.
|
---|
| 145 | this.spacingMeter = distanceBetweenMinor * minorStepsPerMajor;
|
---|
| 146 | this.minorStepsPerMajor = 1;
|
---|
| 147 | } else {
|
---|
| 148 | this.minorStepsPerMajor = minorStepsPerMajor;
|
---|
| 149 | this.spacingMeter = distanceBetweenMinor;
|
---|
| 150 | }
|
---|
| 151 | steps = majorSteps * this.minorStepsPerMajor;
|
---|
[10078] | 152 | }
|
---|
| 153 |
|
---|
[10504] | 154 | /**
|
---|
| 155 | * Paint the ticks to the graphics.
|
---|
| 156 | * @param g The graphics to paint on.
|
---|
| 157 | */
|
---|
[10078] | 158 | public void paintTicks(Graphics g) {
|
---|
| 159 | double spacingPixel = spacingMeter / (dist100Pixel / 100);
|
---|
| 160 | double textBlockedUntil = -1;
|
---|
| 161 | for (int step = 0; step <= steps; step++) {
|
---|
| 162 | int x = (int) (PADDING_LEFT + spacingPixel * step);
|
---|
[10504] | 163 | boolean isMajor = step % minorStepsPerMajor == 0;
|
---|
[10078] | 164 | int paddingY = isMajor ? 0 : 3;
|
---|
| 165 | g.drawLine(x, paddingY, x, 10 - paddingY);
|
---|
| 166 |
|
---|
[10504] | 167 | if (step == 0 || step == steps) {
|
---|
[10078] | 168 | String text;
|
---|
| 169 | if (step == 0) {
|
---|
| 170 | text = "0";
|
---|
| 171 | } else {
|
---|
| 172 | text = NavigatableComponent.getDistText(spacingMeter * step);
|
---|
| 173 | }
|
---|
| 174 | Rectangle2D bound = g.getFontMetrics().getStringBounds(text, g);
|
---|
| 175 | int left = (int) (x - bound.getWidth() / 2);
|
---|
[10504] | 176 | if (textBlockedUntil > left) {
|
---|
| 177 | left = (int) (textBlockedUntil + 5);
|
---|
[10078] | 178 | }
|
---|
[10504] | 179 | g.drawString(text, left, 23);
|
---|
| 180 | textBlockedUntil = left + bound.getWidth() + 2;
|
---|
[10078] | 181 | }
|
---|
| 182 | }
|
---|
| 183 | g.drawLine(PADDING_LEFT + 0, 5, (int) (PADDING_LEFT + spacingPixel * steps), 5);
|
---|
| 184 | }
|
---|
| 185 | }
|
---|
[115] | 186 | }
|
---|