Java Code Examples for javax.swing.text.html.HTMLDocument#getIterator()

The following examples show how to use javax.swing.text.html.HTMLDocument#getIterator() . 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: GHelpBroker.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Rectangle getReferenceArea(String ref) {
	HTMLDocument document = (HTMLDocument) htmlEditorPane.getDocument();
	HTMLDocument.Iterator iter = document.getIterator(HTML.Tag.A);
	for (; iter.isValid(); iter.next()) {
		AttributeSet attributes = iter.getAttributes();
		String name = (String) attributes.getAttribute(HTML.Attribute.NAME);
		if (name == null || !name.equals(ref)) {
			continue;
		}

		try {
			int start = iter.getStartOffset();
			Rectangle2D startArea = htmlEditorPane.modelToView2D(start);
			return startArea.getBounds();
		}
		catch (BadLocationException ble) {
			Msg.trace(this, "Unexpected exception searching for help reference", ble);
		}
	}
	return null;
}
 
Example 2
Source File: REditorPane.java    From marathonv5 with Apache License 2.0 6 votes vote down vote up
public void setHRef(int pos, Document doc) {
    hRef = null;
    text = null;
    if (!(doc instanceof HTMLDocument)) {
        return;
    }
    HTMLDocument hdoc = (HTMLDocument) doc;
    Iterator iterator = hdoc.getIterator(HTML.Tag.A);
    while (iterator.isValid()) {
        if (pos >= iterator.getStartOffset() && pos < iterator.getEndOffset()) {
            AttributeSet attributes = iterator.getAttributes();
            if (attributes != null && attributes.getAttribute(HTML.Attribute.HREF) != null) {
                try {
                    text = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset()).trim();
                    hRef = attributes.getAttribute(HTML.Attribute.HREF).toString();
                    setIndexOfHrefAndText(hdoc, pos, text, hRef);
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
                return;
            }
        }
        iterator.next();
    }
}
 
Example 3
Source File: JHtmlLabel.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
private void cacheLinkElements() {
  this.linkCache = new ArrayList<HtmlLinkAddress>();
  final View view = (View) this.getClientProperty("html");
  if (view != null) {
    final HTMLDocument doc = (HTMLDocument) view.getDocument();
    final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
      final SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
      final String link = (String) s.getAttribute(HTML.Attribute.HREF);
      if (link != null) {
        this.linkCache.add(new HtmlLinkAddress(link, it.getStartOffset(), it.getEndOffset()));
      }
      it.next();
    }
  }
}
 
Example 4
Source File: OverviewControllerUI.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void showInThreads(Instance instance) {
    if (!showThreads) {
        showThreads = true;
        instanceToSelect = instance;
        refreshSummary();
        return;
    }
    String referenceId = String.valueOf(instance.getInstanceId());
    
    dataArea.scrollToReference(referenceId);
    Document d = dataArea.getDocument();
    HTMLDocument doc = (HTMLDocument) d;
    HTMLDocument.Iterator iter = doc.getIterator(HTML.Tag.A);
    for (; iter.isValid(); iter.next()) {
        AttributeSet a = iter.getAttributes();
        String nm = (String) a.getAttribute(HTML.Attribute.NAME);
        if ((nm != null) && nm.equals(referenceId)) {
            dataArea.select(iter.getStartOffset(),iter.getEndOffset());
            dataArea.requestFocusInWindow();
        }
    }
}
 
Example 5
Source File: HTMLView.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void selectReference(String reference) {
    if (htmlComponent == null || pendingText) {
        pendingReference = reference;
    } else {
        Document d = htmlComponent.getDocument();
        if (d instanceof HTMLDocument) {
            HTMLDocument doc = (HTMLDocument)d;
            HTMLDocument.Iterator iter = doc.getIterator(HTML.Tag.A);
            for (; iter.isValid(); iter.next()) {
                AttributeSet a = iter.getAttributes();
                String nm = (String) a.getAttribute(HTML.Attribute.NAME);
                if (Objects.equals(reference, nm)) {
                    selectReference(iter);
                    return;
                }
            }
        }
    }
}
 
Example 6
Source File: OverviewControllerUI.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void showInThreads(Instance instance) {
    if (!showThreads) {
        showThreads = true;
        instanceToSelect = instance;
        refreshSummary();
        return;
    }
    String referenceId = String.valueOf(instance.getInstanceId());
    
    dataArea.scrollToReference(referenceId);
    Document d = dataArea.getDocument();
    HTMLDocument doc = (HTMLDocument) d;
    HTMLDocument.Iterator iter = doc.getIterator(HTML.Tag.A);
    for (; iter.isValid(); iter.next()) {
        AttributeSet a = iter.getAttributes();
        String nm = (String) a.getAttribute(HTML.Attribute.NAME);
        if ((nm != null) && nm.equals(referenceId)) {
            dataArea.select(iter.getStartOffset(),iter.getEndOffset());
            dataArea.requestFocusInWindow();
        }
    }
}
 
Example 7
Source File: JHtmlLabel.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
private void cacheLinkElements() {
  this.linkCache = new ArrayList<HtmlLinkAddress>();
  final View view = (View) this.getClientProperty("html");
  if (view != null) {
    final HTMLDocument doc = (HTMLDocument) view.getDocument();
    final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
      final SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
      final String link = (String) s.getAttribute(HTML.Attribute.HREF);
      if (link != null) {
        this.linkCache.add(new HtmlLinkAddress(link, it.getStartOffset(), it.getEndOffset()));
      }
      it.next();
    }
  }
}
 
Example 8
Source File: JHtmlLabel.java    From zxpoly with GNU General Public License v3.0 6 votes vote down vote up
private void cacheLinkElements() {
  this.linkCache = new ArrayList<>();
  final View view = (View) this.getClientProperty("html"); //NOI18N
  if (view != null) {
    final HTMLDocument doc = (HTMLDocument) view.getDocument();
    final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
      final SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
      final String link = (String) s.getAttribute(HTML.Attribute.HREF);
      if (link != null) {
        this.linkCache.add(new HtmlLinkAddress(link, it.getStartOffset(), it.getEndOffset()));
      }
      it.next();
    }
  }
}
 
Example 9
Source File: JHtmlLabel.java    From netbeans-mmd-plugin with Apache License 2.0 6 votes vote down vote up
private void cacheLinkElements() {
  this.linkCache = new ArrayList<>();
  final View view = (View) this.getClientProperty("html"); //NOI18N
  if (view != null) {
    final HTMLDocument doc = (HTMLDocument) view.getDocument();
    final HTMLDocument.Iterator it = doc.getIterator(HTML.Tag.A);
    while (it.isValid()) {
      final SimpleAttributeSet s = (SimpleAttributeSet) it.getAttributes();
      final String link = (String) s.getAttribute(HTML.Attribute.HREF);
      if (link != null) {
        this.linkCache.add(new HtmlLinkAddress(link, it.getStartOffset(), it.getEndOffset()));
      }
      it.next();
    }
  }
}
 
Example 10
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
private HTMLDocument.Iterator getLink(int n) {
  if (n >= 0) {
    HTMLDocument document = (HTMLDocument)myEditorPane.getDocument();
    int linkCount = 0;
    for (HTMLDocument.Iterator it = document.getIterator(HTML.Tag.A); it.isValid(); it.next()) {
      if (it.getAttributes().isDefined(HTML.Attribute.HREF) && linkCount++ == n) return it;
    }
  }
  return null;
}
 
Example 11
Source File: DocumentationComponent.java    From consulo with Apache License 2.0 5 votes vote down vote up
private int getLinkCount() {
  HTMLDocument document = (HTMLDocument)myEditorPane.getDocument();
  int linkCount = 0;
  for (HTMLDocument.Iterator it = document.getIterator(HTML.Tag.A); it.isValid(); it.next()) {
    if (it.getAttributes().isDefined(HTML.Attribute.HREF)) linkCount++;
  }
  return linkCount;
}
 
Example 12
Source File: EditableNotificationMessageElement.java    From consulo with Apache License 2.0 5 votes vote down vote up
protected void updateStyle(@Nonnull JEditorPane editorPane, @javax.annotation.Nullable JTree tree, Object value, boolean selected, boolean hasFocus) {
  super.updateStyle(editorPane, tree, value, selected, hasFocus);

  final HTMLDocument htmlDocument = (HTMLDocument)editorPane.getDocument();
  final Style linkStyle = htmlDocument.getStyleSheet().getStyle(LINK_STYLE);
  StyleConstants.setForeground(linkStyle, IdeTooltipManager.getInstance().getLinkForeground(false));
  StyleConstants.setItalic(linkStyle, true);
  HTMLDocument.Iterator iterator = htmlDocument.getIterator(HTML.Tag.A);
  while (iterator.isValid()) {
    boolean disabledLink = false;
    final AttributeSet attributes = iterator.getAttributes();
    if (attributes instanceof SimpleAttributeSet) {
      final Object attribute = attributes.getAttribute(HTML.Attribute.HREF);
      if (attribute instanceof String && disabledLinks.containsKey(attribute)) {
        disabledLink = true;
        //TODO [Vlad] add support for disabled link text update
        ////final String linkText = disabledLinks.get(attribute);
        //if (linkText != null) {
        //}
        ((SimpleAttributeSet)attributes).removeAttribute(HTML.Attribute.HREF);
      }
      if (attribute == null) {
        disabledLink = true;
      }
    }
    if (!disabledLink) {
      htmlDocument.setCharacterAttributes(
              iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset(), linkStyle, false);
    }
    iterator.next();
  }
}
 
Example 13
Source File: REditorPane.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private void setIndexOfHrefAndText(HTMLDocument hdoc, int pos, String text, String hRef) {
    this.hRefIndex = 0;
    this.textIndex = 0;
    Iterator iterator = hdoc.getIterator(HTML.Tag.A);
    while (iterator.isValid()) {
        if (pos >= iterator.getStartOffset() && pos < iterator.getEndOffset()) {
            return;
        } else {
            AttributeSet attributes = iterator.getAttributes();
            if (attributes != null && attributes.getAttribute(HTML.Attribute.HREF) != null) {
                try {
                    String t = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset())
                            .trim();
                    String h = attributes.getAttribute(HTML.Attribute.HREF).toString();
                    if (t.equals(text)) {
                        this.textIndex++;
                    }
                    if (h.equals(hRef)) {
                        this.hRefIndex++;
                    }
                } catch (BadLocationException e) {
                    e.printStackTrace();
                }
            }
        }
        iterator.next();
    }
}
 
Example 14
Source File: JEditorPaneJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private void fillElements(Tag tag, ArrayList<IJavaElement> r, Predicate predicate) {
    HTMLDocument document = (HTMLDocument) ((JEditorPane) getComponent()).getDocument();
    Iterator iterator = document.getIterator(tag);
    int index = 0;
    while (iterator.isValid()) {
        JEditorPaneTagJavaElement e = new JEditorPaneTagJavaElement(this, tag, index++);
        if (predicate.isValid(e)) {
            r.add(e);
        }
        iterator.next();
    }
}
 
Example 15
Source File: JEditorPaneTagJavaElement.java    From marathonv5 with Apache License 2.0 5 votes vote down vote up
private Iterator findTag(HTMLDocument doc) {
    Iterator iterator = doc.getIterator(tag);
    int current = 0;
    while (iterator.isValid()) {
        if (current++ == index) {
            break;
        }
        iterator.next();
    }
    if (!iterator.isValid()) {
        throw new NoSuchElementException("Unable to find tag " + tag + " in document with index " + index, null);
    }
    return iterator;
}
 
Example 16
Source File: AnnotationDrawer.java    From rapidminer-studio with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Creates an image of the given annotation and caches it with the specified cache id.
 *
 * @param anno
 *            the annotation to cache
 * @param cacheId
 *            the cache id for the given annotation
 * @return the cached image
 */
private Image cacheAnnotationImage(final WorkflowAnnotation anno, final int cacheId) {
	Rectangle2D loc = anno.getLocation();
	// paint each annotation with the same JEditorPane
	Dimension size = new Dimension((int) Math.round(loc.getWidth() * rendererModel.getZoomFactor()), (int) Math.round(loc.getHeight() * rendererModel.getZoomFactor()));
	pane.setSize(size);
	float originalSize = AnnotationDrawUtils.ANNOTATION_FONT.getSize();
	// without this, scaling is off even more when zooming out..
	if (rendererModel.getZoomFactor() < 1.0d) {
		originalSize -= 1f;
	}
	float fontSize = (float) (originalSize * rendererModel.getZoomFactor());
	Font annotationFont = AnnotationDrawUtils.ANNOTATION_FONT.deriveFont(fontSize);
	pane.setFont(annotationFont);
	pane.setText(AnnotationDrawUtils.createStyledCommentString(anno));
	pane.setCaretPosition(0);

	// while caching, update the hyperlink bounds visible in this annotation
	HTMLDocument htmlDocument = (HTMLDocument) pane.getDocument();
	HTMLDocument.Iterator linkIterator = htmlDocument.getIterator(HTML.Tag.A);
	List<Pair<String, Rectangle>> hyperlinkBounds = new LinkedList<>();
	while (linkIterator.isValid()) {
		AttributeSet attributes = linkIterator.getAttributes();
		String url = (String) attributes.getAttribute(HTML.Attribute.HREF);
		int startOffset = linkIterator.getStartOffset();
		int endOffset = linkIterator.getEndOffset();
		try {
			// rectangle for leftmost character
			Rectangle rectangleLeft = pane.getUI().modelToView(pane, startOffset, Position.Bias.Forward);
			// rectangle for rightmost character
			Rectangle rectangleRight = pane.getUI().modelToView(pane, endOffset, Position.Bias.Backward);
			// merge both rectangles to get full bounds of hyperlink
			// also remove the zoom factor to not get distorted bounds when zoomed in/out
			int x = (int) (rectangleLeft.getX() / rendererModel.getZoomFactor());
			int y = (int) (rectangleLeft.getY() / rendererModel.getZoomFactor());
			int w = (int) ((rectangleRight.getX() - rectangleLeft.getX() + rectangleRight.getWidth()) / rendererModel.getZoomFactor());
			int h = (int) ((rectangleRight.getY() - rectangleLeft.getY() + rectangleRight.getHeight()) / rendererModel.getZoomFactor());
			hyperlinkBounds.add(new Pair<>(url, new Rectangle(x, y, w, h)));
		} catch (BadLocationException e) {
			// silently ignored because at worst you cannot click a hyperlink, nothing to spam the log with
		}
		linkIterator.next();
	}
	model.setHyperlinkBoundsForAnnotation(anno.getId(), hyperlinkBounds);

	// draw annotation area to image and then to graphics
	// otherwise heavyweight JEdiorPane draws over everything and outside of panel
	BufferedImage img = new BufferedImage((int) (loc.getWidth() * rendererModel.getZoomFactor()), (int) (loc.getHeight() * rendererModel.getZoomFactor()), BufferedImage.TYPE_INT_ARGB);
	Graphics2D gImg = img.createGraphics();
	gImg.setRenderingHints(ProcessDrawer.HI_QUALITY_HINTS);
	// without this, the text is pixelated on half opaque backgrounds
	gImg.setComposite(AlphaComposite.SrcOver);
	// paint JEditorPane to image
	pane.paint(gImg);
	displayCache.put(anno.getId(), new WeakReference<>(img));
	cachedID.put(anno.getId(), cacheId);

	return img;
}
 
Example 17
Source File: REditorPaneTest.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
private void searchAsText(String spec, boolean isText) {
    Document document = getEditor().getDocument();
    hRef = null;
    text = null;
    hRefIndex = 0;
    textIndex = 0;
    linkPosition = -1;
    int lastIndexOf = spec.lastIndexOf('(');
    if (lastIndexOf != -1) {
        if (isText) {
            textIndex = Integer.parseInt(spec.substring(lastIndexOf + 1, spec.length() - 1));
        } else {
            hRefIndex = Integer.parseInt(spec.substring(lastIndexOf + 1, spec.length() - 1));
        }
        spec = spec.substring(0, lastIndexOf);
    }
    if (!(document instanceof HTMLDocument)) {
        return;
    }
    HTMLDocument hdoc = (HTMLDocument) document;
    Iterator iterator = hdoc.getIterator(HTML.Tag.A);
    int curIndex = 0;
    while (iterator.isValid()) {
        String t;
        AttributeSet attributes = iterator.getAttributes();
        try {
            if (isText) {
                t = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset());
            } else {
                t = attributes.getAttribute(HTML.Attribute.HREF).toString();
            }
        } catch (BadLocationException e1) {
            return;
        }
        if (t.contains(spec) && (isText && curIndex++ == textIndex || !isText && curIndex++ == hRefIndex)) {
            if (attributes != null && attributes.getAttribute(HTML.Attribute.HREF) != null) {
                try {
                    text = hdoc.getText(iterator.getStartOffset(), iterator.getEndOffset() - iterator.getStartOffset()).trim();
                    hRef = attributes.getAttribute(HTML.Attribute.HREF).toString();
                    linkPosition = (iterator.getStartOffset() + iterator.getEndOffset()) / 2;
                } catch (BadLocationException e) {
                    return;
                }
                return;
            }
        }
        iterator.next();
    }
}