Package muntjac :: Package ui :: Module custom_component
[hide private]
[frames] | no frames]

Source Code for Module muntjac.ui.custom_component

  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  """Defines a simple implementation of IComponent interface for creation 
 17  of new UI components by composition of existing components.""" 
 18   
 19  from warnings import warn 
 20   
 21  from muntjac.util import fullname 
 22   
 23  from muntjac.ui.abstract_component_container import AbstractComponentContainer 
 24   
 25   
26 -class CustomComponent(AbstractComponentContainer):
27 """Custom component provides simple implementation of Component interface 28 for creation of new UI components by composition of existing components. 29 30 The component is used by inheriting the CustomComponent class and setting 31 composite root inside the Custom component. The composite root itself can 32 contain more components, but their interfaces are hidden from the users. 33 34 @author: Vaadin Ltd. 35 @author: Richard Lincoln 36 @version: 1.1.2 37 """ 38 39 CLIENT_WIDGET = None #ClientWidget(VCustomComponent, LoadStyle.EAGER) 40
41 - def __init__(self, compositionRoot=None):
42 """Constructs a new custom component. 43 44 The component is implemented by wrapping the methods of the 45 composition root component given as parameter. The composition root 46 must not be null and can not be changed after the composition. 47 48 @param compositionRoot: 49 the root of the composition component tree. 50 """ 51 52 # The root component implementing the custom component. 53 self._root = None 54 55 # Type of the component. 56 self._componentType = None 57 58 super(CustomComponent, self).__init__() 59 60 # expand horizontally by default 61 self.setWidth(100, self.UNITS_PERCENTAGE) 62 63 if compositionRoot is not None: 64 self.setCompositionRoot(compositionRoot)
65 66
67 - def getCompositionRoot(self):
68 """Returns the composition root. 69 70 @return: the Component Composition root. 71 """ 72 return self._root
73 74
75 - def setCompositionRoot(self, compositionRoot):
76 """Sets the compositions root. 77 78 The composition root must be set to non-null value before the 79 component can be used. The composition root can only be set once. 80 81 @param compositionRoot: 82 the root of the composition component tree. 83 """ 84 if compositionRoot != self._root: 85 if self._root is not None: 86 # remove old component 87 super(CustomComponent, self).removeComponent(self._root) 88 89 if compositionRoot is not None: 90 # set new component 91 super(CustomComponent, self).addComponent(compositionRoot) 92 93 self._root = compositionRoot 94 self.requestRepaint()
95 96
97 - def paintContent(self, target):
98 if self._root is None: 99 raise ValueError, ('Composition root must be set to' 100 + ' non-null value before the ' + fullname(self) 101 + ' can be painted') 102 103 if self.getComponentType() is not None: 104 target.addAttribute('type', self.getComponentType()) 105 106 self._root.paint(target)
107 108
109 - def getComponentType(self):
110 """Gets the component type. 111 112 The component type is textual type of the component. This is included 113 in the UIDL as component tag attribute. 114 115 @deprecated: not more useful as the whole tag system has been removed 116 117 @return: the component type. 118 """ 119 warn('tag system has been removed', DeprecationWarning) 120 return self._componentType
121 122
123 - def setComponentType(self, componentType):
124 """Sets the component type. 125 126 The component type is textual type of the component. This is included 127 in the UIDL as component tag attribute. 128 129 @deprecated: not more useful as the whole tag system has been removed 130 131 @param componentType: 132 the componentType to set. 133 """ 134 warn('tag system has been removed', DeprecationWarning) 135 self._componentType = componentType
136 137
138 - def getComponentIterator(self):
139 return ComponentIterator(self)
140 141
142 - def getComponentCount(self):
143 """Gets the number of contained components. Consistent with the 144 iterator returned by L{getComponentIterator}. 145 146 @return: the number of contained components (zero or one) 147 """ 148 return 1 if self._root is not None else 0
149 150
151 - def replaceComponent(self, oldComponent, newComponent):
152 """This method is not supported by CustomComponent. 153 154 @see: L{ComponentContainer.replaceComponent()} 155 """ 156 raise NotImplementedError
157 158
159 - def addComponent(self, c):
160 """This method is not supported by CustomComponent. Use 161 L{CustomComponent.setCompositionRoot} to set CustomComponents "child". 162 163 @see: L{AbstractComponentContainer.addComponent} 164 """ 165 raise NotImplementedError
166 167
168 - def moveComponentsFrom(self, source):
169 """This method is not supported by CustomComponent. 170 171 @see: L{AbstractComponentContainer.moveComponentsFrom} 172 """ 173 raise NotImplementedError
174 175
176 - def removeAllComponents(self):
177 """This method is not supported by CustomComponent. 178 179 @see: L{AbstractComponentContainer.removeAllComponents} 180 """ 181 raise NotImplementedError
182 183
184 - def removeComponent(self, c):
185 """This method is not supported by CustomComponent. 186 187 @see: L{AbstractComponentContainer.removeComponent} 188 """ 189 raise NotImplementedError
190 191
192 -class ComponentIterator(object):
193
194 - def __init__(self, c):
195 self._component = c 196 self._first = c.getCompositionRoot() is not None
197 198
199 - def __iter__(self):
200 return self
201 202
203 - def hasNext(self):
204 return self._first
205 206
207 - def next(self): #@PydevCodeAnalysisIgnore
208 if not self._first: 209 raise StopIteration 210 self._first = False 211 return self._component._root
212 213
214 - def remove(self):
215 raise NotImplementedError
216