Java Code Examples for javax.swing.text.Highlighter#Highlight

The following examples show how to use javax.swing.text.Highlighter#Highlight . 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 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 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: 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 4
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 5
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 6
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 7
Source File: SwingUtilities2.java    From hottub 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 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 10
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 11
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 12
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 13
Source File: SwingUtilities2.java    From jdk8u60 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 14
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 15
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 16
Source File: SnippetHighlighter.java    From littleluck with Apache License 2.0 5 votes vote down vote up
/**
    * Makes a copy of the highlights.  Does not actually clone each highlight,
    * but only makes references to them.
    *
    * @return the copy
    * @see Highlighter#getHighlights
    */
   public Highlighter.Highlight[] getHighlights() {
       int size = highlights.size();
       if (size == 0) {
           return noHighlights;
       }
Highlighter.Highlight[] h = new Highlighter.Highlight[size];
highlights.copyInto(h);
return h;
   }
 
Example 17
Source File: SnippetHighlighter.java    From beautyeye with Apache License 2.0 5 votes vote down vote up
/**
    * Makes a copy of the highlights.  Does not actually clone each highlight,
    * but only makes references to them.
    *
    * @return the copy
    * @see Highlighter#getHighlights
    */
   public Highlighter.Highlight[] getHighlights() {
       int size = highlights.size();
       if (size == 0) {
           return noHighlights;
       }
Highlighter.Highlight[] h = new Highlighter.Highlight[size];
highlights.copyInto(h);
return h;
   }
 
Example 18
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 19
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 20
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;
}