001: // MyHTMLEditorKit.java
002: // $Id: MyHTMLEditorKit.java,v 1.4 2000/08/16 21:37:30 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.editors;
007:
008: import javax.swing.JEditorPane;
009: import javax.swing.text.Document;
010: import javax.swing.text.AttributeSet;
011: import javax.swing.text.Element;
012: import javax.swing.text.html.HTML;
013: import javax.swing.text.html.HTMLEditorKit;
014: import javax.swing.text.html.HTMLDocument;
015: import javax.swing.event.HyperlinkEvent;
016: import javax.swing.event.MouseInputAdapter;
017:
018: import java.awt.event.MouseEvent;
019: import java.awt.Point;
020:
021: import java.io.Serializable;
022:
023: import java.net.URL;
024: import java.net.MalformedURLException;
025:
026: /**
027: * A special HTMLEditorKit that handle mouse event.
028: */
029: public class MyHTMLEditorKit extends HTMLEditorKit {
030:
031: public static final int JUMP = 0;
032: public static final int MOVE = 1;
033:
034: LinkController myController = new LinkController();
035:
036: /**
037: * Called when the kit is being installed into the a JEditorPane.
038: * @param c the JEditorPane.
039: */
040: public void install(JEditorPane c) {
041: c.addMouseListener(myController);
042: c.addMouseMotionListener(myController);
043: }
044:
045: /**
046: * Our MouseListener.
047: */
048: public static class LinkController extends MouseInputAdapter
049: implements Serializable {
050:
051: URL currentUrl = null;
052:
053: public void mouseClicked(MouseEvent e) {
054: JEditorPane editor = (JEditorPane) e.getSource();
055:
056: if (!editor.isEditable()) {
057: Point pt = new Point(e.getX(), e.getY());
058: try {
059: int pos = editor.viewToModel(pt);
060: if (pos >= 0) {
061: activateLink(pos, editor, JUMP);
062: }
063: } catch (IllegalArgumentException iae) {
064: }
065: }
066: }
067:
068: public void mouseMoved(MouseEvent e) {
069: JEditorPane editor = (JEditorPane) e.getSource();
070:
071: if (!editor.isEditable()) {
072: Point pt = new Point(e.getX(), e.getY());
073: try {
074: int pos = editor.viewToModel(pt);
075: if (pos >= 0) {
076: activateLink(pos, editor, MOVE);
077: }
078: } catch (IllegalArgumentException iae) {
079: }
080: }
081: }
082:
083: protected void activateLink(int pos, JEditorPane html, int type) {
084: Document doc = html.getDocument();
085: if (doc instanceof HTMLDocument) {
086: HTMLDocument hdoc = (HTMLDocument) doc;
087: Element e = hdoc.getCharacterElement(pos);
088: AttributeSet a = e.getAttributes();
089: AttributeSet anchor = (AttributeSet) a
090: .getAttribute(HTML.Tag.A);
091: String href = (anchor != null) ? (String) anchor
092: .getAttribute(HTML.Attribute.HREF) : null;
093: boolean shouldExit = false;
094:
095: HyperlinkEvent linkEvent = null;
096: if (href != null) {
097: URL u;
098: try {
099: u = new URL(hdoc.getBase(), href);
100: } catch (MalformedURLException m) {
101: u = null;
102: }
103:
104: if ((type == MOVE) && (!u.equals(currentUrl))) {
105: linkEvent = new HyperlinkEvent(html,
106: HyperlinkEvent.EventType.ENTERED, u,
107: href);
108: currentUrl = u;
109: } else if (type == JUMP) {
110: linkEvent = new HyperlinkEvent(html,
111: HyperlinkEvent.EventType.ACTIVATED, u,
112: href);
113: shouldExit = true;
114: } else {
115: return;
116: }
117: html.fireHyperlinkUpdate(linkEvent);
118: } else if (currentUrl != null) {
119: shouldExit = true;
120: }
121: if (shouldExit) {
122: linkEvent = new HyperlinkEvent(html,
123: HyperlinkEvent.EventType.EXITED,
124: currentUrl, null);
125: html.fireHyperlinkUpdate(linkEvent);
126: currentUrl = null;
127: }
128: }
129: }
130: }
131: }
|