[Schmitzm-commits] r858 - in trunk: src/schmitzm/data src/schmitzm/data/resource/locales src/schmitzm/geotools/gui src_junit/schmitzm/geotools/gui
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Fri May 21 16:34:56 CEST 2010
Author: mojays
Date: 2010-05-21 16:34:53 +0200 (Fri, 21 May 2010)
New Revision: 858
Added:
trunk/src/schmitzm/data/DistanceUnit.java
trunk/src_junit/schmitzm/geotools/gui/ScalePanelTest.java
Modified:
trunk/src/schmitzm/data/resource/locales/DataResourceBundle.properties
trunk/src/schmitzm/data/resource/locales/DataResourceBundle_de.properties
trunk/src/schmitzm/geotools/gui/ScalePanel.java
Log:
Own class file for DistanceUnit in schmitzm.data.
Added: trunk/src/schmitzm/data/DistanceUnit.java
===================================================================
--- trunk/src/schmitzm/data/DistanceUnit.java 2010-05-21 14:15:58 UTC (rev 857)
+++ trunk/src/schmitzm/data/DistanceUnit.java 2010-05-21 14:34:53 UTC (rev 858)
@@ -0,0 +1,117 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Krüger - additional utility classes
+ ******************************************************************************/
+
+package schmitzm.data;
+
+import schmitzm.lang.LangUtil;
+import schmitzm.lang.ResourceProvider;
+
+/**
+ * This enum represents distance units and provides several utility
+ * function for conversion.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public enum DistanceUnit {
+ /** Kilometers */
+ KM,
+ /** Meters */
+ METER,
+ /** Miles (= 1.609344km) */
+ MILES,
+ /** Yards (= 1/1760 miles) */
+ YARDS,
+ /** Feet (= 1/3 yard) */
+ FEET;
+
+ private static final ResourceProvider RESOURCE = DataUtil.RESOURCE;
+
+ /** 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 a short abbreviation for the unit.
+ */
+ public String getAbbreviation() {
+ return RESOURCE.getString("DistanceUnit."+this+".Abb");
+ }
+
+ /**
+ * Returns a title for the unit.
+ */
+ public String getTitle() {
+ return RESOURCE.getString("DistanceUnit."+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);
+ }
+}
\ No newline at end of file
Modified: trunk/src/schmitzm/data/resource/locales/DataResourceBundle.properties
===================================================================
--- trunk/src/schmitzm/data/resource/locales/DataResourceBundle.properties 2010-05-21 14:15:58 UTC (rev 857)
+++ trunk/src/schmitzm/data/resource/locales/DataResourceBundle.properties 2010-05-21 14:34:53 UTC (rev 858)
@@ -58,6 +58,18 @@
# ------ in Package schmitzm.data ------
# -----------------------------------------------------------
+DistanceUnit.METERS=meters
+DistanceUnit.KM=kilometers
+DistanceUnit.MILES=miles
+DistanceUnit.YARDS=yards
+DistanceUnit.FEET=feet
+
+DistanceUnit.METERS.Abb=m
+DistanceUnit.KM.Abb=km
+DistanceUnit.MILES.Abb=mi
+DistanceUnit.YARDS.Abb=yt
+DistanceUnit.FEET.Abb=ft
+
RasterDim.GridWidth=Grid width
RasterDim.GridHeight=Grid height
RasterDim.CellWidth=Cell width
Modified: trunk/src/schmitzm/data/resource/locales/DataResourceBundle_de.properties
===================================================================
--- trunk/src/schmitzm/data/resource/locales/DataResourceBundle_de.properties 2010-05-21 14:15:58 UTC (rev 857)
+++ trunk/src/schmitzm/data/resource/locales/DataResourceBundle_de.properties 2010-05-21 14:34:53 UTC (rev 858)
@@ -58,6 +58,12 @@
# ------ in Package schmitzm.data ------
# ------------------------------------------------
+DistanceUnit.METERS=Meter
+DistanceUnit.KM=Kilometer
+DistanceUnit.MILES=Meilen
+DistanceUnit.YARDS=Yards
+DistanceUnit.FEET=Feet
+
RasterDim.GridWidth=Raster-Breite
RasterDim.GridHeight=Raster-Hoehe
RasterDim.CellWidth=Zellen-Breite
Modified: trunk/src/schmitzm/geotools/gui/ScalePanel.java
===================================================================
--- trunk/src/schmitzm/geotools/gui/ScalePanel.java 2010-05-21 14:15:58 UTC (rev 857)
+++ trunk/src/schmitzm/geotools/gui/ScalePanel.java 2010-05-21 14:34:53 UTC (rev 858)
@@ -39,6 +39,7 @@
import javax.measure.unit.Unit;
import javax.swing.JLabel;
+import schmitzm.data.DistanceUnit;
import schmitzm.lang.LangUtil;
import schmitzm.swing.JPanel;
import schmitzm.swing.SwingUtil;
@@ -52,88 +53,19 @@
* @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
+ * Show units in kilometers and switch to meters when the scale becomes
+ * lesser than 1km.
**/
METRIC,
- /** Show units in feet and switch to miles when the numbers get huge **/
+ /**
+ * Show units in miles and switch to feet when the scale becomes
+ * lesser than 1mi.
+ **/
US;
/**
@@ -216,6 +148,7 @@
*/
public void setUnits(ScaleUnits unit) {
this.units = unit;
+ repaint();
}
/**
@@ -265,12 +198,14 @@
double maxW_unit = DistanceUnit.METER.convertDistanceTo(maxW_meter, dispUnit);
if ( maxW_unit < 1 ) {
dispUnit = units.getSecondaryUnit();
- units.getMainUnit().convertDistanceTo(maxW_unit, dispUnit);
+ maxW_unit = units.getMainUnit().convertDistanceTo(maxW_unit, dispUnit);
}
+ // Pixel pro Meter umrechnen in Pixel pro Darstellungseinheit
+ final double pixelsPerUnit = getScaleInPixels() / DistanceUnit.METER.getConversionFactor(dispUnit);
// Ausmasse der Skala (stellt immer volle 10/100/1000/... Einheiten dar)
final long scaleW_unit = (long)Math.pow(10, (long)Math.log10(maxW_unit));
- final int scaleW_pixel = (int)Math.round( scaleW_unit * getScaleInPixels() );
+ final int scaleW_pixel = (int)Math.round( scaleW_unit * pixelsPerUnit );
final int scaleH_pixel = getHeight()-1;
// Aufteilung in der Skala Teil-Balken
@@ -311,7 +246,7 @@
// convert given distance to display main unit
dist = distUnits.convertDistanceTo(dist, mainUnit);
- if ( dist > 1 )
+ if ( dist >= 1 )
return numFormat.format(dist) + mainUnit.getAbbreviation();
// fall back to the secondary unit
Added: trunk/src_junit/schmitzm/geotools/gui/ScalePanelTest.java
===================================================================
--- trunk/src_junit/schmitzm/geotools/gui/ScalePanelTest.java 2010-05-21 14:15:58 UTC (rev 857)
+++ trunk/src_junit/schmitzm/geotools/gui/ScalePanelTest.java 2010-05-21 14:34:53 UTC (rev 858)
@@ -0,0 +1,41 @@
+package schmitzm.geotools.gui;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+import schmitzm.data.DistanceUnit;
+import schmitzm.geotools.gui.ScalePanel.ScaleUnits;
+
+public class ScalePanelTest {
+
+ @Test
+ public void testScalePanel_DistanceUnit() {
+ assertEquals(0.001,DistanceUnit.METER.getConversionFactor(DistanceUnit.KM),0.0);
+ assertEquals(1000.0,DistanceUnit.KM.getConversionFactor(DistanceUnit.METER),0.0);
+
+ assertEquals(1.609344,DistanceUnit.MILES.getConversionFactor(DistanceUnit.KM),0.0);
+ assertEquals(1/1.609344,DistanceUnit.KM.getConversionFactor(DistanceUnit.MILES),0.0);
+
+ assertEquals(1760,DistanceUnit.MILES.getConversionFactor(DistanceUnit.YARDS),0.0);
+ assertEquals(1/1760.0,DistanceUnit.YARDS.getConversionFactor(DistanceUnit.MILES),0.0);
+
+ assertEquals(3,DistanceUnit.YARDS.getConversionFactor(DistanceUnit.FEET),0.0);
+ assertEquals(1/3.0,DistanceUnit.FEET.getConversionFactor(DistanceUnit.YARDS),0.0);
+
+ assertEquals(1760*3,DistanceUnit.MILES.getConversionFactor(DistanceUnit.FEET),0.0);
+ assertEquals(1/(1760.0*3),DistanceUnit.FEET.getConversionFactor(DistanceUnit.MILES),0.0);
+
+ assertEquals(0.9144,DistanceUnit.YARDS.getConversionFactor(DistanceUnit.METER),0.0001);
+ assertEquals(1.0936133,DistanceUnit.METER.getConversionFactor(DistanceUnit.YARDS),0.000001);
+
+ assertEquals(3.2808399,DistanceUnit.METER.getConversionFactor(DistanceUnit.FEET),0.001);
+ assertEquals(1/3.2808399,DistanceUnit.FEET.getConversionFactor(DistanceUnit.METER),0.001);
+
+ assertEquals(1.234,DistanceUnit.METER.convertDistanceTo(1234, DistanceUnit.KM),0.0);
+ assertEquals(12070.08,DistanceUnit.MILES.convertDistanceTo(7.5, DistanceUnit.METER),0.0);
+
+ System.out.println( ScalePanel.convertScaleToLabelUnit(1.68, DistanceUnit.MILES, ScaleUnits.US));
+ }
+
+}
More information about the Schmitzm-commits
mailing list