KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > vlib > EntitySelectionModel


1 // Copyright 2004 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.vlib;
16
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import org.apache.hivemind.ApplicationRuntimeException;
21 import org.apache.tapestry.form.IPropertySelectionModel;
22
23 /**
24  * This class is used as a property selection model to select a primary key.
25  * We assume that the primary keys are integers, which makes it easy to
26  * translate between the various representations.
27  *
28  * @version $Id: EntitySelectionModel.java,v 1.4 2004/04/30 15:17:22 hlship Exp $
29  * @author Howard Lewis Ship
30  *
31  **/

32
33 public class EntitySelectionModel implements IPropertySelectionModel
34 {
35     private static class Entry
36     {
37         Integer JavaDoc primaryKey;
38         String JavaDoc label;
39
40         Entry(Integer JavaDoc primaryKey, String JavaDoc 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 JavaDoc entries = new ArrayList JavaDoc(LIST_SIZE);
51
52     public void add(Integer JavaDoc key, String JavaDoc 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 JavaDoc getOption(int index)
71     {
72         return get(index).primaryKey;
73     }
74
75     public String JavaDoc getLabel(int index)
76     {
77         return get(index).label;
78     }
79
80     public String JavaDoc getValue(int index)
81     {
82         Integer JavaDoc primaryKey;
83
84         primaryKey = get(index).primaryKey;
85
86         if (primaryKey == null)
87             return "";
88
89         return primaryKey.toString();
90     }
91
92     public Object JavaDoc translateValue(String JavaDoc value)
93     {
94         if (value.equals(""))
95             return null;
96
97         try
98         {
99             return new Integer JavaDoc(value);
100         }
101         catch (NumberFormatException JavaDoc e)
102         {
103             throw new ApplicationRuntimeException("Could not convert '" + value + "' to an Integer.", e);
104         }
105     }
106 }
Popular Tags