Пример #1
0
  private final void find(Pattern pattern) {
    final WriteOnce<Integer> once = new WriteOnce<Integer>();
    final Highlighter highlighter = textPane.getHighlighter();
    highlighter.removeAllHighlights();

    int matches = 0;

    final HTMLDocument doc = (HTMLDocument) textPane.getDocument();
    for (final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.CONTENT);
        it.isValid();
        it.next())
      try {
        final String fragment =
            doc.getText(it.getStartOffset(), it.getEndOffset() - it.getStartOffset());
        final Matcher matcher = pattern.matcher(fragment);
        while (matcher.find()) {
          highlighter.addHighlight(
              it.getStartOffset() + matcher.start(),
              it.getStartOffset() + matcher.end(),
              HIGHLIGHTER);
          once.set(it.getStartOffset());
          ++matches;
        }
      } catch (final BadLocationException ex) {
      }
    if (!once.isEmpty()) textPane.setCaretPosition(once.get());
    this.matches.setText(String.format("%d matches", matches));
  }
Пример #2
0
 public void scrollToReference(final String ref) {
   Document doc = getDocument();
   if (ref == null || !(doc instanceof HTMLDocument)) {
     return;
   }
   HTMLDocument.Iterator it = ((HTMLDocument) doc).getIterator(HTML.Tag.A);
   int offset = 0;
   while (it.isValid()) {
     AttributeSet set = it.getAttributes();
     Object name = set.getAttribute(HTML.Attribute.NAME);
     if (ref.equals(name)) {
       offset = it.getStartOffset();
       break;
     }
     it.next();
   }
   Rectangle rect = null;
   try {
     rect = modelToView(offset);
   } catch (BadLocationException e) {
   }
   Rectangle visibleRect = getVisibleRect();
   if (visibleRect != null) {
     rect.height = visibleRect.height;
   }
   scrollRectToVisible(rect);
 }