1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """A simple data object containing one typed value."""
17
18 from muntjac.data.util.abstract_property import AbstractProperty
19 from muntjac.data.property import ReadOnlyException, ConversionException
20
21
23 """A simple data object containing one typed value. This class is a
24 straightforward implementation of the the L{IProperty} interface.
25
26 @author: Vaadin Ltd.
27 @author: Richard Lincoln
28 @version: 1.1.2
29 """
30
31 - def __init__(self, value, typ=None, readOnly=None):
32 """Creates a new instance of ObjectProperty with the given value,
33 type and read-only mode status.
34
35 Any value of type Object is accepted, see L{ObjectProperty}.
36
37 @param value:
38 the Initial value of the property.
39 @param typ:
40 the type of the value. C{value} must be assignable
41 to this type.
42 @param readOnly:
43 Sets the read-only mode.
44 """
45 super(ObjectProperty, self).__init__()
46
47
48 self._value = None
49
50
51 self._type = None
52
53 if typ is None and readOnly is None:
54 ObjectProperty.__init__(self, value, value.__class__)
55 elif readOnly is None:
56 self._type = typ
57 self.setValue(value)
58 else:
59 ObjectProperty.__init__(self, value, typ)
60 self.setReadOnly(readOnly)
61
62
64 """Returns the type of the ObjectProperty. The methods C{getValue}
65 and C{setValue} must be compatible with this type: one must be
66 able to safely cast the value returned from C{getValue} to the
67 given type and pass any variable assignable to this type as an
68 argument to C{setValue}.
69
70 @return: type of the Property
71 """
72 return self._type
73
74
76 """Gets the value stored in the Property.
77
78 @return: the value stored in the Property
79 """
80 return self._value
81
82
84 """Sets the value of the property. This method supports setting from
85 C{str} if either C{str} is directly assignable to property type, or
86 the type class contains a string constructor.
87
88 @param newValue:
89 the New value of the property.
90 @raise ReadOnlyException:
91 if the object is in read-only mode
92 @raise ConversionException:
93 if the newValue can't be converted into the Property's
94 native type directly or through C{str}
95 """
96
97 if self.isReadOnly():
98 raise ReadOnlyException()
99
100
101 if newValue is None or issubclass(newValue.__class__, self._type):
102 self._value = newValue
103 else:
104
105 try:
106
107 constr = self.getType()
108
109 self._value = constr(str(newValue))
110 except Exception, e:
111 raise ConversionException(e)
112
113 self.fireValueChange()
114