Package pyjamas :: Package ui :: Module AutoComplete
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.AutoComplete

  1  # Autocomplete component for Pyjamas 
  2  # Ported by Willie Gollino from Autocomplete component for GWT - 
  3  # Originally by Oliver Albers http://gwt.components.googlepages.com/ 
  4  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  5  # 
  6  # Licensed under the LGPL 2.1 
  7  # 
  8  # TODO: textarea autocomplete 
  9  # http://gwt.components.googlepages.com/auto-completiontextbox 
 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   
18 -class AutoCompleteTextBox(TextBox):
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
45 - def getCompletionItems(self):
46 return self.items
47
48 - def onKeyDown(self, arg0, arg1, arg2):
49 pass
50
51 - def onKeyPress(self, arg0, arg1, arg2):
52 pass
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):
113 self.complete()
114
115 - def onClick(self, arg0):
116 self.complete()
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
129 -class SimpleAutoCompletionItems:
130 - def __init__(self, items = None):
131 if items is None: 132 items = [] 133 self.completions = items
134
135 - def getCompletionItems(self, match):
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