01: // Copyright 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.util;
16:
17: import static org.apache.tapestry.ioc.internal.util.CollectionFactory.newList;
18: import static org.apache.tapestry.ioc.internal.util.Defense.notNull;
19:
20: import java.io.Serializable;
21: import java.util.List;
22:
23: import org.apache.tapestry.OptionGroupModel;
24: import org.apache.tapestry.OptionModel;
25: import org.apache.tapestry.internal.OptionModelImpl;
26: import org.apache.tapestry.internal.TapestryInternalUtils;
27: import org.apache.tapestry.ioc.Messages;
28:
29: /**
30: * A basic select model for a particular Enum type. The labels for each Enum are drawn from the Enum
31: * instance name and the provides message catalog:
32: * <ul>
33: * <li>As key <em>ClassName</em>-<em>name</em> if present. The class name excludes the
34: * package portion. Ex: "ElementType.LOCAL_VARIABLE"
35: * <li>As key <em>name</em> if present, i.e., "LOCAL_VARIABLE".
36: * <li>As a user-presentable version of the name, i.e., "Local Variable".
37: * </ul>
38: */
39: public final class EnumSelectModel extends AbstractSelectModel
40: implements Serializable {
41: private static final long serialVersionUID = -3590412082766899684L;
42:
43: private final List<OptionModel> _options = newList();;
44:
45: public <T extends Enum> EnumSelectModel(Class<T> enumClass,
46: Messages messages) {
47: this (enumClass, messages, enumClass.getEnumConstants());
48: }
49:
50: public <T extends Enum> EnumSelectModel(Class<T> enumClass,
51: Messages messages, T[] values) {
52: notNull(enumClass, "enumClass");
53: notNull(messages, "messages");
54:
55: String prefix = TapestryInternalUtils.lastTerm(enumClass
56: .getName());
57:
58: for (T value : values) {
59: String label = TapestryInternalUtils.getLabelForEnum(
60: messages, prefix, value);
61:
62: _options.add(new OptionModelImpl(label, false, value));
63: }
64: }
65:
66: /** Returns null. */
67: public List<OptionGroupModel> getOptionGroups() {
68: return null;
69: }
70:
71: /** Returns the option groupos created in the constructor. */
72: public List<OptionModel> getOptions() {
73: return _options;
74: }
75:
76: }
|