1 15 package org.apache.tapestry.vlib; 16 17 import java.util.ArrayList ; 18 import java.util.List ; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.tapestry.form.IPropertySelectionModel; 22 23 32 33 public class EntitySelectionModel implements IPropertySelectionModel 34 { 35 private static class Entry 36 { 37 Integer primaryKey; 38 String label; 39 40 Entry(Integer primaryKey, String label) 41 { 42 this.primaryKey = primaryKey; 43 this.label = label; 44 } 45 46 } 47 48 private static final int LIST_SIZE = 20; 49 50 private List entries = new ArrayList (LIST_SIZE); 51 52 public void add(Integer key, String label) 53 { 54 Entry entry; 55 56 entry = new Entry(key, label); 57 entries.add(entry); 58 } 59 60 public int getOptionCount() 61 { 62 return entries.size(); 63 } 64 65 private Entry get(int index) 66 { 67 return (Entry) entries.get(index); 68 } 69 70 public Object getOption(int index) 71 { 72 return get(index).primaryKey; 73 } 74 75 public String getLabel(int index) 76 { 77 return get(index).label; 78 } 79 80 public String getValue(int index) 81 { 82 Integer primaryKey; 83 84 primaryKey = get(index).primaryKey; 85 86 if (primaryKey == null) 87 return ""; 88 89 return primaryKey.toString(); 90 } 91 92 public Object translateValue(String value) 93 { 94 if (value.equals("")) 95 return null; 96 97 try 98 { 99 return new Integer (value); 100 } 101 catch (NumberFormatException e) 102 { 103 throw new ApplicationRuntimeException("Could not convert '" + value + "' to an Integer.", e); 104 } 105 } 106 } | Popular Tags |