Changeset 6422 in josm
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java
r6317 r6422 11 11 import java.awt.geom.AffineTransform; 12 12 import java.awt.geom.Point2D; 13 import java.text.NumberFormat; 13 14 import java.util.ArrayList; 14 15 import java.util.Collection; … … 1462 1463 */ 1463 1464 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) { 1464 1477 double a = dist / aValue; 1465 1478 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); 1469 1482 else 1470 return formatText(a, aName );1483 return formatText(a, aName, format); 1471 1484 } 1472 1485 … … 1478 1491 */ 1479 1492 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) { 1480 1505 double a = area / (aValue*aValue); 1481 1506 boolean lowerOnly = Main.pref.getBoolean("system_of_measurement.use_only_lower_unit", false); 1482 1507 boolean customAreaOnly = Main.pref.getBoolean("system_of_measurement.use_only_custom_area_unit", false); 1483 1508 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); 1485 1510 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); 1489 1514 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 } 1494 1522 return String.format(Locale.US, "%." + (v<9.999999 ? 2 : 1) + "f %s", v, unit); 1495 1523 } -
trunk/test/unit/org/openstreetmap/josm/gui/SystemOfMeasurementTest.java
r5870 r6422 8 8 import org.openstreetmap.josm.data.Preferences; 9 9 import org.openstreetmap.josm.gui.NavigatableComponent.SystemOfMeasurement; 10 11 import java.text.DecimalFormat; 12 import java.text.DecimalFormatSymbols; 13 import java.util.Locale; 10 14 11 15 /** … … 63 67 } 64 68 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 65 79 /** 66 80 * Test of {@link SystemOfMeasurement#getAreaText} method.
Note:
See TracChangeset
for help on using the changeset viewer.