Java Code Examples for javax.swing.text.JTextComponent#getHighlighter()

The following examples show how to use javax.swing.text.JTextComponent#getHighlighter() . 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: 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 2
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
	Highlighter highlighter = comp.getHighlighter();
	highlighter.removeAllHighlights();

	try {
		i = toComponentPosition(comp, i);
		j = toComponentPosition(comp, j);
		highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
		if (scroll) {
			if (comp.getCaretPosition() < i || comp.getCaretPosition() > j) {
				comp.moveCaretPosition(i);
				comp.scrollRectToVisible(comp.modelToView(i));
			}
		}
	}
	catch (BadLocationException ble) {
		errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
	}
}
 
Example 3
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
public static void setHighlight(JTextComponent jtc, String pattern) {
  Highlighter highlighter = jtc.getHighlighter();
  highlighter.removeAllHighlights();
  Document doc = jtc.getDocument();
  try {
    String text = doc.getText(0, doc.getLength());
    Matcher matcher = Pattern.compile(pattern).matcher(text);
    int pos = 0;
    while (matcher.find(pos) && !matcher.group().isEmpty()) {
      int start = matcher.start();
      int end = matcher.end();
      highlighter.addHighlight(start, end, HIGHLIGHT);
      pos = end;
    }
  } catch (BadLocationException | PatternSyntaxException ex) {
    UIManager.getLookAndFeel().provideErrorFeedback(jtc);
  }
}
 
Example 4
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 5
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 6
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 7
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 8
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 9
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 10
Source File: MyMarkers.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
/**
 * add highlights for the given region on the given pane
 *
 * @param pane Editor
 * @param start Start index
 * @param end End index
 * @param marker Marker
 */
public static void markText(JTextComponent pane, int start, int end, Highlighter.HighlightPainter marker) {
    try {
        Highlighter hiliter = pane.getHighlighter();
        int selStart = pane.getSelectionStart();
        int selEnd = pane.getSelectionEnd();
        // if there is no selection or selection does not overlap
        if (selStart == selEnd || end < selStart || start > selStart) {
            hiliter.addHighlight(start, end, marker);
            return;
        }
        // selection starts within the highlight, highlight before slection
        if (selStart > start && selStart < end) {
            hiliter.addHighlight(start, selStart, marker);
        }
        // selection ends within the highlight, highlight remaining
        if (selEnd > start && selEnd < end) {
            hiliter.addHighlight(selEnd, end, marker);
        }

    } catch (BadLocationException ex) {

    }
}
 
Example 11
Source File: Markers.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
/**
 * add highlights for the given region on the given pane
 * @param pane
 * @param start
 * @param end
 * @param marker
 */
public static void markText(JTextComponent pane, int start, int end, SimpleMarker marker) {
    try {
        Highlighter hiliter = pane.getHighlighter();
        int selStart = pane.getSelectionStart();
        int selEnd = pane.getSelectionEnd();
        // if there is no selection or selection does not overlap
        if(selStart == selEnd || end < selStart || start > selStart) {
            hiliter.addHighlight(start, end, marker);
            return;
        }
        // selection starts within the highlight, highlight before slection
        if(selStart > start && selStart < end ) {
            hiliter.addHighlight(start, selStart, marker);
        }
        // selection ends within the highlight, highlight remaining
        if(selEnd > start && selEnd < end ) {
            hiliter.addHighlight(selEnd, end, marker);
        }

    } catch (BadLocationException ex) {
        // nothing we can do if the request is out of bound
        LOG.log(Level.SEVERE, null, ex);
    }
}
 
Example 12
Source File: MainPanel.java    From java-swing-tips with MIT License 6 votes vote down vote up
private static void setHighlight(JTextComponent jtc, String pattern) {
  Highlighter highlighter = jtc.getHighlighter();
  highlighter.removeAllHighlights();
  try {
    Document doc = jtc.getDocument();
    String text = doc.getText(0, doc.getLength());
    Matcher matcher = Pattern.compile(pattern).matcher(text);
    int pos = 0;
    while (matcher.find(pos) && !matcher.group().isEmpty()) {
      pos = matcher.end();
      highlighter.addHighlight(matcher.start(), pos, HIGHLIGHT);
    }
    // while ((pos = text.indexOf(pattern, pos)) >= 0) {
    //   highlighter.addHighlight(pos, pos + pattern.length(), HIGHLIGHT);
    //   pos += pattern.length();
    // }
  } catch (BadLocationException | PatternSyntaxException ex) {
    UIManager.getLookAndFeel().provideErrorFeedback(jtc);
  }
}
 
Example 13
Source File: STViz.java    From codebuff with BSD 2-Clause "Simplified" License 6 votes vote down vote up
protected void highlight(JTextComponent comp, int i, int j, boolean scroll) {
    Highlighter highlighter = comp.getHighlighter();
    highlighter.removeAllHighlights();
    try {
        i = toComponentPosition(comp, i);
        j = toComponentPosition(comp, j);
        highlighter.addHighlight(i, j+1, DefaultHighlighter.DefaultPainter);
        if ( scroll ) {
            if ( comp.getCaretPosition()< i || comp.getCaretPosition()>j ) {
                comp.moveCaretPosition(i);
                comp.scrollRectToVisible(comp.modelToView(i));
            }
        }
    }
    catch (BadLocationException ble) {
        errMgr.internalError(tmodel.root.event.scope.st, "bad highlight location", ble);
    }
}
 
Example 14
Source File: Markers.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
/**
 * add highlights for the given region on the given pane
 * @param pane
 * @param start
 * @param end
 * @param marker
 */
public static void markText(JTextComponent pane, int start, int end, SimpleMarker marker) {
    try {
        Highlighter hiliter = pane.getHighlighter();
        int selStart = pane.getSelectionStart();
        int selEnd = pane.getSelectionEnd();
        // if there is no selection or selection does not overlap
        if(selStart == selEnd || end < selStart || start > selStart) {
            hiliter.addHighlight(start, end, marker);
            return;
        }
        // selection starts within the highlight, highlight before slection
        if(selStart > start && selStart < end ) {
            hiliter.addHighlight(start, selStart, marker);
        }
        // selection ends within the highlight, highlight remaining
        if(selEnd > start && selEnd < end ) {
            hiliter.addHighlight(selEnd, end, marker);
        }

    } catch (BadLocationException ex) {
        // nothing we can do if the request is out of bound
        LOG.log(Level.SEVERE, null, ex);
    }
}
 
Example 15
Source File: TextBoxView.java    From SwingBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Process paint.
 * 
 * @param gg
 *            the graphics context
 * @param a
 *            the allocation
 */
protected void processPaint(Graphics gg, Shape a)
{
    Graphics2D g = (Graphics2D) gg;
    AffineTransform tmpTransform = g.getTransform();
    if (!tmpTransform.equals(transform))
    {
        transform = tmpTransform;
        invalidateTextLayout();
    }

    Component c = container;
    int p0 = getStartOffset();
    int p1 = getEndOffset();
    Color fg = getForeground();

    if (c instanceof JTextComponent)
    {
        JTextComponent tc = (JTextComponent) c;
        if (!tc.isEnabled())
        {
            fg = tc.getDisabledTextColor();
        }

        // javax.swing.plaf.basic.BasicTextUI $ BasicHighlighter
        // >> DefaultHighlighter
        // >> DefaultHighlightPainter

        Highlighter highLighter = tc.getHighlighter();
        if (highLighter instanceof LayeredHighlighter)
        {
            ((LayeredHighlighter) highLighter).paintLayeredHighlights(g, p0, p1, box.getAbsoluteContentBounds(), tc, this);
            // (g, p0, p1, a, tc, this);
        }
    }
    // nothing is selected
    if (!box.isEmpty() && !getText().isEmpty())
        renderContent(g, a, fg, p0, p1);

}
 
Example 16
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 17
Source File: ImageView.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private void paintHighlights(Graphics g, Shape shape) {
    if (container instanceof JTextComponent) {
        JTextComponent tc = (JTextComponent)container;
        Highlighter h = tc.getHighlighter();
        if (h instanceof LayeredHighlighter) {
            ((LayeredHighlighter)h).paintLayeredHighlights
                (g, getStartOffset(), getEndOffset(), shape, tc, this);
        }
    }
}
 
Example 18
Source File: ColorPanel.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void install(JTextComponent component, boolean listener) {
  try {
    Highlighter highlighter = component.getHighlighter();
    if (highlighter != null) highlighter.addHighlight(0, 0, this);
  }
  catch (BadLocationException ignored) {
  }
  if (listener) {
    component.addPropertyChangeListener(PROPERTY, this);
  }
}
 
Example 19
Source File: InlineReplacedBoxView.java    From SwingBox with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void paintHighlights(Graphics g, Shape shape)
{
    if (container instanceof JTextComponent)
    {
        JTextComponent tc = (JTextComponent) container;
        Highlighter h = tc.getHighlighter();
        if (h instanceof LayeredHighlighter)
        {
            ((LayeredHighlighter) h).paintLayeredHighlights(g,
                    getStartOffset(), getEndOffset(), shape, tc, this);
        }
    }
}
 
Example 20
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]);
            }
        }
    }
}