1
2
3
4
5
6
7
8
9
10
11
12 from pyjamas.ui.TextBox import TextBox
13 from pyjamas import Factory
14 from pyjamas.ui.PopupPanel import PopupPanel
15 from pyjamas.ui.ListBox import ListBox
16 from pyjamas.ui import KeyboardListener
17 from pyjamas.ui.RootPanel import RootPanel
18
20 - def __init__(self, **kwargs):
21 self.choicesPopup = PopupPanel(True, False)
22 self.choices = ListBox()
23 self.items = SimpleAutoCompletionItems()
24 self.popupAdded = False
25 self.visible = False
26
27 self.choices.addClickListener(self)
28 self.choices.addChangeListener(self)
29
30 self.choicesPopup.add(self.choices)
31 self.choicesPopup.addStyleName("AutoCompleteChoices")
32
33 self.choices.setStyleName("list")
34
35 if not kwargs.has_key('StyleName'): kwargs['StyleName']="gwt-AutoCompleteTextBox"
36
37 TextBox.__init__(self, **kwargs)
38 self.addKeyboardListener(self)
39
40 - def setCompletionItems(self, items):
41 if not hasattr(items, 'getCompletionItems'):
42 items = SimpleAutoCompletionItems(items)
43
44 self.items = items
45
48
49 - def onKeyDown(self, arg0, arg1, arg2):
51
52 - def onKeyPress(self, arg0, arg1, arg2):
54
55 - def onKeyUp(self, arg0, arg1, arg2):
56 if arg1 == KeyboardListener.KEY_DOWN:
57 selectedIndex = self.choices.getSelectedIndex()
58 selectedIndex += 1
59 if selectedIndex >= self.choices.getItemCount():
60 selectedIndex = 0
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() - 1
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 self.visible = False
126
127 Factory.registerClass('pyjamas.ui.AutoComplete', 'AutoCompleteTextBox', AutoCompleteTextBox)
128
129
132 if items is None:
133 items = []
134 self.completions = items
135
137 matches = []
138 match = match.lower()
139
140 for i in range(len(self.completions)):
141 lower = self.completions[i].lower()
142 if lower.startswith(match):
143 matches.append(self.completions[i])
144
145 return matches
146