30442e8b8d30c11c4d1895251403c867bf36e88e
[sfa-gui.git] / SfaBrowser.py
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
6
7 def alwaysDelegateFocus(elem):
8     return True
9
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         
16         self.setStyleName("ks-popups-Popup")
17
18 class SfaItem(TreeItem):
19     def __init__(self, hrn, kind, truncate = True):
20         self.kind = kind
21         self.hrn = hrn
22         if truncate:
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
30         else:
31             self.label = "<b>%s</b>" % hrn
32
33         TreeItem.__init__(self, self.label)
34
35         if self.kind == "(authority)":
36             self.addItem(PendingItem())
37
38 class PendingItem(TreeItem):
39     def __init__(self):
40         TreeItem.__init__(self, "Please wait...")
41
42     def isPendingItem(self):
43         return True
44
45 class SfaBrowser(Sink):
46     def __init__(self):
47
48         Sink.__init__(self)
49
50         self.data = PlanetLabData()
51
52         self.tree = Tree()
53         self.tree.shouldTreeDelegateFocusToElement = alwaysDelegateFocus
54         
55         item = SfaItem("plc", "(authority)", truncate = False)
56         self.tree.addItem(item)
57
58         item = SfaItem("plc.glc", "(authority)", truncate = False)
59         self.tree.addItem(item)
60
61         item = SfaItem("plc.vini", "(authority)", truncate = False)
62         self.tree.addItem(item)
63
64         item = SfaItem("ple", "(authority)", truncate = False)
65         self.tree.addItem(item)
66
67         item = SfaItem("plj", "(authority)", truncate = False)
68         self.tree.addItem(item)
69
70         self.tree.addTreeListener(self)
71
72         self.initWidget(self.tree)
73
74
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)
81         p.show()
82
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))
91
92     def onShow(self):
93         pass
94
95
96 def init():
97     return SinkInfo("Browse SFA", "SFA Hierarchy Browser", SfaBrowser)