def _highlight_match(self, position, length, color): ''' Changes attributes in text area to show highlighting. ''' attribs = self.match_attribs['black'] StyleConstants.setBackground(attribs, color) self.styledoc.setCharacterAttributes(position, length, attribs, True)
def log(self, msg, color=None, bold=False, end='\n'): msg = str(msg).replace("\n", "\n ") + end doc = self._log.getStyledDocument() aset = SimpleAttributeSet() if color: StyleConstants.setForeground(aset, color) if bold: StyleConstants.setBold(aset, True) doc.insertString(self._log.getDocument().getLength(), msg, aset) self._logButton.setEnabled(True)
def setFontSize(self, size): """ Updates the document to have the provided font size. """ # First, we need to resize all the existing text. attr = SimpleAttributeSet() StyleConstants.setFontSize(attr, size) self.setCharacterAttributes(0, self.getLength(), attr, False) # Next, ensure that new text is given the proper size. for name in self.getStyleNames(): StyleConstants.setFontSize(self.getStyle(name), size)
def __init__(self): """ Line numbers need to be displayed in a separate panel with different styling. """ # Align right para_attribs = SimpleAttributeSet() StyleConstants.setAlignment(para_attribs, StyleConstants.ALIGN_RIGHT) self.setParagraphAttributes(para_attribs, True) # Use default font style default_attribs = SimpleAttributeSet() self.font = set_font() StyleConstants.setFontFamily(default_attribs, self.font.getFamily()) StyleConstants.setFontSize(default_attribs, self.font.getSize()) StyleConstants.setForeground(default_attribs, Color.gray) self.setCharacterAttributes(default_attribs, True) # Initialize content border = BorderFactory.createEmptyBorder(4, 4, 4, 4) self.border = border self.setText("1: \n") self.setEditable(False) # Prevent auto scroll down when line numbers are repainted caret = self.getCaret() caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE)
def __init__(self): """ Line numbers need to be displayed in a separate panel with different styling. """ # Align right para_attribs = SimpleAttributeSet() StyleConstants.setAlignment(para_attribs, StyleConstants.ALIGN_RIGHT) self.setParagraphAttributes(para_attribs, True) # Use default font style default_attribs = SimpleAttributeSet() StyleConstants.setFontFamily(default_attribs, "Monaco") StyleConstants.setFontSize(default_attribs, 14) StyleConstants.setForeground(default_attribs, Color.gray) self.setCharacterAttributes(default_attribs, True) # Initialize content border = BorderFactory.createEmptyBorder(4, 4, 4, 4) self.border = border self.setText("1: \n") self.setEditable(False) # Prevent auto scroll down when line numbers are repainted caret = self.getCaret() caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE)
def get_attribs(color, error=False): ''' Closure to make the generation of font styling cleaner. Note closures need to be defined before being invoked. ''' attribs = SimpleAttributeSet() StyleConstants.setFontFamily(attribs, self.font.getFamily()) StyleConstants.setFontSize(attribs, self.font.getSize()) StyleConstants.setForeground(attribs, Color(*self.colorlut[color])) # Add yellow background to error line styling # White if no error, otherwise it'll keep on being yellow forever if error: StyleConstants.setBackground(attribs, Color.yellow) else: StyleConstants.setBackground(attribs, Color.white) return attribs
def __init__(self): self.textpane = JTextPane() self.doc = self.textpane.getStyledDocument() self.textpane.editable = False style_context = StyleContext.getDefaultStyleContext() default_style = style_context.getStyle(StyleContext.DEFAULT_STYLE) parent_style = self.doc.addStyle("parent", default_style) StyleConstants.setFontFamily(parent_style, "Monospaced") input_style = self.doc.addStyle("input", parent_style) output_style = self.doc.addStyle("output", parent_style) StyleConstants.setForeground(output_style, awtColor.BLUE) error_style = self.doc.addStyle("error", parent_style) StyleConstants.setForeground(error_style, awtColor.RED) # Do a dance to set tab size font = Font("Monospaced", Font.PLAIN, 12) self.textpane.setFont(font) fm = self.textpane.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.textpane.setParagraphAttributes(attr_set, False)
def setTheme(self, themeName): """ Updates all the existing styles, and recolors the command window to match the new styles if any text is present. """ # Pick our fonts! self.defaultFontFamily = \ UIManager.getDefaults().getFont("EditorPane.font").getFamily() self.monoFontFamily = 'Monospaced' # Check that the theme exists if themeName not in THEMES: themeName = DEFAULT_THEME_NAME self.themeName = themeName self.theme = theme = THEMES[themeName] # Set the default style baseStyle = self.getStyle('default') StyleConstants.setBackground(baseStyle, theme.backgroundColor) self._setStyle(baseStyle, theme.defaultStyle) # Set the pretty text styles existingStyles = set(self.getStyleNames()) for name in ALL_STYLES: if name in existingStyles: style = self.getStyle(name) else: style = self.addStyle(name, baseStyle) styleSpec = theme.styles.get(name, (None, None)) styleSpec = ( (theme.defaultStyle[0] if styleSpec[0] is None else styleSpec[0]), (theme.defaultStyle[1] if styleSpec[1] is None else styleSpec[1]) ) self._setStyle(style, styleSpec) self._recolorDocument() self.onThemeSet.send(self)
def setTheme(self, themeName): """ Updates all the existing styles, and recolors the command window to match the new styles if any text is present. """ # Pick our fonts! self.defaultFontFamily = \ UIManager.getDefaults().getFont("EditorPane.font").getFamily() self.monoFontFamily = 'Monospaced' # Check that the theme exists if themeName not in THEMES: themeName = DEFAULT_THEME_NAME self.themeName = themeName self.theme = theme = THEMES[themeName] # Set the default style baseStyle = self.getStyle('default') StyleConstants.setBackground(baseStyle, theme.backgroundColor) self._setStyle(baseStyle, theme.defaultStyle) # Set the pretty text styles existingStyles = set(self.getStyleNames()) for name in ALL_STYLES: if name in existingStyles: style = self.getStyle(name) else: style = self.addStyle(name, baseStyle) styleSpec = theme.styles.get(name, (None, None)) styleSpec = ((theme.defaultStyle[0] if styleSpec[0] is None else styleSpec[0]), (theme.defaultStyle[1] if styleSpec[1] is None else styleSpec[1])) self._setStyle(style, styleSpec) self._recolorDocument() self.onThemeSet.send(self)
def refresh(self): ''' Restyle edit area using user selected appearance settings. ''' config = self.controller.controller.config # Create a new font with the new size font = set_font(config['edit_area_style']['fontsize']['user']) # Update the sytnax highlighter font params, so our changes are not # superceded self.controller.syntax_highlighter.font = font self.controller.syntax_highlighter.setup_attribs() attrs = self.controller.edit_area.getInputAttributes() StyleConstants.setFontSize(attrs, font.getSize()) # Get the Styledoc so we can update it doc = self.controller.edit_area.getStyledDocument() # Apply the new fontsize to the whole document doc.setCharacterAttributes(0, doc.getLength() + 1, attrs, False)
def _setStyle(self, attrSet, styleSpec): flags, color = styleSpec StyleConstants.setForeground(attrSet, color) if flags & MONOSPACE: StyleConstants.setFontFamily(attrSet, self.monoFontFamily) else: StyleConstants.setFontFamily(attrSet, self.defaultFontFamily)
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
def __init__(self): self.textpane = JTextPane() self.doc = self.textpane.getStyledDocument() self.textpane.editable = False default_style = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE) parent_style = self.doc.addStyle("parent", default_style) StyleConstants.setFontFamily(parent_style, "Monospaced") input_style = self.doc.addStyle("input", parent_style) output_style = self.doc.addStyle("output", parent_style) StyleConstants.setForeground(output_style, Color.BLUE) error_style = self.doc.addStyle("error", parent_style) StyleConstants.setForeground(error_style, Color.RED)
def filter2(self, messageContent,styledDoc,style): pattern = '((location\s*[\[.])|([.\[]\s*["\']?\s*(arguments|dialogArguments|innerHTML|write(ln)?|open(Dialog)?|showModalDialog|cookie|URL|documentURI|baseURI|referrer|name|opener|parent|top|content|self|frames)\W)|(localStorage|sessionStorage|Database))|(((src|href|data|location|code|value|action)\s*["\'\]]*\s*\+?\s*=)|((replace|assign|navigate|getResponseHeader|open(Dialog)?|showModalDialog|eval|evaluate|execCommand|execScript|setTimeout|setInterval)\s*["\'\]]*\s*\())|(after\(|\.append\(|\.before\(|\.html\(|\.prepend\(|\.replaceWith\(|\.wrap\(|\.wrapAll\(|\$\(|\.globalEval\(|\.add\(|jQUery\(|\$\(|\.parseHTML\()' compiledPattern = re.compile(pattern) initPos = 0 for find in compiledPattern.finditer(messageContent): StyleConstants.setForeground(style, Color.black) styledDoc.insertString(styledDoc.getLength(),messageContent[initPos:find.start()] , style) StyleConstants.setForeground(style, Color.red) styledDoc.insertString(styledDoc.getLength(),find.group(), style) initPos = find.start()+len(find.group()) StyleConstants.setForeground(style, Color.black) styledDoc.insertString(styledDoc.getLength(),messageContent[initPos:] , style) return
def getUiComponent(self): """Burp uses this method to obtain the component that should be used as the contents of the custom tab when it is displayed. Returns a awt.Component. """ # GUI happens here from javax.swing import (JPanel, JSplitPane, JList, JTextPane, JScrollPane, ListSelectionModel, JLabel, JTabbedPane, JEditorPane) from java.awt import BorderLayout panel = JPanel(BorderLayout()) # create a list and then JList out of it. colors = [ "red", "orange", "yellow", "green", "cyan", "blue", "pink", "magenta", "gray", "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" ] # create a list - the list is not used in this example list1 = JList(colors) list1.selectionMode = ListSelectionModel.SINGLE_SELECTION # create a StyledDocument for tab 1 from javax.swing.text import DefaultStyledDocument doc = DefaultStyledDocument() # create a JTextPane from doc tab1 = JTextPane(doc) tab1.editable = False # we can add more styles # new styles can be a child of previous styles # our first style is a child of the default style from javax.swing.text import StyleContext, StyleConstants defaultStyle = StyleContext.getDefaultStyleContext().getStyle( StyleContext.DEFAULT_STYLE) # returns a Style regular = doc.addStyle("regular", defaultStyle) StyleConstants.setFontFamily(defaultStyle, "Times New Roman") # make different styles from regular style1 = doc.addStyle("italic", regular) StyleConstants.setItalic(style1, True) style1 = doc.addStyle("bold", regular) StyleConstants.setBold(style1, True) style1 = doc.addStyle("small", regular) StyleConstants.setFontSize(style1, 10) style1 = doc.addStyle("large", regular) StyleConstants.setFontSize(style1, 16) # insert text doc.insertString(doc.length, "This is regular\n", doc.getStyle("regular")) doc.insertString(doc.length, "This is italic\n", doc.getStyle("italic")) doc.insertString(doc.length, "This is bold\n", doc.getStyle("bold")) doc.insertString(doc.length, "This is small\n", doc.getStyle("small")) doc.insertString(doc.length, "This is large\n", doc.getStyle("large")) # create the tabbedpane tabs = JTabbedPane() tabs.addTab("Tab 1", tab1) # create splitpane - horizontal split spl = JSplitPane(JSplitPane.HORIZONTAL_SPLIT, JScrollPane(list1), tabs) panel.add(spl) return panel
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)
def get_attribs(color, error=False, match=False): ''' Closure to make the generation of font styling cleaner. Note closures need to be defined before being invoked. ''' attribs = SimpleAttributeSet() StyleConstants.setFontFamily(attribs, self.font.getFamily()) StyleConstants.setFontSize(attribs, self.font.getSize()) StyleConstants.setForeground(attribs, Color(*self.colorlut[color])) # Add yellow background to error line styling # White if no error, otherwise it'll keep on being yellow forever if error: StyleConstants.setBackground(attribs, Color.yellow) elif match: # TODO: Change to another background colour Eleanor likes StyleConstants.setBackground(attribs, Color.yellow) else: StyleConstants.setBackground(attribs, Color.white) return attribs