public HighlightTextFrame(String title, String text) { super(title); JPanel content = new JPanel(new BorderLayout()); setContentPane(content); hglter = new DefaultHighlighter(); painter = new DefaultHighlightPainter(Color.YELLOW); textArea = new JTextArea(text); textArea.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4)); textArea.setFont(new Font("Courier New", Font.PLAIN, 13)); textArea.setEditable(false); textArea.setLineWrap(false); textArea.setHighlighter(hglter); JScrollPane textAreaScrollPane = new JScrollPane(textArea); content.add(textAreaScrollPane, BorderLayout.CENTER); }
public synchronized void setHighlightedText(final DefaultHighlightPainter painter, final TextLocation startLocation, @SuppressWarnings("unused") final TextLocation endLocation) { final Highlighter highlighter = getHighlighter(); if (currentHighlight != null) { highlighter.removeHighlight(currentHighlight); currentHighlight = null; } if (startLocation == null) { return; } final int line = startLocation.getLine() - 1; if (line >= 0) { try { final int start = getLineStartOffset(line); final int end = getLineEndOffset(line); currentHighlight = highlighter.addHighlight(start, end, painter); } catch (final BadLocationException e) { e.printStackTrace(); } } }
public void setMarkupColor(Bounds bounds, Color color) { // Save the currently selected offsets int start = textCode.getSelectionStart(); int end = textCode.getSelectionEnd(); // Remove selection SwingUtilities.invokeLater(() -> { textCode.setSelectionStart(start); textCode.setSelectionEnd(start); }); Stream.of(highlighter.getHighlights()) // Obtain highlights which are within the requested bounds .filter(h -> Bounds.of(h.getStartOffset(), h.getEndOffset()).collidesWith(bounds)) // For each highlight, we need to remove it and add it again as another color. There is no // way to directly change the color of a highlight once set. .forEach(h -> { Bounds b = Bounds.of(h.getStartOffset(), h.getEndOffset()); // TODO: Race Condition!!! highlightMap is added to from UI Thread SwingUtilities.invokeLater(() -> { highlighter.removeHighlight(h); HighlightPainter hp = new DefaultHighlightPainter(ColorUtils.makeTransparent(color)); try { Highlight h1 = (Highlight) highlighter.addHighlight(b.getStart(), b.getEnd(), hp); highlightMap.put(b, h1); } catch (BadLocationException ex) { ex.printStackTrace(); } }); }); // Restore saved offsets if (start != 0 || end != 0) { SwingUtilities.invokeLater(() -> setSelection(Bounds.of(start, end))); } }
private void reapplyErrorHighlighting() { getHighlighter().removeAllHighlights(); for(HighlightRun run : highlightRuns) { try { getHighlighter().addHighlight(run.start, run.end, new DefaultHighlightPainter(run.highlight)); } catch (BadLocationException e) { e.printStackTrace(); } } }
/** * Creates new form GUIWindow */ public GUIWindow() { initComponents(); this.errorHighlighter = new DefaultHighlightPainter(Color.red); this.fileChooser = new JFileChooser(); this.fileChooser.setFileFilter(new FileNameExtensionFilter("SVG file", "svg", "SVG")); }
/** * Adds the highlight to annotation * * @param a * @param editorPane * @param painter */ private Object addHighlight(Annotation a, JTextArea editorPane, DefaultHighlightPainter painter) { try { return editorPane.getHighlighter().addHighlight( a.getStartNode().getOffset().intValue(), a.getEndNode().getOffset().intValue(), painter); } catch(BadLocationException e) { throw new GateRuntimeException(e); } }
public LockedLinesDisplay(JTextPane pane, LockedLinesHandler lockedLinesHandler) { this.pane = pane; lockedLinesHandler.addLockedLinesListener(this); highlighter = (DefaultHighlighter) pane.getHighlighter(); hPainter = new DefaultHighlightPainter(Color.GRAY); }
/** * Creates a label component, if none has already been created. Sets the * prompt components properties to reflect the given {@link JTextComponent}s * properties and returns it. * * @param txt * @return the adjusted prompt component */ public JTextComponent getPromptComponent(JTextComponent txt) { if (promptComponent == null) { promptComponent = createPromptComponent(); } if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIDE_PROMPT) { promptComponent.setText(null); } else { promptComponent.setText(PromptSupport.getPrompt(txt)); } promptComponent.getHighlighter().removeAllHighlights(); if (txt.isFocusOwner() && PromptSupport.getFocusBehavior(txt) == FocusBehavior.HIGHLIGHT_PROMPT) { promptComponent.setForeground(txt.getSelectedTextColor()); try { promptComponent.getHighlighter().addHighlight(0, promptComponent.getText().length(), new DefaultHighlightPainter(txt.getSelectionColor())); } catch (BadLocationException e) { e.printStackTrace(); } } else { promptComponent.setForeground(PromptSupport.getForeground(txt)); } if (PromptSupport.getFontStyle(txt) == null) { promptComponent.setFont(txt.getFont()); } else { promptComponent.setFont(txt.getFont().deriveFont( PromptSupport.getFontStyle(txt))); } promptComponent.setBackground(PromptSupport.getBackground(txt)); promptComponent.setHighlighter(new PainterHighlighter(PromptSupport .getBackgroundPainter(txt))); promptComponent.setEnabled(txt.isEnabled()); promptComponent.setOpaque(txt.isOpaque()); promptComponent.setBounds(txt.getBounds()); Border b = txt.getBorder(); if (b == null) { promptComponent.setBorder(txt.getBorder()); } else { Insets insets = b.getBorderInsets(txt); promptComponent.setBorder( createEmptyBorder(insets.top, insets.left, insets.bottom, insets.right)); } promptComponent.setSelectedTextColor(txt.getSelectedTextColor()); promptComponent.setSelectionColor(txt.getSelectionColor()); promptComponent.setEditable(txt.isEditable()); promptComponent.setMargin(txt.getMargin()); return promptComponent; }
/** * Populates the file reader tab only if the file exists. */ private void populateFileReaderTab() { log.debug("populateFileReaderTab called"); if (fhxFile == null) { txtFHX.setText(""); txtFHX.getHighlighter().removeAllHighlights(); return; } try { txtFHX.setText(fhxFile.getContentsAsString()); // Try to read file into FHX viewer tab /* * FileReader reader = new FileReader(fhxFile); BufferedReader br = new BufferedReader(reader); if (fhxFile.length() < * FIVE_MEGABYTE_LENGTH) // 5mb { txtFHX.read(br, txtFHX); br.close(); } else { txtFHX.setText("File too large to display"); } */ // If there are errors in the file try to highlight the offensive line txtFHX.getHighlighter().removeAllHighlights(); if (fhxFile.getErrorLine() != null && fhxFile.getErrorLine() <= txtFHX.getLineCount()) { DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW); try { int start = txtFHX.getLineStartOffset(fhxFile.getErrorLine() - 1); int end = txtFHX.getLineStartOffset(fhxFile.getErrorLine()); log.debug("Line number offset to highlight: " + start); txtFHX.getHighlighter().addHighlight(start, end, painter); txtFHX.setCaretPosition(start); } catch (BadLocationException e) { log.error("Unable to move caret to position in file. BadLocationException"); e.printStackTrace(); } } } catch (Exception e2) { log.error("Problems setting up file viewer"); e2.printStackTrace(); } txtFHX.setCaretPosition(0); }