Package pyjamas :: Package ui :: Module Widget
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.Widget

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  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  from pyjamas import DOM 
 16  from pyjamas import Factory 
 17  from pyjamas import log 
 18   
 19  from UIObject import UIObject 
 20  from pyjamas.ui import Event 
 21  from ClickListener import ClickHandler 
 22  from FocusListener import FocusHandler 
 23  from KeyboardListener import KeyboardHandler 
 24  from MouseListener import MouseHandler 
 25   
26 -class Widget(UIObject):
27 """ 28 Base class for most of the UI classes. This class provides basic services 29 used by any Widget, including management of parents and adding/removing the 30 event handler association with the DOM. 31 """
32 - def __init__(self, **kwargs):
33 self.attached = False 34 self.parent = None 35 self.layoutData = None 36 self.contextMenu = None 37 38 UIObject.__init__(self, **kwargs)
39
40 - def getLayoutData(self):
41 return self.layoutData
42
43 - def getParent(self):
44 """Widgets are kept in a hierarchy, and widgets that have been added to a panel 45 will have a parent widget that contains them. This retrieves the containing 46 widget for this widget.""" 47 return self.parent
48
49 - def isAttached(self):
50 """Return whether or not this widget has been attached to the document.""" 51 return self.attached
52
53 - def setContextMenu(self, menu):
54 self.contextMenu = menu 55 if menu: 56 self.sinkEvents(Event.ONCONTEXTMENU) 57 else: 58 self.unsinkEvents(Event.ONCONTEXTMENU)
59
60 - def onBrowserEvent(self, event):
61 62 # farm out the event to convenience handlers. 63 # detect existence by checking for the listener lists of each 64 # type of handler. there's probably a better way to do this... 65 if hasattr(self, "_clickListeners"): 66 ClickHandler.onBrowserEvent(self, event) 67 if hasattr(self, "_keyboardListeners"): 68 KeyboardHandler.onBrowserEvent(self, event) 69 if hasattr(self, "_mouseListeners"): 70 MouseHandler.onBrowserEvent(self, event) 71 if hasattr(self, "_focusListeners"): 72 FocusHandler.onBrowserEvent(self, event) 73 74 if self.contextMenu is None: 75 return True 76 77 type = DOM.eventGetType(event) 78 if type == "contextmenu": 79 DOM.eventCancelBubble(event, True) 80 DOM.eventPreventDefault(event) 81 self.contextMenu.onContextMenu(self) 82 return False 83 84 return True
85
86 - def onLoad(self):
87 pass
88
89 - def doDetachChildren(self):
90 pass
91
92 - def doAttachChildren(self):
93 pass
94
95 - def onAttach(self):
96 """Called when this widget has an element, and that element is on the document's 97 DOM tree, and we have a parent widget.""" 98 if self.isAttached(): 99 return 100 self.attached = True 101 DOM.setEventListener(self.getElement(), self) 102 self.doAttachChildren() 103 self.onLoad()
104
105 - def onDetach(self):
106 """Called when this widget is being removed from the DOM tree of the document.""" 107 if not self.isAttached(): 108 return 109 self.doDetachChildren() 110 self.attached = False 111 DOM.setEventListener(self.getElement(), None)
112
113 - def setLayoutData(self, layoutData):
114 self.layoutData = layoutData
115
116 - def setParent(self, parent):
117 """Update the parent attribute. If the parent is currently attached to the DOM this 118 assumes we are being attached also and calls onAttach().""" 119 oldparent = self.parent 120 self.parent = parent 121 if parent is None: 122 if oldparent is not None and oldparent.attached: 123 self.onDetach() 124 elif parent.attached: 125 self.onAttach()
126
127 - def removeFromParent(self):
128 """Remove ourself from our parent. The parent widget will call setParent(None) on 129 us automatically""" 130 if hasattr(self.parent, "remove"): 131 self.parent.remove(self)
132
133 - def getID(self):
134 """Get the id attribute of the associated DOM element.""" 135 return DOM.getAttribute(self.getElement(), "id")
136
137 - def setID(self, id):
138 """Set the id attribute of the associated DOM element.""" 139 DOM.setAttribute(self.getElement(), "id", id)
140 141 Factory.registerClass('pyjamas.ui.Widget', Widget) 142