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 self.setStyleName(tdImg, "gwt-TreeItemTdImg", True)
75 self.setStyleName(tdContent, "gwt-TreeItemTdContent", True)
76
77
78
79 try:
80 DOM.setAttribute(self.getElement(), "position", "relative")
81 except:
82 pass
83 DOM.setStyleAttribute(self.contentElem, "display", "inline")
84 DOM.setStyleAttribute(self.getElement(), "whiteSpace", "nowrap")
85 try:
86 DOM.setAttribute(self.itemTable, "whiteSpace", "nowrap")
87 except:
88 pass
89 DOM.setStyleAttribute(self.childSpanElem, "whiteSpace", "nowrap")
90 self.setStyleName(self.contentElem, "gwt-TreeItem", True)
91
92
93
94 if html is not None:
95 try:
96
97 if isinstance(html, unicode):
98 ka['HTML'] = html
99 elif isinstance(html, basestring):
100 ka['HTML'] = html
101 else:
102 ka['Widget'] = html
103 except:
104 if isinstance(html, basestring):
105 ka['HTML'] = html
106 else:
107 ka['Widget'] = html
108
109 UIObject.__init__(self, **ka)
110
113
116
117
120
121
146
156
164
166 if (index < 0) or (index >= len(self.children)):
167 return None
168
169 return self.children[index]
170
172 return len(self.children)
173
175 return self.children.index(child)
176
179
181 return DOM.getInnerText(self.contentElem)
182
185
188
191
193 return self.userObject
194
200
203
205 if self.parent is not None:
206 self.parent.removeItem(self)
207 elif self.tree is not None:
208 self.tree.removeItem(self)
209
220
224
228
229 - def setText(self, text):
230 self.clearContentPanel()
231 DOM.setInnerText(self.contentElem, text)
232
234 if self.selected == selected:
235 return
236 self.selected = selected
237 self.setStyleName(self.contentElem, "gwt-TreeItem-selected", selected)
238
239 - def setState(self, open, fireEvents=True):
250
252 self.userObject = userObj
253
257
259 if self.contentPanel is not None:
260 child = self.contentPanel.getWidget()
261 if child is not None:
262 self.contentPanel.remove(child)
263
264 if self.tree is not None:
265 self.tree.disown(self.contentPanel)
266 self.contentPanel = None
267
269 if self.contentPanel is None:
270 DOM.setInnerHTML(self.contentElem, "")
271 self.contentPanel = TreeContentPanel(self.contentElem)
272 self.contentPanel.setTreeItem(self)
273 if self.getTree() is not None:
274 self.tree.adopt(self.contentPanel)
275
280
283
284 - def getContentElem(self):
285 return self.contentElem
286
288 return DOM.getIntAttribute(self.itemTable, "offsetHeight")
289
292
302
308
310 if self.tree is None:
311 return img
312 src = self.tree.getImageBase() + img
313 return src
314
317
319 if self.tree == tree:
320 return
321
322 if self.tree is not None:
323 if self.tree.getSelectedItem() == self:
324 self.tree.setSelectedItem(None)
325
326 if self.contentPanel is not None:
327 self.tree.disown(self.contentPanel)
328
329 self.tree = tree
330 for child in self.children:
331 child.setTree(tree)
332 self.updateState()
333 if tree is not None and self.contentPanel is not None:
334 tree.adopt(self.contentPanel)
335
349
355
365
366
391
392 Factory.registerClass('pyjamas.ui.TreeItem', 'TreeItem', TreeItem)
393