PageRenderTime 204ms CodeModel.GetById 101ms app.highlight 7ms RepoModel.GetById 94ms app.codeStats 0ms

/examples/kitchensink/Text.py

http://pyjamas.googlecode.com/
Python | 76 lines | 58 code | 18 blank | 0 comment | 1 complexity | 881c6fd41f344bcc9944c1e6fd907cdd MD5 | raw file
 1from Sink import Sink, SinkInfo
 2from ui import Button, HorizontalPanel, HTML, PasswordTextBox, TextArea, TextBox, TextBoxBase, VerticalPanel, Widget
 3
 4class Text(Sink):
 5    def __init__(self):
 6        self.fPasswordText = PasswordTextBox()
 7        self.fTextArea = TextArea()
 8        self.fTextBox = TextBox()
 9
10        panel = VerticalPanel()
11        panel.setSpacing(8)
12        panel.add(HTML("Normal text box:"))
13        panel.add(self.createTextThing(self.fTextBox))
14        panel.add(HTML("Password text box:"))
15        panel.add(self.createTextThing(self.fPasswordText))
16        panel.add(HTML("Text area:"))
17        panel.add(self.createTextThing(self.fTextArea))
18        self.initWidget(panel)
19
20    def onShow(self):
21        pass
22
23    def createTextThing(self, textBox):
24        p = HorizontalPanel()
25        p.setSpacing(4)
26
27        p.add(textBox)
28
29        echo = HTML()
30        select_all = Button("select all")
31        p.add(select_all)
32        p.add(echo)
33        
34        listener=TextBoxListener(self, textBox, echo, select_all)
35        select_all.addClickListener(listener)
36        textBox.addKeyboardListener(listener)
37        textBox.addClickListener(listener)
38
39        return p
40
41    def updateText(self, text, echo):
42        echo.setHTML("Text: " + text.getText() + "<br>" + "Selection: " + text.getCursorPos() + ", " + text.getSelectionLength())
43
44
45class TextBoxListener:
46    def __init__(self, parent, textBox, echo, select_all):
47        self.textBox=textBox
48        self.echo=echo
49        self.parent=parent
50        self.select_all=select_all
51        
52    def onClick(self, sender):
53        if sender == self.select_all:
54            self.textBox.selectAll()
55            self.textBox.setFocus(True)
56
57        self.parent.updateText(self.textBox, self.echo)
58
59    def onKeyUp(self, sender, keyCode, modifiers):
60        self.parent.updateText(self.textBox, self.echo)
61
62    def onKeyDown(self, sender, keyCode, modifiers):
63        pass
64    
65    def onKeyPress(self, sender, keyCode, modifiers):
66        pass
67
68    
69def init():
70    text="GWT includes the standard complement of text-entry widgets, each of which "
71    text+="supports keyboard and selection events you can use to control text entry.  "
72    text+="In particular, notice that the selection range for each widget is "
73    text+="updated whenever you press a key.  "
74    text+="This can be a bit tricky on some browsers, but the GWT class library "
75    text+="takes care of the plumbing for you automatically."
76    return SinkInfo("Text", text, Text)