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

Source Code for Module pyjamas.ui.TreeItem

  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 UIObject import UIObject 
 19  from TreeContentPanel import TreeContentPanel 
 20   
21 -class TreeItem(UIObject):
22 23 # also callable as TreeItem(widget)
24 - def __init__(self, html=None, **kwargs):
25 self.children = [] 26 self.contentPanel = None 27 self.itemTable = None 28 self.contentElem = None 29 self.imgElem = None 30 self.childSpanElem = None 31 self.open = False 32 self.parent = None 33 self.selected = False 34 self.tree = None 35 self.userObject = None 36 37 if kwargs.has_key('Element'): 38 element = kwargs.pop('Element') 39 else: 40 element = DOM.createDiv() 41 self.setElement(element) 42 43 self.itemTable = DOM.createTable() 44 self.contentElem = DOM.createSpan() 45 self.childSpanElem = DOM.createSpan() 46 self.imgElem = DOM.createImg() 47 48 tbody = DOM.createTBody() 49 tr = DOM.createTR() 50 tdImg = DOM.createTD() 51 tdContent = DOM.createTD() 52 DOM.appendChild(self.itemTable, tbody) 53 DOM.appendChild(tbody, tr) 54 DOM.appendChild(tr, tdImg) 55 DOM.appendChild(tr, tdContent) 56 DOM.setStyleAttribute(tdImg, "verticalAlign", "middle") 57 DOM.setStyleAttribute(tdContent, "verticalAlign", "middle") 58 DOM.setStyleAttribute(self.getElement(), "cursor", "pointer") 59 60 DOM.appendChild(self.getElement(), self.itemTable) 61 DOM.appendChild(self.getElement(), self.childSpanElem) 62 DOM.appendChild(tdImg, self.imgElem) 63 DOM.appendChild(tdContent, self.contentElem) 64 65 # XXX - can't set pos relative on a div node, 66 # or white_space on an HTML Table.. 67 try: 68 DOM.setAttribute(self.getElement(), "position", "relative") 69 except: 70 pass 71 DOM.setStyleAttribute(self.contentElem, "display", "inline") 72 DOM.setStyleAttribute(self.getElement(), "whiteSpace", "nowrap") 73 try: 74 DOM.setAttribute(self.itemTable, "whiteSpace", "nowrap") 75 except: 76 pass 77 DOM.setStyleAttribute(self.childSpanElem, "whiteSpace", "nowrap") 78 self.setStyleName(self.contentElem, "gwt-TreeItem", True) 79 80 #if not kwargs.has_key('StyleName'): kwargs['StyleName']="gwt-TreeItem" 81 82 if html is not None: 83 if isinstance(html, str): 84 kwargs['HTML'] = html 85 else: 86 kwargs['Widget'] = html 87 88 UIObject.__init__(self, **kwargs)
89 90 # also callable as addItem(widget) and addItem(itemText)
91 - def addItem(self, item):
92 return self.insertItem(item)
93 94 # also callable as addItem(widget) and addItem(itemText)
95 - def insertItem(self, item, index=None):
96 if not hasattr(item, "getTree"): 97 #if not item.getTree: 98 item = TreeItem(item) 99 100 if (item.getParentItem() is not None) or (item.getTree() is not None): 101 item.remove() 102 103 item.setTree(self.tree) 104 item.setParentItem(self) 105 if index is None: 106 self.children.append(item) 107 else: 108 self.children.insert(index, item) 109 DOM.setStyleAttribute(item.getElement(), "marginLeft", "16px") 110 if index is None: 111 DOM.appendChild(self.childSpanElem, item.getElement()) 112 else: 113 DOM.insertChild(self.childSpanElem, item.getElement(), index) 114 115 if len(self.children) == 1: 116 self.updateState() 117 118 return item
119
120 - def getChild(self, index):
121 if (index < 0) or (index >= len(self.children)): 122 return None 123 124 return self.children[index]
125
126 - def getChildCount(self):
127 return len(self.children)
128
129 - def getChildIndex(self, child):
130 return self.children.index(child)
131
132 - def getHTML(self):
133 return DOM.getInnerHTML(self.contentElem)
134
135 - def getText(self):
136 return DOM.getInnerText(self.contentElem)
137
138 - def getParentItem(self):
139 return self.parent
140
141 - def getState(self):
142 return self.open
143
144 - def getTree(self):
145 return self.tree
146
147 - def getUserObject(self):
148 return self.userObject
149
150 - def getWidget(self):
151 if self.contentPanel is None: 152 return None 153 154 return self.contentPanel.getWidget()
155
156 - def isSelected(self):
157 return self.selected
158
159 - def remove(self):
160 if self.parent is not None: 161 self.parent.removeItem(self) 162 elif self.tree is not None: 163 self.tree.removeItem(self)
164
165 - def removeItem(self, item):
166 if item not in self.children: 167 return 168 169 item.setTree(None) 170 item.setParentItem(None) 171 self.children.remove(item) 172 DOM.removeChild(self.childSpanElem, item.getElement()) 173 if len(self.children) == 0: 174 self.updateState()
175
176 - def removeItems(self):
177 while self.getChildCount() > 0: 178 self.removeItem(self.getChild(0))
179
180 - def setHTML(self, html):
181 self.clearContentPanel() 182 DOM.setInnerHTML(self.contentElem, html)
183
184 - def setText(self, text):
185 self.clearContentPanel() 186 DOM.setInnerText(self.contentElem, text)
187
188 - def setSelected(self, selected):
189 if self.selected == selected: 190 return 191 self.selected = selected 192 self.setStyleName(self.contentElem, "gwt-TreeItem-selected", selected)
193
194 - def setState(self, open, fireEvents=True):
195 196 # lkcl: experiment with allowing event state changed to be 197 # fired even on items with no children. otherwise you never 198 # get to find out if an end-item was selected! 199 if not open or len(self.children) != 0: 200 self.open = open 201 self.updateState() 202 203 if fireEvents: 204 self.tree.fireStateChanged(self)
205
206 - def setUserObject(self, userObj):
207 self.userObject = userObj
208
209 - def setWidget(self, widget):
210 self.ensureContentPanel() 211 self.contentPanel.setWidget(widget)
212
213 - def clearContentPanel(self):
214 if self.contentPanel is not None: 215 child = self.contentPanel.getWidget() 216 if child is not None: 217 self.contentPanel.remove(child) 218 219 if self.tree is not None: 220 self.tree.disown(self.contentPanel) 221 self.contentPanel = None
222
223 - def ensureContentPanel(self):
224 if self.contentPanel is None: 225 DOM.setInnerHTML(self.contentElem, "") 226 self.contentPanel = TreeContentPanel(self.contentElem) 227 self.contentPanel.setTreeItem(self) 228 if self.getTree() is not None: 229 self.tree.adopt(self.contentPanel)
230
231 - def addTreeItems(self, accum):
232 for item in self.children: 233 accum.append(item) 234 item.addTreeItems(accum)
235
236 - def getChildren(self):
237 return self.children
238
239 - def getContentElem(self):
240 return self.contentElem
241
242 - def getContentHeight(self):
243 return DOM.getIntAttribute(self.itemTable, "offsetHeight")
244
245 - def getImageElement(self):
246 return self.imgElem
247
248 - def getTreeTop(self):
249 item = self 250 ret = 0 251 252 while item is not None: 253 ret += DOM.getIntAttribute(item.getElement(), "offsetTop") 254 item = item.getParentItem() 255 256 return ret
257
258 - def getFocusableWidget(self):
259 widget = self.getWidget() 260 if hasattr(widget, "setFocus"): 261 return widget 262 return None
263
264 - def imgSrc(self, img):
265 if self.tree is None: 266 return img 267 src = self.tree.getImageBase() + img 268 return src
269
270 - def setParentItem(self, parent):
271 self.parent = parent
272
273 - def setTree(self, tree):
274 if self.tree == tree: 275 return 276 277 if self.tree is not None: 278 if self.tree.getSelectedItem() == self: 279 self.tree.setSelectedItem(None) 280 281 if self.contentPanel is not None: 282 self.tree.disown(self.contentPanel) 283 284 self.tree = tree 285 for child in self.children: 286 child.setTree(tree) 287 self.updateState() 288 if tree is not None and self.contentPanel is not None: 289 tree.adopt(self.contentPanel)
290
291 - def updateState(self):
292 if len(self.children) == 0: 293 self.setVisible(self.childSpanElem, False) 294 DOM.setAttribute(self.imgElem, "src", self.imgSrc("tree_white.gif")) 295 return 296 297 if self.open: 298 self.setVisible(self.childSpanElem, True) 299 DOM.setAttribute(self.imgElem, "src", self.imgSrc("tree_open.gif")) 300 else: 301 self.setVisible(self.childSpanElem, False) 302 DOM.setAttribute(self.imgElem, "src", self.imgSrc("tree_closed.gif"))
303
304 - def updateStateRecursive(self):
305 self.updateState() 306 for i in range(len(self.children)): 307 child = self.children[i] 308 child.updateStateRecursive()
309 310
311 -class RootTreeItem(TreeItem):
312 - def addItem(self, item):
313 self.insertItem(item)
314
315 - def insertItem(self, item, index=None):
316 if (item.getParentItem() is not None) or (item.getTree() is not None): 317 item.remove() 318 item.setTree(self.getTree()) 319 320 item.setParentItem(None) 321 if index is None: 322 self.children.append(item) 323 else: 324 self.children.insert(index, item) 325 326 DOM.setIntStyleAttribute(item.getElement(), "marginLeft", 0)
327
328 - def removeItem(self, item):
329 if item not in self.children: 330 return 331 332 item.setTree(None) 333 item.setParentItem(None) 334 self.children.remove(item)
335 336 Factory.registerClass('pyjamas.ui.TreeItem', TreeItem) 337