📄 htmleditorkit.java
字号:
return HTMLEditorKit.class.getResourceAsStream(name); } } /** * Fetches the command list for the editor. This is * the list of commands supported by the superclass * augmented by the collection of commands defined * locally for style operations. * * @return the command list */ public Action[] getActions() { return TextAction.augmentList(super.getActions(), this.defaultActions); } /** * Copies the key/values in <code>element</code>s AttributeSet into * <code>set</code>. This does not copy component, icon, or element * names attributes. Subclasses may wish to refine what is and what * isn't copied here. But be sure to first remove all the attributes that * are in <code>set</code>.<p> * This is called anytime the caret moves over a different location. * */ protected void createInputAttributes(Element element, MutableAttributeSet set) { set.removeAttributes(set); set.addAttributes(element.getAttributes()); set.removeAttribute(StyleConstants.ComposedTextAttribute); Object o = set.getAttribute(StyleConstants.NameAttribute); if (o instanceof HTML.Tag) { HTML.Tag tag = (HTML.Tag)o; // PENDING: we need a better way to express what shouldn't be // copied when editing... if(tag == HTML.Tag.IMG) { // Remove the related image attributes, src, width, height set.removeAttribute(HTML.Attribute.SRC); set.removeAttribute(HTML.Attribute.HEIGHT); set.removeAttribute(HTML.Attribute.WIDTH); set.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT); } else if (tag == HTML.Tag.HR || tag == HTML.Tag.BR) { // Don't copy HRs or BRs either. set.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT); } else if (tag == HTML.Tag.COMMENT) { // Don't copy COMMENTs either set.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT); set.removeAttribute(HTML.Attribute.COMMENT); } else if (tag == HTML.Tag.INPUT) { // or INPUT either set.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT); set.removeAttribute(HTML.Tag.INPUT); } else if (tag instanceof HTML.UnknownTag) { // Don't copy unknowns either:( set.addAttribute(StyleConstants.NameAttribute, HTML.Tag.CONTENT); set.removeAttribute(HTML.Attribute.ENDTAG); } } } /** * Gets the input attributes used for the styled * editing actions. * * @return the attribute set */ public MutableAttributeSet getInputAttributes() { if (input == null) { input = getStyleSheet().addStyle(null, null); } return input; } /** * Sets the default cursor. * * @since 1.3 */ public void setDefaultCursor(Cursor cursor) { defaultCursor = cursor; } /** * Returns the default cursor. * * @since 1.3 */ public Cursor getDefaultCursor() { return defaultCursor; } /** * Sets the cursor to use over links. * * @since 1.3 */ public void setLinkCursor(Cursor cursor) { linkCursor = cursor; } /** * Returns the cursor to use over hyper links. * @since 1.3 */ public Cursor getLinkCursor() { return linkCursor; } /** * Indicates whether an html form submission is processed automatically * or only <code>FormSubmitEvent</code> is fired. * * @return true if html form submission is processed automatically, * false otherwise. * * @see #setAutoFormSubmission * @since 1.5 */ public boolean isAutoFormSubmission() { return isAutoFormSubmission; } /** * Specifies if an html form submission is processed * automatically or only <code>FormSubmitEvent</code> is fired. * By default it is set to true. * * @see #isAutoFormSubmission * @see FormSubmitEvent * @since 1.5 */ public void setAutoFormSubmission(boolean isAuto) { isAutoFormSubmission = isAuto; } /** * Creates a copy of the editor kit. * * @return the copy */ public Object clone() { HTMLEditorKit o = (HTMLEditorKit)super.clone(); if (o != null) { o.input = null; o.linkHandler = new LinkController(); } return o; } /** * Fetch the parser to use for reading HTML streams. * This can be reimplemented to provide a different * parser. The default implementation is loaded dynamically * to avoid the overhead of loading the default parser if * it's not used. The default parser is the HotJava parser * using an HTML 3.2 DTD. */ protected Parser getParser() { if (defaultParser == null) { try { Class c = Class.forName("javax.swing.text.html.parser.ParserDelegator"); defaultParser = (Parser) c.newInstance(); } catch (Throwable e) { } } return defaultParser; } // ----- Accessibility support ----- private AccessibleContext accessibleContext; /** * returns the AccessibleContext associated with this editor kit * * @return the AccessibleContext associated with this editor kit * @since 1.4 */ public AccessibleContext getAccessibleContext() { if (theEditor == null) { return null; } if (accessibleContext == null) { AccessibleHTML a = new AccessibleHTML(theEditor); accessibleContext = a.getAccessibleContext(); } return accessibleContext; } // --- variables ------------------------------------------ private static final Cursor MoveCursor = Cursor.getPredefinedCursor (Cursor.HAND_CURSOR); private static final Cursor DefaultCursor = Cursor.getPredefinedCursor (Cursor.DEFAULT_CURSOR); /** Shared factory for creating HTML Views. */ private static final ViewFactory defaultFactory = new HTMLFactory(); MutableAttributeSet input; private static StyleSheet defaultStyles = null; private LinkController linkHandler = new LinkController(); private static Parser defaultParser = null; private Cursor defaultCursor = DefaultCursor; private Cursor linkCursor = MoveCursor; private boolean isAutoFormSubmission = true; /** * Class to watch the associated component and fire * hyperlink events on it when appropriate. */ public static class LinkController extends MouseAdapter implements MouseMotionListener, Serializable { private Element curElem = null; /** * If true, the current element (curElem) represents an image. */ private boolean curElemImage = false; private String href = null; /** This is used by viewToModel to avoid allocing a new array each * time. */ private transient Position.Bias[] bias = new Position.Bias[1]; /** * Current offset. */ private int curOffset; /** * Called for a mouse click event. * If the component is read-only (ie a browser) then * the clicked event is used to drive an attempt to * follow the reference specified by a link. * * @param e the mouse event * @see MouseListener#mouseClicked */ public void mouseClicked(MouseEvent e) { JEditorPane editor = (JEditorPane) e.getSource(); if (! editor.isEditable() && SwingUtilities.isLeftMouseButton(e)) { Point pt = new Point(e.getX(), e.getY()); int pos = editor.viewToModel(pt); if (pos >= 0) { activateLink(pos, editor, e.getX(), e.getY()); } } } // ignore the drags public void mouseDragged(MouseEvent e) { } // track the moving of the mouse. public void mouseMoved(MouseEvent e) { JEditorPane editor = (JEditorPane) e.getSource(); HTMLEditorKit kit = (HTMLEditorKit)editor.getEditorKit(); boolean adjustCursor = true; Cursor newCursor = kit.getDefaultCursor(); if (!editor.isEditable()) { Point pt = new Point(e.getX(), e.getY()); int pos = editor.getUI().viewToModel(editor, pt, bias); if (bias[0] == Position.Bias.Backward && pos > 0) { pos--; } if (pos >= 0 &&(editor.getDocument() instanceof HTMLDocument)){ HTMLDocument hdoc = (HTMLDocument)editor.getDocument(); Element elem = hdoc.getCharacterElement(pos); if (!doesElementContainLocation(editor, elem, pos, e.getX(), e.getY())) { elem = null; } if (curElem != elem || curElemImage) { Element lastElem = curElem; curElem = elem; String href = null; curElemImage = false; if (elem != null) { AttributeSet a = elem.getAttributes(); AttributeSet anchor = (AttributeSet)a. getAttribute(HTML.Tag.A); if (anchor == null) { curElemImage = (a.getAttribute(StyleConstants. NameAttribute) == HTML.Tag.IMG); if (curElemImage) { href = getMapHREF(editor, hdoc, elem, a, pos, e.getX(), e.getY()); } } else { href = (String)anchor.getAttribute (HTML.Attribute.HREF); } } if (href != this.href) { // reference changed, fire event(s) fireEvents(editor, hdoc, href, lastElem); this.href = href; if (href != null) { newCursor = kit.getLinkCursor(); } } else { adjustCursor = false; } } else { adjustCursor = false; } curOffset = pos; } } if (adjustCursor && editor.getCursor() != newCursor) { editor.setCursor(newCursor); } } /** * Returns a string anchor if the passed in element has a * USEMAP that contains the passed in location. */ private String getMapHREF(JEditorPane html, HTMLDocument hdoc, Element elem, AttributeSet attr, int offset, int x, int y) { Object useMap = attr.getAttribute(HTML.Attribute.USEMAP); if (useMap != null && (useMap instanceof String)) { Map m = hdoc.getMap((String)useMap); if (m != null && offset < hdoc.getLength()) { Rectangle bounds; TextUI ui = html.getUI(); try { Shape lBounds = ui.modelToView(html, offset, Position.Bias.Forward); Shape rBounds = ui.modelToView(html, offset + 1, Position.Bias.Backward); bounds = lBounds.getBounds(); bounds.add((rBounds instanceof Rectangle) ? (Rectangle)rBounds : rBounds.getBounds()); } catch (BadLocationException ble) { bounds = null; } if (bounds != null) { AttributeSet area = m.getArea(x - bounds.x, y - bounds.y, bounds.width, bounds.height); if (area != null) { return (String)area.getAttribute(HTML.Attribute. HREF); } } } } return null; } /** * Returns true if the View representing <code>e</code> contains * the location <code>x</code>, <code>y</code>. <code>offset</code> * gives the offset into the Document to check for. */ private boolean doesElementContainLocation(JEditorPane editor, Element e, int offset, int x, int y) { if (e != null && offset > 0 && e.getStartOffset() == offset) { try { TextUI ui = editor.getUI(); Shape s1 = ui.modelToView(editor, offset, Position.Bias.Forward); if (s1 == null) { return false; } Rectangle r1 = (s1 instanceof Rectangle) ? (Rectangle)s1 : s1.getBounds(); Shape s2 = ui.modelToView(editor, e.getEndOffset(), Position.Bias.Backward); if (s2 != null) { Rectangle r2 = (s2 instanceof Rectangle) ? (Rectangle)s2 :
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -