@Override public void caretUpdate(CaretEvent e) { int cursor = e.getDot(); JTextPane textPane = (JTextPane)e.getSource(); TokenPositionAnalysis analysis = getAnalysisForCharIndex(cursor); Highlighter highlighter = textPane.getHighlighter(); HighlightPainter painter = new DefaultHighlightPainter(Color.orange); try { highlighter.removeAllHighlights(); if ( analysis!=null ) { highlighter.addHighlight(analysis.charIndexStart, analysis.charIndexStop+1, painter); } scope.injectNLConsole.setText(analysis!=null ? analysis.wsAnalysis : ""); scope.injectNLConsole.setCaretPosition(0); scope.alignConsole.setText(analysis!=null ? analysis.alignAnalysis : ""); scope.alignConsole.setCaretPosition(0); } catch (Exception ex) { ex.printStackTrace(System.err); } }
public void setCSVFileHighlight(final int number) { if (logger.isTraceEnabled()) { logger.trace("setCSVFileHighlight()"); } try { csvFileTextArea.getHighlighter().removeAllHighlights(); if (number >= 0) { final int lineStart = csvFileTextArea.getLineStartOffset(number); final int lineEnd = csvFileTextArea.getLineEndOffset(csvFileRowCount-1); final HighlightPainter painter = new DefaultHighlighter. DefaultHighlightPainter( Constants.DEFAULT_HIGHLIGHT_COLOR); csvFileTextArea.getHighlighter().addHighlight(lineStart, lineEnd, painter); } } catch (final BadLocationException e1) { logger.error("Exception thrown: " + e1.getMessage(), e1); } }
public List<Highlight> getParameterHighlights() { List<Highlight> paramHighlights = new ArrayList<Highlight>(2); JTextComponent tc = ac.getTextComponent(); Highlight[] highlights = tc.getHighlighter().getHighlights(); for (int i=0; i<highlights.length; i++) { HighlightPainter painter = highlights[i].getPainter(); if (painter==p || painter==endingP) { paramHighlights.add(highlights[i]); } } return paramHighlights; }
private void jMenuItem60ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem60ActionPerformed try { int selectedIndex = jTabbedPane1.getSelectedIndex(); RSyntaxTextArea textPane = (RSyntaxTextArea) Editor.get(selectedIndex).getTextPane(); Highlighter h = textPane.getHighlighter(); String select = textPane.getSelectedText();String context = textPane.getText(); String words[] = select.split(" "); for (int i = 0 ; i < words.length; i++) { String temp = words[i]; if (temp.equals(select)){ h.addHighlight(i, temp.length(), (HighlightPainter) Color.RED);} }} catch (BadLocationException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
public void setHighlightedToken(Token token, HighlightPainter painter) { try { getHighlighter().addHighlight(token.start, token.end, painter); } catch (BadLocationException ex) { throw new IllegalArgumentException(ex); } }
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))); } }
public List<Highlight> getParameterHighlights() { List<Highlight> paramHighlights = new ArrayList<Highlight>(2); JTextComponent tc = ac.getTextComponent(); Highlight[] highlights = tc.getHighlighter().getHighlights(); for (int i = 0; i < highlights.length; i++) { HighlightPainter painter = highlights[i].getPainter(); if (!isGftErrorHighlighting(painter) && (painter == p || painter == endingP)) paramHighlights.add(highlights[i]); } return paramHighlights; }
public void setHighlightedToken(Token token, HighlightPainter painter) { try { getHighlighter().addHighlight(token.start, token.end, painter); }catch(BadLocationException ex) { throw new IllegalArgumentException(ex); } }
public void highlite(String toFind,int rangeStart,int rangeEnd)throws Exception{ HighlightPainter myHighlighter = new MyHighlightPainter(Color.RED); int index = textarea.getText(rangeStart, rangeEnd-rangeStart).indexOf(toFind); textarea.setCaretPosition(0); textarea.setCaretPosition(rangeStart); Highlighter hilite = textarea.getHighlighter(); hilite.removeAllHighlights(); hilite.addHighlight(rangeStart+index, rangeStart+index+toFind.length(), myHighlighter); hilite.paint(textarea.getGraphics());}
public void setHighlightedTokens(Iterable<Token> tokens, HighlightPainter painter) { for (Token token : tokens) { setHighlightedToken(token, painter); } }
/** This reapplies highlights and AttributeSets to this text component. * * @param text the text to format. * @param selectionStart the current selection start. * @param selectionEnd the current selection end. */ protected void rehighlightText(String text,int selectionStart,int selectionEnd) { for(Object oldHighlight : allHighlights) { jtc.getHighlighter().removeHighlight(oldHighlight); } allHighlights.clear(); Token[] tokens = getTokens(text, true); final Map<Token, AttributeSet> tokenAttributes = new TreeMap<Token, AttributeSet>(); Runnable changeStylesRunnable = new Runnable() { public void run() { removeDocumentListeners(); try { Document doc = jtc.getDocument(); if(! (doc instanceof StyledDocument)) { printOnce("TextComponentHighlighter: Attributes were provided but the document does not support styled attributes."); return; } SimpleAttributeSet defaultAttributes = getDefaultAttributes(); StyledDocument d = (StyledDocument)doc; d.setCharacterAttributes(0, d.getLength(), defaultAttributes, true); if(active) { for(Token token : tokenAttributes.keySet()) { AttributeSet attr = tokenAttributes.get(token); d.setCharacterAttributes(token.getDocumentStartIndex(), token.getLength(), attr, true); } } } finally { addDocumentListeners(); } } }; try { if(active) { for(int index = 0; index<tokens.length; index++) { HighlightPainter painter = getHighlightPainter(tokens, index, selectionStart, selectionEnd); if(painter!=null) { allHighlights.add( jtc.getHighlighter().addHighlight( tokens[index].getDocumentStartIndex(), tokens[index].getDocumentEndIndex(), painter) ); } AttributeSet attributes = getAttributes(tokens, index, selectionStart, selectionEnd); if(attributes!=null) { tokenAttributes.put(tokens[index], attributes); } } } SwingUtilities.invokeLater(changeStylesRunnable); } catch(BadLocationException e) { throw new RuntimeException(e); } }
/** @return the HighlightPainter for tokenIndex in the list of tokens. */ protected HighlightPainter getHighlightPainter(Token[] allToken,int tokenIndex,int selectionStart,int selectionEnd) { return null; }
private boolean isGftErrorHighlighting(HighlightPainter painter) { return painter instanceof UnderlineHighlightPainter; }
public void setHighlightedTokens(Iterable<Token> tokens, HighlightPainter painter) { for(Token token : tokens) setHighlightedToken(token, painter); }
@Override public HighlightPainter linkPainter() { return underLinePainter; }
protected HighlightPainter getSelectionPainter() { InstrumentedUILog .add(new Object[] { "DefaultCaret.getSelectionPainter" }); return super.getSelectionPainter(); }
private void SetPaneStyle(){ painters = new HashMap<String,HighlightPainter>(); painters.put("[ERROR]", new DefaultHighlighter.DefaultHighlightPainter(Color.red)); painters.put("[WARN]", new DefaultHighlighter.DefaultHighlightPainter(Color.orange)); painters.put("[INFO]", new DefaultHighlighter.DefaultHighlightPainter(Color.green)); painters.put("[CLIPS]", new DefaultHighlighter.DefaultHighlightPainter(Color.blue)); painters.put("[INTERNAL]", new DefaultHighlighter.DefaultHighlightPainter(Color.cyan)); }