PageRenderTime 150ms CodeModel.GetById 131ms app.highlight 12ms RepoModel.GetById 5ms app.codeStats 0ms

/addons/AutoComplete.py

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