0N/A/*
3909N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage javax.swing.text.html;
0N/A
2896N/Aimport sun.awt.AppContext;
2896N/A
0N/Aimport java.lang.reflect.Method;
0N/Aimport java.awt.*;
0N/Aimport java.awt.event.*;
0N/Aimport java.io.*;
0N/Aimport java.net.MalformedURLException;
0N/Aimport java.net.URL;
0N/Aimport javax.swing.text.*;
0N/Aimport javax.swing.*;
0N/Aimport javax.swing.border.*;
0N/Aimport javax.swing.event.*;
0N/Aimport javax.swing.plaf.TextUI;
0N/Aimport java.util.*;
0N/Aimport javax.accessibility.*;
0N/Aimport java.lang.ref.*;
0N/A
0N/A/**
0N/A * The Swing JEditorPane text component supports different kinds
0N/A * of content via a plug-in mechanism called an EditorKit. Because
0N/A * HTML is a very popular format of content, some support is provided
0N/A * by default. The default support is provided by this class, which
0N/A * supports HTML version 3.2 (with some extensions), and is migrating
0N/A * toward version 4.0.
0N/A * The <applet> tag is not supported, but some support is provided
0N/A * for the <object> tag.
0N/A * <p>
0N/A * There are several goals of the HTML EditorKit provided, that have
0N/A * an effect upon the way that HTML is modeled. These
0N/A * have influenced its design in a substantial way.
0N/A * <dl>
0N/A * <p>
0N/A * <dt>
0N/A * Support editing
0N/A * <dd>
0N/A * It might seem fairly obvious that a plug-in for JEditorPane
0N/A * should provide editing support, but that fact has several
0N/A * design considerations. There are a substantial number of HTML
0N/A * documents that don't properly conform to an HTML specification.
0N/A * These must be normalized somewhat into a correct form if one
0N/A * is to edit them. Additionally, users don't like to be presented
0N/A * with an excessive amount of structure editing, so using traditional
0N/A * text editing gestures is preferred over using the HTML structure
0N/A * exactly as defined in the HTML document.
0N/A * <p>
0N/A * The modeling of HTML is provided by the class <code>HTMLDocument</code>.
0N/A * Its documention describes the details of how the HTML is modeled.
0N/A * The editing support leverages heavily off of the text package.
0N/A * <p>
0N/A * <dt>
0N/A * Extendable/Scalable
0N/A * <dd>
0N/A * To maximize the usefulness of this kit, a great deal of effort
0N/A * has gone into making it extendable. These are some of the
0N/A * features.
0N/A * <ol>
0N/A * <li>
0N/A * The parser is replacable. The default parser is the Hot Java
0N/A * parser which is DTD based. A different DTD can be used, or an
0N/A * entirely different parser can be used. To change the parser,
0N/A * reimplement the getParser method. The default parser is
0N/A * dynamically loaded when first asked for, so the class files
0N/A * will never be loaded if an alternative parser is used. The
0N/A * default parser is in a separate package called parser below
0N/A * this package.
0N/A * <li>
0N/A * The parser drives the ParserCallback, which is provided by
0N/A * HTMLDocument. To change the callback, subclass HTMLDocument
0N/A * and reimplement the createDefaultDocument method to return
0N/A * document that produces a different reader. The reader controls
0N/A * how the document is structured. Although the Document provides
0N/A * HTML support by default, there is nothing preventing support of
0N/A * non-HTML tags that result in alternative element structures.
0N/A * <li>
0N/A * The default view of the models are provided as a hierarchy of
0N/A * View implementations, so one can easily customize how a particular
0N/A * element is displayed or add capabilities for new kinds of elements
0N/A * by providing new View implementations. The default set of views
0N/A * are provided by the <code>HTMLFactory</code> class. This can
0N/A * be easily changed by subclassing or replacing the HTMLFactory
0N/A * and reimplementing the getViewFactory method to return the alternative
0N/A * factory.
0N/A * <li>
0N/A * The View implementations work primarily off of CSS attributes,
0N/A * which are kept in the views. This makes it possible to have
0N/A * multiple views mapped over the same model that appear substantially
0N/A * different. This can be especially useful for printing. For
0N/A * most HTML attributes, the HTML attributes are converted to CSS
0N/A * attributes for display. This helps make the View implementations
0N/A * more general purpose
0N/A * </ol>
0N/A * <p>
0N/A * <dt>
0N/A * Asynchronous Loading
0N/A * <dd>
0N/A * Larger documents involve a lot of parsing and take some time
0N/A * to load. By default, this kit produces documents that will be
0N/A * loaded asynchronously if loaded using <code>JEditorPane.setPage</code>.
0N/A * This is controlled by a property on the document. The method
3370N/A * {@link #createDefaultDocument createDefaultDocument} can
0N/A * be overriden to change this. The batching of work is done
0N/A * by the <code>HTMLDocument.HTMLReader</code> class. The actual
0N/A * work is done by the <code>DefaultStyledDocument</code> and
0N/A * <code>AbstractDocument</code> classes in the text package.
0N/A * <p>
0N/A * <dt>
0N/A * Customization from current LAF
0N/A * <dd>
0N/A * HTML provides a well known set of features without exactly
0N/A * specifying the display characteristics. Swing has a theme
0N/A * mechanism for its look-and-feel implementations. It is desirable
0N/A * for the look-and-feel to feed display characteristics into the
0N/A * HTML views. An user with poor vision for example would want
0N/A * high contrast and larger than typical fonts.
0N/A * <p>
0N/A * The support for this is provided by the <code>StyleSheet</code>
0N/A * class. The presentation of the HTML can be heavily influenced
0N/A * by the setting of the StyleSheet property on the EditorKit.
0N/A * <p>
0N/A * <dt>
0N/A * Not lossy
0N/A * <dd>
0N/A * An EditorKit has the ability to be read and save documents.
0N/A * It is generally the most pleasing to users if there is no loss
0N/A * of data between the two operation. The policy of the HTMLEditorKit
0N/A * will be to store things not recognized or not necessarily visible
0N/A * so they can be subsequently written out. The model of the HTML document
0N/A * should therefore contain all information discovered while reading the
0N/A * document. This is constrained in some ways by the need to support
0N/A * editing (i.e. incorrect documents sometimes must be normalized).
0N/A * The guiding principle is that information shouldn't be lost, but
0N/A * some might be synthesized to produce a more correct model or it might
0N/A * be rearranged.
0N/A * </dl>
0N/A *
0N/A * @author Timothy Prinzing
0N/A */
0N/Apublic class HTMLEditorKit extends StyledEditorKit implements Accessible {
0N/A
0N/A private JEditorPane theEditor;
0N/A
0N/A /**
0N/A * Constructs an HTMLEditorKit, creates a StyleContext,
0N/A * and loads the style sheet.
0N/A */
0N/A public HTMLEditorKit() {
0N/A
0N/A }
0N/A
0N/A /**
0N/A * Get the MIME type of the data that this
0N/A * kit represents support for. This kit supports
0N/A * the type <code>text/html</code>.
0N/A *
0N/A * @return the type
0N/A */
0N/A public String getContentType() {
0N/A return "text/html";
0N/A }
0N/A
0N/A /**
0N/A * Fetch a factory that is suitable for producing
0N/A * views of any models that are produced by this
0N/A * kit.
0N/A *
0N/A * @return the factory
0N/A */
0N/A public ViewFactory getViewFactory() {
0N/A return defaultFactory;
0N/A }
0N/A
0N/A /**
0N/A * Create an uninitialized text storage model
0N/A * that is appropriate for this type of editor.
0N/A *
0N/A * @return the model
0N/A */
0N/A public Document createDefaultDocument() {
0N/A StyleSheet styles = getStyleSheet();
0N/A StyleSheet ss = new StyleSheet();
0N/A
0N/A ss.addStyleSheet(styles);
0N/A
0N/A HTMLDocument doc = new HTMLDocument(ss);
0N/A doc.setParser(getParser());
0N/A doc.setAsynchronousLoadPriority(4);
0N/A doc.setTokenThreshold(100);
0N/A return doc;
0N/A }
0N/A
0N/A /**
0N/A * Try to get an HTML parser from the document. If no parser is set for
0N/A * the document, return the editor kit's default parser. It is an error
0N/A * if no parser could be obtained from the editor kit.
0N/A */
0N/A private Parser ensureParser(HTMLDocument doc) throws IOException {
0N/A Parser p = doc.getParser();
0N/A if (p == null) {
0N/A p = getParser();
0N/A }
0N/A if (p == null) {
0N/A throw new IOException("Can't load parser");
0N/A }
0N/A return p;
0N/A }
0N/A
0N/A /**
0N/A * Inserts content from the given stream. If <code>doc</code> is
0N/A * an instance of HTMLDocument, this will read
0N/A * HTML 3.2 text. Inserting HTML into a non-empty document must be inside
0N/A * the body Element, if you do not insert into the body an exception will
0N/A * be thrown. When inserting into a non-empty document all tags outside
0N/A * of the body (head, title) will be dropped.
0N/A *
0N/A * @param in the stream to read from
0N/A * @param doc the destination for the insertion
0N/A * @param pos the location in the document to place the
0N/A * content
0N/A * @exception IOException on any I/O error
0N/A * @exception BadLocationException if pos represents an invalid
0N/A * location within the document
0N/A * @exception RuntimeException (will eventually be a BadLocationException)
0N/A * if pos is invalid
0N/A */
0N/A public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException {
0N/A
0N/A if (doc instanceof HTMLDocument) {
0N/A HTMLDocument hdoc = (HTMLDocument) doc;
0N/A if (pos > doc.getLength()) {
0N/A throw new BadLocationException("Invalid location", pos);
0N/A }
0N/A
0N/A Parser p = ensureParser(hdoc);
0N/A ParserCallback receiver = hdoc.getReader(pos);
0N/A Boolean ignoreCharset = (Boolean)doc.getProperty("IgnoreCharsetDirective");
0N/A p.parse(in, receiver, (ignoreCharset == null) ? false : ignoreCharset.booleanValue());
0N/A receiver.flush();
0N/A } else {
0N/A super.read(in, doc, pos);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Inserts HTML into an existing document.
0N/A *
0N/A * @param doc the document to insert into
0N/A * @param offset the offset to insert HTML at
0N/A * @param popDepth the number of ElementSpec.EndTagTypes to generate before
0N/A * inserting
0N/A * @param pushDepth the number of ElementSpec.StartTagTypes with a direction
0N/A * of ElementSpec.JoinNextDirection that should be generated
0N/A * before inserting, but after the end tags have been generated
0N/A * @param insertTag the first tag to start inserting into document
0N/A * @exception RuntimeException (will eventually be a BadLocationException)
0N/A * if pos is invalid
0N/A */
0N/A public void insertHTML(HTMLDocument doc, int offset, String html,
0N/A int popDepth, int pushDepth,
0N/A HTML.Tag insertTag) throws
0N/A BadLocationException, IOException {
0N/A if (offset > doc.getLength()) {
0N/A throw new BadLocationException("Invalid location", offset);
0N/A }
0N/A
0N/A Parser p = ensureParser(doc);
0N/A ParserCallback receiver = doc.getReader(offset, popDepth, pushDepth,
0N/A insertTag);
0N/A Boolean ignoreCharset = (Boolean)doc.getProperty
0N/A ("IgnoreCharsetDirective");
0N/A p.parse(new StringReader(html), receiver, (ignoreCharset == null) ?
0N/A false : ignoreCharset.booleanValue());
0N/A receiver.flush();
0N/A }
0N/A
0N/A /**
0N/A * Write content from a document to the given stream
0N/A * in a format appropriate for this kind of content handler.
0N/A *
0N/A * @param out the stream to write to
0N/A * @param doc the source for the write
0N/A * @param pos the location in the document to fetch the
0N/A * content
0N/A * @param len the amount to write out
0N/A * @exception IOException on any I/O error
0N/A * @exception BadLocationException if pos represents an invalid
0N/A * location within the document
0N/A */
0N/A public void write(Writer out, Document doc, int pos, int len)
0N/A throws IOException, BadLocationException {
0N/A
0N/A if (doc instanceof HTMLDocument) {
0N/A HTMLWriter w = new HTMLWriter(out, (HTMLDocument)doc, pos, len);
0N/A w.write();
0N/A } else if (doc instanceof StyledDocument) {
0N/A MinimalHTMLWriter w = new MinimalHTMLWriter(out, (StyledDocument)doc, pos, len);
0N/A w.write();
0N/A } else {
0N/A super.write(out, doc, pos, len);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Called when the kit is being installed into the
0N/A * a JEditorPane.
0N/A *
0N/A * @param c the JEditorPane
0N/A */
0N/A public void install(JEditorPane c) {
0N/A c.addMouseListener(linkHandler);
0N/A c.addMouseMotionListener(linkHandler);
0N/A c.addCaretListener(nextLinkAction);
0N/A super.install(c);
0N/A theEditor = c;
0N/A }
0N/A
0N/A /**
0N/A * Called when the kit is being removed from the
0N/A * JEditorPane. This is used to unregister any
0N/A * listeners that were attached.
0N/A *
0N/A * @param c the JEditorPane
0N/A */
0N/A public void deinstall(JEditorPane c) {
0N/A c.removeMouseListener(linkHandler);
0N/A c.removeMouseMotionListener(linkHandler);
0N/A c.removeCaretListener(nextLinkAction);
0N/A super.deinstall(c);
0N/A theEditor = null;
0N/A }
0N/A
0N/A /**
0N/A * Default Cascading Style Sheet file that sets
0N/A * up the tag views.
0N/A */
0N/A public static final String DEFAULT_CSS = "default.css";
0N/A
0N/A /**
0N/A * Set the set of styles to be used to render the various
0N/A * HTML elements. These styles are specified in terms of
0N/A * CSS specifications. Each document produced by the kit
0N/A * will have a copy of the sheet which it can add the
0N/A * document specific styles to. By default, the StyleSheet
0N/A * specified is shared by all HTMLEditorKit instances.
0N/A * This should be reimplemented to provide a finer granularity
0N/A * if desired.
0N/A */
0N/A public void setStyleSheet(StyleSheet s) {
2896N/A if (s == null) {
2896N/A AppContext.getAppContext().remove(DEFAULT_STYLES_KEY);
2896N/A } else {
2896N/A AppContext.getAppContext().put(DEFAULT_STYLES_KEY, s);
2896N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the set of styles currently being used to render the
0N/A * HTML elements. By default the resource specified by
0N/A * DEFAULT_CSS gets loaded, and is shared by all HTMLEditorKit
0N/A * instances.
0N/A */
0N/A public StyleSheet getStyleSheet() {
2896N/A AppContext appContext = AppContext.getAppContext();
2896N/A StyleSheet defaultStyles = (StyleSheet) appContext.get(DEFAULT_STYLES_KEY);
2896N/A
0N/A if (defaultStyles == null) {
0N/A defaultStyles = new StyleSheet();
2896N/A appContext.put(DEFAULT_STYLES_KEY, defaultStyles);
0N/A try {
0N/A InputStream is = HTMLEditorKit.getResourceAsStream(DEFAULT_CSS);
0N/A Reader r = new BufferedReader(
0N/A new InputStreamReader(is, "ISO-8859-1"));
0N/A defaultStyles.loadRules(r, null);
0N/A r.close();
0N/A } catch (Throwable e) {
0N/A // on error we simply have no styles... the html
0N/A // will look mighty wrong but still function.
0N/A }
0N/A }
0N/A return defaultStyles;
0N/A }
0N/A
0N/A /**
0N/A * Fetch a resource relative to the HTMLEditorKit classfile.
0N/A * If this is called on 1.2 the loading will occur under the
0N/A * protection of a doPrivileged call to allow the HTMLEditorKit
0N/A * to function when used in an applet.
0N/A *
0N/A * @param name the name of the resource, relative to the
0N/A * HTMLEditorKit class
0N/A * @return a stream representing the resource
0N/A */
0N/A static InputStream getResourceAsStream(String name) {
0N/A try {
0N/A return ResourceLoader.getResourceAsStream(name);
0N/A } catch (Throwable e) {
0N/A // If the class doesn't exist or we have some other
0N/A // problem we just try to call getResourceAsStream directly.
0N/A return HTMLEditorKit.class.getResourceAsStream(name);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Fetches the command list for the editor. This is
0N/A * the list of commands supported by the superclass
0N/A * augmented by the collection of commands defined
0N/A * locally for style operations.
0N/A *
0N/A * @return the command list
0N/A */
0N/A public Action[] getActions() {
0N/A return TextAction.augmentList(super.getActions(), this.defaultActions);
0N/A }
0N/A
0N/A /**
0N/A * Copies the key/values in <code>element</code>s AttributeSet into
0N/A * <code>set</code>. This does not copy component, icon, or element
0N/A * names attributes. Subclasses may wish to refine what is and what
0N/A * isn't copied here. But be sure to first remove all the attributes that
0N/A * are in <code>set</code>.<p>
0N/A * This is called anytime the caret moves over a different location.
0N/A *
0N/A */
0N/A protected void createInputAttributes(Element element,
0N/A MutableAttributeSet set) {
0N/A set.removeAttributes(set);
0N/A set.addAttributes(element.getAttributes());
0N/A set.removeAttribute(StyleConstants.ComposedTextAttribute);
0N/A
0N/A Object o = set.getAttribute(StyleConstants.NameAttribute);
0N/A if (o instanceof HTML.Tag) {
0N/A HTML.Tag tag = (HTML.Tag)o;
0N/A // PENDING: we need a better way to express what shouldn't be
0N/A // copied when editing...
0N/A if(tag == HTML.Tag.IMG) {
0N/A // Remove the related image attributes, src, width, height
0N/A set.removeAttribute(HTML.Attribute.SRC);
0N/A set.removeAttribute(HTML.Attribute.HEIGHT);
0N/A set.removeAttribute(HTML.Attribute.WIDTH);
0N/A set.addAttribute(StyleConstants.NameAttribute,
0N/A HTML.Tag.CONTENT);
0N/A }
0N/A else if (tag == HTML.Tag.HR || tag == HTML.Tag.BR) {
0N/A // Don't copy HRs or BRs either.
0N/A set.addAttribute(StyleConstants.NameAttribute,
0N/A HTML.Tag.CONTENT);
0N/A }
0N/A else if (tag == HTML.Tag.COMMENT) {
0N/A // Don't copy COMMENTs either
0N/A set.addAttribute(StyleConstants.NameAttribute,
0N/A HTML.Tag.CONTENT);
0N/A set.removeAttribute(HTML.Attribute.COMMENT);
0N/A }
0N/A else if (tag == HTML.Tag.INPUT) {
0N/A // or INPUT either
0N/A set.addAttribute(StyleConstants.NameAttribute,
0N/A HTML.Tag.CONTENT);
0N/A set.removeAttribute(HTML.Tag.INPUT);
0N/A }
0N/A else if (tag instanceof HTML.UnknownTag) {
0N/A // Don't copy unknowns either:(
0N/A set.addAttribute(StyleConstants.NameAttribute,
0N/A HTML.Tag.CONTENT);
0N/A set.removeAttribute(HTML.Attribute.ENDTAG);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the input attributes used for the styled
0N/A * editing actions.
0N/A *
0N/A * @return the attribute set
0N/A */
0N/A public MutableAttributeSet getInputAttributes() {
0N/A if (input == null) {
0N/A input = getStyleSheet().addStyle(null, null);
0N/A }
0N/A return input;
0N/A }
0N/A
0N/A /**
0N/A * Sets the default cursor.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public void setDefaultCursor(Cursor cursor) {
0N/A defaultCursor = cursor;
0N/A }
0N/A
0N/A /**
0N/A * Returns the default cursor.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public Cursor getDefaultCursor() {
0N/A return defaultCursor;
0N/A }
0N/A
0N/A /**
0N/A * Sets the cursor to use over links.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public void setLinkCursor(Cursor cursor) {
0N/A linkCursor = cursor;
0N/A }
0N/A
0N/A /**
0N/A * Returns the cursor to use over hyper links.
0N/A * @since 1.3
0N/A */
0N/A public Cursor getLinkCursor() {
0N/A return linkCursor;
0N/A }
0N/A
0N/A /**
0N/A * Indicates whether an html form submission is processed automatically
0N/A * or only <code>FormSubmitEvent</code> is fired.
0N/A *
0N/A * @return true if html form submission is processed automatically,
0N/A * false otherwise.
0N/A *
0N/A * @see #setAutoFormSubmission
0N/A * @since 1.5
0N/A */
0N/A public boolean isAutoFormSubmission() {
0N/A return isAutoFormSubmission;
0N/A }
0N/A
0N/A /**
0N/A * Specifies if an html form submission is processed
0N/A * automatically or only <code>FormSubmitEvent</code> is fired.
0N/A * By default it is set to true.
0N/A *
3370N/A * @see #isAutoFormSubmission()
0N/A * @see FormSubmitEvent
0N/A * @since 1.5
0N/A */
0N/A public void setAutoFormSubmission(boolean isAuto) {
0N/A isAutoFormSubmission = isAuto;
0N/A }
0N/A
0N/A /**
0N/A * Creates a copy of the editor kit.
0N/A *
0N/A * @return the copy
0N/A */
0N/A public Object clone() {
0N/A HTMLEditorKit o = (HTMLEditorKit)super.clone();
0N/A if (o != null) {
0N/A o.input = null;
0N/A o.linkHandler = new LinkController();
0N/A }
0N/A return o;
0N/A }
0N/A
0N/A /**
0N/A * Fetch the parser to use for reading HTML streams.
0N/A * This can be reimplemented to provide a different
0N/A * parser. The default implementation is loaded dynamically
0N/A * to avoid the overhead of loading the default parser if
0N/A * it's not used. The default parser is the HotJava parser
0N/A * using an HTML 3.2 DTD.
0N/A */
0N/A protected Parser getParser() {
0N/A if (defaultParser == null) {
0N/A try {
0N/A Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator");
0N/A defaultParser = (Parser) c.newInstance();
0N/A } catch (Throwable e) {
0N/A }
0N/A }
0N/A return defaultParser;
0N/A }
0N/A
0N/A // ----- Accessibility support -----
0N/A private AccessibleContext accessibleContext;
0N/A
0N/A /**
0N/A * returns the AccessibleContext associated with this editor kit
0N/A *
0N/A * @return the AccessibleContext associated with this editor kit
0N/A * @since 1.4
0N/A */
0N/A public AccessibleContext getAccessibleContext() {
0N/A if (theEditor == null) {
0N/A return null;
0N/A }
0N/A if (accessibleContext == null) {
0N/A AccessibleHTML a = new AccessibleHTML(theEditor);
0N/A accessibleContext = a.getAccessibleContext();
0N/A }
0N/A return accessibleContext;
0N/A }
0N/A
0N/A // --- variables ------------------------------------------
0N/A
0N/A private static final Cursor MoveCursor = Cursor.getPredefinedCursor
0N/A (Cursor.HAND_CURSOR);
0N/A private static final Cursor DefaultCursor = Cursor.getPredefinedCursor
0N/A (Cursor.DEFAULT_CURSOR);
0N/A
0N/A /** Shared factory for creating HTML Views. */
0N/A private static final ViewFactory defaultFactory = new HTMLFactory();
0N/A
0N/A MutableAttributeSet input;
2896N/A private static final Object DEFAULT_STYLES_KEY = new Object();
0N/A private LinkController linkHandler = new LinkController();
0N/A private static Parser defaultParser = null;
0N/A private Cursor defaultCursor = DefaultCursor;
0N/A private Cursor linkCursor = MoveCursor;
0N/A private boolean isAutoFormSubmission = true;
0N/A
0N/A /**
0N/A * Class to watch the associated component and fire
0N/A * hyperlink events on it when appropriate.
0N/A */
0N/A public static class LinkController extends MouseAdapter implements MouseMotionListener, Serializable {
0N/A private Element curElem = null;
0N/A /**
0N/A * If true, the current element (curElem) represents an image.
0N/A */
0N/A private boolean curElemImage = false;
0N/A private String href = null;
0N/A /** This is used by viewToModel to avoid allocing a new array each
0N/A * time. */
0N/A private transient Position.Bias[] bias = new Position.Bias[1];
0N/A /**
0N/A * Current offset.
0N/A */
0N/A private int curOffset;
0N/A
0N/A /**
0N/A * Called for a mouse click event.
0N/A * If the component is read-only (ie a browser) then
0N/A * the clicked event is used to drive an attempt to
0N/A * follow the reference specified by a link.
0N/A *
0N/A * @param e the mouse event
0N/A * @see MouseListener#mouseClicked
0N/A */
0N/A public void mouseClicked(MouseEvent e) {
0N/A JEditorPane editor = (JEditorPane) e.getSource();
0N/A
0N/A if (! editor.isEditable() && editor.isEnabled() &&
0N/A SwingUtilities.isLeftMouseButton(e)) {
0N/A Point pt = new Point(e.getX(), e.getY());
0N/A int pos = editor.viewToModel(pt);
0N/A if (pos >= 0) {
0N/A activateLink(pos, editor, e);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // ignore the drags
0N/A public void mouseDragged(MouseEvent e) {
0N/A }
0N/A
0N/A // track the moving of the mouse.
0N/A public void mouseMoved(MouseEvent e) {
0N/A JEditorPane editor = (JEditorPane) e.getSource();
0N/A if (!editor.isEnabled()) {
0N/A return;
0N/A }
0N/A
0N/A HTMLEditorKit kit = (HTMLEditorKit)editor.getEditorKit();
0N/A boolean adjustCursor = true;
0N/A Cursor newCursor = kit.getDefaultCursor();
0N/A if (!editor.isEditable()) {
0N/A Point pt = new Point(e.getX(), e.getY());
0N/A int pos = editor.getUI().viewToModel(editor, pt, bias);
0N/A if (bias[0] == Position.Bias.Backward && pos > 0) {
0N/A pos--;
0N/A }
0N/A if (pos >= 0 &&(editor.getDocument() instanceof HTMLDocument)){
0N/A HTMLDocument hdoc = (HTMLDocument)editor.getDocument();
0N/A Element elem = hdoc.getCharacterElement(pos);
0N/A if (!doesElementContainLocation(editor, elem, pos,
0N/A e.getX(), e.getY())) {
0N/A elem = null;
0N/A }
0N/A if (curElem != elem || curElemImage) {
0N/A Element lastElem = curElem;
0N/A curElem = elem;
0N/A String href = null;
0N/A curElemImage = false;
0N/A if (elem != null) {
0N/A AttributeSet a = elem.getAttributes();
0N/A AttributeSet anchor = (AttributeSet)a.
0N/A getAttribute(HTML.Tag.A);
0N/A if (anchor == null) {
0N/A curElemImage = (a.getAttribute(StyleConstants.
0N/A NameAttribute) == HTML.Tag.IMG);
0N/A if (curElemImage) {
0N/A href = getMapHREF(editor, hdoc, elem, a,
0N/A pos, e.getX(), e.getY());
0N/A }
0N/A }
0N/A else {
0N/A href = (String)anchor.getAttribute
0N/A (HTML.Attribute.HREF);
0N/A }
0N/A }
0N/A
0N/A if (href != this.href) {
0N/A // reference changed, fire event(s)
0N/A fireEvents(editor, hdoc, href, lastElem, e);
0N/A this.href = href;
0N/A if (href != null) {
0N/A newCursor = kit.getLinkCursor();
0N/A }
0N/A }
0N/A else {
0N/A adjustCursor = false;
0N/A }
0N/A }
0N/A else {
0N/A adjustCursor = false;
0N/A }
0N/A curOffset = pos;
0N/A }
0N/A }
0N/A if (adjustCursor && editor.getCursor() != newCursor) {
0N/A editor.setCursor(newCursor);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a string anchor if the passed in element has a
0N/A * USEMAP that contains the passed in location.
0N/A */
0N/A private String getMapHREF(JEditorPane html, HTMLDocument hdoc,
0N/A Element elem, AttributeSet attr, int offset,
0N/A int x, int y) {
0N/A Object useMap = attr.getAttribute(HTML.Attribute.USEMAP);
0N/A if (useMap != null && (useMap instanceof String)) {
0N/A Map m = hdoc.getMap((String)useMap);
0N/A if (m != null && offset < hdoc.getLength()) {
0N/A Rectangle bounds;
0N/A TextUI ui = html.getUI();
0N/A try {
0N/A Shape lBounds = ui.modelToView(html, offset,
0N/A Position.Bias.Forward);
0N/A Shape rBounds = ui.modelToView(html, offset + 1,
0N/A Position.Bias.Backward);
0N/A bounds = lBounds.getBounds();
0N/A bounds.add((rBounds instanceof Rectangle) ?
0N/A (Rectangle)rBounds : rBounds.getBounds());
0N/A } catch (BadLocationException ble) {
0N/A bounds = null;
0N/A }
0N/A if (bounds != null) {
0N/A AttributeSet area = m.getArea(x - bounds.x,
0N/A y - bounds.y,
0N/A bounds.width,
0N/A bounds.height);
0N/A if (area != null) {
0N/A return (String)area.getAttribute(HTML.Attribute.
0N/A HREF);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the View representing <code>e</code> contains
0N/A * the location <code>x</code>, <code>y</code>. <code>offset</code>
0N/A * gives the offset into the Document to check for.
0N/A */
0N/A private boolean doesElementContainLocation(JEditorPane editor,
0N/A Element e, int offset,
0N/A int x, int y) {
0N/A if (e != null && offset > 0 && e.getStartOffset() == offset) {
0N/A try {
0N/A TextUI ui = editor.getUI();
0N/A Shape s1 = ui.modelToView(editor, offset,
0N/A Position.Bias.Forward);
0N/A if (s1 == null) {
0N/A return false;
0N/A }
0N/A Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 :
0N/A s1.getBounds();
0N/A Shape s2 = ui.modelToView(editor, e.getEndOffset(),
0N/A Position.Bias.Backward);
0N/A if (s2 != null) {
0N/A Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
0N/A s2.getBounds();
0N/A r1.add(r2);
0N/A }
0N/A return r1.contains(x, y);
0N/A } catch (BadLocationException ble) {
0N/A }
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Calls linkActivated on the associated JEditorPane
0N/A * if the given position represents a link.<p>This is implemented
0N/A * to forward to the method with the same name, but with the following
0N/A * args both == -1.
0N/A *
0N/A * @param pos the position
0N/A * @param editor the editor pane
0N/A */
0N/A protected void activateLink(int pos, JEditorPane editor) {
0N/A activateLink(pos, editor, null);
0N/A }
0N/A
0N/A /**
0N/A * Calls linkActivated on the associated JEditorPane
0N/A * if the given position represents a link. If this was the result
0N/A * of a mouse click, <code>x</code> and
0N/A * <code>y</code> will give the location of the mouse, otherwise
0N/A * they will be < 0.
0N/A *
0N/A * @param pos the position
0N/A * @param html the editor pane
0N/A */
0N/A void activateLink(int pos, JEditorPane html, MouseEvent mouseEvent) {
0N/A Document doc = html.getDocument();
0N/A if (doc instanceof HTMLDocument) {
0N/A HTMLDocument hdoc = (HTMLDocument) doc;
0N/A Element e = hdoc.getCharacterElement(pos);
0N/A AttributeSet a = e.getAttributes();
0N/A AttributeSet anchor = (AttributeSet)a.getAttribute(HTML.Tag.A);
0N/A HyperlinkEvent linkEvent = null;
0N/A String description;
0N/A int x = -1;
0N/A int y = -1;
0N/A
0N/A if (mouseEvent != null) {
0N/A x = mouseEvent.getX();
0N/A y = mouseEvent.getY();
0N/A }
0N/A
0N/A if (anchor == null) {
0N/A href = getMapHREF(html, hdoc, e, a, pos, x, y);
0N/A }
0N/A else {
0N/A href = (String)anchor.getAttribute(HTML.Attribute.HREF);
0N/A }
0N/A
0N/A if (href != null) {
0N/A linkEvent = createHyperlinkEvent(html, hdoc, href, anchor,
0N/A e, mouseEvent);
0N/A }
0N/A if (linkEvent != null) {
0N/A html.fireHyperlinkUpdate(linkEvent);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Creates and returns a new instance of HyperlinkEvent. If
0N/A * <code>hdoc</code> is a frame document a HTMLFrameHyperlinkEvent
0N/A * will be created.
0N/A */
0N/A HyperlinkEvent createHyperlinkEvent(JEditorPane html,
0N/A HTMLDocument hdoc, String href,
0N/A AttributeSet anchor,
0N/A Element element,
0N/A MouseEvent mouseEvent) {
0N/A URL u;
0N/A try {
0N/A URL base = hdoc.getBase();
0N/A u = new URL(base, href);
0N/A // Following is a workaround for 1.2, in which
0N/A // new URL("file://...", "#...") causes the filename to
0N/A // be lost.
0N/A if (href != null && "file".equals(u.getProtocol()) &&
0N/A href.startsWith("#")) {
0N/A String baseFile = base.getFile();
0N/A String newFile = u.getFile();
0N/A if (baseFile != null && newFile != null &&
0N/A !newFile.startsWith(baseFile)) {
0N/A u = new URL(base, baseFile + href);
0N/A }
0N/A }
0N/A } catch (MalformedURLException m) {
0N/A u = null;
0N/A }
611N/A HyperlinkEvent linkEvent;
0N/A
0N/A if (!hdoc.isFrameDocument()) {
0N/A linkEvent = new HyperlinkEvent(
0N/A html, HyperlinkEvent.EventType.ACTIVATED, u, href,
0N/A element, mouseEvent);
0N/A } else {
0N/A String target = (anchor != null) ?
0N/A (String)anchor.getAttribute(HTML.Attribute.TARGET) : null;
0N/A if ((target == null) || (target.equals(""))) {
0N/A target = hdoc.getBaseTarget();
0N/A }
0N/A if ((target == null) || (target.equals(""))) {
0N/A target = "_self";
0N/A }
0N/A linkEvent = new HTMLFrameHyperlinkEvent(
0N/A html, HyperlinkEvent.EventType.ACTIVATED, u, href,
0N/A element, mouseEvent, target);
0N/A }
0N/A return linkEvent;
0N/A }
0N/A
0N/A void fireEvents(JEditorPane editor, HTMLDocument doc, String href,
0N/A Element lastElem, MouseEvent mouseEvent) {
0N/A if (this.href != null) {
0N/A // fire an exited event on the old link
0N/A URL u;
0N/A try {
0N/A u = new URL(doc.getBase(), this.href);
0N/A } catch (MalformedURLException m) {
0N/A u = null;
0N/A }
0N/A HyperlinkEvent exit = new HyperlinkEvent(editor,
0N/A HyperlinkEvent.EventType.EXITED, u, this.href,
0N/A lastElem, mouseEvent);
0N/A editor.fireHyperlinkUpdate(exit);
0N/A }
0N/A if (href != null) {
0N/A // fire an entered event on the new link
0N/A URL u;
0N/A try {
0N/A u = new URL(doc.getBase(), href);
0N/A } catch (MalformedURLException m) {
0N/A u = null;
0N/A }
0N/A HyperlinkEvent entered = new HyperlinkEvent(editor,
0N/A HyperlinkEvent.EventType.ENTERED,
0N/A u, href, curElem, mouseEvent);
0N/A editor.fireHyperlinkUpdate(entered);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Interface to be supported by the parser. This enables
0N/A * providing a different parser while reusing some of the
0N/A * implementation provided by this editor kit.
0N/A */
0N/A public static abstract class Parser {
0N/A /**
0N/A * Parse the given stream and drive the given callback
0N/A * with the results of the parse. This method should
0N/A * be implemented to be thread-safe.
0N/A */
0N/A public abstract void parse(Reader r, ParserCallback cb, boolean ignoreCharSet) throws IOException;
0N/A
0N/A }
0N/A
0N/A /**
0N/A * The result of parsing drives these callback methods.
0N/A * The open and close actions should be balanced. The
0N/A * <code>flush</code> method will be the last method
0N/A * called, to give the receiver a chance to flush any
0N/A * pending data into the document.
0N/A * <p>Refer to DocumentParser, the default parser used, for further
0N/A * information on the contents of the AttributeSets, the positions, and
0N/A * other info.
0N/A *
0N/A * @see javax.swing.text.html.parser.DocumentParser
0N/A */
0N/A public static class ParserCallback {
0N/A /**
0N/A * This is passed as an attribute in the attributeset to indicate
0N/A * the element is implied eg, the string '&lt;&gt;foo&lt;\t&gt;'
0N/A * contains an implied html element and an implied body element.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public static final Object IMPLIED = "_implied_";
0N/A
0N/A
0N/A public void flush() throws BadLocationException {
0N/A }
0N/A
0N/A public void handleText(char[] data, int pos) {
0N/A }
0N/A
0N/A public void handleComment(char[] data, int pos) {
0N/A }
0N/A
0N/A public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
0N/A }
0N/A
0N/A public void handleEndTag(HTML.Tag t, int pos) {
0N/A }
0N/A
0N/A public void handleSimpleTag(HTML.Tag t, MutableAttributeSet a, int pos) {
0N/A }
0N/A
0N/A public void handleError(String errorMsg, int pos){
0N/A }
0N/A
0N/A /**
0N/A * This is invoked after the stream has been parsed, but before
0N/A * <code>flush</code>. <code>eol</code> will be one of \n, \r
0N/A * or \r\n, which ever is encountered the most in parsing the
0N/A * stream.
0N/A *
0N/A * @since 1.3
0N/A */
0N/A public void handleEndOfLineString(String eol) {
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * A factory to build views for HTML. The following
0N/A * table describes what this factory will build by
0N/A * default.
0N/A *
0N/A * <table summary="Describes the tag and view created by this factory by default">
0N/A * <tr>
0N/A * <th align=left>Tag<th align=left>View created
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.CONTENT<td>InlineView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.IMPLIED<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.P<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.H1<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.H2<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.H3<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.H4<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.H5<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.H6<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.DT<td>javax.swing.text.html.ParagraphView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.MENU<td>ListView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.DIR<td>ListView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.UL<td>ListView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.OL<td>ListView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.LI<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.DL<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.DD<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.BODY<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.HTML<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.CENTER<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.DIV<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.BLOCKQUOTE<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.PRE<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.BLOCKQUOTE<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.PRE<td>BlockView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.IMG<td>ImageView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.HR<td>HRuleView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.BR<td>BRView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.TABLE<td>javax.swing.text.html.TableView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.INPUT<td>FormView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.SELECT<td>FormView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.TEXTAREA<td>FormView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.OBJECT<td>ObjectView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.FRAMESET<td>FrameSetView
0N/A * </tr><tr>
0N/A * <td>HTML.Tag.FRAME<td>FrameView
0N/A * </tr>
0N/A * </table>
0N/A */
0N/A public static class HTMLFactory implements ViewFactory {
0N/A
0N/A /**
0N/A * Creates a view from an element.
0N/A *
0N/A * @param elem the element
0N/A * @return the view
0N/A */
0N/A public View create(Element elem) {
0N/A AttributeSet attrs = elem.getAttributes();
0N/A Object elementName =
0N/A attrs.getAttribute(AbstractDocument.ElementNameAttribute);
0N/A Object o = (elementName != null) ?
0N/A null : attrs.getAttribute(StyleConstants.NameAttribute);
0N/A if (o instanceof HTML.Tag) {
0N/A HTML.Tag kind = (HTML.Tag) o;
0N/A if (kind == HTML.Tag.CONTENT) {
0N/A return new InlineView(elem);
0N/A } else if (kind == HTML.Tag.IMPLIED) {
0N/A String ws = (String) elem.getAttributes().getAttribute(
0N/A CSS.Attribute.WHITE_SPACE);
0N/A if ((ws != null) && ws.equals("pre")) {
0N/A return new LineView(elem);
0N/A }
0N/A return new javax.swing.text.html.ParagraphView(elem);
0N/A } else if ((kind == HTML.Tag.P) ||
0N/A (kind == HTML.Tag.H1) ||
0N/A (kind == HTML.Tag.H2) ||
0N/A (kind == HTML.Tag.H3) ||
0N/A (kind == HTML.Tag.H4) ||
0N/A (kind == HTML.Tag.H5) ||
0N/A (kind == HTML.Tag.H6) ||
0N/A (kind == HTML.Tag.DT)) {
0N/A // paragraph
0N/A return new javax.swing.text.html.ParagraphView(elem);
0N/A } else if ((kind == HTML.Tag.MENU) ||
0N/A (kind == HTML.Tag.DIR) ||
0N/A (kind == HTML.Tag.UL) ||
0N/A (kind == HTML.Tag.OL)) {
0N/A return new ListView(elem);
0N/A } else if (kind == HTML.Tag.BODY) {
0N/A return new BodyBlockView(elem);
0N/A } else if (kind == HTML.Tag.HTML) {
0N/A return new BlockView(elem, View.Y_AXIS);
0N/A } else if ((kind == HTML.Tag.LI) ||
0N/A (kind == HTML.Tag.CENTER) ||
0N/A (kind == HTML.Tag.DL) ||
0N/A (kind == HTML.Tag.DD) ||
0N/A (kind == HTML.Tag.DIV) ||
0N/A (kind == HTML.Tag.BLOCKQUOTE) ||
0N/A (kind == HTML.Tag.PRE) ||
0N/A (kind == HTML.Tag.FORM)) {
0N/A // vertical box
0N/A return new BlockView(elem, View.Y_AXIS);
0N/A } else if (kind == HTML.Tag.NOFRAMES) {
0N/A return new NoFramesView(elem, View.Y_AXIS);
0N/A } else if (kind==HTML.Tag.IMG) {
0N/A return new ImageView(elem);
0N/A } else if (kind == HTML.Tag.ISINDEX) {
0N/A return new IsindexView(elem);
0N/A } else if (kind == HTML.Tag.HR) {
0N/A return new HRuleView(elem);
0N/A } else if (kind == HTML.Tag.BR) {
0N/A return new BRView(elem);
0N/A } else if (kind == HTML.Tag.TABLE) {
0N/A return new javax.swing.text.html.TableView(elem);
0N/A } else if ((kind == HTML.Tag.INPUT) ||
0N/A (kind == HTML.Tag.SELECT) ||
0N/A (kind == HTML.Tag.TEXTAREA)) {
0N/A return new FormView(elem);
0N/A } else if (kind == HTML.Tag.OBJECT) {
0N/A return new ObjectView(elem);
0N/A } else if (kind == HTML.Tag.FRAMESET) {
0N/A if (elem.getAttributes().isDefined(HTML.Attribute.ROWS)) {
0N/A return new FrameSetView(elem, View.Y_AXIS);
0N/A } else if (elem.getAttributes().isDefined(HTML.Attribute.COLS)) {
0N/A return new FrameSetView(elem, View.X_AXIS);
0N/A }
0N/A throw new RuntimeException("Can't build a" + kind + ", " + elem + ":" +
0N/A "no ROWS or COLS defined.");
0N/A } else if (kind == HTML.Tag.FRAME) {
0N/A return new FrameView(elem);
0N/A } else if (kind instanceof HTML.UnknownTag) {
0N/A return new HiddenTagView(elem);
0N/A } else if (kind == HTML.Tag.COMMENT) {
0N/A return new CommentView(elem);
0N/A } else if (kind == HTML.Tag.HEAD) {
0N/A // Make the head never visible, and never load its
0N/A // children. For Cursor positioning,
0N/A // getNextVisualPositionFrom is overriden to always return
0N/A // the end offset of the element.
0N/A return new BlockView(elem, View.X_AXIS) {
0N/A public float getPreferredSpan(int axis) {
0N/A return 0;
0N/A }
0N/A public float getMinimumSpan(int axis) {
0N/A return 0;
0N/A }
0N/A public float getMaximumSpan(int axis) {
0N/A return 0;
0N/A }
0N/A protected void loadChildren(ViewFactory f) {
0N/A }
0N/A public Shape modelToView(int pos, Shape a,
0N/A Position.Bias b) throws BadLocationException {
0N/A return a;
0N/A }
0N/A public int getNextVisualPositionFrom(int pos,
0N/A Position.Bias b, Shape a,
0N/A int direction, Position.Bias[] biasRet) {
0N/A return getElement().getEndOffset();
0N/A }
0N/A };
0N/A } else if ((kind == HTML.Tag.TITLE) ||
0N/A (kind == HTML.Tag.META) ||
0N/A (kind == HTML.Tag.LINK) ||
0N/A (kind == HTML.Tag.STYLE) ||
0N/A (kind == HTML.Tag.SCRIPT) ||
0N/A (kind == HTML.Tag.AREA) ||
0N/A (kind == HTML.Tag.MAP) ||
0N/A (kind == HTML.Tag.PARAM) ||
0N/A (kind == HTML.Tag.APPLET)) {
0N/A return new HiddenTagView(elem);
0N/A }
0N/A }
0N/A // If we get here, it's either an element we don't know about
0N/A // or something from StyledDocument that doesn't have a mapping to HTML.
0N/A String nm = (elementName != null) ? (String)elementName :
0N/A elem.getName();
0N/A if (nm != null) {
0N/A if (nm.equals(AbstractDocument.ContentElementName)) {
0N/A return new LabelView(elem);
0N/A } else if (nm.equals(AbstractDocument.ParagraphElementName)) {
0N/A return new ParagraphView(elem);
0N/A } else if (nm.equals(AbstractDocument.SectionElementName)) {
0N/A return new BoxView(elem, View.Y_AXIS);
0N/A } else if (nm.equals(StyleConstants.ComponentElementName)) {
0N/A return new ComponentView(elem);
0N/A } else if (nm.equals(StyleConstants.IconElementName)) {
0N/A return new IconView(elem);
0N/A }
0N/A }
0N/A
0N/A // default to text display
0N/A return new LabelView(elem);
0N/A }
0N/A
0N/A static class BodyBlockView extends BlockView implements ComponentListener {
0N/A public BodyBlockView(Element elem) {
0N/A super(elem,View.Y_AXIS);
0N/A }
0N/A // reimplement major axis requirements to indicate that the
0N/A // block is flexible for the body element... so that it can
0N/A // be stretched to fill the background properly.
0N/A protected SizeRequirements calculateMajorAxisRequirements(int axis, SizeRequirements r) {
0N/A r = super.calculateMajorAxisRequirements(axis, r);
0N/A r.maximum = Integer.MAX_VALUE;
0N/A return r;
0N/A }
0N/A
0N/A protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets, int[] spans) {
0N/A Container container = getContainer();
0N/A Container parentContainer;
0N/A if (container != null
0N/A && (container instanceof javax.swing.JEditorPane)
0N/A && (parentContainer = container.getParent()) != null
0N/A && (parentContainer instanceof javax.swing.JViewport)) {
0N/A JViewport viewPort = (JViewport)parentContainer;
0N/A if (cachedViewPort != null) {
611N/A JViewport cachedObject = cachedViewPort.get();
611N/A if (cachedObject != null) {
0N/A if (cachedObject != viewPort) {
611N/A cachedObject.removeComponentListener(this);
0N/A }
0N/A } else {
0N/A cachedViewPort = null;
0N/A }
0N/A }
0N/A if (cachedViewPort == null) {
0N/A viewPort.addComponentListener(this);
611N/A cachedViewPort = new WeakReference<JViewport>(viewPort);
0N/A }
0N/A
0N/A componentVisibleWidth = viewPort.getExtentSize().width;
0N/A if (componentVisibleWidth > 0) {
0N/A Insets insets = container.getInsets();
0N/A viewVisibleWidth = componentVisibleWidth - insets.left - getLeftInset();
0N/A //try to use viewVisibleWidth if it is smaller than targetSpan
0N/A targetSpan = Math.min(targetSpan, viewVisibleWidth);
0N/A }
0N/A } else {
0N/A if (cachedViewPort != null) {
611N/A JViewport cachedObject = cachedViewPort.get();
611N/A if (cachedObject != null) {
611N/A cachedObject.removeComponentListener(this);
0N/A }
0N/A cachedViewPort = null;
0N/A }
0N/A }
0N/A super.layoutMinorAxis(targetSpan, axis, offsets, spans);
0N/A }
0N/A
0N/A public void setParent(View parent) {
0N/A //if parent == null unregister component listener
0N/A if (parent == null) {
0N/A if (cachedViewPort != null) {
0N/A Object cachedObject;
0N/A if ((cachedObject = cachedViewPort.get()) != null) {
0N/A ((JComponent)cachedObject).removeComponentListener(this);
0N/A }
0N/A cachedViewPort = null;
0N/A }
0N/A }
0N/A super.setParent(parent);
0N/A }
0N/A
0N/A public void componentResized(ComponentEvent e) {
0N/A if ( !(e.getSource() instanceof JViewport) ) {
0N/A return;
0N/A }
0N/A JViewport viewPort = (JViewport)e.getSource();
0N/A if (componentVisibleWidth != viewPort.getExtentSize().width) {
0N/A Document doc = getDocument();
0N/A if (doc instanceof AbstractDocument) {
0N/A AbstractDocument document = (AbstractDocument)getDocument();
0N/A document.readLock();
0N/A try {
0N/A layoutChanged(X_AXIS);
0N/A preferenceChanged(null, true, true);
0N/A } finally {
0N/A document.readUnlock();
0N/A }
0N/A
0N/A }
0N/A }
0N/A }
0N/A public void componentHidden(ComponentEvent e) {
0N/A }
0N/A public void componentMoved(ComponentEvent e) {
0N/A }
0N/A public void componentShown(ComponentEvent e) {
0N/A }
0N/A /*
0N/A * we keep weak reference to viewPort if and only if BodyBoxView is listening for ComponentEvents
0N/A * only in that case cachedViewPort is not equal to null.
0N/A * we need to keep this reference in order to remove BodyBoxView from viewPort listeners.
0N/A *
0N/A */
611N/A private Reference<JViewport> cachedViewPort = null;
0N/A private boolean isListening = false;
0N/A private int viewVisibleWidth = Integer.MAX_VALUE;
0N/A private int componentVisibleWidth = Integer.MAX_VALUE;
0N/A }
0N/A
0N/A }
0N/A
0N/A // --- Action implementations ------------------------------
0N/A
0N/A/** The bold action identifier
0N/A*/
0N/A public static final String BOLD_ACTION = "html-bold-action";
0N/A/** The italic action identifier
0N/A*/
0N/A public static final String ITALIC_ACTION = "html-italic-action";
0N/A/** The paragraph left indent action identifier
0N/A*/
0N/A public static final String PARA_INDENT_LEFT = "html-para-indent-left";
0N/A/** The paragraph right indent action identifier
0N/A*/
0N/A public static final String PARA_INDENT_RIGHT = "html-para-indent-right";
0N/A/** The font size increase to next value action identifier
0N/A*/
0N/A public static final String FONT_CHANGE_BIGGER = "html-font-bigger";
0N/A/** The font size decrease to next value action identifier
0N/A*/
0N/A public static final String FONT_CHANGE_SMALLER = "html-font-smaller";
0N/A/** The Color choice action identifier
0N/A The color is passed as an argument
0N/A*/
0N/A public static final String COLOR_ACTION = "html-color-action";
0N/A/** The logical style choice action identifier
0N/A The logical style is passed in as an argument
0N/A*/
0N/A public static final String LOGICAL_STYLE_ACTION = "html-logical-style-action";
0N/A /**
0N/A * Align images at the top.
0N/A */
0N/A public static final String IMG_ALIGN_TOP = "html-image-align-top";
0N/A
0N/A /**
0N/A * Align images in the middle.
0N/A */
0N/A public static final String IMG_ALIGN_MIDDLE = "html-image-align-middle";
0N/A
0N/A /**
0N/A * Align images at the bottom.
0N/A */
0N/A public static final String IMG_ALIGN_BOTTOM = "html-image-align-bottom";
0N/A
0N/A /**
0N/A * Align images at the border.
0N/A */
0N/A public static final String IMG_BORDER = "html-image-border";
0N/A
0N/A
0N/A /** HTML used when inserting tables. */
0N/A private static final String INSERT_TABLE_HTML = "<table border=1><tr><td></td></tr></table>";
0N/A
0N/A /** HTML used when inserting unordered lists. */
0N/A private static final String INSERT_UL_HTML = "<ul><li></li></ul>";
0N/A
0N/A /** HTML used when inserting ordered lists. */
0N/A private static final String INSERT_OL_HTML = "<ol><li></li></ol>";
0N/A
0N/A /** HTML used when inserting hr. */
0N/A private static final String INSERT_HR_HTML = "<hr>";
0N/A
0N/A /** HTML used when inserting pre. */
0N/A private static final String INSERT_PRE_HTML = "<pre></pre>";
0N/A
0N/A private static final NavigateLinkAction nextLinkAction =
0N/A new NavigateLinkAction("next-link-action");
0N/A
0N/A private static final NavigateLinkAction previousLinkAction =
0N/A new NavigateLinkAction("previous-link-action");
0N/A
0N/A private static final ActivateLinkAction activateLinkAction =
0N/A new ActivateLinkAction("activate-link-action");
0N/A
0N/A private static final Action[] defaultActions = {
0N/A new InsertHTMLTextAction("InsertTable", INSERT_TABLE_HTML,
0N/A HTML.Tag.BODY, HTML.Tag.TABLE),
0N/A new InsertHTMLTextAction("InsertTableRow", INSERT_TABLE_HTML,
0N/A HTML.Tag.TABLE, HTML.Tag.TR,
0N/A HTML.Tag.BODY, HTML.Tag.TABLE),
0N/A new InsertHTMLTextAction("InsertTableDataCell", INSERT_TABLE_HTML,
0N/A HTML.Tag.TR, HTML.Tag.TD,
0N/A HTML.Tag.BODY, HTML.Tag.TABLE),
0N/A new InsertHTMLTextAction("InsertUnorderedList", INSERT_UL_HTML,
0N/A HTML.Tag.BODY, HTML.Tag.UL),
0N/A new InsertHTMLTextAction("InsertUnorderedListItem", INSERT_UL_HTML,
0N/A HTML.Tag.UL, HTML.Tag.LI,
0N/A HTML.Tag.BODY, HTML.Tag.UL),
0N/A new InsertHTMLTextAction("InsertOrderedList", INSERT_OL_HTML,
0N/A HTML.Tag.BODY, HTML.Tag.OL),
0N/A new InsertHTMLTextAction("InsertOrderedListItem", INSERT_OL_HTML,
0N/A HTML.Tag.OL, HTML.Tag.LI,
0N/A HTML.Tag.BODY, HTML.Tag.OL),
0N/A new InsertHRAction(),
0N/A new InsertHTMLTextAction("InsertPre", INSERT_PRE_HTML,
0N/A HTML.Tag.BODY, HTML.Tag.PRE),
0N/A nextLinkAction, previousLinkAction, activateLinkAction,
0N/A
0N/A new BeginAction(beginAction, false),
0N/A new BeginAction(selectionBeginAction, true)
0N/A };
0N/A
0N/A // link navigation support
0N/A private boolean foundLink = false;
0N/A private int prevHypertextOffset = -1;
0N/A private Object linkNavigationTag;
0N/A
0N/A
0N/A /**
0N/A * An abstract Action providing some convenience methods that may
0N/A * be useful in inserting HTML into an existing document.
0N/A * <p>NOTE: None of the convenience methods obtain a lock on the
0N/A * document. If you have another thread modifying the text these
0N/A * methods may have inconsistent behavior, or return the wrong thing.
0N/A */
0N/A public static abstract class HTMLTextAction extends StyledTextAction {
0N/A public HTMLTextAction(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A /**
0N/A * @return HTMLDocument of <code>e</code>.
0N/A */
0N/A protected HTMLDocument getHTMLDocument(JEditorPane e) {
0N/A Document d = e.getDocument();
0N/A if (d instanceof HTMLDocument) {
0N/A return (HTMLDocument) d;
0N/A }
0N/A throw new IllegalArgumentException("document must be HTMLDocument");
0N/A }
0N/A
0N/A /**
0N/A * @return HTMLEditorKit for <code>e</code>.
0N/A */
0N/A protected HTMLEditorKit getHTMLEditorKit(JEditorPane e) {
0N/A EditorKit k = e.getEditorKit();
0N/A if (k instanceof HTMLEditorKit) {
0N/A return (HTMLEditorKit) k;
0N/A }
0N/A throw new IllegalArgumentException("EditorKit must be HTMLEditorKit");
0N/A }
0N/A
0N/A /**
0N/A * Returns an array of the Elements that contain <code>offset</code>.
0N/A * The first elements corresponds to the root.
0N/A */
0N/A protected Element[] getElementsAt(HTMLDocument doc, int offset) {
0N/A return getElementsAt(doc.getDefaultRootElement(), offset, 0);
0N/A }
0N/A
0N/A /**
0N/A * Recursive method used by getElementsAt.
0N/A */
0N/A private Element[] getElementsAt(Element parent, int offset,
0N/A int depth) {
0N/A if (parent.isLeaf()) {
0N/A Element[] retValue = new Element[depth + 1];
0N/A retValue[depth] = parent;
0N/A return retValue;
0N/A }
0N/A Element[] retValue = getElementsAt(parent.getElement
0N/A (parent.getElementIndex(offset)), offset, depth + 1);
0N/A retValue[depth] = parent;
0N/A return retValue;
0N/A }
0N/A
0N/A /**
0N/A * Returns number of elements, starting at the deepest leaf, needed
0N/A * to get to an element representing <code>tag</code>. This will
0N/A * return -1 if no elements is found representing <code>tag</code>,
0N/A * or 0 if the parent of the leaf at <code>offset</code> represents
0N/A * <code>tag</code>.
0N/A */
0N/A protected int elementCountToTag(HTMLDocument doc, int offset,
0N/A HTML.Tag tag) {
0N/A int depth = -1;
0N/A Element e = doc.getCharacterElement(offset);
0N/A while (e != null && e.getAttributes().getAttribute
0N/A (StyleConstants.NameAttribute) != tag) {
0N/A e = e.getParentElement();
0N/A depth++;
0N/A }
0N/A if (e == null) {
0N/A return -1;
0N/A }
0N/A return depth;
0N/A }
0N/A
0N/A /**
0N/A * Returns the deepest element at <code>offset</code> matching
0N/A * <code>tag</code>.
0N/A */
0N/A protected Element findElementMatchingTag(HTMLDocument doc, int offset,
0N/A HTML.Tag tag) {
0N/A Element e = doc.getDefaultRootElement();
0N/A Element lastMatch = null;
0N/A while (e != null) {
0N/A if (e.getAttributes().getAttribute
0N/A (StyleConstants.NameAttribute) == tag) {
0N/A lastMatch = e;
0N/A }
0N/A e = e.getElement(e.getElementIndex(offset));
0N/A }
0N/A return lastMatch;
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * InsertHTMLTextAction can be used to insert an arbitrary string of HTML
0N/A * into an existing HTML document. At least two HTML.Tags need to be
0N/A * supplied. The first Tag, parentTag, identifies the parent in
0N/A * the document to add the elements to. The second tag, addTag,
0N/A * identifies the first tag that should be added to the document as
0N/A * seen in the HTML string. One important thing to remember, is that
0N/A * the parser is going to generate all the appropriate tags, even if
0N/A * they aren't in the HTML string passed in.<p>
0N/A * For example, lets say you wanted to create an action to insert
0N/A * a table into the body. The parentTag would be HTML.Tag.BODY,
0N/A * addTag would be HTML.Tag.TABLE, and the string could be something
0N/A * like &lt;table&gt;&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;.
0N/A * <p>There is also an option to supply an alternate parentTag and
0N/A * addTag. These will be checked for if there is no parentTag at
0N/A * offset.
0N/A */
0N/A public static class InsertHTMLTextAction extends HTMLTextAction {
0N/A public InsertHTMLTextAction(String name, String html,
0N/A HTML.Tag parentTag, HTML.Tag addTag) {
0N/A this(name, html, parentTag, addTag, null, null);
0N/A }
0N/A
0N/A public InsertHTMLTextAction(String name, String html,
0N/A HTML.Tag parentTag,
0N/A HTML.Tag addTag,
0N/A HTML.Tag alternateParentTag,
0N/A HTML.Tag alternateAddTag) {
0N/A this(name, html, parentTag, addTag, alternateParentTag,
0N/A alternateAddTag, true);
0N/A }
0N/A
0N/A /* public */
0N/A InsertHTMLTextAction(String name, String html,
0N/A HTML.Tag parentTag,
0N/A HTML.Tag addTag,
0N/A HTML.Tag alternateParentTag,
0N/A HTML.Tag alternateAddTag,
0N/A boolean adjustSelection) {
0N/A super(name);
0N/A this.html = html;
0N/A this.parentTag = parentTag;
0N/A this.addTag = addTag;
0N/A this.alternateParentTag = alternateParentTag;
0N/A this.alternateAddTag = alternateAddTag;
0N/A this.adjustSelection = adjustSelection;
0N/A }
0N/A
0N/A /**
0N/A * A cover for HTMLEditorKit.insertHTML. If an exception it
0N/A * thrown it is wrapped in a RuntimeException and thrown.
0N/A */
0N/A protected void insertHTML(JEditorPane editor, HTMLDocument doc,
0N/A int offset, String html, int popDepth,
0N/A int pushDepth, HTML.Tag addTag) {
0N/A try {
0N/A getHTMLEditorKit(editor).insertHTML(doc, offset, html,
0N/A popDepth, pushDepth,
0N/A addTag);
0N/A } catch (IOException ioe) {
0N/A throw new RuntimeException("Unable to insert: " + ioe);
0N/A } catch (BadLocationException ble) {
0N/A throw new RuntimeException("Unable to insert: " + ble);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This is invoked when inserting at a boundary. It determines
0N/A * the number of pops, and then the number of pushes that need
0N/A * to be performed, and then invokes insertHTML.
0N/A * @since 1.3
0N/A */
0N/A protected void insertAtBoundary(JEditorPane editor, HTMLDocument doc,
0N/A int offset, Element insertElement,
0N/A String html, HTML.Tag parentTag,
0N/A HTML.Tag addTag) {
0N/A insertAtBoundry(editor, doc, offset, insertElement, html,
0N/A parentTag, addTag);
0N/A }
0N/A
0N/A /**
0N/A * This is invoked when inserting at a boundary. It determines
0N/A * the number of pops, and then the number of pushes that need
0N/A * to be performed, and then invokes insertHTML.
0N/A * @deprecated As of Java 2 platform v1.3, use insertAtBoundary
0N/A */
0N/A @Deprecated
0N/A protected void insertAtBoundry(JEditorPane editor, HTMLDocument doc,
0N/A int offset, Element insertElement,
0N/A String html, HTML.Tag parentTag,
0N/A HTML.Tag addTag) {
0N/A // Find the common parent.
0N/A Element e;
0N/A Element commonParent;
0N/A boolean isFirst = (offset == 0);
0N/A
0N/A if (offset > 0 || insertElement == null) {
0N/A e = doc.getDefaultRootElement();
0N/A while (e != null && e.getStartOffset() != offset &&
0N/A !e.isLeaf()) {
0N/A e = e.getElement(e.getElementIndex(offset));
0N/A }
0N/A commonParent = (e != null) ? e.getParentElement() : null;
0N/A }
0N/A else {
0N/A // If inserting at the origin, the common parent is the
0N/A // insertElement.
0N/A commonParent = insertElement;
0N/A }
0N/A if (commonParent != null) {
0N/A // Determine how many pops to do.
0N/A int pops = 0;
0N/A int pushes = 0;
0N/A if (isFirst && insertElement != null) {
0N/A e = commonParent;
0N/A while (e != null && !e.isLeaf()) {
0N/A e = e.getElement(e.getElementIndex(offset));
0N/A pops++;
0N/A }
0N/A }
0N/A else {
0N/A e = commonParent;
0N/A offset--;
0N/A while (e != null && !e.isLeaf()) {
0N/A e = e.getElement(e.getElementIndex(offset));
0N/A pops++;
0N/A }
0N/A
0N/A // And how many pushes
0N/A e = commonParent;
0N/A offset++;
0N/A while (e != null && e != insertElement) {
0N/A e = e.getElement(e.getElementIndex(offset));
0N/A pushes++;
0N/A }
0N/A }
0N/A pops = Math.max(0, pops - 1);
0N/A
0N/A // And insert!
0N/A insertHTML(editor, doc, offset, html, pops, pushes, addTag);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * If there is an Element with name <code>tag</code> at
0N/A * <code>offset</code>, this will invoke either insertAtBoundary
0N/A * or <code>insertHTML</code>. This returns true if there is
0N/A * a match, and one of the inserts is invoked.
0N/A */
0N/A /*protected*/
0N/A boolean insertIntoTag(JEditorPane editor, HTMLDocument doc,
0N/A int offset, HTML.Tag tag, HTML.Tag addTag) {
0N/A Element e = findElementMatchingTag(doc, offset, tag);
0N/A if (e != null && e.getStartOffset() == offset) {
0N/A insertAtBoundary(editor, doc, offset, e, html,
0N/A tag, addTag);
0N/A return true;
0N/A }
0N/A else if (offset > 0) {
0N/A int depth = elementCountToTag(doc, offset - 1, tag);
0N/A if (depth != -1) {
0N/A insertHTML(editor, doc, offset, html, depth, 0, addTag);
0N/A return true;
0N/A }
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Called after an insertion to adjust the selection.
0N/A */
0N/A /* protected */
0N/A void adjustSelection(JEditorPane pane, HTMLDocument doc,
0N/A int startOffset, int oldLength) {
0N/A int newLength = doc.getLength();
0N/A if (newLength != oldLength && startOffset < newLength) {
0N/A if (startOffset > 0) {
0N/A String text;
0N/A try {
0N/A text = doc.getText(startOffset - 1, 1);
0N/A } catch (BadLocationException ble) {
0N/A text = null;
0N/A }
0N/A if (text != null && text.length() > 0 &&
0N/A text.charAt(0) == '\n') {
0N/A pane.select(startOffset, startOffset);
0N/A }
0N/A else {
0N/A pane.select(startOffset + 1, startOffset + 1);
0N/A }
0N/A }
0N/A else {
0N/A pane.select(1, 1);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Inserts the HTML into the document.
0N/A *
0N/A * @param ae the event
0N/A */
0N/A public void actionPerformed(ActionEvent ae) {
0N/A JEditorPane editor = getEditor(ae);
0N/A if (editor != null) {
0N/A HTMLDocument doc = getHTMLDocument(editor);
0N/A int offset = editor.getSelectionStart();
0N/A int length = doc.getLength();
0N/A boolean inserted;
0N/A // Try first choice
0N/A if (!insertIntoTag(editor, doc, offset, parentTag, addTag) &&
0N/A alternateParentTag != null) {
0N/A // Then alternate.
0N/A inserted = insertIntoTag(editor, doc, offset,
0N/A alternateParentTag,
0N/A alternateAddTag);
0N/A }
0N/A else {
0N/A inserted = true;
0N/A }
0N/A if (adjustSelection && inserted) {
0N/A adjustSelection(editor, doc, offset, length);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /** HTML to insert. */
0N/A protected String html;
0N/A /** Tag to check for in the document. */
0N/A protected HTML.Tag parentTag;
0N/A /** Tag in HTML to start adding tags from. */
0N/A protected HTML.Tag addTag;
0N/A /** Alternate Tag to check for in the document if parentTag is
0N/A * not found. */
0N/A protected HTML.Tag alternateParentTag;
0N/A /** Alternate tag in HTML to start adding tags from if parentTag
0N/A * is not found and alternateParentTag is found. */
0N/A protected HTML.Tag alternateAddTag;
0N/A /** True indicates the selection should be adjusted after an insert. */
0N/A boolean adjustSelection;
0N/A }
0N/A
0N/A
0N/A /**
0N/A * InsertHRAction is special, at actionPerformed time it will determine
0N/A * the parent HTML.Tag based on the paragraph element at the selection
0N/A * start.
0N/A */
0N/A static class InsertHRAction extends InsertHTMLTextAction {
0N/A InsertHRAction() {
0N/A super("InsertHR", "<hr>", null, HTML.Tag.IMPLIED, null, null,
0N/A false);
0N/A }
0N/A
0N/A /**
0N/A * Inserts the HTML into the document.
0N/A *
0N/A * @param ae the event
0N/A */
0N/A public void actionPerformed(ActionEvent ae) {
0N/A JEditorPane editor = getEditor(ae);
0N/A if (editor != null) {
0N/A HTMLDocument doc = getHTMLDocument(editor);
0N/A int offset = editor.getSelectionStart();
0N/A Element paragraph = doc.getParagraphElement(offset);
0N/A if (paragraph.getParentElement() != null) {
0N/A parentTag = (HTML.Tag)paragraph.getParentElement().
0N/A getAttributes().getAttribute
0N/A (StyleConstants.NameAttribute);
0N/A super.actionPerformed(ae);
0N/A }
0N/A }
0N/A }
0N/A
0N/A }
0N/A
0N/A /*
0N/A * Returns the object in an AttributeSet matching a key
0N/A */
0N/A static private Object getAttrValue(AttributeSet attr, HTML.Attribute key) {
0N/A Enumeration names = attr.getAttributeNames();
0N/A while (names.hasMoreElements()) {
0N/A Object nextKey = names.nextElement();
0N/A Object nextVal = attr.getAttribute(nextKey);
0N/A if (nextVal instanceof AttributeSet) {
0N/A Object value = getAttrValue((AttributeSet)nextVal, key);
0N/A if (value != null) {
0N/A return value;
0N/A }
0N/A } else if (nextKey == key) {
0N/A return nextVal;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Action to move the focus on the next or previous hypertext link
0N/A * or object. TODO: This method relies on support from the
0N/A * javax.accessibility package. The text package should support
0N/A * keyboard navigation of text elements directly.
0N/A */
0N/A static class NavigateLinkAction extends TextAction implements CaretListener {
0N/A
0N/A private static final FocusHighlightPainter focusPainter =
0N/A new FocusHighlightPainter(null);
0N/A private final boolean focusBack;
0N/A
0N/A /*
0N/A * Create this action with the appropriate identifier.
0N/A */
0N/A public NavigateLinkAction(String actionName) {
0N/A super(actionName);
0N/A focusBack = "previous-link-action".equals(actionName);
0N/A }
0N/A
0N/A /**
0N/A * Called when the caret position is updated.
0N/A *
0N/A * @param e the caret event
0N/A */
0N/A public void caretUpdate(CaretEvent e) {
0N/A Object src = e.getSource();
0N/A if (src instanceof JTextComponent) {
0N/A JTextComponent comp = (JTextComponent) src;
0N/A HTMLEditorKit kit = getHTMLEditorKit(comp);
0N/A if (kit != null && kit.foundLink) {
0N/A kit.foundLink = false;
0N/A // TODO: The AccessibleContext for the editor should register
0N/A // as a listener for CaretEvents and forward the events to
0N/A // assistive technologies listening for such events.
0N/A comp.getAccessibleContext().firePropertyChange(
0N/A AccessibleContext.ACCESSIBLE_HYPERTEXT_OFFSET,
215N/A Integer.valueOf(kit.prevHypertextOffset),
215N/A Integer.valueOf(e.getDot()));
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The operation to perform when this action is triggered.
0N/A */
0N/A public void actionPerformed(ActionEvent e) {
0N/A JTextComponent comp = getTextComponent(e);
0N/A if (comp == null || comp.isEditable()) {
0N/A return;
0N/A }
0N/A
0N/A Document doc = comp.getDocument();
0N/A HTMLEditorKit kit = getHTMLEditorKit(comp);
0N/A if (doc == null || kit == null) {
0N/A return;
0N/A }
0N/A
0N/A // TODO: Should start successive iterations from the
0N/A // current caret position.
0N/A ElementIterator ei = new ElementIterator(doc);
0N/A int currentOffset = comp.getCaretPosition();
0N/A int prevStartOffset = -1;
0N/A int prevEndOffset = -1;
0N/A
0N/A // highlight the next link or object after the current caret position
611N/A Element nextElement;
0N/A while ((nextElement = ei.next()) != null) {
0N/A String name = nextElement.getName();
0N/A AttributeSet attr = nextElement.getAttributes();
0N/A
0N/A Object href = getAttrValue(attr, HTML.Attribute.HREF);
0N/A if (!(name.equals(HTML.Tag.OBJECT.toString())) && href == null) {
0N/A continue;
0N/A }
0N/A
0N/A int elementOffset = nextElement.getStartOffset();
0N/A if (focusBack) {
0N/A if (elementOffset >= currentOffset &&
0N/A prevStartOffset >= 0) {
0N/A
0N/A kit.foundLink = true;
0N/A comp.setCaretPosition(prevStartOffset);
0N/A moveCaretPosition(comp, kit, prevStartOffset,
0N/A prevEndOffset);
0N/A kit.prevHypertextOffset = prevStartOffset;
0N/A return;
0N/A }
0N/A } else { // focus forward
0N/A if (elementOffset > currentOffset) {
0N/A
0N/A kit.foundLink = true;
0N/A comp.setCaretPosition(elementOffset);
0N/A moveCaretPosition(comp, kit, elementOffset,
0N/A nextElement.getEndOffset());
0N/A kit.prevHypertextOffset = elementOffset;
0N/A return;
0N/A }
0N/A }
0N/A prevStartOffset = nextElement.getStartOffset();
0N/A prevEndOffset = nextElement.getEndOffset();
0N/A }
0N/A if (focusBack && prevStartOffset >= 0) {
0N/A kit.foundLink = true;
0N/A comp.setCaretPosition(prevStartOffset);
0N/A moveCaretPosition(comp, kit, prevStartOffset, prevEndOffset);
0N/A kit.prevHypertextOffset = prevStartOffset;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Moves the caret from mark to dot
0N/A */
0N/A private void moveCaretPosition(JTextComponent comp, HTMLEditorKit kit,
0N/A int mark, int dot) {
0N/A Highlighter h = comp.getHighlighter();
0N/A if (h != null) {
0N/A int p0 = Math.min(dot, mark);
0N/A int p1 = Math.max(dot, mark);
0N/A try {
0N/A if (kit.linkNavigationTag != null) {
0N/A h.changeHighlight(kit.linkNavigationTag, p0, p1);
0N/A } else {
0N/A kit.linkNavigationTag =
0N/A h.addHighlight(p0, p1, focusPainter);
0N/A }
0N/A } catch (BadLocationException e) {
0N/A }
0N/A }
0N/A }
0N/A
0N/A private HTMLEditorKit getHTMLEditorKit(JTextComponent comp) {
0N/A if (comp instanceof JEditorPane) {
0N/A EditorKit kit = ((JEditorPane) comp).getEditorKit();
0N/A if (kit instanceof HTMLEditorKit) {
0N/A return (HTMLEditorKit) kit;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * A highlight painter that draws a one-pixel border around
0N/A * the highlighted area.
0N/A */
0N/A static class FocusHighlightPainter extends
0N/A DefaultHighlighter.DefaultHighlightPainter {
0N/A
0N/A FocusHighlightPainter(Color color) {
0N/A super(color);
0N/A }
0N/A
0N/A /**
0N/A * Paints a portion of a highlight.
0N/A *
0N/A * @param g the graphics context
0N/A * @param offs0 the starting model offset >= 0
0N/A * @param offs1 the ending model offset >= offs1
0N/A * @param bounds the bounding box of the view, which is not
0N/A * necessarily the region to paint.
0N/A * @param c the editor
0N/A * @param view View painting for
0N/A * @return region in which drawing occurred
0N/A */
0N/A public Shape paintLayer(Graphics g, int offs0, int offs1,
0N/A Shape bounds, JTextComponent c, View view) {
0N/A
0N/A Color color = getColor();
0N/A
0N/A if (color == null) {
0N/A g.setColor(c.getSelectionColor());
0N/A }
0N/A else {
0N/A g.setColor(color);
0N/A }
0N/A if (offs0 == view.getStartOffset() &&
0N/A offs1 == view.getEndOffset()) {
0N/A // Contained in view, can just use bounds.
0N/A Rectangle alloc;
0N/A if (bounds instanceof Rectangle) {
0N/A alloc = (Rectangle)bounds;
0N/A }
0N/A else {
0N/A alloc = bounds.getBounds();
0N/A }
0N/A g.drawRect(alloc.x, alloc.y, alloc.width - 1, alloc.height);
0N/A return alloc;
0N/A }
0N/A else {
0N/A // Should only render part of View.
0N/A try {
0N/A // --- determine locations ---
0N/A Shape shape = view.modelToView(offs0, Position.Bias.Forward,
0N/A offs1,Position.Bias.Backward,
0N/A bounds);
0N/A Rectangle r = (shape instanceof Rectangle) ?
0N/A (Rectangle)shape : shape.getBounds();
0N/A g.drawRect(r.x, r.y, r.width - 1, r.height);
0N/A return r;
0N/A } catch (BadLocationException e) {
0N/A // can't render
0N/A }
0N/A }
0N/A // Only if exception
0N/A return null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Action to activate the hypertext link that has focus.
0N/A * TODO: This method relies on support from the
0N/A * javax.accessibility package. The text package should support
0N/A * keyboard navigation of text elements directly.
0N/A */
0N/A static class ActivateLinkAction extends TextAction {
0N/A
0N/A /**
0N/A * Create this action with the appropriate identifier.
0N/A */
0N/A public ActivateLinkAction(String actionName) {
0N/A super(actionName);
0N/A }
0N/A
0N/A /*
0N/A * activates the hyperlink at offset
0N/A */
0N/A private void activateLink(String href, HTMLDocument doc,
0N/A JEditorPane editor, int offset) {
0N/A try {
0N/A URL page =
0N/A (URL)doc.getProperty(Document.StreamDescriptionProperty);
0N/A URL url = new URL(page, href);
0N/A HyperlinkEvent linkEvent = new HyperlinkEvent
0N/A (editor, HyperlinkEvent.EventType.
0N/A ACTIVATED, url, url.toExternalForm(),
0N/A doc.getCharacterElement(offset));
0N/A editor.fireHyperlinkUpdate(linkEvent);
0N/A } catch (MalformedURLException m) {
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Invokes default action on the object in an element
0N/A */
0N/A private void doObjectAction(JEditorPane editor, Element elem) {
0N/A View view = getView(editor, elem);
0N/A if (view != null && view instanceof ObjectView) {
0N/A Component comp = ((ObjectView)view).getComponent();
0N/A if (comp != null && comp instanceof Accessible) {
611N/A AccessibleContext ac = comp.getAccessibleContext();
0N/A if (ac != null) {
0N/A AccessibleAction aa = ac.getAccessibleAction();
0N/A if (aa != null) {
0N/A aa.doAccessibleAction(0);
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Returns the root view for a document
0N/A */
0N/A private View getRootView(JEditorPane editor) {
0N/A return editor.getUI().getRootView(editor);
0N/A }
0N/A
0N/A /*
0N/A * Returns a view associated with an element
0N/A */
0N/A private View getView(JEditorPane editor, Element elem) {
0N/A Object lock = lock(editor);
0N/A try {
0N/A View rootView = getRootView(editor);
0N/A int start = elem.getStartOffset();
0N/A if (rootView != null) {
0N/A return getView(rootView, elem, start);
0N/A }
0N/A return null;
0N/A } finally {
0N/A unlock(lock);
0N/A }
0N/A }
0N/A
0N/A private View getView(View parent, Element elem, int start) {
0N/A if (parent.getElement() == elem) {
0N/A return parent;
0N/A }
0N/A int index = parent.getViewIndex(start, Position.Bias.Forward);
0N/A
0N/A if (index != -1 && index < parent.getViewCount()) {
0N/A return getView(parent.getView(index), elem, start);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * If possible acquires a lock on the Document. If a lock has been
0N/A * obtained a key will be retured that should be passed to
0N/A * <code>unlock</code>.
0N/A */
0N/A private Object lock(JEditorPane editor) {
0N/A Document document = editor.getDocument();
0N/A
0N/A if (document instanceof AbstractDocument) {
0N/A ((AbstractDocument)document).readLock();
0N/A return document;
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /*
0N/A * Releases a lock previously obtained via <code>lock</code>.
0N/A */
0N/A private void unlock(Object key) {
0N/A if (key != null) {
0N/A ((AbstractDocument)key).readUnlock();
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * The operation to perform when this action is triggered.
0N/A */
0N/A public void actionPerformed(ActionEvent e) {
0N/A
0N/A JTextComponent c = getTextComponent(e);
0N/A if (c.isEditable() || !(c instanceof JEditorPane)) {
0N/A return;
0N/A }
0N/A JEditorPane editor = (JEditorPane)c;
0N/A
0N/A Document d = editor.getDocument();
0N/A if (d == null || !(d instanceof HTMLDocument)) {
0N/A return;
0N/A }
0N/A HTMLDocument doc = (HTMLDocument)d;
0N/A
0N/A ElementIterator ei = new ElementIterator(doc);
0N/A int currentOffset = editor.getCaretPosition();
0N/A
0N/A // invoke the next link or object action
0N/A String urlString = null;
0N/A String objString = null;
611N/A Element currentElement;
0N/A while ((currentElement = ei.next()) != null) {
0N/A String name = currentElement.getName();
0N/A AttributeSet attr = currentElement.getAttributes();
0N/A
0N/A Object href = getAttrValue(attr, HTML.Attribute.HREF);
0N/A if (href != null) {
0N/A if (currentOffset >= currentElement.getStartOffset() &&
0N/A currentOffset <= currentElement.getEndOffset()) {
0N/A
0N/A activateLink((String)href, doc, editor, currentOffset);
0N/A return;
0N/A }
0N/A } else if (name.equals(HTML.Tag.OBJECT.toString())) {
0N/A Object obj = getAttrValue(attr, HTML.Attribute.CLASSID);
0N/A if (obj != null) {
0N/A if (currentOffset >= currentElement.getStartOffset() &&
0N/A currentOffset <= currentElement.getEndOffset()) {
0N/A
0N/A doObjectAction(editor, currentElement);
0N/A return;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private static int getBodyElementStart(JTextComponent comp) {
0N/A Element rootElement = comp.getDocument().getRootElements()[0];
0N/A for (int i = 0; i < rootElement.getElementCount(); i++) {
0N/A Element currElement = rootElement.getElement(i);
0N/A if("body".equals(currElement.getName())) {
0N/A return currElement.getStartOffset();
0N/A }
0N/A }
0N/A return 0;
0N/A }
0N/A
0N/A /*
0N/A * Move the caret to the beginning of the document.
0N/A * @see DefaultEditorKit#beginAction
0N/A * @see HTMLEditorKit#getActions
0N/A */
0N/A
0N/A static class BeginAction extends TextAction {
0N/A
0N/A /* Create this object with the appropriate identifier. */
0N/A BeginAction(String nm, boolean select) {
0N/A super(nm);
0N/A this.select = select;
0N/A }
0N/A
0N/A /** The operation to perform when this action is triggered. */
0N/A public void actionPerformed(ActionEvent e) {
0N/A JTextComponent target = getTextComponent(e);
0N/A int bodyStart = getBodyElementStart(target);
0N/A
0N/A if (target != null) {
0N/A if (select) {
0N/A target.moveCaretPosition(bodyStart);
0N/A } else {
0N/A target.setCaretPosition(bodyStart);
0N/A }
0N/A }
0N/A }
0N/A
0N/A private boolean select;
0N/A }
0N/A}