[Schmitzm-commits] r855 - trunk/src/schmitzm/geotools/gui
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Fri May 21 15:16:28 CEST 2010
Author: mojays
Date: 2010-05-21 15:16:28 +0200 (Fri, 21 May 2010)
New Revision: 855
Modified:
trunk/src/schmitzm/geotools/gui/ScalePanel.java
Log:
new enum DistanceUnit in ScalePanel
Modified: trunk/src/schmitzm/geotools/gui/ScalePanel.java
===================================================================
--- trunk/src/schmitzm/geotools/gui/ScalePanel.java 2010-05-21 13:15:20 UTC (rev 854)
+++ trunk/src/schmitzm/geotools/gui/ScalePanel.java 2010-05-21 13:16:28 UTC (rev 855)
@@ -35,8 +35,11 @@
import java.awt.RenderingHints;
import java.text.DecimalFormat;
+import javax.measure.converter.UnitConverter;
+import javax.measure.unit.Unit;
import javax.swing.JLabel;
+import schmitzm.lang.LangUtil;
import schmitzm.swing.JPanel;
import schmitzm.swing.SwingUtil;
@@ -49,19 +52,114 @@
* @version 1.0
*/
public class ScalePanel extends JPanel {
+ public static enum DistanceUnit {
+ KM,
+ METER,
+ MILES,
+ YARDS,
+ FEET;
+
+
+ /** Factor to convert 1 mile to kilometers. */
+ public static final double MILE_TO_KM = 1.609344;
+ /** Factor to convert 1 mile to yard. 1 mile = 1760 yards. */
+ public static final double MILE_TO_YARD = 1760.0;
+ /** Factor to convert 1 yard to feed. 1 yard = 3 feed. */
+ public static final double YARD_TO_FEET = 3.0;
+ /** Factor to convert 1 mile to feed. 1 mile = 1760 yards with 1 yard = 3 feed.
+ * So 1 mile = 5280 feed. */
+ public static final double MILE_TO_FEET = MILE_TO_YARD * YARD_TO_FEET;
+
+ /**
+ * Returns an abbreviation for the unit.
+ */
+ public String getAbbreviation() {
+ switch ( this ) {
+ case METER: return "m";
+ case KM: return "km";
+ case MILES: return "miles";
+ case YARDS: return "yt";
+ case FEET: return "ft";
+ }
+ throw new UnsupportedOperationException("Unexpected DistanceUnit type: "+LangUtil.getSimpleClassName(this));
+
+ }
+ /**
+ * Returns the factor to convert 1 in "this" unit to another unit.<br>
+ * {@code x = A.getConversionFactor(B) <=> 1 A = x B}
+ * @param units destination unit
+ */
+ public double getConversionFactor(DistanceUnit units) {
+ switch ( this ) {
+ case METER: return KM.getConversionFactor(units)/1000.0;
+ case KM: switch (units) {
+ case METER: return 1000;
+ case KM: return 1;
+ case MILES: return 1/MILE_TO_KM;
+ case YARDS: return getConversionFactor(MILES) * MILE_TO_YARD;
+ case FEET: return getConversionFactor(MILES) * MILE_TO_FEET;
+ }
+ break;
+ case MILES: switch (units) {
+ case METER: return getConversionFactor(KM) * 1000;
+ case KM: return MILE_TO_KM;
+ case MILES: return 1;
+ case YARDS: return MILE_TO_YARD;
+ case FEET: return MILE_TO_FEET;
+ }
+ break;
+ case YARDS: return MILES.getConversionFactor(units) / MILE_TO_YARD;
+ case FEET: return MILES.getConversionFactor(units) / MILE_TO_FEET;
+ }
+ throw new UnsupportedOperationException("Can not convert "+this+" to "+units+".");
+ }
+
+ /**
+ * Converts a distance to another unit.
+ * @param dist distance to convert
+ * @param units destination unit to convert to
+ */
+ public double convertDistanceTo(double dist, DistanceUnit units) {
+ return dist * getConversionFactor(units);
+ }
+ }
+
/**
* ScalePane can show the scale in the following units.
*/
public static enum ScaleUnits {
- /**
- * Show units in meters and switch to kilometers when the numbers get
- * huge
- **/
- METRIC,
- /** Show units in feet and switch to miles when the numbers get huge **/
- US
- }
+ /**
+ * Show units in meters and switch to kilometers when the numbers get
+ * huge
+ **/
+ METRIC,
+ /** Show units in feet and switch to miles when the numbers get huge **/
+ US;
+
+ /**
+ * Returns the main unit the scale is displayed in.
+ */
+ public DistanceUnit getMainUnit() {
+ switch (this) {
+ case METRIC: return DistanceUnit.KM;
+ case US: return DistanceUnit.MILES;
+ }
+ throw new UnsupportedOperationException("Unexpected ScaleUnits type: "+LangUtil.getSimpleClassName(this));
+ }
+ /**
+ * Returns the secondary unit the scale is displayed in if
+ * the scale is less than 1 in the main unit.
+ */
+ public DistanceUnit getSecondaryUnit() {
+ switch (this) {
+ case METRIC: return DistanceUnit.METER;
+ case US: return DistanceUnit.FEET;
+ }
+ throw new UnsupportedOperationException("Unexpected ScaleUnits type: "+LangUtil.getSimpleClassName(this));
+ }
+}
+
private static final DecimalFormat numFormat = new DecimalFormat("###,###,##0");
public final static Color[] barColor = new Color[] { Color.BLACK, Color.WHITE };
@@ -98,6 +196,13 @@
}
/**
+ * Liefert die dargestellte Aufloesung in Meilen pro Pixel.
+ */
+ public double getScaleInMiles() {
+ return DistanceUnit.METER.convertDistanceTo(getScaleInMeters(),DistanceUnit.MILES);
+ }
+
+ /**
* Returns the units used in the scale.
* Not all possible Unit values are allowed.
*/
@@ -179,8 +284,30 @@
g2.setFont( new JLabel().getFont() );
//TODO This should be switchable from km to miles
- final String scaleString = scaleW_meter >= 1000 ? numFormat.format(scaleW_meter/1000)+"km" : numFormat.format(scaleW_meter)+"m";
+ final String scaleString = convertScaleToLabelUnit(scaleW_meter,DistanceUnit.METER,units);
g2.drawString(scaleString,scaleW_pixel+3,tileScaleHeight);
}
+
+ /**
+ * Returns a formated string containing the given distance value (meters or miles)
+ * in a "nice" and displayable format.
+ * @param dist distance (in meters or miles) to format
+ * @param distUnits defines whether the given distance is given in meters or miles
+ * @param displayUnits defines the unit to display
+ * @return
+ */
+ public static String convertScaleToLabelUnit(double dist, DistanceUnit distUnits, ScaleUnits displayUnits) {
+ DistanceUnit mainUnit = displayUnits.getMainUnit();
+ DistanceUnit secUnit = displayUnits.getSecondaryUnit();
+
+ // convert given distance to display main unit
+ dist = distUnits.convertDistanceTo(dist, mainUnit);
+
+ if ( dist > 1 )
+ return numFormat.format(dist) + mainUnit.getAbbreviation();
+
+ // fall back to the secondary unit
+ return numFormat.format( mainUnit.convertDistanceTo(dist, secUnit) ) + secUnit.getAbbreviation();
+ }
}
More information about the Schmitzm-commits
mailing list