Class to watch the associated component and fire
hyperlink events on it when appropriate.
| Method from javax.swing.text.html.HTMLEditorKit$LinkController Detail: |
protected void activateLink(int pos,
JEditorPane editor) {
activateLink(pos, editor, null);
}
Calls linkActivated on the associated JEditorPane
if the given position represents a link.This is implemented
to forward to the method with the same name, but with the following
args both == -1. |
void activateLink(int pos,
JEditorPane html,
MouseEvent mouseEvent) {
Document doc = html.getDocument();
if (doc instanceof HTMLDocument) {
HTMLDocument hdoc = (HTMLDocument) doc;
Element e = hdoc.getCharacterElement(pos);
AttributeSet a = e.getAttributes();
AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A);
HyperlinkEvent linkEvent = null;
String description;
int x = -1;
int y = -1;
if (mouseEvent != null) {
x = mouseEvent.getX();
y = mouseEvent.getY();
}
if (anchor == null) {
href = getMapHREF(html, hdoc, e, a, pos, x, y);
}
else {
href = (String)anchor.getAttribute(HTML.Attribute.HREF);
}
if (href != null) {
linkEvent = createHyperlinkEvent(html, hdoc, href, anchor,
e, mouseEvent);
}
if (linkEvent != null) {
html.fireHyperlinkUpdate(linkEvent);
}
}
}
Calls linkActivated on the associated JEditorPane
if the given position represents a link. If this was the result
of a mouse click, x and
y will give the location of the mouse, otherwise
they will be < 0. |
HyperlinkEvent createHyperlinkEvent(JEditorPane html,
HTMLDocument hdoc,
String href,
AttributeSet anchor,
Element element,
MouseEvent mouseEvent) {
URL u;
try {
URL base = hdoc.getBase();
u = new URL(base, href);
// Following is a workaround for 1.2, in which
// new URL("file://...", "#...") causes the filename to
// be lost.
if (href != null && "file".equals(u.getProtocol()) &&
href.startsWith("#")) {
String baseFile = base.getFile();
String newFile = u.getFile();
if (baseFile != null && newFile != null &&
!newFile.startsWith(baseFile)) {
u = new URL(base, baseFile + href);
}
}
} catch (MalformedURLException m) {
u = null;
}
HyperlinkEvent linkEvent = null;
if (!hdoc.isFrameDocument()) {
linkEvent = new HyperlinkEvent(
html, HyperlinkEvent.EventType.ACTIVATED, u, href,
element, mouseEvent);
} else {
String target = (anchor != null) ?
(String)anchor.getAttribute(HTML.Attribute.TARGET) : null;
if ((target == null) || (target.equals(""))) {
target = hdoc.getBaseTarget();
}
if ((target == null) || (target.equals(""))) {
target = "_self";
}
linkEvent = new HTMLFrameHyperlinkEvent(
html, HyperlinkEvent.EventType.ACTIVATED, u, href,
element, mouseEvent, target);
}
return linkEvent;
}
Creates and returns a new instance of HyperlinkEvent. If
hdoc is a frame document a HTMLFrameHyperlinkEvent
will be created. |
void fireEvents(JEditorPane editor,
HTMLDocument doc,
String href,
Element lastElem,
MouseEvent mouseEvent) {
if (this.href != null) {
// fire an exited event on the old link
URL u;
try {
u = new URL(doc.getBase(), this.href);
} catch (MalformedURLException m) {
u = null;
}
HyperlinkEvent exit = new HyperlinkEvent(editor,
HyperlinkEvent.EventType.EXITED, u, this.href,
lastElem, mouseEvent);
editor.fireHyperlinkUpdate(exit);
}
if (href != null) {
// fire an entered event on the new link
URL u;
try {
u = new URL(doc.getBase(), href);
} catch (MalformedURLException m) {
u = null;
}
HyperlinkEvent entered = new HyperlinkEvent(editor,
HyperlinkEvent.EventType.ENTERED,
u, href, curElem, mouseEvent);
editor.fireHyperlinkUpdate(entered);
}
}
|
public void mouseClicked(MouseEvent e) {
JEditorPane editor = (JEditorPane) e.getSource();
if (! editor.isEditable() && editor.isEnabled() &&
SwingUtilities.isLeftMouseButton(e)) {
Point pt = new Point(e.getX(), e.getY());
int pos = editor.viewToModel(pt);
if (pos >= 0) {
activateLink(pos, editor, e);
}
}
}
Called for a mouse click event.
If the component is read-only (ie a browser) then
the clicked event is used to drive an attempt to
follow the reference specified by a link. |
public void mouseDragged(MouseEvent e) {
}
|
public void mouseMoved(MouseEvent e) {
JEditorPane editor = (JEditorPane) e.getSource();
if (!editor.isEnabled()) {
return;
}
HTMLEditorKit kit = (HTMLEditorKit)editor.getEditorKit();
boolean adjustCursor = true;
Cursor newCursor = kit.getDefaultCursor();
if (!editor.isEditable()) {
Point pt = new Point(e.getX(), e.getY());
int pos = editor.getUI().viewToModel(editor, pt, bias);
if (bias[0] == Position.Bias.Backward && pos > 0) {
pos--;
}
if (pos >= 0 &&(editor.getDocument() instanceof HTMLDocument)){
HTMLDocument hdoc = (HTMLDocument)editor.getDocument();
Element elem = hdoc.getCharacterElement(pos);
if (!doesElementContainLocation(editor, elem, pos,
e.getX(), e.getY())) {
elem = null;
}
if (curElem != elem || curElemImage) {
Element lastElem = curElem;
curElem = elem;
String href = null;
curElemImage = false;
if (elem != null) {
AttributeSet a = elem.getAttributes();
AttributeSet anchor = (AttributeSet)a.
getAttribute(HTML.Tag.A);
if (anchor == null) {
curElemImage = (a.getAttribute(StyleConstants.
NameAttribute) == HTML.Tag.IMG);
if (curElemImage) {
href = getMapHREF(editor, hdoc, elem, a,
pos, e.getX(), e.getY());
}
}
else {
href = (String)anchor.getAttribute
(HTML.Attribute.HREF);
}
}
if (href != this.href) {
// reference changed, fire event(s)
fireEvents(editor, hdoc, href, lastElem, e);
this.href = href;
if (href != null) {
newCursor = kit.getLinkCursor();
}
}
else {
adjustCursor = false;
}
}
else {
adjustCursor = false;
}
curOffset = pos;
}
}
if (adjustCursor && editor.getCursor() != newCursor) {
editor.setCursor(newCursor);
}
}
|