def set_advisory_tab_pane(self, scanner_issue): advisory_pane = JEditorPane() advisory_pane.setEditable(False) advisory_pane.setEnabled(True) advisory_pane.setContentType("text/html") link_listener = LinkListener() advisory_pane.addHyperlinkListener(link_listener) advisory = "<html><b>Location</b>: {}<br><br>{}</html>" advisory_pane.setText( advisory.format(scanner_issue.getUrl().encode("utf-8"), scanner_issue.getIssueDetail())) return JScrollPane(advisory_pane)
def set_advisory_tab_pane(self, scanner_issue): advisory_pane = JEditorPane() advisory_pane.setEditable(False) advisory_pane.setEnabled(True) advisory_pane.setContentType("text/html") link_listener = LinkListener() advisory_pane.addHyperlinkListener(link_listener) fmt = "<html><b>Location</b>: {}<br><br>{}</html>" advisory_pane.setText(fmt.format(scanner_issue.getUrl(), scanner_issue.getIssueDetail())) # Set a context menu self.set_context_menu(advisory_pane, scanner_issue) return JScrollPane(advisory_pane)
class HelpPanel(JPanel): def __init__(self): JPanel.__init__(self,BorderLayout()) self.editor=JEditorPane() self.editor.setEditable(0) #self.editor.setContentType('text/html') self.editor.setContentType('text/html') sp=JScrollPane(self.editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) self.add(sp,'Center') #html="<html><h1>Test</h1></html>" #self.editor.setText(html) #self.lines='HELP' self.editor.setText(HELP_TEXT) def append(self,line): self.lines="%s\n%s"%(self.lines,line) self.editor.setText(self.lines)
class DebugPanel(JPanel): def __init__(self): JPanel.__init__(self,BorderLayout()) self.editor=JEditorPane() self.editor.setEditable(0) #self.editor.setContentType('text/html') self.editor.setContentType('text/plain') sp=JScrollPane(self.editor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED) self.add(sp,'Center') #html="<html><h1>Test</h1></html>" #self.editor.setText(html) self.lines='DEBUG OUTPUT:' self.editor.setText(self.lines) def append(self,line): try: self.lines="%s\n%s"%(self.lines,line) self.editor.setText(self.lines) except Exception,e: print e
class ConsoleView(JPanel): ''' Initializes the console view and sets its layout. ''' def __init__(self, controller): ''' Creates default empty console-looking panel. It should be separated from the rest of the GUI so that users can choose to show or hide the console. Or should it be a split panel? This panel will display log and validation/lemmatization messages. It might need its own toolbar for searching, etc. It will also accept commands in later stages of development, if need be. ''' # Give reference to controller to delegate action response self.controller = controller # Make text area occupy all available space and resize with parent # window self.setLayout(BorderLayout()) # Create console-looking area self.edit_area = JEditorPane() # Although most of the styling is done using css, we need to set these # properties to ensure the html is rendered properly in the console self.edit_area.border = BorderFactory.createEmptyBorder(6, 6, 6, 6) self.edit_area.setContentType("text/html") # Disable writing in the console - required to render hyperlinks self.edit_area.setEditable(False) # Map CSS color strings to Java Color objects self.colors = {'Gray': Color(238, 238, 238), 'Black': Color(0, 0, 0), 'Yellow': Color(255, 255, 0)} # Initial call to refresh console to set the console style properties self.refreshConsole() # Set up a hyperlink listener listener = addEventListener(self.edit_area, HyperlinkListener, 'hyperlinkUpdate', self.handleEvent) # Will need scrolling controls scrollingText = JScrollPane(self.edit_area) scrollingText.setPreferredSize(Dimension(1, 150)) # Make text area auto scroll down to last printed line caret = self.edit_area.getCaret() caret.setUpdatePolicy(DefaultCaret.ALWAYS_UPDATE) # Add to parent panel self.add(scrollingText, BorderLayout.CENTER) def refreshConsole(self): ''' Restyle console using CSS with user selected appearance settings. ''' fontsize = self.controller.config['console_style']['fontsize']['user'] background_color = self.controller.config[ 'console_style']['background_color']['user'] font_color = self.controller.config[ 'console_style']['font_color']['user'] bodyRule = ("body {{ font-family: Monaco; font-size: {0} pt; " "font-weight: bold; color: {1} }}").format(fontsize, font_color) # Set font properties doc = self.edit_area.getDocument() doc.getStyleSheet().addRule(bodyRule) # Set background color self.edit_area.background = self.colors[background_color] self.edit_area.repaint() def scroll(self): ''' Scroll down to bottom. ''' length = self.edit_area.getDocument().getLength() self.edit_area.setCaretPosition(length) def handleEvent(self, event): ''' A simple event handler for clicked hyperlinks. ''' if event.getEventType() is EventType.ACTIVATED: atfCont = self.controller.controller.atfAreaController error_line = int(event.getDescription()) text = atfCont.getAtfAreaText() pos = atfCont.getPositionFromLine(text, error_line) # pos gives the position of the final character on the previous # line, so add 1 char to move the caret to the start of error_line # The method is called twice to catch the edge case where the user # has the caret in the correct location prior to the click # resulting in the screen not scrolling to the error line. # This would be done with some logic around getCaretPosition(), but # this would need a caret listener to be constructed. for i in xrange(2): atfCont.setCaretPosition(pos + i) # Return focus to the editor window atfCont.edit_area.requestFocusInWindow()