Ignore:
Timestamp:
2014-04-22T01:06:55+02:00 (10 years ago)
Author:
Don-vip
Message:

cleanup/refactor of NavigatableComponent

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r6873 r6992  
    11// License: GPL. See LICENSE file for details.
    22package org.openstreetmap.josm.gui;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.marktr;
    53
    64import java.awt.Cursor;
     
    119import java.awt.geom.AffineTransform;
    1210import java.awt.geom.Point2D;
    13 import java.text.NumberFormat;
    1411import java.util.ArrayList;
    1512import java.util.Collection;
     
    1714import java.util.Date;
    1815import java.util.HashSet;
    19 import java.util.LinkedHashMap;
    2016import java.util.LinkedList;
    2117import java.util.List;
    22 import java.util.Locale;
    2318import java.util.Map;
    2419import java.util.Map.Entry;
     
    3328import org.openstreetmap.josm.data.Bounds;
    3429import org.openstreetmap.josm.data.ProjectionBounds;
     30import org.openstreetmap.josm.data.SystemOfMeasurement;
    3531import org.openstreetmap.josm.data.coor.CachedLatLon;
    3632import org.openstreetmap.josm.data.coor.EastNorth;
     
    5450
    5551/**
    56  * An component that can be navigated by a mapmover. Used as map view and for the
     52 * A component that can be navigated by a {@link MapMover}. Used as map view and for the
    5753 * zoomer in the download dialog.
    5854 *
    5955 * @author imi
     56 * @since 41
    6057 */
    6158public class NavigatableComponent extends JComponent implements Helpful {
     
    6562     */
    6663    public interface ZoomChangeListener {
     64        /**
     65         * Method called when the zoom area has changed.
     66         */
    6767        void zoomChanged();
    6868    }
     
    8181    }
    8282
    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 center
    97          * @return the center
    98          */
    99         public EastNorth getCenter() {
    100             return center;
    101         }
    102 
    103         /**
    104          * Return the scale factor in east-/north-units per pixel.
    105          * @return the scale
    106          */
    107         public Double getScale() {
    108             return scale;
    109         }
    110     }
    111 
    11283    public static final IntegerProperty PROP_SNAP_DISTANCE = new IntegerProperty("mappaint.node.snap-distance", 10);
    11384
     
    146117    }
    147118
    148     /**
    149      * the SoM listeners
    150      */
    151119    private static final CopyOnWriteArrayList<SoMChangeListener> somChangeListeners = new CopyOnWriteArrayList<SoMChangeListener>();
    152120
     
    185153     */
    186154    private double scale = Main.getProjection().getDefaultZoomInPPD();
     155
    187156    /**
    188157     * Center n/e coordinate of the desired screen center.
     
    233202    }
    234203
    235     public String getDist100PixelText()
    236     {
     204    public String getDist100PixelText() {
    237205        return getDistText(getDist100Pixel());
    238206    }
    239207
    240     public double getDist100Pixel()
    241     {
     208    public double getDist100Pixel() {
    242209        int w = getWidth()/2;
    243210        int h = getHeight()/2;
     
    263230     * @param y Y-Pixelposition to get coordinate from
    264231     *
    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.
    267233     */
    268234    public EastNorth getEastNorth(int x, int y) {
     
    549515
    550516    private class ZoomData {
    551         LatLon center;
    552         double scale;
     517        final LatLon center;
     518        final double scale;
    553519
    554520        public ZoomData(EastNorth center, double scale) {
     
    731697     *        give the nearest node that is tagged.
    732698     */
    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);
    735701    }
    736702
     
    811777    /**
    812778     * 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.
    813782     *
    814783     * @return The nearest node to point p.
     
    940909     * @param p the point for which to search the nearest segment.
    941910     * @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) {
    945914        WaySegment wayseg = null, ntsel = null;
    946915
     
    959928        }
    960929
    961         return (ntsel != null && use_selected) ? ntsel : wayseg;
     930        return (ntsel != null && useSelected) ? ntsel : wayseg;
    962931    }
    963932
     
    1017986    /**
    1018987     * 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.
    1019990     *
    1020991     * @return The nearest way segment to point p.
     
    11771148     *      that is chosen by the algorithm described.
    11781149     * @see #getNearestNode(Point, Predicate)
    1179      * @see #getNearestNodesImpl(Point, Predicate)
    11801150     * @see #getNearestWay(Point, Predicate)
    11811151     *
     
    12231193    }
    12241194
    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 
    12341195    public static double perDist(Point2D pt, Point2D a, Point2D b) {
    12351196        if (pt != null && a != null && b != null) {
     
    13601321    /**
    13611322     * 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
    13621324     */
    13631325    public int getViewID() {
     
    13751337     */
    13761338    public static SystemOfMeasurement getSystemOfMeasurement() {
    1377         SystemOfMeasurement som = SYSTEMS_OF_MEASUREMENT.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get());
     1339        SystemOfMeasurement som = SystemOfMeasurement.ALL_SYSTEMS.get(ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get());
    13781340        if (som == null)
    1379             return METRIC_SOM;
     1341            return SystemOfMeasurement.METRIC;
    13801342        return som;
    13811343    }
     
    13831345    /**
    13841346     * 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}.
    13861348     * @since 6056
    13871349     * @throws IllegalArgumentException if {@code somKey} is not known
    13881350     */
    13891351    public static void setSystemOfMeasurement(String somKey) {
    1390         if (!SYSTEMS_OF_MEASUREMENT.containsKey(somKey)) {
     1352        if (!SystemOfMeasurement.ALL_SYSTEMS.containsKey(somKey)) {
    13911353            throw new IllegalArgumentException("Invalid system of measurement: "+somKey);
    13921354        }
     
    13971359    }
    13981360
    1399     /**
    1400      * A system of units used to express length and area measurements.
    1401      * @since 3406
    1402      */
    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 as
    1424          * x_a == x_m / aValue
    1425          *
    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 as
    1439          * x_a == x_m / aValue
    1440          *
    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 5870
    1450          */
    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 metres
    1463          * @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 metres
    1472          * @param format A {@link NumberFormat} to format the area value
    1473          * @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 6422
    1476          */
    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             else
    1484                 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 metres
    1490          * @return The text describing the given area in this system of measurement.
    1491          * @since 5560
    1492          */
    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 metres
    1500          * @param format A {@link NumberFormat} to format the area value
    1501          * @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 6422
    1504          */
    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             else
    1516                 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 3406
    1530      */
    1531     public static final SystemOfMeasurement METRIC_SOM = new SystemOfMeasurement(1, "m", 1000, "km", 10000, "ha");
    1532 
    1533     /**
    1534      * Chinese system.
    1535      * @since 3406
    1536      */
    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 3406
    1542      */
    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 5549
    1548      */
    1549     public static final SystemOfMeasurement NAUTICAL_MILE_SOM = new SystemOfMeasurement(185.2, "kbl", 1852, "NM");
    1550 
    1551     /**
    1552      * Known systems of measurement.
    1553      * @since 3406
    1554      */
    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 
    15641361    private static class CursorInfo {
    1565         public Cursor cursor;
    1566         public Object object;
     1362        final Cursor cursor;
     1363        final Object object;
    15671364        public CursorInfo(Cursor c, Object o) {
    15681365            cursor = c;
Note: See TracChangeset for help on using the changeset viewer.