SFA browser basic functionality
[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 class PopupRecord(PopupPanel):
8     def __init__(self, record):
9         PopupPanel.__init__(self, True)
10         contents = HTML("<pre>" + record + "</pre>")
11         self.setWidget(contents)
12         
13         self.setStyleName("ks-popups-Popup")
14
15 class SfaItem(TreeItem):
16     def __init__(self, hrn, authority = False):
17         self.isAuthority = authority
18         self.hrn = hrn
19         last = hrn.split('.')[-1]
20         self.label = last
21         TreeItem.__init__(self, self.label)
22
23         if self.isAuthority:
24             self.addItem(PendingItem())
25
26 class PendingItem(TreeItem):
27     def __init__(self):
28         TreeItem.__init__(self, "Please wait...")
29
30     def isPendingItem(self):
31         return True
32
33 class SfaBrowser(Sink):
34     def __init__(self):
35
36         Sink.__init__(self)
37
38         self.data = PlanetLabData()
39
40         self.tree = Tree()
41         
42         item = SfaItem("plc", True)
43         self.tree.addItem(item)
44
45         item = SfaItem("ple", True)
46         self.tree.addItem(item)
47
48         self.tree.addTreeListener(self)
49
50         self.initWidget(self.tree)
51
52
53     def onTreeItemSelected(self, item):
54         record = self.data.getRecord(item.hrn)
55         p = PopupRecord(record)
56         left = item.getAbsoluteLeft() + 10
57         top = item.getAbsoluteTop() + 10
58         p.setPopupPosition(left, top)
59         p.show()
60
61     def onTreeItemStateChanged(self, item):
62         child = item.getChild(0)
63         if hasattr(child, "isPendingItem"):
64             item.removeItem(child)
65             hrns = self.data.listChildren(item.hrn)
66             for (hrn, kind) in hrns:
67                 item.addItem(SfaItem(hrn, (kind=="(authority)")))
68
69     def onShow(self):
70         pass
71
72
73 def init():
74     return SinkInfo("Browse SFA", "SFA Hierarchy Browser", SfaBrowser)