1
2
3
4
5
6
7
8
9
10
11 from TextBox import TextBox
12 from pyjamas import Factory
13 from PopupPanel import PopupPanel
14 from ListBox import ListBox
15 from pyjamas.ui import KeyboardListener
16 from RootPanel import RootPanel
17
19 - def __init__(self, **kwargs):
20 self.choicesPopup = PopupPanel(True, False)
21 self.choices = ListBox()
22 self.items = SimpleAutoCompletionItems()
23 self.popupAdded = False
24 self.visible = False
25
26 self.choices.addClickListener(self)
27 self.choices.addChangeListener(self)
28
29 self.choicesPopup.add(self.choices)
30 self.choicesPopup.addStyleName("AutoCompleteChoices")
31
32 self.choices.setStyleName("list")
33
34 if not kwargs.has_key('StyleName'): kwargs['StyleName']="gwt-AutoCompleteTextBox"
35
36 TextBox.__init__(self, **kwargs)
37 self.addKeyboardListener(self)
38
39 - def setCompletionItems(self, items):
40 if not hasattr(items, 'getCompletionItems'):
41 items = SimpleAutoCompletionItems(items)
42
43 self.items = items
44
47
48 - def onKeyDown(self, arg0, arg1, arg2):
50
51 - def onKeyPress(self, arg0, arg1, arg2):
53
54 - def onKeyUp(self, arg0, arg1, arg2):
55 if arg1 == KeyboardListener.KEY_DOWN:
56 selectedIndex = self.choices.getSelectedIndex()
57 selectedIndex += 1
58 if selectedIndex > self.choices.getItemCount():
59 selectedIndex = 0
60
61 self.choices.setSelectedIndex(selectedIndex)
62 return
63
64 if arg1 == KeyboardListener.KEY_UP:
65 selectedIndex = self.choices.getSelectedIndex()
66 selectedIndex -= 1
67 if selectedIndex < 0:
68 selectedIndex = self.choices.getItemCount()
69 self.choices.setSelectedIndex(selectedIndex)
70 return
71
72 if arg1 == KeyboardListener.KEY_ENTER:
73 if self.visible:
74 self.complete()
75 return
76
77 if arg1 == KeyboardListener.KEY_ESCAPE:
78 self.choices.clear()
79 self.choicesPopup.hide()
80 self.visible = False
81 return
82
83 text = self.getText()
84 matches = []
85 if len(text) > 0:
86 matches = self.items.getCompletionItems(text)
87
88 if len(matches) > 0:
89 self.choices.clear()
90
91 for i in range(len(matches)):
92 self.choices.addItem(matches[i])
93
94 if len(matches) == 1 and matches[0] == text:
95 self.choicesPopup.hide()
96 else:
97 self.choices.setSelectedIndex(0)
98 self.choices.setVisibleItemCount(len(matches) + 1)
99
100 if not self.popupAdded:
101 RootPanel().add(self.choicesPopup)
102 self.popupAdded = True
103
104 self.choicesPopup.show()
105 self.visible = True
106 self.choicesPopup.setPopupPosition(self.getAbsoluteLeft(), self.getAbsoluteTop() + self.getOffsetHeight())
107 self.choices.setWidth("%dpx" % self.getOffsetWidth())
108 else:
109 self.visible = False
110 self.choicesPopup.hide()
111
112 - def onChange(self, arg0):
114
115 - def onClick(self, arg0):
117
118 - def complete(self):
119 if self.choices.getItemCount() > 0:
120 self.setText(self.choices.getItemText(self.choices.getSelectedIndex()))
121
122 self.choices.clear()
123 self.choicesPopup.hide()
124 self.setFocus(True)
125
126 Factory.registerClass('pyjamas.ui.AutoCompleteTextBox', AutoCompleteTextBox)
127
128
131 if items is None:
132 items = []
133 self.completions = items
134
136 matches = []
137 match = match.lower()
138
139 for i in range(len(self.completions)):
140 lower = self.completions[i].lower()
141 if lower.startswith(match):
142 matches.append(self.completions[i])
143
144 return matches
145