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

The following examples show how to use javax.swing.text.Highlighter#getHighlights() . 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: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
  super.paintTrack(g, c, trackBounds);

  Rectangle rect = textArea.getBounds();
  double sy = trackBounds.getHeight() / rect.getHeight();
  AffineTransform at = AffineTransform.getScaleInstance(1d, sy);
  Highlighter highlighter = textArea.getHighlighter();
  g.setColor(Color.YELLOW);
  try {
    for (Highlighter.Highlight hh: highlighter.getHighlights()) {
      Rectangle r = textArea.modelToView(hh.getStartOffset());
      Rectangle s = at.createTransformedShape(r).getBounds();
      int h = 2; // Math.max(2, s.height - 2);
      g.fillRect(trackBounds.x, trackBounds.y + s.y, trackBounds.width, h);
    }
  } catch (BadLocationException ex) {
    // should never happen
    RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
    wrap.initCause(ex);
    throw wrap;
  }
}
 
Example 2
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
@Override protected void paintTrack(Graphics g, JComponent c, Rectangle trackBounds) {
  super.paintTrack(g, c, trackBounds);

  Rectangle rect = textArea.getBounds();
  double sy = trackBounds.getHeight() / rect.getHeight();
  AffineTransform at = AffineTransform.getScaleInstance(1d, sy);
  Highlighter highlighter = textArea.getHighlighter();
  g.setColor(Color.YELLOW);
  try {
    for (Highlighter.Highlight hh: highlighter.getHighlights()) {
      Rectangle r = textArea.modelToView(hh.getStartOffset());
      Rectangle s = at.createTransformedShape(r).getBounds();
      int h = 2; // Math.max(2, s.height - 2);
      g.fillRect(trackBounds.x, trackBounds.y + s.y, trackBounds.width, h);
    }
  } catch (BadLocationException ex) {
    // should never happen
    RuntimeException wrap = new StringIndexOutOfBoundsException(ex.offsetRequested());
    wrap.initCause(ex);
    throw wrap;
  }
}
 
Example 3
Source File: Markers.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Removes only our private highlights
 * This is public so that we can remove the highlights when the editorKit
 * is unregistered.  SimpleMarker can be null, in which case all instances of
 * our Markers are removed.
 * @param component the text component whose markers are to be removed
 * @param marker the SimpleMarker to remove
 */
public static void removeMarkers(JTextComponent component, SimpleMarker marker) {
    Highlighter hilite = component.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

    for (int i = 0; i < hilites.length; i++) {
        if (hilites[i].getPainter() instanceof SimpleMarker) {
            SimpleMarker hMarker = (SimpleMarker) hilites[i].getPainter();
            if (marker == null || hMarker.equals(marker)) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }
}
 
Example 4
Source File: MyMarkers.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes only our private highlights This is public so that we can remove
 * the highlights when the editorKit is unregistered. SimpleMarker can be
 * null, in which case all instances of our Markers are removed.
 *
 * @param component the text component whose markers are to be removed
 * @param marker the SimpleMarker to remove
 */
public static void removeMarkers(JTextComponent component, Highlighter.HighlightPainter marker) {
    Highlighter hilite = component.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

    for (int i = 0; i < hilites.length; i++) {
        Highlighter.HighlightPainter hMarker = hilites[i].getPainter();
        if (marker == null || hMarker.equals(marker)) {
            hilite.removeHighlight(hilites[i]);
        }
    }
}
 
Example 5
Source File: Markers.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Removes only our private highlights
 * This is public so that we can remove the highlights when the editorKit
 * is unregistered.  SimpleMarker can be null, in which case all instances of
 * our Markers are removed.
 * @param component the text component whose markers are to be removed
 * @param marker the SimpleMarker to remove
 */
public static void removeMarkers(JTextComponent component, SimpleMarker marker) {
    Highlighter hilite = component.getHighlighter();
    Highlighter.Highlight[] hilites = hilite.getHighlights();

    for (int i = 0; i < hilites.length; i++) {
        if (hilites[i].getPainter() instanceof SimpleMarker) {
            SimpleMarker hMarker = (SimpleMarker) hilites[i].getPainter();
            if (marker == null || hMarker.equals(marker)) {
                hilite.removeHighlight(hilites[i]);
            }
        }
    }
}
 
Example 6
Source File: ColorHighlighter.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public int highlight(String word) {        
    Highlighter highlighter = comp.getHighlighter();

    // remove old highlight before applying new one
    for (Highlighter.Highlight h : highlighter.getHighlights()) {
        if (h.getPainter() instanceof ColorHighlightPainter) {
            highlighter.removeHighlight(h);
        }
    }
    
    if (word == null || word.equals("")) {
        return -1;
    }

    // search for the word, case insentitive
    String content = null;
    try {
        Document d = comp.getDocument();
        content = d.getText(0, d.getLength()).toLowerCase();
    } catch (BadLocationException e) {
        return -1;
    }

    word = word.toLowerCase();
    int lastIndex = 0;
    int firstOffset = -1;
    int wordSize = word.length();

    while ((lastIndex = content.indexOf(word, lastIndex)) != -1) {
        int endIndex = lastIndex + wordSize;
        try {
            highlighter.addHighlight(lastIndex, endIndex, painter);
        } catch (BadLocationException ex) {
            // ignore
        }
        if (firstOffset == -1) {
            firstOffset = lastIndex;
        }
        lastIndex = endIndex;
    }

    return firstOffset;
}