/** * Triggers the listener to follow an HTML href link that has been clicked. * * @param e event */ public void mouseClicked(MouseEvent e) { AccessibleJLabel acc = (AccessibleJLabel) getAccessibleContext(); int stringIndexAtPoint = acc.getIndexAtPoint(e.getPoint()); if (stringIndexAtPoint < 0) { return; } AttributeSet attr = (AttributeSet) acc.getCharacterAttribute( acc.getIndexAtPoint(e.getPoint())).getAttribute(HTML.Tag.A); if (attr != null) { String href = (String) attr.getAttribute(HTML.Attribute.HREF); String target = (String) attr.getAttribute(HTML.Attribute.TARGET); try { if (listener == null) { log.warn("No HyperlinkActivatedListener available to follow HTML link for label: " + getText()); } else { listener.followHyperlink(new URL(href), target); } } catch (Exception ex) { log.error("Unable to load URL: " + href, ex); } } }
/** * Returns the accessible name of this label. * Unlike the superclass behavior, this implementation * cuts off a trailing colon (':') - if any. * * @return the label name * * @see AccessibleContext#setAccessibleName */ @Override public String getAccessibleName() { if (accessibleName != null) { return accessibleName; } String text = FormsLabel.this.getText(); if (text == null) { return super.getAccessibleName(); } return text.endsWith(":") ? text.substring(0, text.length()-1) : text; }
/** * Triggers the listener to follow an HTML href link that has been clicked. * * @param e event */ public void mouseClicked(MouseEvent e) { AccessibleJLabel acc = (AccessibleJLabel) getAccessibleContext(); int stringIndexAtPoint = acc.getIndexAtPoint(e.getPoint()); if (stringIndexAtPoint < 0) { return; } AttributeSet attr = (AttributeSet) acc.getCharacterAttribute( acc.getIndexAtPoint(e.getPoint())).getAttribute(HTML.Tag.A); if (attr != null) { String href = (String) attr.getAttribute(HTML.Attribute.HREF); String target = (String) attr.getAttribute(HTML.Attribute.TARGET); try { if (listener == null) { log.warn("No HyperlinkActivatedListener available to follow HTML link for label: " + getText()); } else { listener.followHyperlink(new URL(href), target); } } catch (Exception ex) { log.error("Unable to load URL: " + href, ex); } } }
/** * Changes the mouse cursor to a hand to indicate when the mouse moves over a clickable * HTML link. * * @param e event */ public void mouseMoved(MouseEvent e) { AccessibleJLabel acc = (AccessibleJLabel) getAccessibleContext(); int stringIndexAtPoint = acc.getIndexAtPoint(e.getPoint()); if (stringIndexAtPoint < 0) { return; } javax.swing.text.AttributeSet attr = acc.getCharacterAttribute(stringIndexAtPoint); if (attr.getAttribute(HTML.Tag.A) == null) { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } else { setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } }
/** * Changes the mouse cursor to a hand to indicate when the mouse moves over a clickable * HTML link. * * @param e event */ public void mouseMoved(MouseEvent e) { AccessibleJLabel acc = (AccessibleJLabel) getAccessibleContext(); int stringIndexAtPoint = acc.getIndexAtPoint(e.getPoint()); if (stringIndexAtPoint < 0) { return; } javax.swing.text.AttributeSet attr = acc.getCharacterAttribute(stringIndexAtPoint); if (attr.getAttribute(HTML.Tag.A) == null) { setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } else { setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); } }
@Nullable private String getLinkAtPosition(@Nonnull final Point point) { if (this.linkCache == null) { cacheLinkElements(); } final AccessibleJLabel accessibleJLabel = (AccessibleJLabel) this.getAccessibleContext().getAccessibleComponent(); final int textIndex = accessibleJLabel.getIndexAtPoint(point); for (final HtmlLinkAddress l : this.linkCache) { if (l.checkPosition(textIndex)) { return l.getHREF(); } } return null; } }
protected String getLink(Point p) { final AccessibleContext aC = getAccessibleContext(); if (aC instanceof AccessibleJLabel) { final AccessibleJLabel aL = (AccessibleJLabel) aC; final AccessibleText aT = aL.getAccessibleText(); if (aT == null) { return null; } final int index = aL.getIndexAtPoint(p); for (final LinkDescriptor entry : listLinks) { if (index >= entry.getStart() && index <= entry.getEnd()) { return entry.getUrl(); } } } return null; }