PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/kitchensink/Lists.py

http://pyjamas.googlecode.com/
Python | 70 lines | 54 code | 16 blank | 0 comment | 8 complexity | 2f2e56a2a48be1301ca7c85263a8475d MD5 | raw file
 1from Sink import Sink, SinkInfo
 2from ui import ListBox, HorizontalPanel, VerticalPanel, HasAlignment, Label, Widget
 3
 4class Lists(Sink):
 5    def __init__(self):
 6        self.sStrings=[["foo0", "bar0", "baz0", "toto0", "tintin0"],
 7            ["foo1", "bar1", "baz1", "toto1", "tintin1"],
 8            ["foo2", "bar2", "baz2", "toto2", "tintin2"],
 9            ["foo3", "bar3", "baz3", "toto3", "tintin3"],
10            ["foo4", "bar4", "baz4", "toto4", "tintin4"]]
11
12        self.combo=ListBox()
13        self.list=ListBox()
14        self.echo=Label()
15
16        self.combo.setVisibleItemCount(1)
17        self.combo.addChangeListener(self)
18        self.list.setVisibleItemCount(10)
19        self.list.setMultipleSelect(True)
20        
21        for i in range(len(self.sStrings)):
22            self.combo.addItem("List " + i)
23        self.combo.setSelectedIndex(0)
24        self.fillList(0)
25        
26        self.list.addChangeListener(self)
27        
28        horz = HorizontalPanel()
29        horz.setVerticalAlignment(HasAlignment.ALIGN_TOP)
30        horz.setSpacing(8)
31        horz.add(self.combo)
32        horz.add(self.list)
33        
34        panel = VerticalPanel()
35        panel.setHorizontalAlignment(HasAlignment.ALIGN_LEFT)
36        panel.add(horz)
37        panel.add(self.echo)
38        self.initWidget(panel)
39        
40        self.echoSelection()
41
42    def onChange(self, sender):
43        if sender == self.combo:
44            self.fillList(self.combo.getSelectedIndex())
45        elif sender == self.list:
46            self.echoSelection()
47
48    def onShow(self):
49        pass
50    
51    def fillList(self, idx):
52        self.list.clear()
53        strings = self.sStrings[idx]
54        for i in range(len(strings)):
55            self.list.addItem(strings[i])
56
57        self.echoSelection()
58
59    def echoSelection(self):
60        msg = "Selected items: "
61        for i in range(self.list.getItemCount()):
62            if self.list.isItemSelected(i):
63                msg += self.list.getItemText(i) + " "
64        self.echo.setText(msg)
65
66
67def init():
68    text="Here is the ListBox widget in its two major forms."
69    return SinkInfo("Lists", text, Lists)
70