class Console: PROMPT = sys.ps1 PROCESS = sys.ps2 BANNER = ["Jython Completion Shell", InteractiveConsole.getDefaultBanner()] def __init__(self, frame): self.frame = frame # TODO do I need a reference to frame after the constructor? self.history = History(self) self.bs = 0 # what is this? # command buffer self.buffer = [] self.locals = {"gvSIG": sys.gvSIG} self.interp = Interpreter(self, self.locals) sys.stdout = StdOutRedirector(self) # create a textpane self.output = JTextPane(keyTyped=self.keyTyped, keyPressed=self.keyPressed) # TODO rename output to textpane # CTRL UP AND DOWN don't work keyBindings = [ (KeyEvent.VK_ENTER, 0, "jython.enter", self.enter), (KeyEvent.VK_DELETE, 0, "jython.delete", self.delete), (KeyEvent.VK_HOME, 0, "jython.home", self.home), (KeyEvent.VK_UP, 0, "jython.up", self.history.historyUp), (KeyEvent.VK_DOWN, 0, "jython.down", self.history.historyDown), (KeyEvent.VK_PERIOD, 0, "jython.showPopup", self.showPopup), (KeyEvent.VK_ESCAPE, 0, "jython.hide", self.hide), ("(", 0, "jython.showTip", self.showTip), (")", 0, "jython.hideTip", self.hideTip), # (KeyEvent.VK_UP, InputEvent.CTRL_MASK, DefaultEditorKit.upAction, self.output.keymap.getAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0))), # (KeyEvent.VK_DOWN, InputEvent.CTRL_MASK, DefaultEditorKit.downAction, self.output.keymap.getAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0))) ] # TODO rename newmap to keymap newmap = JTextComponent.addKeymap("jython", self.output.keymap) for (key, modifier, name, function) in keyBindings: newmap.addActionForKeyStroke(KeyStroke.getKeyStroke(key, modifier), ActionDelegator(name, function)) self.output.keymap = newmap self.doc = self.output.document # self.panel.add(BorderLayout.CENTER, JScrollPane(self.output)) self.__propertiesChanged() self.__inittext() self.initialLocation = self.doc.createPosition(self.doc.length - 1) # Don't pass frame to popups. JWindows with null owners are not focusable # this fixes the focus problem on Win32, but make the mouse problem worse self.popup = Popup(None, self.output) self.tip = Tip(None) # get fontmetrics info so we can position the popup metrics = self.output.getFontMetrics(self.output.getFont()) self.dotWidth = metrics.charWidth(".") self.textHeight = metrics.getHeight() # add some handles to our objects self.locals["console"] = self self.caret = self.output.getCaret() # TODO refactor me def getinput(self): offsets = self.__lastLine() text = self.doc.getText(offsets[0], offsets[1] - offsets[0]) return text def getDisplayPoint(self): """Get the point where the popup window should be displayed""" screenPoint = self.output.getLocationOnScreen() caretPoint = self.output.caret.getMagicCaretPosition() # TODO use SwingUtils to do this translation x = screenPoint.getX() + caretPoint.getX() + self.dotWidth y = screenPoint.getY() + caretPoint.getY() + self.textHeight return Point(int(x), int(y)) def hide(self, event=None): """Hide the popup or tip window if visible""" if self.popup.visible: self.popup.hide() if self.tip.visible: self.tip.hide() def hideTip(self, event=None): self.tip.hide() # TODO this needs to insert ')' at caret! self.write(")") def showTip(self, event=None): # get the display point before writing text # otherwise magicCaretPosition is None displayPoint = self.getDisplayPoint() if self.popup.visible: self.popup.hide() line = self.getinput() # debug("line", line) # Hack 'o rama line = line[:-1] # remove \n line += "(" # debug("line", line) # TODO this needs to insert '(' at caret! self.write("(") (name, argspec, tip) = jintrospect.getCallTipJava(line, self.locals) # debug("name", name) # debug("argspec", argspec) # debug("tip", tip) if tip: self.tip.setLocation(displayPoint) self.tip.setText(tip) self.tip.show() def showPopup(self, event=None): line = self.getinput() # this is silly, I have to add the '.' and the other code removes it. line = line[:-1] # remove \n line = line + "." # print >> sys.stderr, "line:",line # TODO get this code into Popup # TODO handle errors gracefully try: list = jintrospect.getAutoCompleteList(line, self.locals) except Exception, e: # TODO handle this gracefully print >>sys.stderr, e return if len(list) == 0: # print >> sys.stderr, "list was empty" return self.popup.setLocation(self.getDisplayPoint()) self.popup.setMethods(list) self.popup.show() self.popup.list.setSelectedIndex(0)
class Console: PROMPT = sys.ps1 PROCESS = sys.ps2 BANNER = ["Jython Completion Shell", InteractiveConsole.getDefaultBanner()] def __init__(self, frame): self.frame = frame # TODO do I need a reference to frame after the constructor? self.history = History(self) self.bs = 0 # what is this? # command buffer self.buffer = [] self.locals = {"gvSIG": sys.gvSIG} self.interp = Interpreter(self, self.locals) sys.stdout = StdOutRedirector(self) # create a textpane self.output = JTextPane(keyTyped=self.keyTyped, keyPressed=self.keyPressed) # TODO rename output to textpane # CTRL UP AND DOWN don't work keyBindings = [ (KeyEvent.VK_ENTER, 0, "jython.enter", self.enter), (KeyEvent.VK_DELETE, 0, "jython.delete", self.delete), (KeyEvent.VK_HOME, 0, "jython.home", self.home), (KeyEvent.VK_UP, 0, "jython.up", self.history.historyUp), (KeyEvent.VK_DOWN, 0, "jython.down", self.history.historyDown), (KeyEvent.VK_PERIOD, 0, "jython.showPopup", self.showPopup), (KeyEvent.VK_ESCAPE, 0, "jython.hide", self.hide), ('(', 0, "jython.showTip", self.showTip), (')', 0, "jython.hideTip", self.hideTip), #(KeyEvent.VK_UP, InputEvent.CTRL_MASK, DefaultEditorKit.upAction, self.output.keymap.getAction(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0))), #(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK, DefaultEditorKit.downAction, self.output.keymap.getAction(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0))) ] # TODO rename newmap to keymap newmap = JTextComponent.addKeymap("jython", self.output.keymap) for (key, modifier, name, function) in keyBindings: newmap.addActionForKeyStroke(KeyStroke.getKeyStroke(key, modifier), ActionDelegator(name, function)) self.output.keymap = newmap self.doc = self.output.document #self.panel.add(BorderLayout.CENTER, JScrollPane(self.output)) self.__propertiesChanged() self.__inittext() self.initialLocation = self.doc.createPosition(self.doc.length - 1) # Don't pass frame to popups. JWindows with null owners are not focusable # this fixes the focus problem on Win32, but make the mouse problem worse self.popup = Popup(None, self.output) self.tip = Tip(None) # get fontmetrics info so we can position the popup metrics = self.output.getFontMetrics(self.output.getFont()) self.dotWidth = metrics.charWidth('.') self.textHeight = metrics.getHeight() # add some handles to our objects self.locals['console'] = self self.caret = self.output.getCaret() # TODO refactor me def getinput(self): offsets = self.__lastLine() text = self.doc.getText(offsets[0], offsets[1] - offsets[0]) return text def getDisplayPoint(self): """Get the point where the popup window should be displayed""" screenPoint = self.output.getLocationOnScreen() caretPoint = self.output.caret.getMagicCaretPosition() # TODO use SwingUtils to do this translation x = screenPoint.getX() + caretPoint.getX() + self.dotWidth y = screenPoint.getY() + caretPoint.getY() + self.textHeight return Point(int(x), int(y)) def hide(self, event=None): """Hide the popup or tip window if visible""" if self.popup.visible: self.popup.hide() if self.tip.visible: self.tip.hide() def hideTip(self, event=None): self.tip.hide() # TODO this needs to insert ')' at caret! self.write(')') def showTip(self, event=None): # get the display point before writing text # otherwise magicCaretPosition is None displayPoint = self.getDisplayPoint() if self.popup.visible: self.popup.hide() line = self.getinput() #debug("line", line) # Hack 'o rama line = line[:-1] # remove \n line += '(' #debug("line", line) # TODO this needs to insert '(' at caret! self.write('(') (name, argspec, tip) = jintrospect.getCallTipJava(line, self.locals) #debug("name", name) #debug("argspec", argspec) #debug("tip", tip) if tip: self.tip.setLocation(displayPoint) self.tip.setText(tip) self.tip.show() def showPopup(self, event=None): line = self.getinput() # this is silly, I have to add the '.' and the other code removes it. line = line[:-1] # remove \n line = line + '.' #print >> sys.stderr, "line:",line # TODO get this code into Popup # TODO handle errors gracefully try: list = jintrospect.getAutoCompleteList(line, self.locals) except Exception, e: # TODO handle this gracefully print >> sys.stderr, e return if len(list) == 0: #print >> sys.stderr, "list was empty" return self.popup.setLocation(self.getDisplayPoint()) self.popup.setMethods(list) self.popup.show() self.popup.list.setSelectedIndex(0)