Author: hlship
Date: Mon Feb 19 18:38:47 2007
New Revision: 509414
URL: http://svn.apache.org/viewvc?view=rev&rev=509414
Log:
Add a Map --> SelectModel coercion.
[TAPESTRY-1265]
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/TapestryUtils.java
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/TapestryUtilsTest.java
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/TapestryUtils.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/TapestryUtils.java?view=diff&rev=509414&r1=509413&r2=509414
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/TapestryUtils.java
(original)
+++
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/internal/TapestryUtils.java
Mon Feb 19 18:38:47 2007
@@ -185,6 +185,56 @@
}
/**
+ * Converts a map entry to an [EMAIL PROTECTED] OptionModel}.
+ *
+ * @param input
+ * @return
+ */
+ public static OptionModel toOptionModel(Map.Entry input)
+ {
+ Defense.notNull(input, "input");
+
+ String value = (input.getKey() != null ?
String.valueOf(input.getKey()) : "");
+ String label = (input.getValue() != null ?
String.valueOf(input.getValue()) : "");
+
+ return new OptionModelImpl(label, false, value);
+ }
+
+ /**
+ * Processes a map input into a series of map entries compatible with
+ * [EMAIL PROTECTED] #toOptionModel(Map.Entry)}.
+ *
+ * @param input
+ * map of elements
+ * @return list of option models
+ */
+ public static <K,V> List<OptionModel> toOptionModels(Map<K,V> input)
+ {
+ Defense.notNull(input, "input");
+
+ List<OptionModel> result = newList();
+
+ for (Map.Entry entry : input.entrySet())
+ result.add(toOptionModel(entry));
+
+ return result;
+ }
+
+ /**
+ * Wraps the result of [EMAIL PROTECTED] #toOptionModels(Map)} as a [EMAIL
PROTECTED] SelectModel} (with no option
+ * groups).
+ *
+ * @param input
+ * @return
+ */
+ public static <K,V> SelectModel toSelectModel(Map<K,V> input)
+ {
+ List<OptionModel> options = toOptionModels(input);
+
+ return new SelectModelImpl(null, options);
+ }
+
+ /**
* Parses a key/value pair where the key and the value are seperated by an
equals sign. The key
* and value are trimmed of leading and trailing whitespace, and returned
as a [EMAIL PROTECTED] KeyValue}.
*
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java?view=diff&rev=509414&r1=509413&r2=509414
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
(original)
+++
tapestry/tapestry5/tapestry-core/trunk/src/main/java/org/apache/tapestry/services/TapestryModule.java
Mon Feb 19 18:38:47 2007
@@ -1196,6 +1196,7 @@
* Adds coercions:
* <ul>
* <li>String to [EMAIL PROTECTED] SelectModel}
+ * <li>Map to [EMAIL PROTECTED] SelectModel}
* <li>List to [EMAIL PROTECTED] GridDataSource}
* <li>null to [EMAIL PROTECTED] GridDataSource}
* <li>String to [EMAIL PROTECTED] GridPagerPosition}
@@ -1207,6 +1208,14 @@
add(configuration, String.class, SelectModel.class, new
Coercion<String, SelectModel>()
{
public SelectModel coerce(String input)
+ {
+ return TapestryUtils.toSelectModel(input);
+ }
+ });
+
+ add(configuration, Map.class, SelectModel.class, new Coercion<Map,
SelectModel>()
+ {
+ public SelectModel coerce(Map input)
{
return TapestryUtils.toSelectModel(input);
}
Modified:
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/TapestryUtilsTest.java
URL:
http://svn.apache.org/viewvc/tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/TapestryUtilsTest.java?view=diff&rev=509414&r1=509413&r2=509414
==============================================================================
---
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/TapestryUtilsTest.java
(original)
+++
tapestry/tapestry5/tapestry-core/trunk/src/test/java/org/apache/tapestry/internal/TapestryUtilsTest.java
Mon Feb 19 18:38:47 2007
@@ -1,4 +1,4 @@
-// Copyright 2006 The Apache Software Foundation
+// Copyright 2006, 2007 The Apache Software Foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -17,8 +17,10 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
+import java.util.TreeMap;
import org.apache.tapestry.OptionModel;
import org.apache.tapestry.PropertyConduit;
@@ -158,7 +160,7 @@
}
@Test
- public void to_option_models()
+ public void string_to_option_models()
{
List<OptionModel> options =
TapestryUtils.toOptionModels("UK,USA,DE=Germany");
@@ -172,6 +174,39 @@
assertEquals(options.get(2).getLabel(), "Germany");
assertEquals(options.get(2).getValue(), "DE");
+ }
+
+ @Test
+ public void map_entry_to_option_model()
+ {
+ Map<String, String> map = Collections.singletonMap("key", "value");
+ Map.Entry entry = map.entrySet().iterator().next();
+ OptionModel model = TapestryUtils.toOptionModel(entry);
+
+ assertEquals(model.getLabel(), "value");
+ assertEquals(model.getValue(), "key");
+ }
+
+ @Test
+ public void map_to_option_models()
+ {
+ Map<Integer, String> map = new TreeMap<Integer, String>();
+ map.put(1, "A");
+ map.put(2, null);
+ map.put(3, "C");
+
+ List<OptionModel> options = TapestryUtils.toOptionModels(map);
+
+ assertEquals(options.size(), 3);
+
+ assertEquals(options.get(0).getLabel(), "A");
+ assertEquals(options.get(0).getValue(), "1");
+
+ assertEquals(options.get(1).getLabel(), "");
+ assertEquals(options.get(1).getValue(), "2");
+
+ assertEquals(options.get(2).getLabel(), "C");
+ assertEquals(options.get(2).getValue(), "3");
}
@Test