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

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