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)
16 self.setStyleName("ks-popups-Popup")
18 class SfaItem(TreeItem):
19 def __init__(self, hrn, kind, truncate = True):
23 hrn = hrn.split('.')[-1]
24 if self.kind == "(authority)":
25 self.label = "<b>%s</b>" % hrn
26 elif self.kind == "(slice)":
27 self.label = "<u><i>%s</i></u>" % hrn
28 elif self.kind == "(user)":
29 self.label = "%s" % hrn
31 self.label = "<b>%s</b>" % hrn
33 TreeItem.__init__(self, self.label)
35 if self.kind == "(authority)":
36 self.addItem(PendingItem())
38 class PendingItem(TreeItem):
40 TreeItem.__init__(self, "Please wait...")
42 def isPendingItem(self):
45 class SfaBrowser(Sink):
50 self.data = PlanetLabData()
53 self.tree.shouldTreeDelegateFocusToElement = alwaysDelegateFocus
55 item = SfaItem("plc", "(authority)", truncate = False)
56 self.tree.addItem(item)
58 item = SfaItem("plc.glc", "(authority)", truncate = False)
59 self.tree.addItem(item)
61 item = SfaItem("plc.vini", "(authority)", truncate = False)
62 self.tree.addItem(item)
64 item = SfaItem("ple", "(authority)", truncate = False)
65 self.tree.addItem(item)
67 item = SfaItem("plj", "(authority)", truncate = False)
68 self.tree.addItem(item)
70 self.tree.addTreeListener(self)
72 self.initWidget(self.tree)
75 def onTreeItemSelected(self, item):
76 record = self.data.getRecord(item.hrn)
77 p = PopupRecord(record)
78 left = item.getAbsoluteLeft() + 10
79 top = item.getAbsoluteTop() + 10
80 p.setPopupPosition(left, top)
83 def onTreeItemStateChanged(self, item):
84 child = item.getChild(0)
85 if hasattr(child, "isPendingItem"):
86 item.removeItem(child)
87 hrns = sorted(self.data.listChildren(item.hrn),
88 key=lambda hrn: hrn[0])
89 for (hrn, kind) in hrns:
90 item.addItem(SfaItem(hrn, kind))
97 return SinkInfo("Browse SFA", "SFA Hierarchy Browser", SfaBrowser)