[Schmitzm-commits] r1998 - in trunk: schmitzm-core/src/main/java/de/schmitzm/data schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon May 14 17:16:24 CEST 2012


Author: mojays
Date: 2012-05-14 17:16:24 +0200 (Mon, 14 May 2012)
New Revision: 1998

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/UnitUtil.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationDialog.java
   trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationPanel.java
   trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle.properties
   trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle_de.properties
Log:
CSVColumnAllocationPanel/Dialog: possibility to specify unit for CSV column
DistanceUnit: new enums for cm and mm
new utility class UnitUtil

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java	2012-05-14 12:26:39 UTC (rev 1997)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/DistanceUnit.java	2012-05-14 15:16:24 UTC (rev 1998)
@@ -41,6 +41,10 @@
   KM,
   /** Meters */
   METERS,
+  /** Centimeters */
+  CM,
+  /** Millimeters */
+  MM,
   /** Miles (= 1.609344km) */
   MILES,
   /** Yards (= 1/1760 miles) */
@@ -84,8 +88,12 @@
     if ( this.equals(units) )
       return 1.0;
     switch ( this ) {
+      case MM:     return CM.getConversionFactor(units)/10.0;
+      case CM:     return METERS.getConversionFactor(units)/100.0;
       case METERS: return KM.getConversionFactor(units)/1000.0;
       case KM: switch (units) {
+                  case MM: return 1000*1000;
+                  case CM: return 1000*100;
                   case METERS: return 1000;
                   case KM: return 1;
                   case MILES: return 1/MILE_TO_KM;
@@ -94,6 +102,8 @@
                }
                break;
       case MILES: switch (units) {
+                    case MM:     return getConversionFactor(CM) * 10;
+                    case CM:     return getConversionFactor(METERS) * 100;
                     case METERS: return getConversionFactor(KM) * 1000;
                     case KM: return MILE_TO_KM;
                     case MILES: return 1;

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java	2012-05-14 12:26:39 UTC (rev 1997)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/Unit.java	2012-05-14 15:16:24 UTC (rev 1998)
@@ -9,15 +9,6 @@
 public interface Unit<U extends Unit<?>> {
   public static final ResourceProvider RESOURCE = DataUtil.RESOURCE;
   
-  /** Provided units for distance. */
-  public static final DistanceUnit[] DISTANCE_UNITS = DistanceUnit.values();
-  /** Provided units for time. */
-  public static final TimeUnit[] TIME_UNITS = TimeUnit.values();
-  /** Provided units for speed. */
-  public static final SpeedUnit[] SPEED_UNITS = SpeedUnit.values();
-  /** Provided units for temperature. */
-  public static final TemperatureUnit[] TEMPERATURE_UNITS = TemperatureUnit.values();
-
   /**
    * Returns a conversion faction to convert 1 unit to another. 
    * @param unit unit to convert the value to

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/data/UnitUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/data/UnitUtil.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/data/UnitUtil.java	2012-05-14 15:16:24 UTC (rev 1998)
@@ -0,0 +1,56 @@
+/**
+ * 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. Tzeggai - additional utility classes
+ */
+package de.schmitzm.data;
+
+/**
+ * Utility methods for working with {@link Unit} types.
+ * @author Martin O.J. Schmitz
+ */
+public class UnitUtil {
+  /** Provided units for distance. */
+  public static final DistanceUnit[] DISTANCE_UNITS = DistanceUnit.values();
+  /** Provided units for time. */
+  public static final TimeUnit[] TIME_UNITS = TimeUnit.values();
+  /** Provided units for speed. */
+  public static final SpeedUnit[] SPEED_UNITS = SpeedUnit.values();
+  /** Provided units for temperature. */
+  public static final TemperatureUnit[] TEMPERATURE_UNITS = TemperatureUnit.values();
+
+  /**
+   * Returns the abbreviations for some units.
+   */
+  public static String[] getUnitsAbbreviation(Unit[] units) {
+    String[] abb = new String[units.length];
+    for (int i=0; i<units.length; i++) {
+      abb[i] = units[i].getAbbreviation();
+    }
+    return abb;
+  }
+}

Modified: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationDialog.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationDialog.java	2012-05-14 12:26:39 UTC (rev 1997)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationDialog.java	2012-05-14 15:16:24 UTC (rev 1998)
@@ -35,6 +35,7 @@
 import java.util.List;
 import java.util.Map;
 
+import de.schmitzm.data.Unit;
 import de.schmitzm.lang.LangUtil;
 import de.schmitzm.swing.OkCancelDialog;
 import de.schmitzm.swing.SwingUtil;
@@ -104,22 +105,48 @@
    *               for each entry a combo box is created to allocate a CSV column
    * @param csvColumns list of CSV column names which can be allocated to the fields
    * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   * @param csvUnits optional unit set for CSV column (by field ID); can be {@code null}
+   * @param defaultUnitMapping default unit for CSV column (by field ID); can be {@code null}
    */
-  public void setAllocationOptions(Map<String,String> fields, List<String> csvColumns, Map<String,String> defaultMapping) {
-    allocationPanel.init(fields, csvColumns, defaultMapping);
+  public void setAllocationOptions(Map<String,String> fields, List<String> csvColumns, Map<String,String> defaultMapping, Map<String,Unit<?>[]> csvUnits, Map<String,String> defaultUnitMapping) {
+    allocationPanel.init(fields, csvColumns, defaultMapping, csvUnits, defaultUnitMapping);
     pack();
     SwingUtil.setRelativeFramePosition(this, getOwner(), 0.5, 0.5);
   }
 
   /**
    * Initializes the panel with a new set of fields.
+   * @param fields map of fields (key = field ID; value = description for GUI label);
+   *               for each entry a combo box is created to allocate a CSV column
+   * @param csvColumns list of CSV column names which can be allocated to the fields
+   * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   */
+  public void setAllocationOptions(Map<String,String> fields, List<String> csvColumns, Map<String,String> defaultMapping) {
+    setAllocationOptions(fields, csvColumns, defaultMapping, null, null);
+  }
+
+  /**
+   * Initializes the panel with a new set of fields.
    * @param fields list of fields (entry is used as ID and GUI label);
    *               for each entry a combo box is created to allocate a CSV column
    * @param csvColumns list of CSV column names which can be allocated to the fields
    * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   * @param csvUnits optional unit set for CSV column (by field ID); can be {@code null}
+   * @param defaultUnitMapping default unit for CSV column (by field ID); can be {@code null}
    */
+  public void setAllocationOptions(List<String> fields, List<String> csvColumns, Map<String,String> defaultMapping, Map<String,Unit<?>[]> csvUnits, Map<String,String> defaultUnitMapping) {
+    setAllocationOptions( LangUtil.convertToMap(fields), csvColumns, defaultMapping, csvUnits, defaultUnitMapping );
+  }
+
+  /**
+   * Initializes the panel with a new set of fields.
+   * @param fields list of fields (entry is used as ID and GUI label);
+   *               for each entry a combo box is created to allocate a CSV column
+   * @param csvColumns list of CSV column names which can be allocated to the fields
+   * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   */
   public void setAllocationOptions(List<String> fields, List<String> csvColumns, Map<String,String> defaultMapping) {
-    setAllocationOptions( LangUtil.convertToMap(fields), csvColumns, defaultMapping );
+    setAllocationOptions(fields, csvColumns, defaultMapping, null, null);
   }
   
   /**
@@ -138,4 +165,19 @@
     return allocationPanel.getMappingCsvToField();
   }
 
+  /**
+   * Returns the mapping specified by the input fields. The map key identifies
+   * the CSV column and the value the specified unit.
+   */
+  public Map<String,Unit<?>> getMappingCsvToUnit() {
+    return allocationPanel.getMappingCsvToUnit();
+  }
+
+  /**
+   * Returns the mapping specified by the input fields. The map key identifies
+   * the CSV column and the value the abbreviation of the specified unit.
+   */
+  public Map<String,String> getMappingCsvToUnitAbbr() {
+    return allocationPanel.getMappingCsvToUnitAbbr();
+  }
 }

Modified: trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationPanel.java
===================================================================
--- trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationPanel.java	2012-05-14 12:26:39 UTC (rev 1997)
+++ trunk/schmitzm-excelcsv/src/main/java/de/schmitzm/csv/gui/CSVColumnAllocationPanel.java	2012-05-14 15:16:24 UTC (rev 1998)
@@ -39,6 +39,8 @@
 import net.miginfocom.swing.MigLayout;
 
 import de.schmitzm.csv.CsvUtil;
+import de.schmitzm.data.Unit;
+import de.schmitzm.data.UnitUtil;
 import de.schmitzm.lang.LangUtil;
 import de.schmitzm.lang.SequentialOrderedMap;
 import de.schmitzm.swing.JPanel;
@@ -63,11 +65,16 @@
   protected JLabel fieldsHeader;
   /** Header caption for list of CSV column combo boxes. */
   protected JLabel columnsHeader;
+  /** Header caption for list of CSV unit combo boxes. */
+  protected JLabel unitsHeader;
   /** Holds for each field (ID) the GUI label. */
   protected Map<String,JLabel> allocationCaptions = new SequentialOrderedMap<String, JLabel>();
   /** Holds for each field (ID) the combo box to select the CSV column. */
   protected Map<String,SelectionInputOption.Combo<String>> allocationColumns = new SequentialOrderedMap<String, SelectionInputOption.Combo<String>>();
   
+  /** Holds for each field (ID) the combo box to select an optional CSV column unit. */
+  protected Map<String,SelectionInputOption.Combo<Unit<?>>> allocationUnits = new SequentialOrderedMap<String, SelectionInputOption.Combo<Unit<?>>>();
+
   /**
    * Creates a new allocation panel.
    * @param fieldHeaderCaption short description/title for application depending fields (first column) 
@@ -76,13 +83,15 @@
    * @param csvColumns list of CSV column names which can be allocated to the fields
    */
   public CSVColumnAllocationPanel(String fieldHeaderCaption, Map<String,String> fields, List<String> csvColumns) {
-    super( new MigLayout("wrap 2","[]20[grow]","") );
+    super( new MigLayout("wrap 3","[]20[grow]5[grow]","") );
     this.fieldsHeader = new JLabel(fieldHeaderCaption);
     fieldsHeader.setFont(HEADER_FONT);
     this.columnsHeader = new JLabel( CsvUtil.R("CSVColumnAllocationPanel.columns.label") );
     columnsHeader.setFont(HEADER_FONT);
+    this.unitsHeader = new JLabel( CsvUtil.R("CSVColumnAllocationPanel.units.label") );
+    unitsHeader.setFont(HEADER_FONT);
     
-    init(fields,csvColumns,null);
+    init(fields,csvColumns,null,null,null);
   }
 
   /**
@@ -124,8 +133,10 @@
    *               for each entry a combo box is created to allocate a CSV column
    * @param csvColumns list of CSV column names which can be allocated to the fields
    * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   * @param csvUnits optional unit set for CSV column (by field ID); can be {@code null}
+   * @param defaultUnitMapping default unit for CSV column (by field ID); can be {@code null}
    */
-  public void init(Map<String,String> fields, List<String> csvColumns, Map<String,String> defaultMapping) {
+  public void init(Map<String,String> fields, List<String> csvColumns, Map<String,String> defaultMapping, Map<String,Unit<?>[]> csvUnits, Map<String,String> defaultUnitMapping) {
     allocationCaptions.clear();
     allocationColumns.clear();
     removeAll();
@@ -134,7 +145,12 @@
     
     // Add header fields
     add(fieldsHeader,"");
-    add(columnsHeader,"growx");
+    if ( csvUnits != null && csvUnits.size() > 0 ) {
+      add(columnsHeader, "growx");
+      add(unitsHeader,"wrap");
+    } else
+      add(columnsHeader, "growx, wrap");
+
     // Add row field for each field defined in the map
     String[] csvColumnArray = csvColumns.toArray(new String[0]);
     for (String fieldID : fields.keySet()) {
@@ -146,22 +162,57 @@
       allocationCaptions.put(fieldID, fieldCaption);
       allocationColumns.put(fieldID, fieldColumn);
       
+      // if units specified add combo box for unit selection
+      Unit[] units = csvUnits != null ? csvUnits.get(fieldID) : null;
+      SelectionInputOption.Combo<Unit<?>> fieldUnit = null;
+      if ( units != null ) {
+        fieldUnit = new SelectionInputOption.Combo<Unit<?>>(
+            null, false, units, null, UnitUtil.getUnitsAbbreviation(units));
+        allocationUnits.put(fieldID, fieldUnit);
+      }
+      
       add(fieldCaption,"");
-      add(fieldColumn,"growx");
+      add(fieldColumn, fieldUnit != null ? "growx" : "growx, wrap");
+      if ( fieldUnit != null )
+        add(fieldUnit,"growx, wrap");
     }
     
-    setMapping(defaultMapping, true);
+    setMapping(defaultMapping, defaultUnitMapping, true);
   }
 
   /**
    * Initializes the panel with a new set of fields.
+   * @param fields map of fields (key = field ID; value = description for GUI label);
+   *               for each entry a combo box is created to allocate a CSV column
+   * @param csvColumns list of CSV column names which can be allocated to the fields
+   * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   */
+  public void init(Map<String,String> fields, List<String> csvColumns, Map<String,String> defaultMapping) {
+    init(fields, csvColumns, defaultMapping, null, null);
+  }
+
+  /**
+   * Initializes the panel with a new set of fields.
    * @param fields list of fields (entry is used as ID and GUI label);
    *               for each entry a combo box is created to allocate a CSV column
    * @param csvColumns list of CSV column names which can be allocated to the fields
    * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   * @param csvUnits optional unit set for CSV column (by field ID); can be {@code null}
+   * @param defaultUnitMapping default unit for CSV column (by field ID); can be {@code null}
    */
+  public void init(List<String> fields, List<String> csvColumns, Map<String,String> defaultMapping, Map<String,Unit<?>[]> csvUnits, Map<String,String> defaultUnitMapping) {
+    init( LangUtil.convertToMap(fields), csvColumns, defaultMapping, csvUnits, defaultUnitMapping );
+  }
+
+  /**
+   * Initializes the panel with a new set of fields.
+   * @param fields list of fields (entry is used as ID and GUI label);
+   *               for each entry a combo box is created to allocate a CSV column
+   * @param csvColumns list of CSV column names which can be allocated to the fields
+   * @param defaultMapping default CSV column names by field (ID); can be {@code null}
+   */
   public void init(List<String> fields, List<String> csvColumns, Map<String,String> defaultMapping) {
-    init( LangUtil.convertToMap(fields), csvColumns, defaultMapping );
+    init( fields, csvColumns, defaultMapping, null, null );
   }
   
   
@@ -200,12 +251,18 @@
    * @param ignoreNulls indicates whether {@code null} values are ignored
    *                    or applied to combo box
    */
-  public void setMapping(Map<String,String> mapping, boolean ignoreNulls) {
+  public void setMapping(Map<String,String> mapping, Map<String,String> unitMapping, boolean ignoreNulls) {
     for (String fieldID : allocationColumns.keySet()) {
       SelectionInputOption.Combo<String> fieldColumn = allocationColumns.get(fieldID);
-      String defaultValue = mapping != null ? mapping.get(fieldID) : null;
-      if ( !ignoreNulls || defaultValue != null )
-        fieldColumn.setValue(defaultValue);
+      SelectionInputOption.Combo<Unit<?>>  units = allocationUnits.get(fieldID);
+      String defaultCsvColumn = mapping != null ? mapping.get(fieldID) : null;
+      if ( !ignoreNulls || defaultCsvColumn != null )
+        fieldColumn.setValue(defaultCsvColumn);
+      // Einheit vorbelegen (nur, wenn auch CSV Spalte zugeordnet wurde!)
+      if ( units != null && fieldColumn.getValue() != null ) {
+        String defaultUnit = unitMapping.get(defaultCsvColumn);
+        units.setSelectedDisplayItem(defaultUnit);
+      }
     }
   }
 
@@ -223,4 +280,34 @@
     }
     return allocation;
   }
+
+  /**
+   * Returns the mapping specified by the input fields. The map key identifies
+   * the CSV column and the value the specified unit.
+   */
+  public Map<String,Unit<?>> getMappingCsvToUnit() {
+    Map<String,Unit<?>> allocation = new SequentialOrderedMap<String, Unit<?>>();
+    for (String fieldID : allocationColumns.keySet()) {
+      SelectionInputOption.Combo<String> fieldColumn = allocationColumns.get(fieldID);
+      SelectionInputOption.Combo<Unit<?>> units = allocationUnits.get(fieldID);
+      String columnName = (String)fieldColumn.getValue();
+      if ( columnName != null && units != null)
+        allocation.put(columnName,(Unit<?>)units.getValue());
+    }
+    return allocation;
+  }
+
+  /**
+   * Returns the mapping specified by the input fields. The map key identifies
+   * the CSV column and the value the abbreviation of the specified unit.
+   */
+  public Map<String,String> getMappingCsvToUnitAbbr() {
+    Map<String,Unit<?>> allocation = getMappingCsvToUnit();
+    Map<String,String>  allocationAbbr = new SequentialOrderedMap<String, String>();
+    for (String csvColumn : allocation.keySet()) {
+      Unit<?> unit = allocation.get(csvColumn);
+      allocationAbbr.put(csvColumn, unit != null ? unit.getAbbreviation() : null);
+    }
+    return allocationAbbr;
+  }
 }

Modified: trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle.properties
===================================================================
--- trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle.properties	2012-05-14 12:26:39 UTC (rev 1997)
+++ trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle.properties	2012-05-14 15:16:24 UTC (rev 1998)
@@ -58,4 +58,5 @@
 CSVPreviewPanel.preview.limit.label=Limit for number of preview rows
 CSVPreviewPanel.preview.firstLine.label=First line for preview
 
-CSVColumnAllocationPanel.columns.label=CSV column allocation
\ No newline at end of file
+CSVColumnAllocationPanel.columns.label=CSV column allocation
+CSVColumnAllocationPanel.units.label=CSV Unit
\ No newline at end of file

Modified: trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle_de.properties
===================================================================
--- trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle_de.properties	2012-05-14 12:26:39 UTC (rev 1997)
+++ trunk/schmitzm-excelcsv/src/main/resources/de/schmitzm/csv/resource/locales/CsvResourceBundle_de.properties	2012-05-14 15:16:24 UTC (rev 1998)
@@ -59,3 +59,4 @@
 CSVPreviewPanel.preview.firstLine.label=Erste Zeile für Vorschau
 
 CSVColumnAllocationPanel.columns.label=CSV-Spalten Zuweisung
+CSVColumnAllocationPanel.units.label=CSV Einheit
\ No newline at end of file



More information about the Schmitzm-commits mailing list