class Console: PROMPT = sys.ps1 PROCESS = sys.ps2 BANNER = ["Jython Completion Shell", InteractiveConsole.getDefaultBanner()] include_single_underscore_methods = False include_double_underscore_methods = False def __init__(self, namespace=None): """ Create a Jython Console. namespace is an optional and should be a dictionary or Map """ self.history = History(self) if namespace != None: self.locals = namespace else: self.locals = {} self.buffer = [] # buffer for multi-line commands self.interp = Interpreter(self, self.locals) sys.stdout = StdOutRedirector(self) self.text_pane = JTextPane(keyTyped=self.keyTyped, keyPressed=self.keyPressed) self.__initKeyMap() self.doc = self.text_pane.document 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.text_pane) self.tip = Tip(None) # get fontmetrics info so we can position the popup metrics = self.text_pane.getFontMetrics(self.text_pane.getFont()) self.dotWidth = metrics.charWidth('.') self.textHeight = metrics.getHeight() # add some handles to our objects self.locals['console'] = self def insertText(self, text): """insert text at the current caret position""" # seems like there should be a better way to do this.... # might be better as a method on the text component? caretPosition = self.text_pane.getCaretPosition() self.text_pane.select(caretPosition, caretPosition) self.text_pane.replaceSelection(text) self.text_pane.setCaretPosition(caretPosition + len(text)) def getText(self): """get text from last line of console""" offsets = self.__lastLine() text = self.doc.getText(offsets[0], offsets[1] - offsets[0]) return text.rstrip() def getDisplayPoint(self): """Get the point where the popup window should be displayed""" screenPoint = self.text_pane.getLocationOnScreen() caretPoint = self.text_pane.caret.getMagicCaretPosition() # BUG: sometimes caretPoint is None # To duplicate type "java.aw" and hit '.' to complete selection while popup is visible 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() self.insertText(')') 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.getText() self.insertText('(') (name, argspec, tip) = jintrospect.getCallTipJava(line, self.locals) if tip: self.tip.showTip(tip, displayPoint) def showPopup(self, event=None): """show code completion popup""" try: line = self.getText() list = jintrospect.getAutoCompleteList( line, self.locals, includeSingle=self.include_single_underscore_methods, includeDouble=self.include_double_underscore_methods) if len(list) > 0: self.popup.showMethodCompletionList(list, self.getDisplayPoint()) except Exception, e: print >> sys.stderr, "Error getting completion list: ", e
class Console: PROMPT = sys.ps1 PROCESS = sys.ps2 BANNER = ["Jython Completion Shell", InteractiveConsole.getDefaultBanner()] include_single_underscore_methods = False include_double_underscore_methods = False def __init__(self, namespace=None): """ Create a Jython Console. namespace is an optional and should be a dictionary or Map """ self.history = History(self) if namespace != None: self.locals = namespace else: self.locals = {} self.buffer = [] # buffer for multi-line commands self.interp = Interpreter(self, self.locals) sys.stdout = StdOutRedirector(self) self.text_pane = JTextPane(keyTyped = self.keyTyped, keyPressed = self.keyPressed) self.__initKeyMap() self.doc = self.text_pane.document 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.text_pane) self.tip = Tip(None) # get fontmetrics info so we can position the popup metrics = self.text_pane.getFontMetrics(self.text_pane.getFont()) self.dotWidth = metrics.charWidth('.') self.textHeight = metrics.getHeight() # add some handles to our objects self.locals['console'] = self def insertText(self, text): """insert text at the current caret position""" # seems like there should be a better way to do this.... # might be better as a method on the text component? caretPosition = self.text_pane.getCaretPosition() self.text_pane.select(caretPosition, caretPosition) self.text_pane.replaceSelection(text) self.text_pane.setCaretPosition(caretPosition + len(text)) def getText(self): """get text from last line of console""" offsets = self.__lastLine() text = self.doc.getText(offsets[0], offsets[1]-offsets[0]) return text.rstrip() def getDisplayPoint(self): """Get the point where the popup window should be displayed""" screenPoint = self.text_pane.getLocationOnScreen() caretPoint = self.text_pane.caret.getMagicCaretPosition() # BUG: sometimes caretPoint is None # To duplicate type "java.aw" and hit '.' to complete selection while popup is visible 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() self.insertText(')') 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.getText() self.insertText('(') (name, argspec, tip) = jintrospect.getCallTipJava(line, self.locals) if tip: self.tip.showTip(tip, displayPoint) def showPopup(self, event=None): """show code completion popup""" try: line = self.getText() list = jintrospect.getAutoCompleteList(line, self.locals, includeSingle=self.include_single_underscore_methods, includeDouble=self.include_double_underscore_methods) if len(list) > 0: self.popup.showMethodCompletionList(list, self.getDisplayPoint()) except Exception, e: print >> sys.stderr, "Error getting completion list: ", e