Changeset 6992 in josm for trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java
- Timestamp:
- 2014-04-22T01:06:55+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java
r6873 r6992 1 1 // License: GPL. See LICENSE file for details. 2 2 package org.openstreetmap.josm.gui; 3 4 import static org.openstreetmap.josm.tools.I18n.marktr;5 3 6 4 import java.awt.Cursor; … … 11 9 import java.awt.geom.AffineTransform; 12 10 import java.awt.geom.Point2D; 13 import java.text.NumberFormat;14 11 import java.util.ArrayList; 15 12 import java.util.Collection; … … 17 14 import java.util.Date; 18 15 import java.util.HashSet; 19 import java.util.LinkedHashMap;20 16 import java.util.LinkedList; 21 17 import java.util.List; 22 import java.util.Locale;23 18 import java.util.Map; 24 19 import java.util.Map.Entry; … … 33 28 import org.openstreetmap.josm.data.Bounds; 34 29 import org.openstreetmap.josm.data.ProjectionBounds; 30 import org.openstreetmap.josm.data.SystemOfMeasurement; 35 31 import org.openstreetmap.josm.data.coor.CachedLatLon; 36 32 import org.openstreetmap.josm.data.coor.EastNorth; … … 54 50 55 51 /** 56 * A n component that can be navigated by a mapmover. Used as map view and for the52 * A component that can be navigated by a {@link MapMover}. Used as map view and for the 57 53 * zoomer in the download dialog. 58 54 * 59 55 * @author imi 56 * @since 41 60 57 */ 61 58 public class NavigatableComponent extends JComponent implements Helpful { … … 65 62 */ 66 63 public interface ZoomChangeListener { 64 /** 65 * Method called when the zoom area has changed. 66 */ 67 67 void zoomChanged(); 68 68 } … … 81 81 } 82 82 83 /**84 * Simple data class that keeps map center and scale in one object.85 */86 public static class ViewportData {87 private EastNorth center;88 private Double scale;89 90 public ViewportData(EastNorth center, Double scale) {91 this.center = center;92 this.scale = scale;93 }94 95 /**96 * Return the projected coordinates of the map center97 * @return the center98 */99 public EastNorth getCenter() {100 return center;101 }102 103 /**104 * Return the scale factor in east-/north-units per pixel.105 * @return the scale106 */107 public Double getScale() {108 return scale;109 }110 }111 112 83 public static final IntegerProperty PROP_SNAP_DISTANCE = new IntegerProperty("mappaint.node.snap-distance", 10); 113 84 … … 146 117 } 147 118 148 /**149 * the SoM listeners150 */151 119 private static final CopyOnWriteArrayList<SoMChangeListener> somChangeListeners = new CopyOnWriteArrayList<SoMChangeListener>(); 152 120 … … 185 153 */ 186 154 private double scale = Main.getProjection().getDefaultZoomInPPD(); 155 187 156 /** 188 157 * Center n/e coordinate of the desired screen center. … … 233 202 } 234 203 235 public String getDist100PixelText() 236 { 204 public String getDist100PixelText() { 237 205 return getDistText(getDist100Pixel()); 238 206 } 239 207 240 public double getDist100Pixel() 241 { 208 public double getDist100Pixel() { 242 209 int w = getWidth()/2; 243 210 int h = getHeight()/2; … … 263 230 * @param y Y-Pixelposition to get coordinate from 264 231 * 265 * @return Geographic coordinates from a specific pixel coordination 266 * on the screen. 232 * @return Geographic coordinates from a specific pixel coordination on the screen. 267 233 */ 268 234 public EastNorth getEastNorth(int x, int y) { … … 549 515 550 516 private class ZoomData { 551 LatLon center;552 double scale;517 final LatLon center; 518 final double scale; 553 519 554 520 public ZoomData(EastNorth center, double scale) { … … 731 697 * give the nearest node that is tagged. 732 698 */ 733 public final Node getNearestNode(Point p, Predicate<OsmPrimitive> predicate, boolean use _selected) {734 return getNearestNode(p, predicate, use _selected, null);699 public final Node getNearestNode(Point p, Predicate<OsmPrimitive> predicate, boolean useSelected) { 700 return getNearestNode(p, predicate, useSelected, null); 735 701 } 736 702 … … 811 777 /** 812 778 * Convenience method to {@link #getNearestNode(Point, Predicate, boolean)}. 779 * @param p the screen point 780 * @param predicate this parameter imposes a condition on the returned object, e.g. 781 * give the nearest node that is tagged. 813 782 * 814 783 * @return The nearest node to point p. … … 940 909 * @param p the point for which to search the nearest segment. 941 910 * @param predicate the returned object has to fulfill certain properties. 942 * @param use _selected whether selected way segments should be preferred.943 */ 944 public final WaySegment getNearestWaySegment(Point p, Predicate<OsmPrimitive> predicate, boolean use _selected) {911 * @param useSelected whether selected way segments should be preferred. 912 */ 913 public final WaySegment getNearestWaySegment(Point p, Predicate<OsmPrimitive> predicate, boolean useSelected) { 945 914 WaySegment wayseg = null, ntsel = null; 946 915 … … 959 928 } 960 929 961 return (ntsel != null && use _selected) ? ntsel : wayseg;930 return (ntsel != null && useSelected) ? ntsel : wayseg; 962 931 } 963 932 … … 1017 986 /** 1018 987 * Convenience method to {@link #getNearestWaySegment(Point, Predicate, boolean)}. 988 * @param p the point for which to search the nearest segment. 989 * @param predicate the returned object has to fulfill certain properties. 1019 990 * 1020 991 * @return The nearest way segment to point p. … … 1177 1148 * that is chosen by the algorithm described. 1178 1149 * @see #getNearestNode(Point, Predicate) 1179 * @see #getNearestNodesImpl(Point, Predicate)1180 1150 * @see #getNearestWay(Point, Predicate) 1181 1151 * … … 1223 1193 } 1224 1194 1225 /**1226 * @return o as collection of o's type.1227 */1228 public static <T> Collection<T> asColl(T o) {1229 if (o == null)1230 return Collections.emptySet();1231 return Collections.singleton(o);1232 }1233 1234 1195 public static double perDist(Point2D pt, Point2D a, Point2D b) { 1235 1196 if (pt != null && a != null && b != null) { … … 1360 1321 /** 1361 1322 * Return a ID which is unique as long as viewport dimensions are the same 1323 * @return A unique ID, as long as viewport dimensions are the same 1362 1324 */ 1363 1325 public int getViewID() { … … 1375 1337 */ 1376 1338 public static SystemOfMeasurement getSystemOfMeasurement() { 1377 SystemOfMeasurement som = S YSTEMS_OF_MEASUREMENT.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get());1339 SystemOfMeasurement som = SystemOfMeasurement.ALL_SYSTEMS.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get()); 1378 1340 if (som == null) 1379 return METRIC_SOM;1341 return SystemOfMeasurement.METRIC; 1380 1342 return som; 1381 1343 } … … 1383 1345 /** 1384 1346 * Sets the current system of measurement. 1385 * @param somKey The system of measurement key. Must be defined in {@link NavigatableComponent#SYSTEMS_OF_MEASUREMENT}.1347 * @param somKey The system of measurement key. Must be defined in {@link SystemOfMeasurement#ALL_SYSTEMS}. 1386 1348 * @since 6056 1387 1349 * @throws IllegalArgumentException if {@code somKey} is not known 1388 1350 */ 1389 1351 public static void setSystemOfMeasurement(String somKey) { 1390 if (!S YSTEMS_OF_MEASUREMENT.containsKey(somKey)) {1352 if (!SystemOfMeasurement.ALL_SYSTEMS.containsKey(somKey)) { 1391 1353 throw new IllegalArgumentException("Invalid system of measurement: "+somKey); 1392 1354 } … … 1397 1359 } 1398 1360 1399 /**1400 * A system of units used to express length and area measurements.1401 * @since 34061402 */1403 public static class SystemOfMeasurement {1404 1405 /** First value, in meters, used to translate unit according to above formula. */1406 public final double aValue;1407 /** Second value, in meters, used to translate unit according to above formula. */1408 public final double bValue;1409 /** First unit used to format text. */1410 public final String aName;1411 /** Second unit used to format text. */1412 public final String bName;1413 /** Specific optional area value, in squared meters, between {@code aValue*aValue} and {@code bValue*bValue}. Set to {@code -1} if not used.1414 * @since 5870 */1415 public final double areaCustomValue;1416 /** Specific optional area unit. Set to {@code null} if not used.1417 * @since 5870 */1418 public final String areaCustomName;1419 1420 /**1421 * System of measurement. Currently covers only length (and area) units.1422 *1423 * If a quantity x is given in m (x_m) and in unit a (x_a) then it translates as1424 * x_a == x_m / aValue1425 *1426 * @param aValue First value, in meters, used to translate unit according to above formula.1427 * @param aName First unit used to format text.1428 * @param bValue Second value, in meters, used to translate unit according to above formula.1429 * @param bName Second unit used to format text.1430 */1431 public SystemOfMeasurement(double aValue, String aName, double bValue, String bName) {1432 this(aValue, aName, bValue, bName, -1, null);1433 }1434 1435 /**1436 * System of measurement. Currently covers only length (and area) units.1437 *1438 * If a quantity x is given in m (x_m) and in unit a (x_a) then it translates as1439 * x_a == x_m / aValue1440 *1441 * @param aValue First value, in meters, used to translate unit according to above formula.1442 * @param aName First unit used to format text.1443 * @param bValue Second value, in meters, used to translate unit according to above formula.1444 * @param bName Second unit used to format text.1445 * @param areaCustomValue Specific optional area value, in squared meters, between {@code aValue*aValue} and {@code bValue*bValue}.1446 * Set to {@code -1} if not used.1447 * @param areaCustomName Specific optional area unit. Set to {@code null} if not used.1448 *1449 * @since 58701450 */1451 public SystemOfMeasurement(double aValue, String aName, double bValue, String bName, double areaCustomValue, String areaCustomName) {1452 this.aValue = aValue;1453 this.aName = aName;1454 this.bValue = bValue;1455 this.bName = bName;1456 this.areaCustomValue = areaCustomValue;1457 this.areaCustomName = areaCustomName;1458 }1459 1460 /**1461 * Returns the text describing the given distance in this system of measurement.1462 * @param dist The distance in metres1463 * @return The text describing the given distance in this system of measurement.1464 */1465 public String getDistText(double dist) {1466 return getDistText(dist, null, 0.01);1467 }1468 1469 /**1470 * Returns the text describing the given distance in this system of measurement.1471 * @param dist The distance in metres1472 * @param format A {@link NumberFormat} to format the area value1473 * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}1474 * @return The text describing the given distance in this system of measurement.1475 * @since 64221476 */1477 public String getDistText(final double dist, final NumberFormat format, final double threshold) {1478 double a = dist / aValue;1479 if (!Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false) && a > bValue / aValue)1480 return formatText(dist / bValue, bName, format);1481 else if (a < threshold)1482 return "< " + formatText(threshold, aName, format);1483 else1484 return formatText(a, aName, format);1485 }1486 1487 /**1488 * Returns the text describing the given area in this system of measurement.1489 * @param area The area in square metres1490 * @return The text describing the given area in this system of measurement.1491 * @since 55601492 */1493 public String getAreaText(double area) {1494 return getAreaText(area, null, 0.01);1495 }1496 1497 /**1498 * Returns the text describing the given area in this system of measurement.1499 * @param area The area in square metres1500 * @param format A {@link NumberFormat} to format the area value1501 * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}1502 * @return The text describing the given area in this system of measurement.1503 * @since 64221504 */1505 public String getAreaText(final double area, final NumberFormat format, final double threshold) {1506 double a = area / (aValue*aValue);1507 boolean lowerOnly = Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false);1508 boolean customAreaOnly = Main.pref.getBoolean("system_of_measurement.use_only_custom_area_unit", false);1509 if ((!lowerOnly && areaCustomValue > 0 && a > areaCustomValue / (aValue*aValue) && a < (bValue*bValue) / (aValue*aValue)) || customAreaOnly)1510 return formatText(area / areaCustomValue, areaCustomName, format);1511 else if (!lowerOnly && a >= (bValue*bValue) / (aValue*aValue))1512 return formatText(area / (bValue * bValue), bName + "\u00b2", format);1513 else if (a < threshold)1514 return "< " + formatText(threshold, aName + "\u00b2", format);1515 else1516 return formatText(a, aName + "\u00b2", format);1517 }1518 1519 private static String formatText(double v, String unit, NumberFormat format) {1520 if (format != null) {1521 return format.format(v) + " " + unit;1522 }1523 return String.format(Locale.US, "%." + (v<9.999999 ? 2 : 1) + "f %s", v, unit);1524 }1525 }1526 1527 /**1528 * Metric system (international standard).1529 * @since 34061530 */1531 public static final SystemOfMeasurement METRIC_SOM = new SystemOfMeasurement(1, "m", 1000, "km", 10000, "ha");1532 1533 /**1534 * Chinese system.1535 * @since 34061536 */1537 public static final SystemOfMeasurement CHINESE_SOM = new SystemOfMeasurement(1.0/3.0, "\u5e02\u5c3a" /* chi */, 500, "\u5e02\u91cc" /* li */);1538 1539 /**1540 * Imperial system (British Commonwealth and former British Empire).1541 * @since 34061542 */1543 public static final SystemOfMeasurement IMPERIAL_SOM = new SystemOfMeasurement(0.3048, "ft", 1609.344, "mi", 4046.86, "ac");1544 1545 /**1546 * Nautical mile system (navigation, polar exploration).1547 * @since 55491548 */1549 public static final SystemOfMeasurement NAUTICAL_MILE_SOM = new SystemOfMeasurement(185.2, "kbl", 1852, "NM");1550 1551 /**1552 * Known systems of measurement.1553 * @since 34061554 */1555 public static final Map<String, SystemOfMeasurement> SYSTEMS_OF_MEASUREMENT;1556 static {1557 SYSTEMS_OF_MEASUREMENT = new LinkedHashMap<String, SystemOfMeasurement>();1558 SYSTEMS_OF_MEASUREMENT.put(marktr("Metric"), METRIC_SOM);1559 SYSTEMS_OF_MEASUREMENT.put(marktr("Chinese"), CHINESE_SOM);1560 SYSTEMS_OF_MEASUREMENT.put(marktr("Imperial"), IMPERIAL_SOM);1561 SYSTEMS_OF_MEASUREMENT.put(marktr("Nautical Mile"), NAUTICAL_MILE_SOM);1562 }1563 1564 1361 private static class CursorInfo { 1565 publicCursor cursor;1566 publicObject object;1362 final Cursor cursor; 1363 final Object object; 1567 1364 public CursorInfo(Cursor c, Object o) { 1568 1365 cursor = c;
Note:
See TracChangeset
for help on using the changeset viewer.