Statistics
| Revision:

gvsig-scripting / org.gvsig.scripting / trunk / org.gvsig.scripting / org.gvsig.scripting.app / org.gvsig.scripting.app.mainplugin / src / main / resources-plugin / scripting / lib / console / popup.py @ 582

History | View | Annotate | Download (4.3 KB)

1
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: popup.py 5782 2006-06-12 07:56:49Z jmvivo $"
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
        try:
88
            value = self.list.getSelectedValue()
89
            startPosition = self.dotPosition + 1
90
            caretPosition = self.textComponent.getCaretPosition()
91
            self.textComponent.select(startPosition, caretPosition) 
92
            self.textComponent.replaceSelection(value)
93
            self.textComponent.setCaretPosition(startPosition + len(value))
94
        except:
95
            pass
96
        self.hide()
97

    
98
    def setMethods(self, methodList):
99
        methodList.sort()
100
        self.data = methodList
101
        self.originalData = methodList
102
        self.typed = ""
103
        self.list.setListData(methodList)
104

    
105
    def show(self):
106
        # when the popup becomes visible, get the cursor
107
        # so we know how to replace the selection
108
        self.dotPosition = self.textComponent.getCaretPosition()
109
        self.setSize(self.getPreferredSize())
110
        JWindow.show(self)
111

    
112
    def getPreferredSize(self):
113
        # need to add a magic amount to the size to avoid scrollbars
114
        # I'm sure there's a better way to do this
115
        MAGIC = 20
116
        size = self.list.getPreferredScrollableViewportSize()
117
        height = size.height + MAGIC
118
        width = size.width + MAGIC
119
        if height > Popup.MAX_HEIGHT:
120
            height = Popup.MAX_HEIGHT
121
        if width > Popup.MAX_WIDTH:
122
            width = Popup.MAX_WIDTH
123
        if width < Popup.MIN_WIDTH:
124
            widht = Popup.MIN_WIDTH
125
        return Dimension(width, height)
126

    
127
    
128
# this needs a list renderer that will hilight the prefix
129
def filter(list, prefix):
130
    prefix = prefix.lower()
131
    list = [eachItem for eachItem in list if str(eachItem).lower().startswith(prefix)]
132
    return list
133