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 * 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 * 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 * 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. 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 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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 * 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 * Asynchronous Loading 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 * work is done by the <code>DefaultStyledDocument</code> and 0N/A * <code>AbstractDocument</code> classes in the text package. 0N/A * Customization from current LAF 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 * 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 * 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 * @author Timothy Prinzing 0N/A * Constructs an HTMLEditorKit, creates a StyleContext, 0N/A * and loads the style sheet. 0N/A * Get the MIME type of the data that this 0N/A * kit represents support for. This kit supports 0N/A * Fetch a factory that is suitable for producing 0N/A * views of any models that are produced by this 0N/A * @return the factory 0N/A * Create an uninitialized text storage model 0N/A * that is appropriate for this type of editor. 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 * 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 * @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 * @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 * Inserts HTML into an existing document. 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 * @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 (
"IgnoreCharsetDirective");
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 * @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 * @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 * Called when the kit is being installed into the 0N/A * @param c the JEditorPane 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 * @param c the JEditorPane 0N/A * Default Cascading Style Sheet file that sets 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 * 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 // on error we simply have no styles... the html 0N/A // will look mighty wrong but still function. 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 * @param name the name of the resource, relative to the 0N/A * HTMLEditorKit class 0N/A * @return a stream representing the resource 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 * 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 * @return the command list 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 // PENDING: we need a better way to express what shouldn't be 0N/A // copied when editing... 0N/A // Remove the related image attributes, src, width, height 0N/A // Don't copy HRs or BRs either. 0N/A // Don't copy COMMENTs either 0N/A // Don't copy unknowns either:( 0N/A * Gets the input attributes used for the styled 0N/A * @return the attribute set 0N/A * Sets the default cursor. 0N/A * Returns the default cursor. 0N/A * Sets the cursor to use over links. 0N/A * Returns the cursor to use over hyper links. 0N/A * Indicates whether an html form submission is processed automatically 0N/A * or only <code>FormSubmitEvent</code> is fired. 0N/A * @return true if html form submission is processed automatically, 0N/A * @see #setAutoFormSubmission 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. 3370N/A * @see #isAutoFormSubmission() 0N/A * @see FormSubmitEvent 0N/A * Creates a copy of the editor kit. 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 // ----- Accessibility support ----- 0N/A * returns the AccessibleContext associated with this editor kit 0N/A * @return the AccessibleContext associated with this editor kit 0N/A // --- variables ------------------------------------------ 0N/A /** Shared factory for creating HTML Views. */ 0N/A * Class to watch the associated component and fire 0N/A * hyperlink events on it when appropriate. 0N/A * If true, the current element (curElem) represents an image. 0N/A /** This is used by viewToModel to avoid allocing a new array each 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 * @param e the mouse event 0N/A * @see MouseListener#mouseClicked 0N/A // track the moving of the mouse. 0N/A // reference changed, fire event(s) 0N/A * Returns a string anchor if the passed in element has a 0N/A * USEMAP that contains the passed in location. 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 * 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 * @param pos the position 0N/A * @param editor the editor pane 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 * @param pos the position 0N/A * @param html the editor pane 0N/A * Creates and returns a new instance of HyperlinkEvent. If 0N/A * <code>hdoc</code> is a frame document a HTMLFrameHyperlinkEvent 0N/A // Following is a workaround for 1.2, in which 0N/A // new URL("file://...", "#...") causes the filename to 0N/A // fire an exited event on the old link 0N/A // fire an entered event on the new link 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 * 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 * 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 * This is passed as an attribute in the attributeset to indicate 0N/A * the element is implied eg, the string '<>foo<\t>' 0N/A * contains an implied html element and an implied body element. 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 * A factory to build views for HTML. The following 0N/A * table describes what this factory will build by 0N/A * <table summary="Describes the tag and view created by this factory by default"> 0N/A * <th align=left>Tag<th align=left>View created 0N/A * <td>HTML.Tag.CONTENT<td>InlineView 0N/A * <td>HTML.Tag.MENU<td>ListView 0N/A * <td>HTML.Tag.DIR<td>ListView 0N/A * <td>HTML.Tag.UL<td>ListView 0N/A * <td>HTML.Tag.OL<td>ListView 0N/A * <td>HTML.Tag.LI<td>BlockView 0N/A * <td>HTML.Tag.DL<td>BlockView 0N/A * <td>HTML.Tag.DD<td>BlockView 0N/A * <td>HTML.Tag.BODY<td>BlockView 0N/A * <td>HTML.Tag.CENTER<td>BlockView 0N/A * <td>HTML.Tag.DIV<td>BlockView 0N/A * <td>HTML.Tag.BLOCKQUOTE<td>BlockView 0N/A * <td>HTML.Tag.PRE<td>BlockView 0N/A * <td>HTML.Tag.BLOCKQUOTE<td>BlockView 0N/A * <td>HTML.Tag.PRE<td>BlockView 0N/A * <td>HTML.Tag.IMG<td>ImageView 0N/A * <td>HTML.Tag.HR<td>HRuleView 0N/A * <td>HTML.Tag.BR<td>BRView 0N/A * <td>HTML.Tag.INPUT<td>FormView 0N/A * <td>HTML.Tag.SELECT<td>FormView 0N/A * <td>HTML.Tag.TEXTAREA<td>FormView 0N/A * <td>HTML.Tag.OBJECT<td>ObjectView 0N/A * <td>HTML.Tag.FRAMESET<td>FrameSetView 0N/A * <td>HTML.Tag.FRAME<td>FrameView 0N/A * Creates a view from an element. 0N/A * @param elem the element 0N/A "no ROWS or COLS defined.");
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 // 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 // default to text display 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 //try to use viewVisibleWidth if it is smaller than targetSpan 0N/A //if parent == null unregister component listener 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 // --- Action implementations ------------------------------ 0N/A/** The bold action identifier 0N/A/** The italic action identifier 0N/A/** The paragraph left indent action identifier 0N/A/** The paragraph right indent action identifier 0N/A/** The font size increase to next value action identifier 0N/A/** The font size decrease to next value action identifier 0N/A/** The Color choice action identifier 0N/A The color is passed as an argument 0N/A/** The logical style choice action identifier 0N/A The logical style is passed in as an argument 0N/A * Align images at the top. 0N/A * Align images in the middle. 0N/A * Align images at the bottom. 0N/A * Align images at the border. 0N/A /** HTML used when inserting tables. */ 0N/A /** HTML used when inserting unordered lists. */ 0N/A /** HTML used when inserting ordered lists. */ 0N/A /** HTML used when inserting hr. */ 0N/A /** HTML used when inserting pre. */ 0N/A // link navigation support 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 * @return HTMLDocument of <code>e</code>. 0N/A * @return HTMLEditorKit for <code>e</code>. 0N/A * Returns an array of the Elements that contain <code>offset</code>. 0N/A * The first elements corresponds to the root. 0N/A * Recursive method used by getElementsAt. 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 * Returns the deepest element at <code>offset</code> matching 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 <table><tr><td></td></tr></table>. 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 * A cover for HTMLEditorKit.insertHTML. If an exception it 0N/A * thrown it is wrapped in a RuntimeException and thrown. 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 * 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 // Find the common parent. 0N/A // If inserting at the origin, the common parent is the 0N/A // Determine how many pops to do. 0N/A // And how many pushes 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 * Called after an insertion to adjust the selection. 0N/A * Inserts the HTML into the document. 0N/A * @param ae the event 0N/A /** HTML to insert. */ 0N/A /** Tag to check for in the document. */ 0N/A /** Tag in HTML to start adding tags from. */ 0N/A /** Alternate Tag to check for in the document if parentTag is 0N/A /** Alternate tag in HTML to start adding tags from if parentTag 0N/A * is not found and alternateParentTag is found. */ 0N/A /** True indicates the selection should be adjusted after an insert. */ 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 * Inserts the HTML into the document. 0N/A * @param ae the event 0N/A * Returns the object in an AttributeSet matching a key 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 * Create this action with the appropriate identifier. 0N/A * Called when the caret position is updated. 0N/A * @param e the caret event 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 * The operation to perform when this action is triggered. 0N/A // TODO: Should start successive iterations from the 0N/A // current caret position. 0N/A // highlight the next link or object after the current caret position 0N/A }
else {
// focus forward 0N/A * Moves the caret from mark to dot 0N/A * A highlight painter that draws a one-pixel border around 0N/A * the highlighted area. 0N/A * Paints a portion of a highlight. 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 // Contained in view, can just use bounds. 0N/A // Should only render part of View. 0N/A // --- determine locations --- 0N/A // Only if exception 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 * Create this action with the appropriate identifier. 0N/A * activates the hyperlink at offset 0N/A * Invokes default action on the object in an element 0N/A * Returns the root view for a document 0N/A * Returns a view associated with an element 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 * Releases a lock previously obtained via <code>lock</code>. 0N/A * The operation to perform when this action is triggered. 0N/A // invoke the next link or object action 0N/A * Move the caret to the beginning of the document. 0N/A * @see DefaultEditorKit#beginAction 0N/A * @see HTMLEditorKit#getActions 0N/A /* Create this object with the appropriate identifier. */ 0N/A /** The operation to perform when this action is triggered. */