Changeset 6422 in josm


Ignore:
Timestamp:
2013-11-29T00:10:15+01:00 (5 years ago)
Author:
simon04
Message:

see #9331 - allow custom formatters in SystemOfMeasurement methods

Location:
trunk
Files:
2 edited

Legend:

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

    r6317 r6422  
    1111import java.awt.geom.AffineTransform;
    1212import java.awt.geom.Point2D;
     13import java.text.NumberFormat;
    1314import java.util.ArrayList;
    1415import java.util.Collection;
     
    14621463         */
    14631464        public String getDistText(double dist) {
     1465            return getDistText(dist, null, 0.01);
     1466        }
     1467
     1468        /**
     1469         * Returns the text describing the given distance in this system of measurement.
     1470         * @param dist The distance in metres
     1471         * @param format A {@link NumberFormat} to format the area value
     1472         * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}
     1473         * @return The text describing the given distance in this system of measurement.
     1474         * @since 6422
     1475         */
     1476        public String getDistText(final double dist, final NumberFormat format, final double threshold) {
    14641477            double a = dist / aValue;
    14651478            if (!Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false) && a > bValue / aValue)
    1466                 return formatText(dist / bValue, bName);
    1467             else if (a < 0.01)
    1468                 return "< 0.01 " + aName;
     1479                return formatText(dist / bValue, bName, format);
     1480            else if (a < threshold)
     1481                return "< " + formatText(threshold, aName, format);
    14691482            else
    1470                 return formatText(a, aName);
     1483                return formatText(a, aName, format);
    14711484        }
    14721485
     
    14781491         */
    14791492        public String getAreaText(double area) {
     1493            return getAreaText(area, null, 0.01);
     1494        }
     1495
     1496        /**
     1497         * Returns the text describing the given area in this system of measurement.
     1498         * @param area The area in square metres
     1499         * @param format A {@link NumberFormat} to format the area value
     1500         * @param threshold Values lower than this {@code threshold} are displayed as {@code "< [threshold]"}
     1501         * @return The text describing the given area in this system of measurement.
     1502         * @since 6422
     1503         */
     1504        public String getAreaText(final double area, final NumberFormat format, final double threshold) {
    14801505            double a = area / (aValue*aValue);
    14811506            boolean lowerOnly = Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false);
    14821507            boolean customAreaOnly = Main.pref.getBoolean("system_of_measurement.use_only_custom_area_unit", false);
    14831508            if ((!lowerOnly && areaCustomValue > 0 && a > areaCustomValue / (aValue*aValue) && a < (bValue*bValue) / (aValue*aValue)) || customAreaOnly)
    1484                 return formatText(area / areaCustomValue, areaCustomName);
     1509                return formatText(area / areaCustomValue, areaCustomName, format);
    14851510            else if (!lowerOnly && a >= (bValue*bValue) / (aValue*aValue))
    1486                 return formatText(area / (bValue*bValue), bName+"\u00b2");
    1487             else if (a < 0.01)
    1488                 return "< 0.01 " + aName+"\u00b2";
     1511                return formatText(area / (bValue * bValue), bName + "\u00b2", format);
     1512            else if (a < threshold)
     1513                return "< " + formatText(threshold, aName + "\u00b2", format);
    14891514            else
    1490                 return formatText(a, aName+"\u00b2");
    1491         }
    1492 
    1493         private static String formatText(double v, String unit) {
     1515                return formatText(a, aName + "\u00b2", format);
     1516        }
     1517
     1518        private static String formatText(double v, String unit, NumberFormat format) {
     1519            if (format != null) {
     1520                return format.format(v) + " " + unit;
     1521            }
    14941522            return String.format(Locale.US, "%." + (v<9.999999 ? 2 : 1) + "f %s", v, unit);
    14951523        }
  • trunk/test/unit/org/openstreetmap/josm/gui/SystemOfMeasurementTest.java

    r5870 r6422  
    88import org.openstreetmap.josm.data.Preferences;
    99import org.openstreetmap.josm.gui.NavigatableComponent.SystemOfMeasurement;
     10
     11import java.text.DecimalFormat;
     12import java.text.DecimalFormatSymbols;
     13import java.util.Locale;
    1014
    1115/**
     
    6367    }
    6468
     69    @Test
     70    public void testGetDistTextLocalized() {
     71        final DecimalFormat format = new DecimalFormat("0.000", DecimalFormatSymbols.getInstance(Locale.GERMAN));
     72        assertEquals("0,001 m", NavigatableComponent.METRIC_SOM.getDistText(0.001, format, 1e-6));
     73        assertEquals("< 0,010 m", NavigatableComponent.METRIC_SOM.getDistText(0.001, format, 0.01));
     74        assertEquals("10,051 m", NavigatableComponent.METRIC_SOM.getDistText(10.0514, format, 0.01));
     75        assertEquals("10,052 m", NavigatableComponent.METRIC_SOM.getDistText(10.0515, format, 0.01));
     76        assertEquals("100,000 km", NavigatableComponent.METRIC_SOM.getDistText(100000.0, format, 0.01));
     77    }
     78
    6579    /**
    6680     * Test of {@link SystemOfMeasurement#getAreaText} method.
Note: See TracChangeset for help on using the changeset viewer.