You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by ta...@jakarta.apache.org on 2005/03/04 22:33:43 UTC

[Jakarta Tapestry Wiki] Updated: DataSqueezer

   Date: 2005-03-04T13:33:42
   Editor: JonathanMillett
   Wiki: Jakarta Tapestry Wiki
   Page: DataSqueezer
   URL: http://wiki.apache.org/jakarta-tapestry/DataSqueezer

   no comment

Change Log:

------------------------------------------------------------------------------
@@ -65,3 +65,84 @@
         return new DataSqueezer(getResourceResolver(), adapt);
     }
 }}}
+
+==== Example PropertySelection using Data Squeezer ====
+This example shows how create a DataSqueezer enabled PropertySelection.  DataObject is the base class for all the squeezeable objects, and DataObjectAdaptor is the squeeze adaptor that uses a orm service to load and save DataObjects.  In contrast to the first example, the adaptor class is not anonymous because it is used both here and in the engine class (not shown).
+
+{{{
+package foo.web;
+
+import foo.orm.DataObject;
+import foo.orm.DataObjectAdaptor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tapestry.form.IPropertySelectionModel;
+
+public class EntitySelectionModel implements IPropertySelectionModel
+{
+    private static class Entry
+    {
+    	DataObject option;
+        String value;
+        String label;
+
+        Entry(DataObject option, String value, String label)
+        {
+        	this.option = option;
+            this.value = value;
+            this.label = label;
+        }
+    }
+
+    private static final int LIST_SIZE = 20;
+
+    private DataObjectAdaptor adaptor;
+    private List<Entry> entries = new ArrayList<Entry>(LIST_SIZE);
+
+    public EntitySelectionModel(DataObjectAdaptor adaptor)
+    {
+    	this.adaptor= adaptor;
+    	entries.add(new Entry(null, "", ""));
+    }
+    
+    public <T extends DataObject> void add(List<T> entities)
+    {
+    	for (T entity : entities)
+    	{
+    		String value = adaptor.squeeze(null, entity);
+    		String label = entity.toString();
+    		entries.add(new Entry(entity, value, label));
+    	}
+    }
+    
+    public int getOptionCount()
+    {
+        return entries.size();
+    }
+
+    public Object getOption(int index)
+    {
+        return entries.get(index).option;
+    }
+
+    public String getLabel(int index)
+    {
+        return entries.get(index).label;
+    }
+
+    public String getValue(int index)
+    {
+    	return entries.get(index).value;
+    }
+
+    public Object translateValue(String value)
+    {
+        if (value.equals(""))
+            return null;
+
+        return adaptor.unsqueeze(null, value);
+    }
+}
+}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org