下面列出了javax.swing.text.html.HTMLDocument#getIterator ( ) 实例代码,或者点击链接到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 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();
}
}
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;
}
}
}
}
}
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();
}
}
}
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;
}
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();
}
}
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();
}
}
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 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();
}
}
/**
* 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;
}