1 from Sink import Sink, SinkInfo
2 from pyjamas.ui.Tree import Tree, TreeItem
3 from pyjamas.ui.PopupPanel import PopupPanel
4 from pyjamas.ui.HTML import HTML
5 from SfaData import PlanetLabData
7 def alwaysDelegateFocus(elem):
10 class PopupRecord(PopupPanel):
11 def __init__(self, record):
12 PopupPanel.__init__(self, True)
13 contents = HTML("<pre>" + record + "</pre>")
14 self.setWidget(contents)
15 self.setStyleName("ks-popups-Popup")
17 class SfaItem(TreeItem):
18 def __init__(self, hrn, kind, truncate = True):
22 hrn = hrn.split('.')[-1]
23 if self.kind == "(authority)":
24 self.label = "<b>%s</b>" % hrn
25 elif self.kind == "(slice)":
26 self.label = "<u><i>%s</i></u>" % hrn
27 elif self.kind == "(user)":
28 self.label = "%s" % hrn
30 self.label = "<b>%s</b>" % hrn
32 TreeItem.__init__(self, self.label)
34 if self.kind == "(authority)":
35 self.addItem(PendingItem())
37 class PendingItem(TreeItem):
39 TreeItem.__init__(self, "Please wait...")
41 def isPendingItem(self):
44 class SfaBrowser(Sink):
49 self.data = PlanetLabData()
52 self.tree.shouldTreeDelegateFocusToElement = alwaysDelegateFocus
54 item = SfaItem("plc", "(authority)", truncate = False)
55 self.tree.addItem(item)
57 item = SfaItem("plc.glc", "(authority)", truncate = False)
58 self.tree.addItem(item)
60 item = SfaItem("plc.vini", "(authority)", truncate = False)
61 self.tree.addItem(item)
63 item = SfaItem("ple", "(authority)", truncate = False)
64 self.tree.addItem(item)
66 item = SfaItem("plj", "(authority)", truncate = False)
67 self.tree.addItem(item)
69 self.tree.addTreeListener(self)
71 self.initWidget(self.tree)
74 def onTreeItemSelected(self, item):
75 record = self.data.getRecord(item.hrn)
76 p = PopupRecord(record)
77 left = item.getAbsoluteLeft() + 10
78 top = item.getAbsoluteTop() + 10
79 p.setPopupPosition(left, top)
82 def onTreeItemStateChanged(self, item):
83 child = item.getChild(0)
84 if hasattr(child, "isPendingItem"):
85 item.removeItem(child)
86 hrns = sorted(self.data.listChildren(item.hrn),
87 key=lambda hrn: hrn[0])
88 for (hrn, kind) in hrns:
89 item.addItem(SfaItem(hrn, kind))
96 return SinkInfo("Browse SFA", "SFA Hierarchy Browser", SfaBrowser)