1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from pyjamas import DOM
17 from pyjamas import Factory
18 from pyjamas.Canvas.GWTCanvas import GWTCanvas
19 from pyjamas.Canvas import Color
20
21 from pyjamas.ui.UIObject import UIObject
22 from pyjamas.ui.TreeContentPanel import TreeContentPanel
23
24
25
26
27
28 tree_closed = "data:image/gif;base64,R0lGODlhEAAQAJECAMzMzAAAAP///wAAACH5BAEAAAIALAAAAAAQABAAAAIjlI+py+1vgJxzAYtNOFd1sUVYQJLCh3zhmYFcm1oUBdU2VAAAOw=="
29 tree_open = "data:image/gif;base64,R0lGODlhEAAQAJECAAAAAMDAwAAAAP///yH5BAEAAAIALAAAAAAQABAAAAIflI+py+1vgpxzBYvV1TldAILC5nUIeZoHulIUBMdQAQA7"
30 tree_white = "data:image/gif;base64,R0lGODlhEAAQAJEAAP///wAAAP///wAAACH5BAEAAAIALAAAAAAQABAAAAIOlI+py+0Po5y02ouzPgUAOw=="
31
33
34
36 self.children = []
37 self.attached = False
38 self.contentPanel = None
39 self.itemTable = None
40 self.contentElem = None
41 self.imgElem = None
42 self.childSpanElem = None
43 self.open = False
44 self.parent = None
45 self.selected = False
46 self.tree = None
47 self.userObject = None
48
49 element = ka.pop('Element', None) or DOM.createDiv()
50 self.setElement(element)
51
52 self.itemTable = DOM.createTable()
53 self.contentElem = DOM.createSpan()
54 self.childSpanElem = DOM.createSpan()
55 self.imgElem = self.createImage()
56
57 tbody = DOM.createTBody()
58 tr = DOM.createTR()
59 tdImg = DOM.createTD()
60 tdContent = DOM.createTD()
61 DOM.appendChild(self.itemTable, tbody)
62 DOM.appendChild(tbody, tr)
63 DOM.appendChild(tr, tdImg)
64 DOM.appendChild(tr, tdContent)
65 DOM.setStyleAttribute(tdImg, "verticalAlign", "middle")
66 DOM.setStyleAttribute(tdContent, "verticalAlign", "middle")
67 DOM.setStyleAttribute(self.getElement(), "cursor", "pointer")
68
69 DOM.appendChild(self.getElement(), self.itemTable)
70 DOM.appendChild(self.getElement(), self.childSpanElem)
71 DOM.appendChild(tdImg, self.imgElem)
72 DOM.appendChild(tdContent, self.contentElem)
73
74
75
76 try:
77 DOM.setAttribute(self.getElement(), "position", "relative")
78 except:
79 pass
80 DOM.setStyleAttribute(self.contentElem, "display", "inline")
81 DOM.setStyleAttribute(self.getElement(), "whiteSpace", "nowrap")
82 try:
83 DOM.setAttribute(self.itemTable, "whiteSpace", "nowrap")
84 except:
85 pass
86 DOM.setStyleAttribute(self.childSpanElem, "whiteSpace", "nowrap")
87 self.setStyleName(self.contentElem, "gwt-TreeItem", True)
88
89
90
91 if html is not None:
92 try:
93
94 if isinstance(html, unicode):
95 ka['HTML'] = html
96 elif isinstance(html, basestring):
97 ka['HTML'] = html
98 else:
99 ka['Widget'] = html
100 except:
101 if isinstance(html, basestring):
102 ka['HTML'] = html
103 else:
104 ka['Widget'] = html
105
106 UIObject.__init__(self, **ka)
107
110
113
114
117
118
143
153
161
163 if (index < 0) or (index >= len(self.children)):
164 return None
165
166 return self.children[index]
167
169 return len(self.children)
170
172 return self.children.index(child)
173
176
178 return DOM.getInnerText(self.contentElem)
179
182
185
188
190 return self.userObject
191
197
200
202 if self.parent is not None:
203 self.parent.removeItem(self)
204 elif self.tree is not None:
205 self.tree.removeItem(self)
206
217
221
225
226 - def setText(self, text):
227 self.clearContentPanel()
228 DOM.setInnerText(self.contentElem, text)
229
231 if self.selected == selected:
232 return
233 self.selected = selected
234 self.setStyleName(self.contentElem, "gwt-TreeItem-selected", selected)
235
236 - def setState(self, open, fireEvents=True):
247
249 self.userObject = userObj
250
254
256 if self.contentPanel is not None:
257 child = self.contentPanel.getWidget()
258 if child is not None:
259 self.contentPanel.remove(child)
260
261 if self.tree is not None:
262 self.tree.disown(self.contentPanel)
263 self.contentPanel = None
264
266 if self.contentPanel is None:
267 DOM.setInnerHTML(self.contentElem, "")
268 self.contentPanel = TreeContentPanel(self.contentElem)
269 self.contentPanel.setTreeItem(self)
270 if self.getTree() is not None:
271 self.tree.adopt(self.contentPanel)
272
277
280
281 - def getContentElem(self):
282 return self.contentElem
283
285 return DOM.getIntAttribute(self.itemTable, "offsetHeight")
286
289
299
305
307 if self.tree is None:
308 return img
309 src = self.tree.getImageBase() + img
310 return src
311
314
316 if self.tree == tree:
317 return
318
319 if self.tree is not None:
320 if self.tree.getSelectedItem() == self:
321 self.tree.setSelectedItem(None)
322
323 if self.contentPanel is not None:
324 self.tree.disown(self.contentPanel)
325
326 self.tree = tree
327 for child in self.children:
328 child.setTree(tree)
329 self.updateState()
330 if tree is not None and self.contentPanel is not None:
331 tree.adopt(self.contentPanel)
332
346
352
362
363
388
389 Factory.registerClass('pyjamas.ui.TreeItem', 'TreeItem', TreeItem)
390