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