01: /*
02: * Copyright (C) 2004 NNL Technology AB
03: * Visit www.infonode.net for information about InfoNode(R)
04: * products and how to contact NNL Technology AB.
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19: * MA 02111-1307, USA.
20: */
21:
22: // $Id: EnumProperty.java,v 1.7 2005/02/16 11:28:15 jesper Exp $
23: package net.infonode.properties.types;
24:
25: import net.infonode.properties.base.PropertyGroup;
26: import net.infonode.properties.util.PropertyValueHandler;
27: import net.infonode.properties.util.ValueHandlerProperty;
28: import net.infonode.util.ArrayUtil;
29:
30: /**
31: * A property which value is one in a fixed set of values.
32: *
33: * @author $Author: jesper $
34: * @version $Revision: 1.7 $
35: */
36: public class EnumProperty extends ValueHandlerProperty {
37: private Object[] validValues;
38:
39: /**
40: * Constructor.
41: *
42: * @param group the property group
43: * @param name the property name
44: * @param type the property type
45: * @param description the property description
46: * @param valueHandler handles values for this property
47: * @param validValues valid values for this property
48: */
49: public EnumProperty(PropertyGroup group, String name, Class type,
50: String description, PropertyValueHandler valueHandler,
51: Object[] validValues) {
52: super (group, name, type, description, valueHandler);
53: this .validValues = (Object[]) validValues.clone();
54: }
55:
56: public void setValue(Object object, Object value) {
57: if (!ArrayUtil.contains(validValues, value))
58: throw new IllegalArgumentException("Invalid enum value!");
59:
60: super .setValue(object, value);
61: }
62:
63: /**
64: * Returns the valid values for this property.
65: *
66: * @return the valid values for this property
67: */
68: public Object[] getValidValues() {
69: return (Object[]) validValues.clone();
70: }
71:
72: public Object getValue(Object object) {
73: Object value = super .getValue(object);
74: return value == null ? validValues[0] : value;
75: }
76: }
|