Package muntjac :: Package data :: Package util :: Module object_property
[hide private]
[frames] | no frames]

Source Code for Module muntjac.data.util.object_property

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 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   
22 -class ObjectProperty(AbstractProperty):
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 #: The value contained by the Property. 48 self._value = None 49 50 #: Data type of the Property's value. 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
63 - def getType(self):
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
75 - def getValue(self):
76 """Gets the value stored in the Property. 77 78 @return: the value stored in the Property 79 """ 80 return self._value
81 82
83 - def setValue(self, newValue):
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 # Checks the mode 97 if self.isReadOnly(): 98 raise ReadOnlyException() 99 100 # Tries to assign the compatible value directly 101 if newValue is None or issubclass(newValue.__class__, self._type): 102 self._value = newValue 103 else: 104 # Gets the string constructor 105 try: 106 #constr = self.getType().getConstructor([str]) 107 constr = self.getType() # FIXME: getConstructor 108 # Creates new object from the string 109 self._value = constr(str(newValue)) # FIXME: *args 110 except Exception, e: 111 raise ConversionException(e) 112 113 self.fireValueChange()
114