PageRenderTime 33ms CodeModel.GetById 19ms RepoModel.GetById 2ms app.codeStats 0ms

/addons/AutoComplete.py

http://pyjamas.googlecode.com/
Python | 127 lines | 95 code | 30 blank | 2 comment | 21 complexity | 502e0a58a0e81c3b8b6aef3d1f1e37e9 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  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. from ui import TextBox, PopupPanel, ListBox, KeyboardListener, RootPanel
  4. class AutoCompleteTextBox(TextBox):
  5. def __init__(self):
  6. self.choicesPopup = PopupPanel(True)
  7. self.choices = ListBox()
  8. self.items = SimpleAutoCompletionItems()
  9. self.popupAdded = False
  10. self.visible = False
  11. TextBox.__init__(self)
  12. self.addKeyboardListener(self)
  13. self.choices.addChangeListener(self)
  14. self.setStyleName("AutoCompleteTextBox")
  15. self.choicesPopup.add(self.choices)
  16. self.choicesPopup.addStyleName("AutoCompleteChoices")
  17. self.choices.setStyleName("list")
  18. def setCompletionItems(self, items):
  19. if not items.getCompletionItems:
  20. items = SimpleAutoCompletionItems(items)
  21. self.items = items
  22. def getCompletionItems(self):
  23. return self.items
  24. def onKeyDown(self, arg0, arg1, arg2):
  25. pass
  26. def onKeyPress(self, arg0, arg1, arg2):
  27. pass
  28. def onKeyUp(self, arg0, arg1, arg2):
  29. if arg1 == KeyboardListener.KEY_DOWN:
  30. selectedIndex = self.choices.getSelectedIndex()
  31. selectedIndex += 1
  32. if selectedIndex > self.choices.getItemCount():
  33. selectedIndex = 0
  34. self.choices.setSelectedIndex(selectedIndex)
  35. return
  36. if arg1 == KeyboardListener.KEY_UP:
  37. selectedIndex = self.choices.getSelectedIndex()
  38. selectedIndex -= 1
  39. if selectedIndex < 0:
  40. selectedIndex = self.choices.getItemCount()
  41. self.choices.setSelectedIndex(selectedIndex)
  42. return
  43. if arg1 == KeyboardListener.KEY_ENTER:
  44. if self.visible:
  45. self.complete()
  46. return
  47. if arg1 == KeyboardListener.KEY_ESCAPE:
  48. self.choices.clear()
  49. self.choicesPopup.hide()
  50. self.visible = False
  51. return
  52. text = self.getText()
  53. matches = []
  54. if len(text) > 0:
  55. matches = self.items.getCompletionItems(text)
  56. if len(matches) > 0:
  57. self.choices.clear()
  58. for i in range(len(matches)):
  59. self.choices.addItem(matches[i])
  60. if len(matches) == 1 and matches[0] == text:
  61. self.choicesPopup.hide()
  62. else:
  63. self.choices.setSelectedIndex(0)
  64. self.choices.setVisibleItemCount(len(matches) + 1)
  65. if not self.popupAdded:
  66. RootPanel().add(self.choicesPopup)
  67. self.popupAdded = True
  68. self.choicesPopup.show()
  69. self.visible = True
  70. self.choicesPopup.setPopupPosition(self.getAbsoluteLeft(), self.getAbsoluteTop() + self.getOffsetHeight())
  71. self.choices.setWidth(self.getOffsetWidth() + "px")
  72. else:
  73. self.visible = False
  74. self.choicesPopup.hide()
  75. def onChange(self, arg0):
  76. self.complete()
  77. def onClick(self, arg0):
  78. self.complete()
  79. def complete(self):
  80. if self.choices.getItemCount() > 0:
  81. self.setText(self.choices.getItemText(self.choices.getSelectedIndex()))
  82. self.choices.clear()
  83. self.choicesPopup.hide()
  84. self.setFocus(True)
  85. class SimpleAutoCompletionItems:
  86. def __init__(self, items = None):
  87. if items == None:
  88. items = []
  89. self.completions = items
  90. def getCompletionItems(self, match):
  91. matches = []
  92. match = match.lower()
  93. for i in range(len(self.completions)):
  94. lower = self.completions[i].lower()
  95. if lower.startswith(match):
  96. matches.append(self.completions[i])
  97. return matches