下面列出了javax.swing.text.html.HTMLDocument#Iterator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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();
}
}
}
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;
}
}
}
}
}
private void selectReference(HTMLDocument.Iterator iter) {
htmlComponent.requestFocus();
int start = iter.getStartOffset();
htmlComponent.select(start, iter.getEndOffset());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
Rectangle rect = htmlComponent.modelToView(start);
if (rect != null) {
rect.x -= htmlComponent.getInsets().left + component.getInsets().left;
rect.height = htmlComponent.getVisibleRect().height;
htmlComponent.scrollRectToVisible(rect);
}
} catch (BadLocationException e) {}
}
});
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
}
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();
}
}
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;
}
@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;
}
private void highlightLink(int n) {
myHighlightedLink = n;
Highlighter highlighter = myEditorPane.getHighlighter();
HTMLDocument.Iterator link = getLink(n);
if (link != null) {
int startOffset = link.getStartOffset();
int endOffset = link.getEndOffset();
try {
if (myHighlightingTag == null) {
myHighlightingTag = highlighter.addHighlight(startOffset, endOffset, LINK_HIGHLIGHTER);
}
else {
highlighter.changeHighlight(myHighlightingTag, startOffset, endOffset);
}
myEditorPane.setCaretPosition(startOffset);
if (Registry.is("editor.new.mouse.hover.popups") && !ScreenReader.isActive()) {
// scrolling to target location explicitly, as we've disabled auto-scrolling to caret
myEditorPane.scrollRectToVisible(myEditorPane.modelToView(startOffset));
}
}
catch (BadLocationException e) {
LOG.warn("Error highlighting link", e);
}
}
else if (myHighlightingTag != null) {
highlighter.removeHighlight(myHighlightingTag);
myHighlightingTag = null;
}
}
private void activateLink(int n) {
HTMLDocument.Iterator link = getLink(n);
if (link != null) {
String href = (String)link.getAttributes().getAttribute(HTML.Attribute.HREF);
myManager.navigateByLink(this, href);
}
}
/**
* 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;
}