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

Source Code for Module muntjac.ui.embedded

  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 component for embedding external objects.""" 
 17   
 18  from muntjac.ui.abstract_component import AbstractComponent 
 19  from muntjac.event.mouse_events import IClickListener, ClickEvent 
 20  from muntjac.terminal.gwt.client.mouse_event_details import MouseEventDetails 
 21   
 22  from muntjac.terminal.gwt.client.ui.v_embedded import VEmbedded 
 23   
 24   
25 -class Embedded(AbstractComponent):
26 """Component for embedding external objects. 27 28 @author: Vaadin Ltd. 29 @author: Richard Lincoln 30 @version: 1.1.2 31 """ 32 33 CLIENT_WIDGET = None #ClientWidget(VEmbedded, LoadStyle.EAGER) 34 35 _CLICK_EVENT = VEmbedded.CLICK_EVENT_IDENTIFIER 36 37 #: General object type. 38 TYPE_OBJECT = 0 39 40 #: Image types. 41 TYPE_IMAGE = 1 42 43 #: Browser ("iframe") type. 44 TYPE_BROWSER = 2 45
46 - def __init__(self, caption=None, source=None):
47 """Creates a new Embedded object whose contents is loaded from 48 given resource. The dimensions are assumed if possible. The 49 type is guessed from resource. 50 51 @param caption: 52 @param source: 53 the Source of the embedded object. 54 """ 55 super(Embedded, self).__init__() 56 57 #: Type of the object. 58 self._type = self.TYPE_OBJECT 59 60 #: Source of the embedded object. 61 self._source = None 62 63 #: Generic object attributes. 64 self._mimeType = None 65 self._standby = None 66 67 #: Hash of object parameters. 68 self._parameters = dict() 69 70 #: Applet or other client side runnable properties. 71 self._codebase = None 72 self._codetype = None 73 self._classId = None 74 self._archive = None 75 76 if caption is not None: 77 self.setCaption(caption) 78 79 if source is not None: 80 self.setSource(source)
81 82
83 - def paintContent(self, target):
84 """Invoked when the component state should be painted.""" 85 86 if self._type == self.TYPE_IMAGE: 87 target.addAttribute('type', 'image') 88 elif self._type == self.TYPE_BROWSER: 89 target.addAttribute('type', 'browser') 90 91 92 if self.getSource() is not None: 93 target.addAttribute('src', self.getSource()) 94 95 if self._mimeType is not None and not ('' == self._mimeType): 96 target.addAttribute('mimetype', self._mimeType) 97 98 if self._classId is not None and not ('' == self._classId): 99 target.addAttribute('classid', self._classId) 100 101 if self._codebase is not None and not ('' == self._codebase): 102 target.addAttribute('codebase', self._codebase) 103 104 if self._codetype is not None and not ('' == self._codetype): 105 target.addAttribute('codetype', self._codetype) 106 107 if self._standby is not None and not ('' == self._standby): 108 target.addAttribute('standby', self._standby) 109 110 if self._archive is not None and not ('' == self._archive): 111 target.addAttribute('archive', self._archive) 112 113 # Params 114 for key in self.getParameterNames(): 115 target.startTag('embeddedparam') 116 target.addAttribute('name', key) 117 target.addAttribute('value', self.getParameter(key)) 118 target.endTag('embeddedparam')
119 120
121 - def setParameter(self, name, value):
122 """Sets an object parameter. Parameters are optional information, 123 and they are passed to the instantiated object. Parameters are are 124 stored as name value pairs. This overrides the previous value 125 assigned to this parameter. 126 127 @param name: the name of the parameter. 128 @param value: the value of the parameter. 129 """ 130 self._parameters[name] = value 131 self.requestRepaint()
132 133
134 - def getParameter(self, name):
135 """Gets the value of an object parameter. Parameters are optional 136 information, and they are passed to the instantiated object. 137 Parameters are are stored as name value pairs. 138 139 @return: the Value of parameter or null if not found. 140 """ 141 return self._parameters.get(name)
142 143
144 - def removeParameter(self, name):
145 """Removes an object parameter from the list. 146 147 @param name: 148 the name of the parameter to remove. 149 """ 150 if name in self._parameters: 151 del self._parameters[name] 152 self.requestRepaint()
153 154
155 - def getParameterNames(self):
156 """Gets the embedded object parameter names. 157 158 @return: the Iterator of parameters names. 159 """ 160 return self._parameters.keys()
161 162
163 - def getCodebase(self):
164 """This attribute specifies the base path used to resolve relative 165 URIs specified by the classid, data, and archive attributes. When 166 absent, its default value is the base URI of the current document. 167 168 @return: the code base. 169 """ 170 return self._codebase
171 172
173 - def getCodetype(self):
174 """Gets the MIME-Type of the code. 175 176 @return: the MIME-Type of the code. 177 """ 178 return self._codetype
179 180
181 - def getMimeType(self):
182 """Gets the MIME-Type of the object. 183 184 @return: the MIME-Type of the object. 185 """ 186 return self._mimeType
187 188
189 - def getStandby(self):
190 """This attribute specifies a message that a user agent may render 191 while loading the object's implementation and data. 192 193 @return: The text displayed when loading 194 """ 195 return self._standby
196 197
198 - def setCodebase(self, codebase):
199 """This attribute specifies the base path used to resolve relative 200 URIs specified by the classid, data, and archive attributes. When 201 absent, its default value is the base URI of the current document. 202 203 @param codebase: 204 The base path 205 """ 206 if (codebase != self._codebase 207 or (codebase is not None and codebase != self._codebase)): 208 self._codebase = codebase 209 self.requestRepaint()
210 211
212 - def setCodetype(self, codetype):
213 """This attribute specifies the content type of data expected when 214 downloading the object specified by classid. This attribute is 215 optional but recommended when classid is specified since it allows 216 the user agent to avoid loading information for unsupported content 217 types. When absent, it defaults to the value of the type attribute. 218 219 @param codetype: 220 the codetype to set. 221 """ 222 if (codetype != self._codetype 223 or (codetype is not None and codetype != self._codetype)): 224 self._codetype = codetype 225 self.requestRepaint()
226 227
228 - def setMimeType(self, mimeType):
229 """Sets the mimeType, the MIME-Type of the object. 230 231 @param mimeType: the mimeType to set. 232 """ 233 if (mimeType != self._mimeType 234 or (mimeType is not None and mimeType != self._mimeType)): 235 self._mimeType = mimeType 236 if 'application/x-shockwave-flash' == mimeType: 237 # Automatically add wmode transparent as we use lots of 238 # floating layers in Muntjac. If developers need better flash 239 # performance, they can override this value programmatically 240 # back to "window" (the defautl). 241 if self.getParameter('wmode') is None: 242 self.setParameter('wmode', 'transparent') 243 244 self.requestRepaint()
245 246
247 - def setStandby(self, standby):
248 """This attribute specifies a message that a user agent may render 249 while loading the object's implementation and data. 250 251 @param standby: The text to display while loading 252 """ 253 if (standby != self._standby 254 or (standby is not None and standby != self._standby)): 255 self._standby = standby 256 self.requestRepaint()
257 258
259 - def getClassId(self):
260 """This attribute may be used to specify the location of an object's 261 implementation via a URI. 262 263 @return: the classid. 264 """ 265 return self._classId
266 267
268 - def setClassId(self, classId):
269 """This attribute may be used to specify the location of an object's 270 implementation via a URI. 271 272 @param classId: 273 the classId to set. 274 """ 275 if (classId != self._classId 276 or (classId is not None and classId != self._classId)): 277 self._classId = classId 278 self.requestRepaint()
279 280
281 - def getSource(self):
282 """Gets the resource contained in the embedded object. 283 284 @return: the Resource 285 """ 286 return self._source
287 288
289 - def getType(self):
290 """Gets the type of the embedded object. 291 292 This can be one of the following: 293 294 - TYPE_OBJECT I{(This is the default)} 295 - TYPE_IMAGE 296 297 @return: the type. 298 """ 299 return self._type
300 301
302 - def setSource(self, source):
303 """Sets the object source resource. The dimensions are assumed 304 if possible. The type is guessed from resource. 305 306 @param source: the source to set. 307 """ 308 if source is not None and source != self._source: 309 self._source = source 310 mt = source.getMIMEType() 311 312 if self._mimeType is None: 313 self._mimeType = mt 314 315 if mt == 'image/svg+xml': 316 self._type = self.TYPE_OBJECT 317 elif mt[:mt.find('/')].lower() == 'image': 318 self._type = self.TYPE_IMAGE 319 else: 320 pass # Keep previous type 321 322 self.requestRepaint()
323 324
325 - def setType(self, typ):
326 """Sets the object type. 327 328 This can be one of the following: 329 330 - TYPE_OBJECT I{(This is the default)} 331 - TYPE_IMAGE 332 - TYPE_BROWSER 333 334 @param typ: the type to set. 335 """ 336 if (typ != self.TYPE_OBJECT and typ != self.TYPE_IMAGE 337 and typ != self.TYPE_BROWSER): 338 raise ValueError, 'Unsupported typ' 339 340 if typ != self._type: 341 self._type = typ 342 self.requestRepaint()
343 344
345 - def getArchive(self):
346 """This attribute may be used to specify a space-separated list of 347 URIs for archives containing resources relevant to the object, which 348 may include the resources specified by the classid and data 349 attributes. Preloading archives will generally result in reduced load 350 times for objects. Archives specified as relative URIs should be 351 interpreted relative to the codebase attribute. 352 353 @return: Space-separated list of URIs with resources relevant to the 354 object 355 """ 356 return self._archive
357 358
359 - def setArchive(self, archive):
360 """This attribute may be used to specify a space-separated list of 361 URIs for archives containing resources relevant to the object, which 362 may include the resources specified by the classid and data 363 attributes. Preloading archives will generally result in reduced load 364 times for objects. Archives specified as relative URIs should be 365 interpreted relative to the codebase attribute. 366 367 @param archive: 368 Space-separated list of URIs with resources relevant to 369 the object 370 """ 371 if (archive != self._archive 372 or (archive is not None and archive != self._archive)): 373 self._archive = archive 374 self.requestRepaint()
375 376
377 - def addListener(self, listener, iface=None):
378 """Add a click listener to the component. The listener is called 379 whenever the user clicks inside the component. Depending on the 380 content the event may be blocked and in that case no event is fired. 381 382 Use L{removeListener} to remove the listener. 383 384 @param listener: 385 The listener to add 386 """ 387 if (isinstance(listener, IClickListener) and 388 (iface is None or issubclass(iface, IClickListener))): 389 self.registerListener(self._CLICK_EVENT, ClickEvent, 390 listener, IClickListener.clickMethod) 391 392 super(Embedded, self).addListener(listener, iface)
393 394
395 - def addCallback(self, callback, eventType=None, *args):
396 if eventType is None: 397 eventType = callback._eventType 398 399 if issubclass(eventType, ClickEvent): 400 self.registerCallback(ClickEvent, callback, 401 self._CLICK_EVENT, *args) 402 else: 403 super(Embedded, self).addCallback(callback, eventType, *args)
404 405
406 - def removeListener(self, listener, iface=None):
407 """Remove a click listener from the component. The listener should 408 earlier have been added using L{addListener}. 409 410 @param listener: 411 The listener to remove 412 """ 413 if (isinstance(listener, IClickListener) and 414 (iface is None or issubclass(iface, IClickListener))): 415 self.withdrawListener(self._CLICK_EVENT, ClickEvent, listener) 416 417 super(Embedded, self).removeListener(listener, iface)
418 419
420 - def removeCallback(self, callback, eventType=None):
421 if eventType is None: 422 eventType = callback._eventType 423 424 if issubclass(eventType, ClickEvent): 425 self.withdrawCallback(ClickEvent, callback, self._CLICK_EVENT) 426 else: 427 super(Embedded, self).removeCallback(callback, eventType)
428 429
430 - def changeVariables(self, source, variables):
431 super(Embedded, self).changeVariables(source, variables) 432 433 if self._CLICK_EVENT in variables: 434 self.fireClick(variables.get(self._CLICK_EVENT))
435 436
437 - def fireClick(self, parameters):
438 """Notifies click-listeners that a mouse click event has occurred. 439 """ 440 mouseDetails = \ 441 MouseEventDetails.deSerialize(parameters['mouseDetails']) 442 self.fireEvent( ClickEvent(self, mouseDetails) )
443