summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Kennke <roman@kennke.org>2006-11-02 14:00:45 +0000
committerRoman Kennke <roman@kennke.org>2006-11-02 14:00:45 +0000
commit94cabdb3cceb881100f56124e89016bbf9fffe67 (patch)
tree9e4a12bc27e16178ce44f086dada20fac82655e2
parent67a4082d7ea2f9cdd4e4c4afd50797d779aef367 (diff)
downloadclasspath-94cabdb3cceb881100f56124e89016bbf9fffe67.tar.gz
2006-11-02 Roman Kennke <kennke@aicas.com>
* javax/swing/JEditorPane.java (setContentType): Strip off attributes. * javax/swing/text/html/HTMLEditorKit.java (LinkController.activateLink(int,JEditorPane,int,int): New method. Implements activation of a hyperlink. (LinkController.activateLinke(int,JEditorPane)): Delegate to the other activateLink() method. (LinkController.createHyperlinkEvent): New helper method. (LinkController.mouseClicked): Implemented to activate the link. (LinkController.mouseDragged): Added comment that this method does nothing. (LinkController.mouseMoved): Update cursor for hyperlinks. (mouseHandler): Renamed field to linkController. (HTMLEditorKit): Create a link controller. (clone): Give the clone a new link controller. (deinstall): De-install link controller as mouseMotionListener too. (install): Install link controller as mouseMotionListener too.
-rw-r--r--ChangeLog20
-rw-r--r--javax/swing/JEditorPane.java7
-rw-r--r--javax/swing/text/html/HTMLEditorKit.java160
3 files changed, 151 insertions, 36 deletions
diff --git a/ChangeLog b/ChangeLog
index 7584969dc..635c089e8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,25 @@
2006-11-02 Roman Kennke <kennke@aicas.com>
+ * javax/swing/JEditorPane.java
+ (setContentType): Strip off attributes.
+ * javax/swing/text/html/HTMLEditorKit.java
+ (LinkController.activateLink(int,JEditorPane,int,int): New
+ method. Implements activation of a hyperlink.
+ (LinkController.activateLinke(int,JEditorPane)): Delegate
+ to the other activateLink() method.
+ (LinkController.createHyperlinkEvent): New helper method.
+ (LinkController.mouseClicked): Implemented to activate the link.
+ (LinkController.mouseDragged): Added comment that this
+ method does nothing.
+ (LinkController.mouseMoved): Update cursor for hyperlinks.
+ (mouseHandler): Renamed field to linkController.
+ (HTMLEditorKit): Create a link controller.
+ (clone): Give the clone a new link controller.
+ (deinstall): De-install link controller as mouseMotionListener too.
+ (install): Install link controller as mouseMotionListener too.
+
+2006-11-02 Roman Kennke <kennke@aicas.com>
+
PR 29644
* javax/swing/text/FlowView.java
(FlowStrategy.changedUpdate): Reversed condition. This caused
diff --git a/javax/swing/JEditorPane.java b/javax/swing/JEditorPane.java
index 06844355a..a503bb6e8 100644
--- a/javax/swing/JEditorPane.java
+++ b/javax/swing/JEditorPane.java
@@ -869,6 +869,13 @@ public class JEditorPane extends JTextComponent
public final void setContentType(String type)
{
+ // Strip off content type parameters.
+ int paramIndex = type.indexOf(';');
+ if (paramIndex > -1)
+ {
+ // TODO: Handle character encoding.
+ type = type.substring(0, paramIndex).trim();
+ }
if (editorKit != null
&& editorKit.getContentType().equals(type))
return;
diff --git a/javax/swing/text/html/HTMLEditorKit.java b/javax/swing/text/html/HTMLEditorKit.java
index 0c13c46d0..fa879bc49 100644
--- a/javax/swing/text/html/HTMLEditorKit.java
+++ b/javax/swing/text/html/HTMLEditorKit.java
@@ -46,6 +46,7 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.Cursor;
+import java.awt.Point;
import java.io.IOException;
import java.io.InputStream;
@@ -54,12 +55,17 @@ import java.io.Reader;
import java.io.Serializable;
import java.io.StringReader;
import java.io.Writer;
+import java.net.MalformedURLException;
+import java.net.URL;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.swing.Action;
import javax.swing.JEditorPane;
+import javax.swing.SwingUtilities;
+import javax.swing.event.HyperlinkEvent;
+import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.EditorKit;
@@ -112,11 +118,14 @@ public class HTMLEditorKit
*/
public void mouseClicked(MouseEvent e)
{
- /*
- These MouseInputAdapter methods generate mouse appropriate events around
- hyperlinks (entering, exiting, and activating).
- */
- // FIXME: Not implemented.
+ JEditorPane editor = (JEditorPane) e.getSource();
+ if (! editor.isEditable() && SwingUtilities.isLeftMouseButton(e))
+ {
+ Point loc = e.getPoint();
+ int pos = editor.viewToModel(loc);
+ if (pos >= 0)
+ activateLink(pos, editor, e.getX(), e.getY());
+ }
}
/**
@@ -126,11 +135,7 @@ public class HTMLEditorKit
*/
public void mouseDragged(MouseEvent e)
{
- /*
- These MouseInputAdapter methods generate mouse appropriate events around
- hyperlinks (entering, exiting, and activating).
- */
- // FIXME: Not implemented.
+ // Nothing to do here.
}
/**
@@ -140,29 +145,111 @@ public class HTMLEditorKit
*/
public void mouseMoved(MouseEvent e)
{
- /*
- These MouseInputAdapter methods generate mouse appropriate events around
- hyperlinks (entering, exiting, and activating).
- */
- // FIXME: Not implemented.
+ JEditorPane editor = (JEditorPane) e.getSource();
+ HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit();
+ if (! editor.isEditable())
+ {
+ Document doc = editor.getDocument();
+ if (doc instanceof HTMLDocument)
+ {
+ Cursor newCursor = kit.getDefaultCursor();
+ HTMLDocument htmlDoc = (HTMLDocument) doc;
+ Point loc = e.getPoint();
+ int pos = editor.viewToModel(loc);
+ Element el = htmlDoc.getCharacterElement(pos);
+ if (pos < el.getStartOffset() || pos >= el.getEndOffset())
+ el = null;
+ if (el != null)
+ {
+ AttributeSet aAtts = (AttributeSet)
+ el.getAttributes().getAttribute(HTML.Tag.A);
+ if (aAtts != null)
+ newCursor = kit.getLinkCursor();
+ }
+ if (editor.getCursor() != newCursor)
+ editor.setCursor(newCursor);
+ }
+ }
}
-
+
/**
* If the given position represents a link, then linkActivated is called
- * on the JEditorPane. Implemented to forward to the method with the same
- * name, but pos == editor == -1.
- *
- * @param pos - the position
- * @param editor - the editor pane
+ * on the JEditorPane.
+ *
+ * @param pos the position
+ * @param editor the editor pane
*/
- protected void activateLink(int pos,
- JEditorPane editor)
+ protected void activateLink(int pos, JEditorPane editor)
{
- /*
- This method creates and fires a HyperlinkEvent if the document is an
- instance of HTMLDocument and the href tag of the link is not null.
- */
- // FIXME: Not implemented.
+ activateLink(pos, editor);
+ }
+
+ private void activateLink(int pos, JEditorPane editor, int x, int y)
+ {
+ // TODO: This is here for future extension for mapped links support.
+ // For the time beeing we implement simple hyperlinks.
+ Document doc = editor.getDocument();
+ if (doc instanceof HTMLDocument)
+ {
+ HTMLDocument htmlDoc = (HTMLDocument) doc;
+ Element el = htmlDoc.getCharacterElement(pos);
+ AttributeSet atts = el.getAttributes();
+ AttributeSet anchorAtts =
+ (AttributeSet) atts.getAttribute(HTML.Tag.A);
+ String href = null;
+ if (anchorAtts != null)
+ {
+ href = (String) anchorAtts.getAttribute(HTML.Attribute.HREF);
+ }
+ else
+ {
+ // TODO: Implement link maps here.
+ }
+ HyperlinkEvent event = null;
+ if (href != null)
+ event = createHyperlinkEvent(editor, htmlDoc, href,
+ anchorAtts, el);
+ if (event != null)
+ editor.fireHyperlinkUpdate(event);
+ }
+
+ }
+
+ /**
+ * Creates a HyperlinkEvent for the specified href and anchor if
+ * possible. If for some reason this won't work, return null.
+ *
+ * @param editor the editor
+ * @param doc the document
+ * @param href the href link
+ * @param anchor the anchor
+ * @param el the element
+ *
+ * @return the hyperlink event, or <code>null</code> if we couldn't
+ * create one
+ */
+ private HyperlinkEvent createHyperlinkEvent(JEditorPane editor,
+ HTMLDocument doc,
+ String href,
+ AttributeSet anchor,
+ Element el)
+ {
+ URL url;
+ try
+ {
+ URL base = doc.getBase();
+ url = new URL(base, href);
+
+ }
+ catch (MalformedURLException ex)
+ {
+ url = null;
+ }
+ // TODO: Handle frame documents and target here.
+ HyperlinkEvent ev =
+ new HyperlinkEvent(editor, HyperlinkEvent.EventType.ACTIVATED, url,
+ href, el);
+ return ev;
}
}
@@ -831,7 +918,7 @@ public class HTMLEditorKit
/**
* The mouse listener used for links.
*/
- LinkController mouseListener;
+ private LinkController linkController;
/** The content type */
String contentType = "text/html";
@@ -847,7 +934,7 @@ public class HTMLEditorKit
*/
public HTMLEditorKit()
{
- // Nothing to do here.
+ linkController = new LinkController();
}
/**
@@ -1016,7 +1103,9 @@ public class HTMLEditorKit
public Object clone()
{
// FIXME: Need to clone all fields
- return (HTMLEditorKit) super.clone();
+ HTMLEditorKit copy = (HTMLEditorKit) super.clone();
+ copy.linkController = new LinkController();
+ return copy;
}
/**
@@ -1043,10 +1132,9 @@ public class HTMLEditorKit
public void install(JEditorPane c)
{
super.install(c);
- mouseListener = new LinkController();
- c.addMouseListener(mouseListener);
+ c.addMouseListener(linkController);
+ c.addMouseMotionListener(linkController);
editorPane = c;
- // FIXME: need to set up hyperlinklistener object
}
/**
@@ -1058,8 +1146,8 @@ public class HTMLEditorKit
public void deinstall(JEditorPane c)
{
super.deinstall(c);
- c.removeMouseListener(mouseListener);
- mouseListener = null;
+ c.removeMouseListener(linkController);
+ c.removeMouseMotionListener(linkController);
editorPane = null;
}