root / trunk / extensions / extScripting / scripts / jython / console / popup.py @ 11445
History | View | Annotate | Download (4.19 KB)
1 | 5782 | jmvivo | from java.lang import Character |
---|---|---|---|
2 | from javax.swing import JWindow, JList, JScrollPane |
||
3 | from java.awt import Color, Dimension |
||
4 | from java.awt.event import KeyAdapter |
||
5 | from java.awt.event import KeyEvent |
||
6 | import string |
||
7 | |||
8 | __author__ = "Don Coleman <dcoleman@chariotsolutions.com>"
|
||
9 | __cvsid__ = "$Id$"
|
||
10 | |||
11 | class Popup(JWindow): |
||
12 | """Popup window to display list of methods for completion"""
|
||
13 | |||
14 | MAX_HEIGHT = 300
|
||
15 | MIN_WIDTH = 200
|
||
16 | MAX_WIDTH = 400
|
||
17 | |||
18 | def __init__(self, frame, textComponent): |
||
19 | JWindow.__init__(self, frame)
|
||
20 | self.textComponent = textComponent
|
||
21 | self.size = (200,200) |
||
22 | self.list = JList(keyPressed=self.key) |
||
23 | self.list.setBackground(Color(255,255,225)) # TODO move this color |
||
24 | self.getContentPane().add(JScrollPane(self.list)) |
||
25 | self.list.setSelectedIndex(0) |
||
26 | |||
27 | self.originalData = "" |
||
28 | self.data = "" |
||
29 | self.typed = "" |
||
30 | |||
31 | def key(self, e): |
||
32 | # key listener
|
||
33 | #print "Other Listener"
|
||
34 | code = e.getKeyCode() |
||
35 | #print 'keychar:',e.getKeyChar()
|
||
36 | |||
37 | if code == KeyEvent.VK_ESCAPE:
|
||
38 | self.hide()
|
||
39 | |||
40 | elif code == KeyEvent.VK_ENTER or code == KeyEvent.VK_TAB: |
||
41 | self.chooseSelected()
|
||
42 | e.consume() |
||
43 | |||
44 | elif code == 8: # BACKSPACE |
||
45 | self.typed = self.typed[:-1] |
||
46 | self.data = filter(self.originalData, self.typed) |
||
47 | self.list.setListData(self.data) |
||
48 | self.list.setSelectedIndex(0) |
||
49 | |||
50 | elif code == KeyEvent.VK_UP:
|
||
51 | self.previous()
|
||
52 | # consume event to avoid history previous
|
||
53 | e.consume() |
||
54 | |||
55 | elif code == KeyEvent.VK_DOWN:
|
||
56 | self.next()
|
||
57 | # consume event to avoid history next
|
||
58 | e.consume() |
||
59 | |||
60 | else:
|
||
61 | char = e.getKeyChar() |
||
62 | if Character.isJavaLetterOrDigit(char):
|
||
63 | self.typed += char
|
||
64 | self.data = filter(self.data, self.typed) |
||
65 | self.list.setListData(self.data) |
||
66 | self.list.setSelectedIndex(0) |
||
67 | |||
68 | def next(self): |
||
69 | index = self.list.getSelectedIndex()
|
||
70 | max = (self.list.getModel().getSize() - 1) |
||
71 | |||
72 | if index < max: |
||
73 | index += 1
|
||
74 | self.list.setSelectedIndex(index)
|
||
75 | self.list.ensureIndexIsVisible(index)
|
||
76 | |||
77 | def previous(self): |
||
78 | index = self.list.getSelectedIndex()
|
||
79 | |||
80 | if index > 0: |
||
81 | index -= 1
|
||
82 | self.list.setSelectedIndex(index)
|
||
83 | self.list.ensureIndexIsVisible(index)
|
||
84 | |||
85 | def chooseSelected(self): |
||
86 | """Choose the selected value in the list"""
|
||
87 | value = self.list.getSelectedValue()
|
||
88 | startPosition = self.dotPosition + 1 |
||
89 | caretPosition = self.textComponent.getCaretPosition()
|
||
90 | self.textComponent.select(startPosition, caretPosition)
|
||
91 | self.textComponent.replaceSelection(value)
|
||
92 | self.textComponent.setCaretPosition(startPosition + len(value)) |
||
93 | self.hide()
|
||
94 | |||
95 | def setMethods(self, methodList): |
||
96 | methodList.sort() |
||
97 | self.data = methodList
|
||
98 | self.originalData = methodList
|
||
99 | self.typed = "" |
||
100 | self.list.setListData(methodList)
|
||
101 | |||
102 | def show(self): |
||
103 | # when the popup becomes visible, get the cursor
|
||
104 | # so we know how to replace the selection
|
||
105 | self.dotPosition = self.textComponent.getCaretPosition() |
||
106 | self.setSize(self.getPreferredSize()) |
||
107 | JWindow.show(self)
|
||
108 | |||
109 | def getPreferredSize(self): |
||
110 | # need to add a magic amount to the size to avoid scrollbars
|
||
111 | # I'm sure there's a better way to do this
|
||
112 | MAGIC = 20
|
||
113 | size = self.list.getPreferredScrollableViewportSize()
|
||
114 | height = size.height + MAGIC |
||
115 | width = size.width + MAGIC |
||
116 | if height > Popup.MAX_HEIGHT:
|
||
117 | height = Popup.MAX_HEIGHT |
||
118 | if width > Popup.MAX_WIDTH:
|
||
119 | width = Popup.MAX_WIDTH |
||
120 | if width < Popup.MIN_WIDTH:
|
||
121 | widht = Popup.MIN_WIDTH |
||
122 | return Dimension(width, height)
|
||
123 | |||
124 | |||
125 | # this needs a list renderer that will hilight the prefix
|
||
126 | def filter(list, prefix): |
||
127 | prefix = prefix.lower() |
||
128 | list = [eachItem for eachItem in list if str(eachItem).lower().startswith(prefix)] |
||
129 | return list |