Ejemplo n.º 1
0
    def __init__(self):
        self.component = NoWrapJTextPane(
            border=BorderFactory.createEmptyBorder(5, 5, 5, 5)
        )
        
        def new_style(name, color=None, bold=None, italic=None, underline=None):
            style = self.doc.addStyle(name, self.parent_style)
            if color is not None:
                if isinstance(color, str):
                    color = awtColor(
                        int(color[0:2], 16),
                        int(color[2:4], 16),
                        int(color[4:6], 16)
                    )
                StyleConstants.setForeground(style, color)
            if bold is not None:
                StyleConstants.setBold(style, bold)
            if italic is not None:
                StyleConstants.setItalic(style, italic)
            if underline is not None:
                StyleConstants.setUnderline(style, underline)
            return style
        
        self.doc = self.component.getStyledDocument()

        attrs = SimpleAttributeSet()
        StyleConstants.setLineSpacing(attrs, 0.2)
        self.component.setParagraphAttributes(attrs, True)
        
        style_context = StyleContext.getDefaultStyleContext()
        default_style = style_context.getStyle(StyleContext.DEFAULT_STYLE)
        self.parent_style = self.doc.addStyle("parent", default_style)
        StyleConstants.setFontFamily(self.parent_style, "Monospaced")

        # Set styles for syntax highlighting
        new_style("kw", "990066", bold=True)
        new_style("number", "0033AA")
        new_style("string", "993300")
        new_style("comment", "FF0000")
        new_style("defname", "0033FF", bold=True)
        new_style("classname", "009900", bold=True)
        new_style("builtin", italic=True)
        new_style("geo", underline=True)
        new_style("decorator", "0033FF")
        
        # Do a dance to set tab size
        font = Font("Monospaced", Font.PLAIN, 12)
        self.component.setFont(font)
        fm = self.component.getFontMetrics(font)
        tabw = float(fm.stringWidth(" "*4))
        tabs = [
            TabStop(tabw*i, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE)
            for i in xrange(1, 51)
        ]
        attr_set = style_context.addAttribute(
            SimpleAttributeSet.EMPTY,
            StyleConstants.TabSet,
            TabSet(tabs)
        )
        self.component.setParagraphAttributes(attr_set, False)
        #Dance done!
        
        self.component.addKeyListener(self)
        # Remove?
        # self.nocheck = LockManager()
        self.doc.addDocumentListener(self)