01: /*
02: * Copyright 2005 Keith Lea <keith[remove] at cs dot oswego dot edu>,
03: * Geert Bevin <gbevin[remove] at uwyn dot com>
04: * Distributed under the terms of either:
05: * - the common development and distribution license (CDDL), v1.0; or
06: * - the GNU Lesser General Public License, v2.1 or later
07: * $Id: SelectResourceBundle.java 3308 2006-06-15 18:54:14Z gbevin $
08: */
09: package com.uwyn.rife.site;
10:
11: import java.util.ListResourceBundle;
12: import java.util.Map;
13:
14: /**
15: * A <code>ResourceBundle</code> implementation which facilitates the use of select
16: * boxes in forms for properties with {@link ConstrainedProperty#inList inList}
17: * constraints.
18: *
19: * Example use:
20: * <pre>Template t = ...
21: * List<Club> clubs = ...;
22: * ClubSelectionBean bean = ...;
23: *
24: * Map<String,String> names = new HashMap<String,String>(clubs.size());
25: * for (Club club : clubs) {
26: * names.put(club.getUniqueName(), club.getFullName());
27: * }
28: *
29: * bean.getConstrainedProperty("clubUniqueName")
30: * .setInList(names.keySet());
31: * t.addResourceBundle(new SelectResourceBundle("clubUniqueName", names));
32: *
33: * generateForm(t, bean);
34: * print(t);</pre>
35: *
36: * @author Keith Lea <keith[remove] at cs dot oswego dot edu>,
37: * @author Geert Bevin (gbevin[remove] at uwyn dot com)
38: * @version $Revision: 3308 $
39: * @since 1.0
40: */
41: public class SelectResourceBundle extends ListResourceBundle {
42: /** The list of keys and associated values. */
43: private final Object[][] mObjectNames;
44:
45: /**
46: * Creates a new select tag resource bundle with the given input form
47: * property and the given map from values to displayed strings.
48: *
49: * @param property the property whose possible values are described in
50: * <code>map</code>
51: * @param map a map from possible property values with their corresponding
52: * descriptions
53: */
54: public SelectResourceBundle(String property,
55: Map<? extends CharSequence, ? extends CharSequence> map)
56: throws IllegalArgumentException {
57: if (null == property)
58: throw new IllegalArgumentException("property can't be null");
59: if (null == map)
60: throw new IllegalArgumentException("map can't be null");
61:
62: Object[][] processed = new Object[map.size()][2];
63: int i = 0;
64: for (Map.Entry<? extends CharSequence, ?> entry : map
65: .entrySet()) {
66: if (null == entry.getKey()) {
67: continue;
68: }
69:
70: Object[] objects = processed[i];
71: objects[0] = property + ":" + entry.getKey();
72: objects[1] = entry.getValue();
73: i++;
74: }
75:
76: mObjectNames = processed;
77: }
78:
79: protected Object[][] getContents() {
80: return mObjectNames;
81: }
82: }
|