1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from pyjamas import DOM
16 from pyjamas import Factory
17
18 from UIObject import UIObject
19 from TreeContentPanel import TreeContentPanel
20
22
23
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
66
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
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
93
94
119
121 if (index < 0) or (index >= len(self.children)):
122 return None
123
124 return self.children[index]
125
127 return len(self.children)
128
130 return self.children.index(child)
131
134
136 return DOM.getInnerText(self.contentElem)
137
140
143
146
148 return self.userObject
149
155
158
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
175
179
183
184 - def setText(self, text):
185 self.clearContentPanel()
186 DOM.setInnerText(self.contentElem, text)
187
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):
205
207 self.userObject = userObj
208
212
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
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
235
238
239 - def getContentElem(self):
240 return self.contentElem
241
243 return DOM.getIntAttribute(self.itemTable, "offsetHeight")
244
247
257
263
265 if self.tree is None:
266 return img
267 src = self.tree.getImageBase() + img
268 return src
269
272
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
303
309
310
314
327
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