Java Code Examples for javax.swing.text.DefaultHighlighter#DefaultHighlightPainter

The following examples show how to use javax.swing.text.DefaultHighlighter#DefaultHighlightPainter . 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: SwingUtilities2.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 2
Source File: SwingUtilities2.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 3
Source File: SwingUtilities2.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 4
Source File: DiffDialog.java    From zap-extensions with Apache License 2.0 6 votes vote down vote up
private int appendText(JTextArea area, String text, boolean highlight) {

        int start = area.getDocument().getLength();

        if (text == null || text.length() == 0) {
            return start;
        }

        int end = start + text.length();

        try {

            area.getDocument().insertString(start, text, null);

            if (highlight) {
                Highlighter hilite = area.getHighlighter();
                HighlightPainter painter =
                        new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
                hilite.addHighlight(start, end - 1, painter);
            }

        } catch (BadLocationException e) {
            logger.error(e.getMessage(), e);
        }
        return end;
    }
 
Example 5
Source File: SwingUtilities2.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 6
Source File: SwingUtilities2.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 7
Source File: SwingUtilities2.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 8
Source File: SwingUtilities2.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 9
Source File: SwingUtilities2.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 10
Source File: SwingUtilities2.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 11
Source File: GuiUtils.java    From workcraft with MIT License 6 votes vote down vote up
private static Object highlightText(JTextComponent textComponent, int fromPos, int toPos, Color color,
        boolean drawsLayeredHighlights) {

    if ((color != null) && (textComponent != null) && (toPos > fromPos)) {
        DefaultHighlighter highlighter = (DefaultHighlighter) textComponent.getHighlighter();
        highlighter.setDrawsLayeredHighlights(drawsLayeredHighlights);
        Highlighter.HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(color);
        try {
            return highlighter.addHighlight(
                    Math.max(fromPos, 0),
                    Math.min(toPos, textComponent.getText().length()),
                    painter);
        } catch (BadLocationException e) {
        }
    }
    return null;
}
 
Example 12
Source File: SwingUtilities2.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 13
Source File: LocalFileScriptTab.java    From BowlerStudio with GNU General Public License v3.0 6 votes vote down vote up
public void setHighlight(int lineNumber, Color color) throws BadLocationException {
	if (textArea == null) {
		return;
	}
	painter = new DefaultHighlighter.DefaultHighlightPainter(color);
	int startIndex = textArea.getLineStartOffset(lineNumber - 1);
	int endIndex = textArea.getLineEndOffset(lineNumber - 1);

	try {

		SwingUtilities.invokeLater(() -> {
			textArea.moveCaretPosition(startIndex);
		});
	} catch (Error | Exception ex) {
		ex.printStackTrace();
	}
	SwingUtilities.invokeLater(() -> {
		try {
			textArea.getHighlighter().addHighlight(startIndex, endIndex, painter);
		} catch (BadLocationException e) {
			ISSUE_REPORTING_EXCEPTION_HANDLER.uncaughtException(Thread.currentThread(), e);

		}
	});

}
 
Example 14
Source File: SwingUtilities2.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines whether the SelectedTextColor should be used for painting text
 * foreground for the specified highlight.
 *
 * Returns true only if the highlight painter for the specified highlight
 * is the swing painter (whether inner class of javax.swing.text.DefaultHighlighter
 * or com.sun.java.swing.plaf.windows.WindowsTextUI) and its background color
 * is null or equals to the selection color of the text component.
 *
 * This is a hack for fixing both bugs 4761990 and 5003294
 */
public static boolean useSelectedTextColor(Highlighter.Highlight h, JTextComponent c) {
    Highlighter.HighlightPainter painter = h.getPainter();
    String painterClass = painter.getClass().getName();
    if (painterClass.indexOf("javax.swing.text.DefaultHighlighter") != 0 &&
            painterClass.indexOf("com.sun.java.swing.plaf.windows.WindowsTextUI") != 0) {
        return false;
    }
    try {
        DefaultHighlighter.DefaultHighlightPainter defPainter =
                (DefaultHighlighter.DefaultHighlightPainter) painter;
        if (defPainter.getColor() != null &&
                !defPainter.getColor().equals(c.getSelectionColor())) {
            return false;
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
 
Example 15
Source File: HighlightHelper.java    From r2m-plugin-android with Apache License 2.0 5 votes vote down vote up
private static void setHighlight(JTextArea highlight, JSONError error) {
    Highlighter.HighlightPainter errorHighlighter =
            new DefaultHighlighter.DefaultHighlightPainter(PINK);
    try {
        highlight.getHighlighter().addHighlight(error.getStartIndex(),
                error.getEndIndex(),
                errorHighlighter);
    } catch (Exception ble) {
        ble.printStackTrace();
    }
}
 
Example 16
Source File: DiffDialog.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
private void highlightText(JTextArea area, int start, int end) {
    Highlighter hilite = area.getHighlighter();
    HighlightPainter painter = new DefaultHighlighter.DefaultHighlightPainter(Color.YELLOW);
    try {
        hilite.addHighlight(start, end, painter);
    } catch (BadLocationException e) {
        logger.error(e.getMessage(), e);
    }
}
 
Example 17
Source File: PatternSandbox.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize highlighter and painter.
 */
private void initHighlighter() {
    highlighter = new DefaultHighlighter();
    painter = new DefaultHighlighter.DefaultHighlightPainter(
            chooseHighlightColor());
    textPane.setHighlighter(highlighter);
}
 
Example 18
Source File: ColorHandler.java    From mae-annotation with GNU General Public License v3.0 4 votes vote down vote up
public static Highlighter.HighlightPainter getCustomHighliter(Color color) {
    return new DefaultHighlighter.DefaultHighlightPainter(color);
}
 
Example 19
Source File: ColorHandler.java    From mae-annotation with GNU General Public License v3.0 4 votes vote down vote up
public static Highlighter.HighlightPainter getFadingHighlighter() {
    return new DefaultHighlighter.DefaultHighlightPainter(getFadingForeground());
}
 
Example 20
Source File: ColorHandler.java    From mae-annotation with GNU General Public License v3.0 4 votes vote down vote up
public static Highlighter.HighlightPainter getVividHighliter() {
    return new DefaultHighlighter.DefaultHighlightPainter(getVividForeground());
}