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

Source Code for Module muntjac.data.util.abstract_container

  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  """Abstract container class that manages event listeners and sending events 
 17  to them.""" 
 18   
 19  from muntjac.util import EventObject 
 20   
 21  from muntjac.data.container import \ 
 22      (IContainer, IItemSetChangeEvent, IPropertySetChangeEvent, 
 23       IPropertySetChangeListener, IItemSetChangeListener) 
 24   
 25   
26 -class AbstractContainer(IContainer):
27 """Abstract container class that manages event listeners and sending events 28 to them (L{PropertySetChangeNotifier}, L{ItemSetChangeNotifier}). 29 30 Note that this class provides the internal implementations for both types 31 of events and notifiers as protected methods, but does not implement the 32 L{IPropertySetChangeNotifier} and L{ItemSetChangeNotifier} interfaces 33 directly. This way, subclasses can choose not to implement them. 34 Subclasses implementing those interfaces should also override the 35 corresponding L{addListener} and L{removeListener} methods to make them 36 public. 37 """ 38
39 - def __init__(self):
40 #: List of all Property set change event listeners. 41 self._propertySetChangeListeners = list() 42 43 #: List of all container Item set change event listeners. 44 self._itemSetChangeListeners = list() 45 46 self._propertySetChangeCallbacks = dict() 47 48 self._itemSetChangeCallbacks = dict()
49 50
51 - def addListener(self, listener, iface=None):
52 """Implementation of the corresponding method in 53 L{IPropertySetChangeNotifier} and L{ItemSetChangeNotifier}, override 54 and implement the interface to use this. 55 56 @see: L{IPropertySetChangeNotifier.addListener} 57 @see: L{IItemSetChangeNotifier.addListener} 58 """ 59 if (isinstance(listener, IItemSetChangeListener) and 60 (iface is None or issubclass(iface, IItemSetChangeListener))): 61 62 self.getItemSetChangeListeners().append(listener) 63 64 if (isinstance(listener, IPropertySetChangeListener) and 65 (iface is None or 66 issubclass(iface, IPropertySetChangeListener))): 67 68 self.getPropertySetChangeListeners().append(listener)
69 70
71 - def addCallback(self, callback, eventType=None, *args):
72 if eventType is None: 73 eventType = callback._eventType 74 75 if issubclass(eventType, IItemSetChangeEvent): 76 self._itemSetChangeCallbacks[callback] = args 77 78 elif issubclass(eventType, IPropertySetChangeEvent): 79 self._propertySetChangeCallbacks[callback] = args 80 81 else: 82 super(AbstractContainer, self).addCallback(callback, 83 eventType, args)
84 85
86 - def removeListener(self, listener, iface=None):
87 """Implementation of the corresponding method in 88 L{IPropertySetChangeNotifier} and L{ItemSetChangeNotifier}, override 89 and implement the interface to use this. 90 91 @see: L{IPropertySetChangeNotifier.removeListener} 92 @see: L{ItemSetChangeNotifier.removeListener} 93 """ 94 if (isinstance(listener, IItemSetChangeListener) and 95 (iface is None or issubclass(iface, IItemSetChangeListener))): 96 if listener in self.getItemSetChangeListeners(): 97 self.getItemSetChangeListeners().remove(listener) 98 99 if (isinstance(listener, IPropertySetChangeListener) and 100 (iface is None or 101 issubclass(iface, IPropertySetChangeListener))): 102 if listener in self.getPropertySetChangeListeners(): 103 self.getPropertySetChangeListeners().remove(listener)
104 105
106 - def removeCallback(self, callback, eventType=None):
107 if eventType is None: 108 eventType = callback._eventType 109 110 if (issubclass(eventType, IItemSetChangeEvent) and 111 callback in self._itemSetChangeCallbacks): 112 del self._itemSetChangeCallbacks[callback] 113 114 elif (issubclass(eventType, IPropertySetChangeEvent) and 115 callback in self._propertySetChangeCallbacks): 116 del self._propertySetChangeCallbacks[callback] 117 118 else: 119 super(AbstractContainer, self).removeCallback(callback, eventType)
120 121
122 - def fireContainerPropertySetChange(self, event=None):
123 """Sends a simple Property set change event to all interested 124 listeners. 125 126 Use L{fireContainerPropertySetChange} instead of this method 127 unless additional information about the exact changes is available and 128 should be included in the event. 129 130 @param event: 131 the property change event to send, optionally with 132 additional information 133 """ 134 if event is None: 135 event = BasePropertySetChangeEvent(self) 136 137 for listener in self.getPropertySetChangeListeners(): 138 listener.containerPropertySetChange(event) 139 140 for callback, args in self._propertySetChangeCallbacks.iteritems(): 141 callback(event, *args)
142 143
144 - def fireItemSetChange(self, event=None):
145 """Sends a simple Item set change event to all interested listeners, 146 indicating that anything in the contents may have changed (items added, 147 removed etc.). 148 149 @param event: 150 the item set change event to send, optionally with 151 additional information 152 """ 153 if event is None: 154 event = BaseItemSetChangeEvent(self) 155 156 for listener in self.getItemSetChangeListeners(): 157 listener.containerItemSetChange(event) 158 159 for callback, args in self._itemSetChangeCallbacks.iteritems(): 160 callback(event, *args)
161 162
163 - def setPropertySetChangeListeners(self, propertySetChangeListeners):
164 """Sets the property set change listener collection. For internal 165 use only. 166 """ 167 self._propertySetChangeListeners = propertySetChangeListeners
168 169
171 """Returns the property set change listener collection. For internal 172 use only. 173 """ 174 return self._propertySetChangeListeners
175 176
177 - def setItemSetChangeListeners(self, itemSetChangeListeners):
178 """Sets the item set change listener collection. For internal use only. 179 """ 180 self._itemSetChangeListeners = itemSetChangeListeners
181 182
184 """Returns the item set change listener collection. For internal use 185 only.""" 186 return self._itemSetChangeListeners
187 188
189 - def getListeners(self, eventType):
190 if issubclass(eventType, IPropertySetChangeEvent): 191 return list(self._propertySetChangeListeners) 192 193 elif issubclass(eventType, IItemSetChangeEvent): 194 return list(self._itemSetChangeListeners) 195 196 return list()
197 198
199 - def getCallbacks(self, eventType):
200 if issubclass(eventType, IPropertySetChangeEvent): 201 return dict(self._propertySetChangeCallbacks) 202 203 elif issubclass(eventType, IItemSetChangeEvent): 204 return dict(self._itemSetChangeCallbacks) 205 206 return dict()
207 208
209 -class BasePropertySetChangeEvent(EventObject, IContainer, 210 IPropertySetChangeEvent):
211 """An C{event} object specifying the container whose Property 212 set has changed. 213 214 This class does not provide information about which properties were 215 concerned by the change, but subclasses can provide additional 216 information about the changes. 217 """ 218
219 - def __init__(self, source):
220 super(BasePropertySetChangeEvent, self).__init__(source)
221 222
223 - def getContainer(self):
224 return self.getSource()
225 226
227 -class BaseItemSetChangeEvent(EventObject, IContainer, IItemSetChangeEvent):
228 """An C{event} object specifying the container whose Item set 229 has changed. 230 231 This class does not provide information about the exact changes 232 performed, but subclasses can add provide additional information about 233 the changes. 234 """ 235
236 - def __init__(self, source):
237 super(BaseItemSetChangeEvent, self).__init__(source)
238 239
240 - def getContainer(self):
241 return self.getSource()
242