Example usage for javax.swing.text Highlighter addHighlight

List of usage examples for javax.swing.text Highlighter addHighlight

Introduction

In this page you can find the example usage for javax.swing.text Highlighter addHighlight.

Prototype

public Object addHighlight(int p0, int p1, HighlightPainter p) throws BadLocationException;

Source Link

Document

Adds a highlight to the view.

Usage

From source file:logdruid.ui.table.StatRecordingEditorTable.java

public void FixValues() {
    String patternString = "";
    Matcher matcher;/*from  w  w w  .  j  a va2s.c  o  m*/
    PatternCache patternCache = new PatternCache();
    Iterator it = data.iterator();
    Object[] obj;

    while (it.hasNext()) {
        obj = (Object[]) it.next();
        String stBefore = (String) obj[1];
        String stType = (String) obj[2];
        String stAfter = (String) obj[3];
        logger.info("stType: " + stType);
        if (stType.equals("date") && rep.getDateFormat(recording.getDateFormatID()).getPattern() != null) {
            patternString += stBefore + "(" + rep.getDateFormat(recording.getDateFormatID()).getPattern() + ")"
                    + stAfter;
            logger.info("getTypeString(stType) getPattern -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getPattern());
            logger.info("getTypeString(stType) getDateFormat -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getDateFormat());
        } else {
            patternString += stBefore + "(" + DataMiner.getTypeString(stType) + ")" + stAfter;
            logger.info("getTypeString(stType) -: " + DataMiner.getTypeString(stType));
        }
    }

    try {
        logger.info("theLine: " + examplePane.getText());
        logger.info("patternString: " + patternString);

        Highlighter h = examplePane.getHighlighter();
        h.removeAllHighlights();
        int currIndex = 0;

        String[] lines = examplePane.getText().split(System.getProperty("line.separator"));
        if (lines.length >= 1) {
            for (int i = 0; i < lines.length; i++) {
                matcher = patternCache.getPattern(patternString).matcher(lines[i]);
                if (matcher.find()) {
                    // int currIndex = 0;
                    // doc.insertString(doc.getLength(),line+"\n", null);

                    for (int i2 = 1; i2 <= matcher.groupCount(); i2++) {
                        model.setValueAt(matcher.group(i2), i2 - 1, 5);
                        h.addHighlight(currIndex + matcher.start(i2), +currIndex + matcher.end(i2),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE));
                    }

                }
                currIndex += lines[i].length() + 1;
            }
        }

    } catch (Exception e1) {
        e1.printStackTrace();
        // System.exit(1);
    }

}

From source file:logdruid.ui.table.EventRecordingEditorTable.java

public void FixValues() {
    String patternString = "";
    Matcher matcher = null;/*from  w w w  .j  a v  a2 s. co  m*/
    PatternCache patternCache = new PatternCache();
    Iterator it = data.iterator();
    Object[] obj;

    while (it.hasNext()) {
        obj = (Object[]) it.next();
        String stBefore = (String) obj[1];
        String stType = (String) obj[2];
        String stAfter = (String) obj[4];
        logger.info("stType: " + stType);
        if (stType.equals("date") && rep.getDateFormat(recording.getDateFormatID()).getPattern() != null) {
            patternString += stBefore + "(" + rep.getDateFormat(recording.getDateFormatID()).getPattern() + ")"
                    + stAfter;
            logger.info("getTypeString(stType) getPattern -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getPattern());
            logger.info("getTypeString(stType) getDateFormat -: "
                    + rep.getDateFormat(recording.getDateFormatID()).getDateFormat());
        } else {
            patternString += stBefore + "(" + DataMiner.getTypeString(stType) + ")" + stAfter;
            logger.info("getTypeString(stType) -: " + DataMiner.getTypeString(stType));
        }
    }

    try {
        logger.info("theLine: " + examplePane.getText());
        logger.info("patternString: " + patternString);
        Highlighter h = examplePane.getHighlighter();
        h.removeAllHighlights();
        int currIndex = 0;

        String[] lines = examplePane.getText().split(System.getProperty("line.separator"));
        if (lines.length >= 1) {
            for (int i = 0; i < lines.length; i++) {
                matcher = patternCache.getPattern(patternString).matcher(lines[i]);
                if (matcher.find()) {
                    // int currIndex = 0;
                    // doc.insertString(doc.getLength(),line+"\n", null);

                    for (int i2 = 1; i2 <= matcher.groupCount(); i2++) {
                        model.setValueAt(matcher.group(i2), i2 - 1, 6);
                        h.addHighlight(currIndex + matcher.start(i2), +currIndex + matcher.end(i2),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.ORANGE));
                    }
                }
                logger.info("currIndex: " + currIndex + "matcher.end(i2): " + lines[i].length() + ",l: "
                        + lines[i]);
                currIndex += lines[i].length() + 1;
            }
        }

    } catch (Exception e1) {
        e1.printStackTrace();
        // System.exit(1);
    }

}

From source file:com.petersoft.advancedswing.enhancedtextarea.EnhancedTextArea.java

private void jSearchTextFieldKeyReleased(KeyEvent evt) {
    String text = jTextArea.getText().toLowerCase();
    String searchPattern = jSearchTextField.getText().toLowerCase();

    if (evt != null && evt.getKeyCode() == 10) {
        int caretPosition = jTextArea.getCaretPosition();
        boolean found = false;
        for (int j = caretPosition + 1; j < text.length() - searchPattern.length(); j += 1) {
            if (searchPattern.equals(text.substring(j, j + searchPattern.length()))) {
                jTextArea.setCaretPosition(j);
                found = true;//from www.  j a v a2 s .  com
                break;
            }
        }
        if (!found) {
            for (int j = 0; j < caretPosition; j++) {
                if (searchPattern.equals(text.substring(j, j + searchPattern.length()))) {
                    jTextArea.setCaretPosition(j);
                    break;
                }
            }
        }
    }

    if (searchPattern.length() > 0) {
        Highlighter h = jTextArea.getHighlighter();
        DefaultHighlightPainter painter = new DefaultHighlightPainter(Color.YELLOW);
        DefaultHighlightPainter painter2 = new DefaultHighlightPainter(Color.RED);
        h.removeAllHighlights();

        int count = 0;
        boolean isCurrent = false;
        for (int j = 0; j < text.length(); j += 1) {
            if (j < text.length() - searchPattern.length()
                    && searchPattern.equals(text.substring(j, j + searchPattern.length()))) {
                count++;
                try {
                    if (j >= jTextArea.getCaretPosition() && isCurrent == false) {
                        h.addHighlight(j, j + searchPattern.length(), painter2);
                        isCurrent = true;
                    } else {
                        h.addHighlight(j, j + searchPattern.length(), painter);
                    }
                } catch (BadLocationException ble) {
                }
            }
        }
        jSearchLabel.setText("Match:" + count);
    } else {
        jSearchLabel.setText("");
        Highlighter h = jTextArea.getHighlighter();
        h.removeAllHighlights();
    }
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jSearchObjdumpButtonActionPerformed(ActionEvent evt) {
    if (jTextField1.getText().length() > 0) {
        Highlighter h = jEditorPane1.getHighlighter();
        h.removeAllHighlights();// ww  w. j av a 2 s .c o m
        String text = jEditorPane1.getText().toLowerCase();

        int nextPosition = -1;

        for (int j = 0; j < text.length() - jTextField1.getText().length() + 1; j += 1) {
            if (text.substring(j, j + jTextField1.getText().length())
                    .equals(jTextField1.getText().toLowerCase())) {
                try {
                    if (j >= jEditorPane1.getCaretPosition() && nextPosition == -1) {
                        h.addHighlight(j, j + jTextField1.getText().length(),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.red));
                        nextPosition = j + jTextField1.getText().length();
                    } else {
                        h.addHighlight(j, j + jTextField1.getText().length(),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
                    }
                } catch (BadLocationException ble) {
                }
            }
        }
        if (nextPosition != -1) {
            jEditorPane1.setCaretPosition(nextPosition);
        } else {
            jEditorPane1.setCaretPosition(0);
        }
    }
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jSearchRelPltButtonActionPerformed(ActionEvent evt) {
    if (jSearchRelPltTextField.getText().length() > 0) {
        Highlighter h = jSearchRelPltEditorPane.getHighlighter();
        h.removeAllHighlights();/*  w  w  w.jav  a 2  s . c o m*/
        String text = jSearchRelPltEditorPane.getText().toLowerCase();

        int nextPosition = -1;

        for (int j = 0; j < text.length() - jSearchRelPltTextField.getText().length() + 1; j += 1) {
            if (text.substring(j, j + jSearchRelPltTextField.getText().length())
                    .equals(jSearchRelPltTextField.getText().toLowerCase())) {
                try {
                    if (j >= jSearchRelPltEditorPane.getCaretPosition() && nextPosition == -1) {
                        h.addHighlight(j, j + jSearchRelPltTextField.getText().length(),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.red));
                        nextPosition = j + jSearchRelPltTextField.getText().length();
                    } else {
                        h.addHighlight(j, j + jSearchRelPltTextField.getText().length(),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
                    }
                } catch (BadLocationException ble) {
                }
            }
        }
        if (nextPosition != -1) {
            jSearchRelPltEditorPane.setCaretPosition(nextPosition);
        } else {
            jSearchRelPltEditorPane.setCaretPosition(0);
        }
    }
}

From source file:com.peterbochs.PeterBochsDebugger.java

private void jSearchDynamicButtonActionPerformed(ActionEvent evt) {
    if (jSearchDynamicTextField.getText().length() > 0) {
        Highlighter h = jSearchDynamicEditorPane.getHighlighter();
        h.removeAllHighlights();//from w w  w  .  j a v  a  2  s  .c o m
        String text = jSearchDynamicEditorPane.getText().toLowerCase();

        int nextPosition = -1;

        for (int j = 0; j < text.length() - jSearchDynamicTextField.getText().length() + 1; j += 1) {
            if (text.substring(j, j + jSearchDynamicTextField.getText().length())
                    .equals(jSearchDynamicTextField.getText().toLowerCase())) {
                try {
                    if (j >= jSearchDynamicEditorPane.getCaretPosition() && nextPosition == -1) {
                        h.addHighlight(j, j + jSearchDynamicTextField.getText().length(),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.red));
                        nextPosition = j + jSearchDynamicTextField.getText().length();
                    } else {
                        h.addHighlight(j, j + jSearchDynamicTextField.getText().length(),
                                new DefaultHighlighter.DefaultHighlightPainter(Color.yellow));
                    }
                } catch (BadLocationException ble) {
                }
            }
        }
        if (nextPosition != -1) {
            jSearchDynamicEditorPane.setCaretPosition(nextPosition);
        } else {
            jSearchDynamicEditorPane.setCaretPosition(0);
        }
    }
}

From source file:org.apache.cayenne.swing.components.textpane.JCayenneTextPane.java

public void setHighlightText(int lastIndex, int endIndex) throws BadLocationException {
    Highlighter highlighter = pane.getHighlighter();
    removeHighlightText(highlighter);//from   w  w w  .  j a v  a 2  s .  com
    highlighter.addHighlight(lastIndex, endIndex, painter);
}

From source file:org.apache.cayenne.swing.components.textpane.JCayenneTextPane.java

/**
 * set underlines text in JCayenneTextPane
 * //from w  w w  .  j ava  2s  .  c  o  m
 * @param int line - starting line for underlined text
 * @param int lastIndex - starting position in line for underlined text
 * @param int size
 * @param String message - text for toolTip, contains the text of the error
 */

public void setHighlightText(int line, int lastIndex, int size, String message) {
    Highlighter highlighter = pane.getHighlighter();
    removeHighlightText(highlighter);
    if (getText().length() > 0) {
        try {
            int position = getPosition(line, lastIndex);
            int positionEnd = position + size;
            highlighter.addHighlight(position, positionEnd, painter);
            setToolTipPosition(line, message);
            repaintPane();
        } catch (BadLocationException e) {
            logObj.warn("Error: ", e);
        }
    } else {
        setToolTipPosition(0, "");
    }
}

From source file:org.languagetool.gui.LanguageToolSupport.java

private void updateHighlights() {
    removeHighlights();//from  ww  w. jav a  2 s . co  m

    Highlighter h = textComponent.getHighlighter();

    for (Span span : documentSpans) {
        if (span.start == span.end) {
            continue;
        }
        try {
            if (span.start < span.end) { //to avoid the BadLocationException
                ITSIssueType issueType = span.rule.getLocQualityIssueType();
                Color ulColor = config.getUnderlineColor(span.rule.getCategory().getName());
                Color colorForIssueType = getConfig().getErrorColors().get(issueType);
                Color bgColor = colorForIssueType != null ? colorForIssueType : null;
                Color underlineColor = ITSIssueType.Misspelling == span.rule.getLocQualityIssueType()
                        ? Color.red
                        : ulColor;
                HighlightPainter painter = new HighlightPainter(bgColor, underlineColor);
                h.addHighlight(span.start, span.end, painter);
            }
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:org.monkeys.gui.matcher.MatcherPanel.java

private void highlightMatches(final Collection<RegexMatch> matches) {
    this.clearHighlights();
    if (null == matches || matches.isEmpty()) {
        return;//from w ww.ja v a 2 s. co m
    }

    final Highlighter h = this.textArea.getHighlighter();
    final Highlighter.HighlightPainter style = DefaultHighlighter.DefaultPainter;
    for (final RegexMatch match : matches) {
        final int p0 = match.getStart();
        final int p1 = match.getEnd();
        try {
            h.addHighlight(p0, p1, style);
        } catch (final BadLocationException e) {
            MessageUtils.exceptionDialog("Unanble to highlight match [" + match + "].", e);
        }
    }
}