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

Source Code for Module muntjac.data.util.propertyset_item

  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  from muntjac.data.item import \ 
 17      IItem, IPropertySetChangeEvent, IPropertySetChangeNotifier, \ 
 18      IPropertySetChangeListener 
 19   
 20  from muntjac.util import EventObject 
 21   
 22   
23 -class PropertysetItem(IItem, IPropertySetChangeNotifier): # Cloneable
24 """Class for handling a set of identified Properties. The elements 25 contained in a C{MapItem} can be referenced using locally unique 26 identifiers. The class supports listeners who are interested in changes 27 to the Property set managed by the class. 28 29 @author: Vaadin Ltd. 30 @author: Richard Lincoln 31 @version: 1.1.2 32 """ 33
34 - def __init__(self):
35 # Mapping from property id to property. 36 self._map = dict() 37 38 # List of all property ids to maintain the order. 39 self._list = list() 40 41 # List of property set modification listeners. 42 self._propertySetChangeListeners = list() 43 44 self._propertySetChangeCallbacks = dict()
45 46
47 - def getItemProperty(self, idd):
48 """Gets the Property corresponding to the given Property ID stored in 49 the Item. If the Item does not contain the Property, C{None} is 50 returned. 51 52 @param idd: the identifier of the Property to get. 53 @return: the Property with the given ID or C{None} 54 """ 55 return self._map.get(idd)
56 57
58 - def getItemPropertyIds(self):
59 """Gets the collection of IDs of all Properties stored in the Item. 60 61 @return: collection containing IDs of the Properties 62 stored the Item 63 """ 64 return list(self._list)
65 66
67 - def removeItemProperty(self, idd):
68 """Removes the Property identified by ID from the Item. This 69 functionality is optional. If the method is not implemented, the 70 method always returns C{False}. 71 72 @param idd: the ID of the Property to be removed. 73 @return: C{True} if the operation succeeded C{False} if not 74 """ 75 # Cant remove missing properties 76 if idd not in self._map: 77 return False 78 79 del self._map[idd] 80 81 self._list.remove(idd) 82 83 # Send change events 84 self.fireItemPropertySetChange() 85 86 return True
87 88
89 - def addItemProperty(self, idd, prop):
90 """Tries to add a new Property into the Item. 91 92 @param id: 93 the ID of the new Property. 94 @param prop: 95 the Property to be added and associated with the id. 96 @return: C{True} if the operation succeeded, C{False} if not 97 """ 98 # Null ids are not accepted 99 if idd is None: 100 raise ValueError, 'Item property id can not be null' 101 102 # Cant add a property twice 103 if idd in self._map: 104 return False 105 106 # Put the property to map 107 self._map[idd] = prop 108 self._list.append(idd) 109 110 # Send event 111 self.fireItemPropertySetChange() 112 113 return True
114 115
116 - def __str__(self):
117 """Gets the string representation of the contents of the Item. 118 The format of the string is a space separated catenation of the 119 string representations of the Properties contained by the Item. 120 121 @return: String representation of the Item contents 122 """ 123 retValue = '' 124 for i, propertyId in enumerate(self.getItemPropertyIds()): 125 retValue += str( self.getItemProperty(propertyId) ) 126 if i < len(self.getItemPropertyIds()) - 1: 127 retValue += ' ' 128 return retValue
129 130
131 - def addListener(self, listener, iface=None):
132 """Registers a new property set change listener for this Item. 133 134 @param listener: the new Listener to be registered. 135 """ 136 if (isinstance(listener, IPropertySetChangeListener) and 137 (iface is None or 138 issubclass(iface, IPropertySetChangeListener))): 139 self._propertySetChangeListeners.append(listener)
140 141
142 - def addCallback(self, callback, eventType=None, *args):
143 if eventType is None: 144 eventType = callback._eventType 145 146 if issubclass(eventType, IPropertySetChangeEvent): 147 self._propertySetChangeCallbacks[callback] = args 148 else: 149 super(PropertysetItem, self).addCallback(callback, 150 eventType, *args)
151 152
153 - def removeListener(self, listener, iface=None):
154 """Removes a previously registered property set change listener. 155 156 @param listener: the Listener to be removed. 157 """ 158 if (isinstance(listener, IPropertySetChangeListener) and 159 (iface is None or 160 issubclass(iface, IPropertySetChangeListener))): 161 if listener in self._propertySetChangeListeners: 162 self._propertySetChangeListeners.remove(listener)
163 164
165 - def removeCallback(self, callback, eventType=None):
166 if eventType is None: 167 eventType = callback._eventType 168 169 if issubclass(eventType, IPropertySetChangeEvent): 170 if callback in self._propertySetChangeCallbacks: 171 del self._propertySetChangeCallbacks[callback] 172 else: 173 super(PropertysetItem, self).removeCallback(callback, eventType)
174 175
176 - def fireItemPropertySetChange(self):
177 """Sends a Property set change event to all interested listeners.""" 178 event = PropertySetChangeEvent(self) 179 for listener in self._propertySetChangeListeners: 180 listener.itemPropertySetChange(event) 181 182 for callback, args in self._propertySetChangeCallbacks.iteritems(): 183 callback(event, *args)
184 185
186 - def getListeners(self, eventType):
187 if issubclass(eventType, IPropertySetChangeEvent): 188 return list(self._propertySetChangeListeners) 189 return list()
190 191
192 - def getCallbacks(self, eventType):
193 if issubclass(eventType, IPropertySetChangeEvent): 194 return dict(self._propertySetChangeCallbacks) 195 return dict()
196 197
198 - def clone(self):
199 """Creates and returns a copy of this object. 200 201 The method C{clone} performs a shallow copy of the C{PropertysetItem}. 202 203 Note: All arrays are considered to implement the interface Cloneable. 204 Otherwise, this method creates a new instance of the class of this 205 object and initializes all its fields with exactly the contents of the 206 corresponding fields of this object, as if by assignment, the contents 207 of the fields are not themselves cloned. Thus, this method performs a 208 "shallow copy" of this object, not a "deep copy" operation. 209 210 @raise CloneNotSupportedException: 211 if the object's class does not support the Cloneable 212 interface. 213 """ 214 npsi = PropertysetItem() 215 npsi.list = list(self._list) if self._list is not None else None 216 npsi.propertySetChangeListeners = list(self._propertySetChangeListeners) 217 npsi.map = self._map.copy() 218 return npsi
219 220
221 - def __eq__(self, obj):
222 if (obj is None) or (not isinstance(obj, PropertysetItem)): 223 return False 224 225 other = obj 226 if other._list != self._list: 227 if other._list is None: 228 return False 229 if not (other._list == self._list): 230 return False 231 232 if other._map != self._map: 233 if other._map is None: 234 return False 235 if other._map != self._map: 236 return False 237 238 if other._propertySetChangeListeners != self._propertySetChangeListeners: 239 thisEmpty = ((self._propertySetChangeListeners is None) 240 or len(self._propertySetChangeListeners) == 0) 241 242 otherEmpty = ((other.propertySetChangeListeners is None) 243 or len(other.propertySetChangeListeners) == 0) 244 245 if thisEmpty and otherEmpty: 246 return True 247 248 if otherEmpty: 249 return False 250 251 if (other.propertySetChangeListeners != 252 self._propertySetChangeListeners): 253 return False 254 255 return True
256 257
258 - def __hash__(self):
259 return (((0 if self._list is None else hash(self._list)) 260 ^ (0 if self._map is None else hash(self._map))) 261 ^ (0 if (self._propertySetChangeListeners is None) 262 or (len(self._propertySetChangeListeners) == 0) 263 else hash(self._propertySetChangeListeners)))
264 265
266 -class PropertySetChangeEvent(EventObject, IItem, IPropertySetChangeEvent):
267 """An C{event} object specifying an Item whose Property set has 268 changed. 269 270 @author: Vaadin Ltd. 271 @version: 1.1.2 272 """ 273
274 - def __init__(self, source):
275 super(PropertySetChangeEvent, self).__init__(source)
276 277
278 - def getItem(self):
279 """Gets the Item whose Property set has changed. 280 281 @return: source object of the event as an C{Item} 282 """ 283 return self.getSource()
284