htmleditorkit.java

来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 1,167 行 · 第 1/3 页

JAVA
1,167
字号
/* HTMLEditorKit.java --   Copyright (C) 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.text.html;import java.awt.event.ActionEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.MouseMotionListener;import java.awt.Cursor;import java.io.IOException;import java.io.Reader;import java.io.Serializable;import java.io.StringReader;import java.io.Writer;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.swing.Action;import javax.swing.JEditorPane;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.text.EditorKit;import javax.swing.text.Element;import javax.swing.text.MutableAttributeSet;import javax.swing.text.StyleConstants;import javax.swing.text.StyleContext;import javax.swing.text.StyledEditorKit;import javax.swing.text.TextAction;import javax.swing.text.View;import javax.swing.text.ViewFactory;import javax.swing.text.html.parser.ParserDelegator;/** * @author Lillian Angel (langel at redhat dot com) */public class HTMLEditorKit  extends StyledEditorKit  implements Serializable, Cloneable, Accessible{    /**   * Fires the hyperlink events on the associated component   * when needed.   */  public static class LinkController    extends MouseAdapter    implements MouseMotionListener, Serializable    {            /**       * Constructor       */      public LinkController()       {        super();      }            /**       * Dispatched when the mouse is clicked. If the component       * is read-only, then the clicked event is used to drive an       * attempt to follow the reference specified by a link       *        * @param e - the mouse event       */      public void mouseClicked(MouseEvent e)      {        /*         These MouseInputAdapter methods generate mouse appropriate events around         hyperlinks (entering, exiting, and activating).         */        // FIXME: Not implemented.      }            /**       * Dispatched when the mouse is dragged on a component.       *        * @param e - the mouse event.       */      public void mouseDragged(MouseEvent e)      {        /*        These MouseInputAdapter methods generate mouse appropriate events around        hyperlinks (entering, exiting, and activating).        */        // FIXME: Not implemented.           }            /**       * Dispatched when the mouse cursor has moved into the component.       *        * @param e - the mouse event.       */      public void mouseMoved(MouseEvent e)      {        /*        These MouseInputAdapter methods generate mouse appropriate events around        hyperlinks (entering, exiting, and activating).        */        // FIXME: Not implemented.      }            /**       * If the given position represents a link, then linkActivated is called       * on the JEditorPane. Implemented to forward to the method with the same       * name, but pos == editor == -1.       *        * @param pos - the position       * @param editor - the editor pane       */      protected void activateLink(int pos,                                  JEditorPane editor)      {        /*          This method creates and fires a HyperlinkEvent if the document is an          instance of HTMLDocument and the href tag of the link is not null.         */        // FIXME: Not implemented.      }    }    /**   * This class is used to insert a string of HTML into an existing   * document. At least 2 HTML.Tags need to be supplied. The first Tag (parentTag)   * identifies the parent in the document to add the elements to. The second, (addTag),    * identifies that the first tag should be added to the document as seen in the string.   * The parser will generate all appropriate (opening/closing tags_ even if they are not   * in the HTML string passed in.   */  public static class InsertHTMLTextAction    extends HTMLTextAction    {            /**       * Tag in HTML to start adding tags from.       */      protected HTML.Tag addTag;            /**       * Alternate tag in HTML to start adding tags from if parentTag is       * not found and alternateParentTag is not found.       */            protected HTML.Tag alternateAddTag;            /**       * Alternate tag to check if parentTag is not found.       */      protected HTML.Tag alternateParentTag;            /**       * HTML to insert.       */      protected String html;            /**       * Tag to check for in the document.       */      protected HTML.Tag parentTag;            /**       * Initializes all fields.       *        * @param name - the name of the document.       * @param html - the html to insert       * @param parentTag - the parent tag to check for       * @param addTag - the tag to start adding from       */      public InsertHTMLTextAction(String name, String html,                                   HTML.Tag parentTag, HTML.Tag addTag)      {        this(name, html, parentTag, addTag, null, null);      }            /**       * Initializes all fields and calls super       *        * @param name - the name of the document.       * @param html - the html to insert       * @param parentTag - the parent tag to check for       * @param addTag - the tag to start adding from       * @param alternateParentTag - the alternate parent tag       * @param alternateAddTag - the alternate add tag       */      public InsertHTMLTextAction(String name, String html, HTML.Tag parentTag,                                   HTML.Tag addTag, HTML.Tag alternateParentTag,                                   HTML.Tag alternateAddTag)       {        super(name);        // Fields are for easy access when the action is applied to an actual        // document.        this.html = html;        this.parentTag = parentTag;        this.addTag = addTag;        this.alternateParentTag = alternateParentTag;        this.alternateAddTag = alternateAddTag;      }            /**       * HTMLEditorKit.insertHTML is called. If an exception is       * thrown, it is wrapped in a RuntimeException and thrown.       *        * @param editor - the editor to use to get the editorkit       * @param doc -       *          the Document to insert the HTML into.       * @param offset -       *          where to begin inserting the HTML.       * @param html -       *          the String to insert       * @param popDepth -       *          the number of ElementSpec.EndTagTypes to generate before       *          inserting       * @param pushDepth -       *          the number of ElementSpec.StartTagTypes with a direction of       *          ElementSpec.JoinNextDirection that should be generated before       * @param addTag -       *          the first tag to start inserting into document       */      protected void insertHTML(JEditorPane editor, HTMLDocument doc, int offset,                              String html, int popDepth, int pushDepth,                              HTML.Tag addTag)      {        try          {            super.getHTMLEditorKit(editor).insertHTML(doc, offset, html,                                                      popDepth, pushDepth, addTag);          }        catch (IOException e)          {            throw (RuntimeException) new RuntimeException("Parser is null.").initCause(e);          }        catch (BadLocationException ex)          {            throw (RuntimeException) new RuntimeException("BadLocationException: "                                              + offset).initCause(ex);          }      }            /**       * Invoked when inserting at a boundary. Determines the number of pops,       * and then the number of pushes that need to be performed. The it calls       * insertHTML.       *        * @param editor -       *          the editor to use to get the editorkit       * @param doc -       *          the Document to insert the HTML into.       * @param offset -       *          where to begin inserting the HTML.       * @param insertElement -       *          the element to insert       * @param html -       *          the html to insert       * @param parentTag -       *          the parent tag       * @param addTag -       *          the first tag       */      protected void insertAtBoundary(JEditorPane editor,                                      HTMLDocument doc, int offset,                                      Element insertElement,                                      String html, HTML.Tag parentTag,                                      HTML.Tag addTag)      {        /*        As its name implies, this protected method is used when HTML is inserted at a        boundary. (A boundary in this case is an offset in doc that exactly matches the        beginning offset of the parentTag.) It performs the extra work required to keep        the tag stack in shape and then calls insertHTML(). The editor and doc argu-        ments are the editor pane and document where the HTML should go. The offset        argument represents the cursor location or selection start in doc. The insert-        Element and parentTag arguments are used to calculate the proper number of        tag pops and pushes before inserting the HTML (via html and addTag, which are        passed directly to insertHTML()).        */        // FIXME: not implemented      }            /**       * Invoked when inserting at a boundary. Determines the number of pops,        * and then the number of pushes that need to be performed. The it calls       * insertHTML.       *        * @param editor - the editor to use to get the editorkit       * @param doc -       *          the Document to insert the HTML into.       * @param offset -       *          where to begin inserting the HTML.       * @param insertElement - the element to insert       * @param html - the html to insert       * @param parentTag - the parent tag       * @param addTag - the first tag       *        * @deprecated as of v1.3, use insertAtBoundary       */      protected void insertAtBoundry(JEditorPane editor,                                     HTMLDocument doc,                                     int offset, Element insertElement,                                     String html, HTML.Tag parentTag,                                     HTML.Tag addTag)      {        insertAtBoundary(editor, doc, offset, insertElement,                         html, parentTag, addTag);      }            /**       * Inserts the HTML.       *        * @param ae - the action performed       */      public void actionPerformed(ActionEvent ae)      {        Object source = ae.getSource();        if (source instanceof JEditorPane)          {            JEditorPane pane = ((JEditorPane) source);            Document d = pane.getDocument();            if (d instanceof HTMLDocument)              insertHTML(pane, (HTMLDocument) d, 0, html, 0, 0, addTag);            // FIXME: is this correct parameters?          }        // FIXME: else not implemented      }  }    /**   * Abstract Action class that helps inserting HTML into an existing document.   */  public abstract static class HTMLTextAction    extends StyledEditorKit.StyledTextAction    {            /**       * Constructor       */      public HTMLTextAction(String name)       {        super(name);      }            /**       * Gets the HTMLDocument from the JEditorPane.       *        * @param e - the editor pane       * @return the html document.       */      protected HTMLDocument getHTMLDocument(JEditorPane e)      {        Document d = e.getDocument();        if (d instanceof HTMLDocument)          return (HTMLDocument) d;        throw new IllegalArgumentException("Document is not a HTMLDocument.");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?