Example #1
0
 def enter(self, event):
     """ Triggered when enter is pressed """
     offsets = self.__lastLine()
     text = self.doc.getText(offsets[0], offsets[1] - offsets[0])
     # Detects special keywords
     if text != "\n":
         if text[:-1] == "copyright":
             self.printResult(
                 jEdit.getProperty("jython.interpreter.copyright") +
                 sys.copyright)
             self.printPrompt()
             return
         if text[:-1] == "credits":
             import site
             self.printResult(
                 jEdit.getProperty("jython.interpreter.credits") + "\n" +
                 str(site.__builtin__.credits))
             self.printPrompt()
             return
         if text[:-1] == "license":
             import site
             self.printResult(
                 jEdit.getProperty("jython.interpreter.license"))
             self.printPrompt()
             return
         text = text[:-1]
         if self.indenter.isPushable(text):
             JythonExecutor.getExecutor().execute(self.view, text, self)
             self.history.append(text)
         else:
             self.printOnProcess()
     else:
         JythonExecutor.getExecutor().execute(self.view, text, self)
 def openFileChooser(self, event):
     import sys
     theFilter = FileDialogFilter(
         ["jar", "zip"],
         jEdit.getProperty("jython.pathhandler.compressedfilter"))
     chooser = JFileChooser(len(sys.path) and sys.path[0] or ".", \
      fileSelectionMode = JFileChooser.FILES_AND_DIRECTORIES, dialogTitle =jEdit.getProperty("jython.pathhandler.addtitle"), \
      fileFilter = theFilter)
     if chooser.showOpenDialog(self) == JFileChooser.APPROVE_OPTION:
         self.pathField.text = chooser.selectedFile.path
Example #3
0
 def __init__(self, view, buffer):
     self.target = jEdit.newFile(view)
     self.target.setProperty("jython.originator", buffer)
     self.background = GUIUtilities.parseColor(jEdit.getProperty( \
      "jython.bgColor"))
     self.foreground = GUIUtilities.parseColor(jEdit.getProperty( \
      "jython.plainColor"))
     self.infoColor = GUIUtilities.parseColor(jEdit.getProperty( \
      "jython.infoColor"))
     self.warningColor = GUIUtilities.parseColor(jEdit.getProperty( \
      "jython.warningColor"))
     self.errorColor = GUIUtilities.parseColor(jEdit.getProperty( \
      "jython.errorColor"))
Example #4
0
 def createButton(self, toolBar, i, t, f):
     if i == "separator":
         toolBar.addSeparator()
     else:
         b = JButton(icon = GUIUtilities.loadIcon(i), \
         toolTipText = jEdit.getProperty(t), actionPerformed = f)
         toolBar.add(b)
Example #5
0
 def restart(self, event):
     """ Reinitializes the interpreter """
     from javax.swing import JOptionPane
     result = JOptionPane.showConfirmDialog(self.panel, \
      jEdit.getProperty("jython.console.clean-confirm"), "Confirm", JOptionPane.YES_NO_CANCEL_OPTION)
     if result == JOptionPane.YES_OPTION:
         JythonExecutor.getExecutor().interrupt()
         self.__inittext()
Example #6
0
    def __propertiesChanged(self):
        """ Detects when the properties have changed """
        self.output.background = jEdit.getColorProperty("jython.bgColor")
        self.output.foreground = jEdit.getColorProperty("jython.resultColor")
        self.infoColor = jEdit.getColorProperty("jython.textColor")
        self.errorColor = jEdit.getColorProperty("jython.errorColor")

        family = jEdit.getProperty("jython.font", "Monospaced")
        size = jEdit.getIntegerProperty("jython.fontsize", 14)
        style = jEdit.getIntegerProperty("jython.fontstyle", Font.PLAIN)
        self.output.setFont(Font(family, style, size))
 def __init__(self, view, sys):
     JDialog.__init__(self, view,
                      jEdit.getProperty("jython.pathhandler.title"))
     self.sys = sys
     content = self.contentPane
     content.layout = BorderLayout()
     upperPanel = JPanel(BorderLayout())
     leftPanel = JPanel(BorderLayout(), border = \
      BorderFactory.createTitledBorder(jEdit.getProperty("jython.pathhandler.pathborder")))
     self.model = DefaultListModel()
     for s in sys.path:
         self.model.addElement(s)
     self.pathlist = JList(
         self.model, selectionMode=ListSelectionModel.SINGLE_SELECTION)
     leftPanel.add(JScrollPane(self.pathlist))
     rightPanel = JPanel(GridBagLayout())
     constraints = GridBagConstraints()
     constraints.insets = Insets(5, 5, 5, 5)
     constraints.gridy = GridBagConstraints.RELATIVE
     constraints.gridx = 0
     buttons = [("Plus.png", "New...", self.new), \
      ("ButtonProperties.png", "Edit...", self.edit),
      ("Minus.png", "Remove", self.remove), \
      ("ArrowU.png", "Move Up", self.up), \
      ("ArrowD.png", "Move down", self.down)]
     for (i, t, a) in buttons:
         rightPanel.add(JButton(icon = GUIUtilities.loadIcon(i), \
          toolTipText=t, actionPerformed=a), constraints)
     upperPanel.add(leftPanel, BorderLayout.CENTER)
     upperPanel.add(rightPanel, BorderLayout.EAST)
     content.add(upperPanel, BorderLayout.CENTER)
     lowerPanel = JPanel(FlowLayout(FlowLayout.RIGHT))
     self.saveAsk = JCheckBox(jEdit.getProperty("options.jython.saveJythonPathTitle"), \
      selected = jEdit.getBooleanProperty("options.jython.saveJythonPath"), actionPerformed = self.saveAsk)
     ok = JButton("Ok", actionPerformed=self.__ok)
     lowerPanel.add(self.saveAsk)
     lowerPanel.add(ok)
     self.rootPane.defaultButton = ok
     lowerPanel.add(JButton("Cancel", actionPerformed=self.__cancel))
     content.add(lowerPanel, BorderLayout.SOUTH)
Example #8
0
 def __init__(self, frame, title):
     EnhancedDialog.__init__(self, frame, title, 1)
     self.contentPane.layout = layout(5, 5)
     upperPanel = panel(layout(5, 5))
     topPanel = panel(layout(5, 20))
     topPanel.add(label(getProperty("jython.about.about"), label.CENTER),
                  layout.CENTER)
     topPanel.add(
         label(
             getIconFromPlugin("jython.JythonPlugin",
                               "images/PythonIcon.gif"), label.CENTER),
         layout.EAST)
     upperPanel.add(topPanel, layout.NORTH)
     copy = JTextArea(copyright)
     upperPanel.border = BorderFactory.createTitledBorder(
         getProperty("jython.about.copyright"))
     copy.border = BorderFactory.createEmptyBorder(5, 5, 5, 5)
     upperPanel.add(copy, layout.SOUTH)
     self.contentPane.add(upperPanel, layout.CENTER)
     lowerPanel = panel()
     lowerPanel.add(
         button(getProperty("common.close"), actionPerformed=self._close))
     self.contentPane.add(lowerPanel, layout.SOUTH)
     self.pack()
 def __init__(self, view, doc):
     EnhancedDialog.__init__(self, view,
                             jEdit.getProperty("jython.export.title"), 1)
     self.view = view
     self.doc = doc
     panel = Box.createVerticalBox()
     panel.border = EmptyBorder(10, 10, 10, 10)
     self.header = JCheckBox(jEdit.getProperty("jython.export.header"), 0)
     panel.add(self.header)
     self.prompts = JCheckBox(jEdit.getProperty("jython.export.prompts"), 0)
     panel.add(self.prompts)
     self.errors = JCheckBox(jEdit.getProperty("jython.export.errors"), 0)
     panel.add(self.errors)
     self.contentPane.layout = BorderLayout()
     self.contentPane.add(panel, BorderLayout.CENTER)
     lowerPanel = JPanel(FlowLayout.LEFT)
     lowerPanel.add(JButton(jEdit.getProperty("common.ok"), actionPerformed = \
      self._ok))
     lowerPanel.add(JButton(jEdit.getProperty("common.cancel"), actionPerformed = \
      self._cancel))
     self.contentPane.add(lowerPanel, BorderLayout.SOUTH)
Example #10
0
def _about(view):
    a = AboutDialog(view, getProperty("jython.about.title"))
    centerDialog(a)
    a.visible = 1
Example #11
0
class jiconsole(Console):
    """
		Class jiconsole, it is the main class which handles user interaction
	"""
    PROMPT = sys.ps1
    PROCESS = sys.ps2
    BANNER = (InteractiveConsole.getDefaultBanner(),
              jEdit.getProperty("console-second-line"))

    def __init__(self, view):
        """ Constructor, initialized all the main variables and layout """
        # Initializes variables
        self.view = view
        self.history = History(self)
        self.bs = 0
        self.indenter = TabIndenter()
        self.exporterConsole = None
        self.executor = JythonExecutor.getExecutor()

        # Creates toolbar
        actions = [ ("Run.png", "jython.tooltip-run", self.runBuffer), \
         ("RunToBuffer.png", "jython.tooltip-run-another", self.runBufferToWindow), \
         ("RunAgain.png", "jython.tooltip-import", self.importBuffer), \
         ("MultipleResults.png", "jython.tooltip-path", self.path), \
         ("Open.png", "jython.tooltip-browse-path", self.browse), \
         ("CopyToBuffer.png", "jython.tooltip-save-session", self.savesession), \
         ("separator", None, None), \
         ("Clear.png", "jython.tooltip-restart", self.restart),
         ("separator", None, None),
         ("Parse.png", "jython.tooltip-tabnanny", self.tabnanny),
         ("separator", None, None),
         ("Help.png", "jython.tooltip-about", self.about)]
        self.panel = JPanel(BorderLayout())
        self.panel.add(BorderLayout.NORTH,
                       ToolbarHandler(actions).createToolbar())

        # Creates text pane and make keybindings
        # self.output = JTextPane(keyTyped = self.keyTyped, keyPressed = self.keyPressed, keyReleased = self.keyReleased)
        self.output = JTextPane(keyTyped=self.keyTyped,
                                keyPressed=self.keyPressed)
        if jEdit.getBooleanProperty("options.jython.upDownFlag"):
            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_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)))
            ]
        else:
            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, InputEvent.CTRL_MASK, "jython.historyup", \
              self.history.historyUp),
             (KeyEvent.VK_DOWN, InputEvent.CTRL_MASK, "jython.historydown", \
              self.history.historyDown)
            ]
        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)

    def browse(self, event):
        """ Opens the browse window """
        self.view.dockableWindowManager.toggleDockableWindow(
            "jython-browser-dockable")

    def stop(self, event):
        """ Stops the current execution """
        self.executor.interruptCurrent(self)

    def about(self, event):
        """ Opens the about dialog """
        from jiabout import AboutDialog
        a = AboutDialog(self.view, jEdit.getProperty("jython.about.title"))
        centerDialog(a)
        a.setVisible(1)

    def savesession(self, event):
        """ Saves the current session """
        from exportsession import export
        export(self.view, self.doc)

    def path(self, event):
        """ Opens the Path Handler """
        from pathhandler import PyPathHandler
        v = PyPathHandler(self.view, sys)
        v.pack()
        centerDialog(v)
        v.visible = 1

    def __buffer(self):
        """
			Returns the current buffer ready for run/import, checking if it is
			a python buffer and whether has been saved
		"""
        from javax.swing import JOptionPane
        buf = self.view.editPane.buffer
        if buf.mode.name != "python":
            if buf.getProperty("jython.originator"):
                buf = buf.getProperty("jython.originator")

        if buf.mode.name == "python":
            if buf.dirty:
                if not jEdit.getBooleanProperty("options.jython.autosave"):
                    if jEdit.getBooleanProperty("options.jython.saveask"):
                        result = JOptionPane.showConfirmDialog(
                            self.panel, "Do you want to save before running",
                            "Confirm", JOptionPane.YES_NO_CANCEL_OPTION)
                        if result == JOptionPane.CANCEL_OPTION:
                            return None
                        if result == JOptionPane.YES_OPTION:
                            buf.save(self.view, None)
                else:
                    buf.save(self.view, None)
            return buf

        return None

    def runBuffer(self, event):
        """ Runs the current buffer """
        buffer = self.__buffer()
        if buffer:
            JythonExecutor.getExecutor().execBuffer(self.view, buffer, self)

    def runBufferToWindow(self, event):
        """ Runs the current buffer to and exports the result to another window """
        from exportconsole import ConsoleToBuffer
        buffer = self.__buffer()
        if buffer:
            if jEdit.getBooleanProperty("options.jython.reuseOutputBuffer"):
                if not self.exporterConsole:
                    self.exporterConsole = ConsoleToBuffer(self.view, buffer)
                self.exporterConsole.clear()
            else:
                self.exporterConsole = ConsoleToBuffer(self.view, buffer)
            JythonExecutor.getExecutor().execBuffer(self.view, buffer,
                                                    self.exporterConsole)

    def tabnanny(self, event):
        import jitabnanny
        buffer = self.__buffer()
        jitabnanny.checkBuffer(buffer)

    def importBuffer(self, event):
        """ Imports the current buffer """
        import os.path
        import imp
        b = self.__buffer()
        if b:
            modname, ext = os.path.splitext(os.path.basename(b.path))
            if sys.modules.has_key(modname):
                mod = sys.modules[modname]
            else:
                mod = imp.new_module(modname)
            sys.modules[modname] = mod
            mod.__file__ = b.path
            setattr(sys.modules['__main__'], modname, mod)

            dir = os.path.dirname(b.path)
            dir = os.path.normpath(os.path.abspath(dir))
            if dir not in sys.path:
                sys.path.insert(0, dir)
            JythonExecutor.getExecutor().importBuffer(self.view, b, self)

    def restart(self, event):
        """ Reinitializes the interpreter """
        from javax.swing import JOptionPane
        result = JOptionPane.showConfirmDialog(self.panel, \
         jEdit.getProperty("jython.console.clean-confirm"), "Confirm", JOptionPane.YES_NO_CANCEL_OPTION)
        if result == JOptionPane.YES_OPTION:
            JythonExecutor.getExecutor().interrupt()
            self.__inittext()

    def inLastLine(self, include=1):
        """ Determines whether the cursor is in the last line """
        limits = self.__lastLine()
        caret = self.output.caretPosition
        if self.output.selectedText:
            caret = self.output.selectionStart
        if include:
            return (caret >= limits[0] and caret <= limits[1])
        else:
            return (caret > limits[0] and caret <= limits[1])

    def enter(self, event):
        """ Triggered when enter is pressed """
        offsets = self.__lastLine()
        text = self.doc.getText(offsets[0], offsets[1] - offsets[0])
        # Detects special keywords
        if text != "\n":
            if text[:-1] == "copyright":
                self.printResult(
                    jEdit.getProperty("jython.interpreter.copyright") +
                    sys.copyright)
                self.printPrompt()
                return
            if text[:-1] == "credits":
                import site
                self.printResult(
                    jEdit.getProperty("jython.interpreter.credits") + "\n" +
                    str(site.__builtin__.credits))
                self.printPrompt()
                return
            if text[:-1] == "license":
                import site
                self.printResult(
                    jEdit.getProperty("jython.interpreter.license"))
                self.printPrompt()
                return
            text = text[:-1]
            if self.indenter.isPushable(text):
                JythonExecutor.getExecutor().execute(self.view, text, self)
                self.history.append(text)
            else:
                self.printOnProcess()
        else:
            JythonExecutor.getExecutor().execute(self.view, text, self)

    def home(self, event):
        """ Triggered when HOME is pressed """
        if self.inLastLine():
            self.output.caretPosition = self.__lastLine()[0]
        else:
            lines = self.doc.rootElements[0].elementCount
            for i in xrange(0, lines - 1):
                offsets = (self.doc.rootElements[0].getElement(i).startOffset, \
                 self.doc.rootElements[0].getElement(i).endOffset)
                line = self.doc.getText(offsets[0], offsets[1] - offsets[0])
                if self.output.caretPosition >= offsets[0] and \
                 self.output.caretPosition <= offsets[1]:
                    if line.startswith(jiconsole.PROMPT) or line.startswith(
                            jiconsole.PROCESS):
                        self.output.caretPosition = offsets[0] + len(
                            jiconsole.PROMPT)
                    else:
                        self.output.caretPosition = offsets[0]

    def replaceRow(self, text):
        """ Replaces the last line of the textarea with text """
        offset = self.__lastLine()
        last = self.doc.getText(offset[0], offset[1] - offset[0])
        if last != "\n":
            self.doc.remove(offset[0], offset[1] - offset[0] - 1)
        self.__addOutput(self.infoColor, text)

    def delete(self, event):
        """ Intercepts delete events only allowing it to work in the last line """
        if self.inLastLine():
            if self.output.selectedText:
                self.doc.remove(
                    self.output.selectionStart,
                    self.output.selectionEnd - self.output.selectionStart)
            elif self.output.caretPosition < self.doc.length:
                self.doc.remove(self.output.caretPosition, 1)

    def keyReleased(self, event):
        """ Experimental code completion, disabled for now """
        if event.keyChar == '.':
            offsets = self.__lastLine()
            text = self.doc.getText(offsets[0], offsets[1] - offsets[0] - 1)
            #completions = jintrospect.getAutoCompleteList(text, globals())
            completions = 0
            if completions:
                CompletionPopup(self.view, completions, self.output, self)

    def keyTyped(self, event):
        if not self.inLastLine():
            event.consume()
        if self.bs:
            event.consume()
        self.bs = 0

    def keyPressed(self, event):
        if event.keyCode == KeyEvent.VK_BACK_SPACE:
            offsets = self.__lastLine()
            if not self.inLastLine(include=0):
                self.bs = 1
            else:
                self.bs = 0

    def printResult(self, msg):
        """ Prints the results of an operation """
        self.__addOutput(self.output.foreground, "\n" + str(msg))

    def printOnProcess(self):
        """ Prints the process symbol """
        self.__addOutput(self.infoColor, "\n" + jiconsole.PROCESS)

    def printPrompt(self):
        """ Prints the prompt """
        self.__addOutput(self.infoColor, "\n" + jiconsole.PROMPT)

    def printErrorMsg(self, msg, file, line):
        self.__addOutput(self.errorColor, "\n" + str(msg))
        self.__addErrorButton((file, line))

    def printError(self, e):
        import org
        self.__addOutput(self.errorColor, "\n%s" % str(e)[:-1])

        if isinstance(e, org.python.core.PySyntaxError):
            self.__addErrorButton((e.value[1][0], e.value[1][1]))
        elif isinstance(e, org.python.core.PyException):
            import traceback
            self.__addErrorButton((traceback.extract_tb(e.traceback)[-1][0],
                                   traceback.extract_tb(e.traceback)[-1][1]))

    def _completeToken(self, token):
        """ Adds a token to be completed """
        style = SimpleAttributeSet()

        style.addAttribute(StyleConstants.Foreground, self.infoColor)

        self.doc.insertString(self.output.caretPosition, token, style)

    def __addOutput(self, color, msg):
        """ Adds the output to the text area using a given color """
        style = SimpleAttributeSet()

        if color:
            style.addAttribute(StyleConstants.Foreground, color)

        self.doc.insertString(self.doc.length, msg, style)
        self.output.caretPosition = self.doc.length

    def __propertiesChanged(self):
        """ Detects when the properties have changed """
        self.output.background = jEdit.getColorProperty("jython.bgColor")
        self.output.foreground = jEdit.getColorProperty("jython.resultColor")
        self.infoColor = jEdit.getColorProperty("jython.textColor")
        self.errorColor = jEdit.getColorProperty("jython.errorColor")

        family = jEdit.getProperty("jython.font", "Monospaced")
        size = jEdit.getIntegerProperty("jython.fontsize", 14)
        style = jEdit.getIntegerProperty("jython.fontstyle", Font.PLAIN)
        self.output.setFont(Font(family, style, size))

    def __inittext(self):
        """ Inserts the initial text with the jython banner """
        self.doc.remove(0, self.doc.length)
        for line in "\n".join(jiconsole.BANNER):
            self.__addOutput(self.infoColor, line)
        self.printPrompt()
        self.output.requestFocus()

    def __lastLine(self):
        """ Returns the char offsets of the last line """
        lines = self.doc.rootElements[0].elementCount
        offsets = (self.doc.rootElements[0].getElement(lines-1).startOffset, \
        self.doc.rootElements[0].getElement(lines-1).endOffset)
        line = self.doc.getText(offsets[0], offsets[1] - offsets[0])
        if len(line) >= 4 and (line[0:4] == jiconsole.PROMPT
                               or line[0:4] == jiconsole.PROCESS):
            return (offsets[0] + len(jiconsole.PROMPT), offsets[1])
        return offsets

    def __addErrorButton(self, parsed):
        from error import ErrorButton
        if parsed[0] != "<console>":
            self.__addOutput(self.errorColor, "   ")
            self.output.insertComponent(ErrorButton(self.view, parsed))
Example #12
0
 def about(self, event):
     """ Opens the about dialog """
     from jiabout import AboutDialog
     a = AboutDialog(self.view, jEdit.getProperty("jython.about.title"))
     centerDialog(a)
     a.setVisible(1)