Java Code Examples for javax.swing.text.Highlighter#addHighlight()

The following examples show how to use javax.swing.text.Highlighter#addHighlight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 2
Source File: CheckAttributedTree.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: CheckAttributedTree.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
public static void setHighlight(JTextComponent jtc, String pattern) {
  Highlighter highlighter = jtc.getHighlighter();
  highlighter.removeAllHighlights();
  Document doc = jtc.getDocument();
  try {
    String text = doc.getText(0, doc.getLength());
    Matcher matcher = Pattern.compile(pattern).matcher(text);
    int pos = 0;
    while (matcher.find(pos) && !matcher.group().isEmpty()) {
      int start = matcher.start();
      int end = matcher.end();
      highlighter.addHighlight(start, end, HIGHLIGHT);
      pos = end;
    }
  } catch (BadLocationException | PatternSyntaxException ex) {
    UIManager.getLookAndFeel().provideErrorFeedback(jtc);
  }
}
 
Example 5
Source File: MatchPanel.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
private void update() {
    if (regexModel.getRegex() == null || regexModel.getTestValue() == null) {
        return;
    }

    RegexTestInput input = new RegexTestInput(regexModel.getRegex(), regexModel.getTestValue());
    RegexTestResult result = RegexTester.test(input);

    matchField.setText(String.format(MATCHES_LABEL, result.isMatch()));
    lookingAtField.setText(String.format(LOOKING_AT_LABEL, result.isLookingAt()));
    resultField.setText(result.getResult());
    captureField.setText(result.getCapture());
    captureField.setCaretPosition(0);

    Highlighter highlighter = resultField.getHighlighter();
    for (RegexTestResult.Group group : result.getGroups()) {
        try {
            highlighter.addHighlight(group.getStart(), group.getEnd(), highlightPainter);
        } catch (BadLocationException ignore) {
        }
    }
}
 
Example 6
Source File: CheckAttributedTree.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 8
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 9
Source File: PeriodicTasksService.java    From Open-LaTeX-Studio with MIT License 6 votes vote down vote up
private void spellCheckAllText() throws BadLocationException {   
    Document doc = etc.getrSyntaxTextArea().getDocument();   
    if (doc != null) {                                                
        String editorText = etc.getrSyntaxTextArea().getText(0, doc.getLength());            
        if (editorText != null) {  
            Highlighter highlighter = etc.getrSyntaxTextArea().getHighlighter();                                      
            if(etc.getEditorState().isSpellCheckActive()) {  
                List<RuleMatch> matches = null;  
                try {
                    matches = etc.getLangTool().check(editorText);
                } catch (IOException ex) {
                    Exceptions.printStackTrace(ex);
                }

                highlighter.removeAllHighlights();
                //Highlight the spelling check results
                for (RuleMatch match : matches) {
                    highlighter.addHighlight(match.getFromPos(), match.getToPos(), etc.getPainter());   
                }                        
            } else {  
                highlighter.removeAllHighlights();
            }
        }
    }        
}
 
Example 10
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 11
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 12
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 13
Source File: CheckAttributedTree.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: TreePosTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** Add a highlighted region based on the positions in an Info object. */
private void addHighlight(Highlighter h, Info info, Color c) {
    int start = info.start;
    int end = info.end;
    if (start == -1 && end == -1)
        return;
    if (start == -1)
        start = end;
    if (end == -1)
        end = start;
    try {
        h.addHighlight(info.start, info.end,
                new DefaultHighlighter.DefaultHighlightPainter(c));
        if (info.pos != -1) {
            Color c2 = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int)(.4f * 255)); // 40%
            h.addHighlight(info.pos, info.pos + 1,
                new DefaultHighlighter.DefaultHighlightPainter(c2));
        }
    } catch (BadLocationException e) {
        e.printStackTrace();
    }
}
 
Example 15
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 16
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 17
Source File: DecompilerPanel.java    From Cafebabe with GNU General Public License v3.0 5 votes vote down vote up
private void hightlightText(String searchText) throws BadLocationException {
	Highlighter highlighter = dp.getHighlighter();
	highlighter.removeAllHighlights();
	Document document = dp.getDocument();
	String text = document.getText(0, document.getLength()).toLowerCase();
	int pos = text.indexOf(searchText);
	while (pos >= 0) {
		highlighter.addHighlight(pos, pos + searchText.length(),
				(Highlighter.HighlightPainter) new DefaultHighlighter.DefaultHighlightPainter(Colors.highlightColor));
		pos = text.indexOf(searchText, pos + searchText.length());
	}
}
 
Example 18
Source File: MainPanel.java    From java-swing-tips with MIT License 5 votes vote down vote up
protected void addHighlight(Element element, boolean isBlock) {
  Highlighter highlighter = editorPane.getHighlighter();
  int start = element.getStartOffset();
  int lf = isBlock ? 1 : 0;
  int end = element.getEndOffset() - lf; // lf???, setDrawsLayeredHighlights(false) bug???
  try {
    highlighter.addHighlight(start, end, HIGHLIGHT);
  } catch (BadLocationException ex) {
    // should never happen
    RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
    wrap.initCause(ex);
    throw wrap;
  }
}
 
Example 19
Source File: ColorPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void install(JTextComponent component, boolean listener) {
  try {
    Highlighter highlighter = component.getHighlighter();
    if (highlighter != null) highlighter.addHighlight(0, 0, this);
  }
  catch (BadLocationException ignored) {
  }
  if (listener) {
    component.addPropertyChangeListener(PROPERTY, this);
  }
}
 
Example 20
Source File: FindSupport.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean findNext(Component comp) {
    if (comp == bar) {
        return false;
    }
    if (comp instanceof JTextPane) {
        if (currentComp == null || currentComp == comp) {
            if(!comp.isVisible()) {
                return false;
            }
            JTextPane tcomp = (JTextPane)comp;
            String txt = tcomp.getText();
            Matcher matcher = pattern.matcher(txt);
            int idx = (currentComp==null) ? 0 : currentEnd;
            if (matcher.find(idx)) {
                currentComp = tcomp;
                currentStart = matcher.start();
                currentEnd = matcher.end();
                if (currentStart == currentEnd) {
                    currentComp = null;
                } else {
                    try {
                        Highlighter highlighter = tcomp.getHighlighter();
                        highlighter.addHighlight(currentStart, currentEnd, highlighterCurrent);
                        scrollToCurrent();
                    } catch (BadLocationException blex) {
                        BugtrackingManager.LOG.log(Level.INFO, blex.getMessage(), blex);
                    }
                    return true;
                }
            } else {
                currentComp = null;
            }
        }
    } else if (comp instanceof Container) {
        Container cont = (Container)comp;
        for (Component subComp : cont.getComponents()) {
            if (findNext(subComp)) {
                return true;
            }
        }
    }
    return false;
}