Search in sources :

Example 41 with Highlighter

use of javax.swing.text.Highlighter in project CCDD by nasa.

the class CcddUtilities method highlightSearchText.

/**
 ********************************************************************************************
 * Highlight any text matching the search text in the the specified text component
 *
 * @param component
 *            reference to the table cell renderer component
 *
 * @param text
 *            cell value
 *
 * @param hightlightColor
 *            color used for highlighting the matching text
 *
 * @param searchPattern
 *            search pattern; can be a regular expression (Pattern)
 *
 * @return true if the supplied text contains a match to the specified search pattern
 ********************************************************************************************
 */
protected static boolean highlightSearchText(Component component, String text, Color hightlightColor, Pattern searchPattern) {
    boolean hasHighlight = false;
    // Check if the search pattern exists
    if (searchPattern != null) {
        int adjust = 0;
        // Get the reference to the component's highlighter
        Highlighter highlighter = ((JTextComponent) component).getHighlighter();
        // Remove any existing highlighting from the text
        highlighter.removeAllHighlights();
        // Highlight matching search text instances. Create a highlighter painter
        DefaultHighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(hightlightColor);
        // Check if the text is HTML tagged
        if (text.startsWith("<html>")) {
            // Remove the HTML tags and set the match index adjust
            text = CcddUtilities.removeHTMLTags(text);
            adjust = 1;
        }
        // Create the pattern matcher from the pattern
        Matcher matcher = searchPattern.matcher(text);
        // Check if there is a match in the cell value
        while (matcher.find()) {
            try {
                // Highlight the matching text. Adjust the highlight color to account for the
                // cell selection highlighting so that the matching search text is easily
                // readable
                highlighter.addHighlight(matcher.start() + adjust, matcher.end() + adjust, painter);
                // Set the flag to indicate the text contains a match
                hasHighlight = true;
            } catch (BadLocationException ble) {
            // Ignore highlighting failure
            }
        }
    }
    return hasHighlight;
}
Also used : Matcher(java.util.regex.Matcher) JTextComponent(javax.swing.text.JTextComponent) DefaultHighlightPainter(javax.swing.text.DefaultHighlighter.DefaultHighlightPainter) BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 42 with Highlighter

use of javax.swing.text.Highlighter in project EditCalculateAndChart by mathhobbit.

the class TextAreaMouseListener method mouseExited.

@Override
public void mouseExited(MouseEvent e) {
    JTextArea area = TEdit.getTextArea();
    Highlighter hilite = area.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();
    if (hilites != null && hilites.length > 0)
        TEdit.getSwingPool().put("area.hilites", hilites);
/*
     if(hilites != null&&hilites.length>0){
             TEdit.setEnabled("Delete", true);
             TEdit.setEnabled("Calc",true);
         }
         else{
             TEdit.setEnabled("Delete", false);
             TEdit.setEnabled("Calc",false);
         }
*/
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
Also used : JTextArea(javax.swing.JTextArea) Highlighter(javax.swing.text.Highlighter)

Example 43 with Highlighter

use of javax.swing.text.Highlighter in project constraintlayout by androidx.

the class Main2 method selectKey.

@Override
public void selectKey(String widget) throws CLParsingException {
    CLKey key = CLScan.findCLKey(CLParser.parse(mMainText.getText()), widget);
    clearSelectedKey();
    if (key != null) {
        Highlighter h = mMainText.getHighlighter();
        try {
            mSelectionHighlight = h.addHighlight((int) key.getStart(), (int) key.getEnd() + 1, new DefaultHighlighter.DefaultHighlightPainter(Color.PINK));
        } catch (BadLocationException e) {
            e.printStackTrace();
        }
    }
}
Also used : BadLocationException(javax.swing.text.BadLocationException) DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 44 with Highlighter

use of javax.swing.text.Highlighter in project constraintlayout by androidx.

the class Main2 method clearSelectedKey.

@Override
public void clearSelectedKey() {
    if (mSelectionHighlight == null) {
        return;
    }
    Highlighter h = mMainText.getHighlighter();
    h.removeHighlight(mSelectionHighlight);
}
Also used : DefaultHighlighter(javax.swing.text.DefaultHighlighter) Highlighter(javax.swing.text.Highlighter)

Example 45 with Highlighter

use of javax.swing.text.Highlighter in project cayenne by apache.

the class JCayenneTextPane method setHighlightText.

public void setHighlightText(int lastIndex, int endIndex) throws BadLocationException {
    Highlighter highlighter = pane.getHighlighter();
    removeHighlightText(highlighter);
    highlighter.addHighlight(lastIndex, endIndex, painter);
}
Also used : Highlighter(javax.swing.text.Highlighter)

Aggregations

Highlighter (javax.swing.text.Highlighter)68 BadLocationException (javax.swing.text.BadLocationException)40 DefaultHighlighter (javax.swing.text.DefaultHighlighter)37 Matcher (java.util.regex.Matcher)12 HighlightPainter (javax.swing.text.Highlighter.HighlightPainter)10 JTextArea (javax.swing.JTextArea)9 Document (javax.swing.text.Document)9 Point (java.awt.Point)6 File (java.io.File)6 PatternSyntaxException (java.util.regex.PatternSyntaxException)5 DefaultHighlightPainter (javax.swing.text.DefaultHighlighter.DefaultHighlightPainter)5 Editor (omega.ui.component.Editor)5 StringTokenizer (java.util.StringTokenizer)4 JTextComponent (javax.swing.text.JTextComponent)4 Highlight (omega.instant.support.Highlight)4 IOException (java.io.IOException)3 ImageIcon (javax.swing.ImageIcon)3 AbstractErrorHighlighter (omega.instant.support.AbstractErrorHighlighter)3 JavaSyntaxParserGutterIconInfo (omega.instant.support.java.parser.JavaSyntaxParserGutterIconInfo)3 SquiggleUnderlineHighlightPainter (org.fife.ui.rsyntaxtextarea.SquiggleUnderlineHighlightPainter)3