/
AutoComplete.py
127 lines (97 loc) · 3.95 KB
/
AutoComplete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Autocomplete component for Pyjamas
# Ported by Willie Gollino from Autocomplete component for GWT - Originally by Oliver Albers http://gwt.components.googlepages.com/
from ui import TextBox, PopupPanel, ListBox, KeyboardListener, RootPanel
class AutoCompleteTextBox(TextBox):
def __init__(self):
self.choicesPopup = PopupPanel(True)
self.choices = ListBox()
self.items = SimpleAutoCompletionItems()
self.popupAdded = False
self.visible = False
TextBox.__init__(self)
self.addKeyboardListener(self)
self.choices.addChangeListener(self)
self.setStyleName("AutoCompleteTextBox")
self.choicesPopup.add(self.choices)
self.choicesPopup.addStyleName("AutoCompleteChoices")
self.choices.setStyleName("list")
def setCompletionItems(self, items):
if not items.getCompletionItems:
items = SimpleAutoCompletionItems(items)
self.items = items
def getCompletionItems(self):
return self.items
def onKeyDown(self, arg0, arg1, arg2):
pass
def onKeyPress(self, arg0, arg1, arg2):
pass
def onKeyUp(self, arg0, arg1, arg2):
if arg1 == KeyboardListener.KEY_DOWN:
selectedIndex = self.choices.getSelectedIndex()
selectedIndex += 1
if selectedIndex > self.choices.getItemCount():
selectedIndex = 0
self.choices.setSelectedIndex(selectedIndex)
return
if arg1 == KeyboardListener.KEY_UP:
selectedIndex = self.choices.getSelectedIndex()
selectedIndex -= 1
if selectedIndex < 0:
selectedIndex = self.choices.getItemCount()
self.choices.setSelectedIndex(selectedIndex)
return
if arg1 == KeyboardListener.KEY_ENTER:
if self.visible:
self.complete()
return
if arg1 == KeyboardListener.KEY_ESCAPE:
self.choices.clear()
self.choicesPopup.hide()
self.visible = False
return
text = self.getText()
matches = []
if len(text) > 0:
matches = self.items.getCompletionItems(text)
if len(matches) > 0:
self.choices.clear()
for i in range(len(matches)):
self.choices.addItem(matches[i])
if len(matches) == 1 and matches[0] == text:
self.choicesPopup.hide()
else:
self.choices.setSelectedIndex(0)
self.choices.setVisibleItemCount(len(matches) + 1)
if not self.popupAdded:
RootPanel().add(self.choicesPopup)
self.popupAdded = True
self.choicesPopup.show()
self.visible = True
self.choicesPopup.setPopupPosition(self.getAbsoluteLeft(), self.getAbsoluteTop() + self.getOffsetHeight())
self.choices.setWidth(self.getOffsetWidth() + "px")
else:
self.visible = False
self.choicesPopup.hide()
def onChange(self, arg0):
self.complete()
def onClick(self, arg0):
self.complete()
def complete(self):
if self.choices.getItemCount() > 0:
self.setText(self.choices.getItemText(self.choices.getSelectedIndex()))
self.choices.clear()
self.choicesPopup.hide()
self.setFocus(True)
class SimpleAutoCompletionItems:
def __init__(self, items = None):
if items == None:
items = []
self.completions = items
def getCompletionItems(self, match):
matches = []
match = match.lower()
for i in range(len(self.completions)):
lower = self.completions[i].lower()
if lower.startswith(match):
matches.append(self.completions[i])
return matches