jeditorpane.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 977 行 · 第 1/2 页
JAVA
977 行
private static class PlainEditorKit extends DefaultEditorKit { /** * Returns a ViewFactory that supplies WrappedPlainViews. */ public ViewFactory getViewFactory() { return new ViewFactory() { public View create(Element el) { return new WrappedPlainView(el); } }; } } private static final long serialVersionUID = 3140472492599046285L; private URL page; private EditorKit editorKit; boolean focus_root; // A mapping between content types and registered EditorKit types static HashMap registerMap; // A mapping between content types and used EditorKits HashMap editorMap; public JEditorPane() { init(); setEditorKit(createDefaultEditorKit()); } public JEditorPane(String url) throws IOException { this(new URL(url)); } public JEditorPane(String type, String text) { init(); setEditorKit(createEditorKitForContentType(type)); setText(text); } public JEditorPane(URL url) throws IOException { init (); setEditorKit (createEditorKitForContentType("text/html"));; setPage(url); } /** * Called by the constructors to set up the default bindings for content * types and EditorKits. */ void init() { editorMap = new HashMap(); registerMap = new HashMap(); registerEditorKitForContentType("application/rtf", "javax.swing.text.rtf.RTFEditorKit"); registerEditorKitForContentType("text/plain", "javax.swing.JEditorPane$PlainEditorKit"); registerEditorKitForContentType("text/html", "javax.swing.text.html.HTMLEditorKit"); registerEditorKitForContentType("text/rtf", "javax.swing.text.rtf.RTFEditorKit"); } protected EditorKit createDefaultEditorKit() { return new PlainEditorKit(); } /** * Creates and returns an EditorKit that is appropriate for the given * content type. This is created using the default recognized types * plus any EditorKit types that have been registered. * * @see #registerEditorKitForContentType(String, String) * @see #registerEditorKitForContentType(String, String, ClassLoader) * @param type the content type * @return an EditorKit for use with the given content type */ public static EditorKit createEditorKitForContentType(String type) { // TODO: Have to handle the case where a ClassLoader was specified // when the EditorKit was registered EditorKit e = null; String className = (String)registerMap.get(type); if (className != null) { try { e = (EditorKit) Class.forName(className).newInstance(); } catch (Exception e2) { // TODO: Not sure what to do here. } } return e; } /** * Sends a given <code>HyperlinkEvent</code> to all registered listeners. * * @param event the event to send */ public void fireHyperlinkUpdate(HyperlinkEvent event) { HyperlinkListener[] listeners = getHyperlinkListeners(); for (int index = 0; index < listeners.length; ++index) listeners[index].hyperlinkUpdate(event); } /** * Returns the accessible context associated with this editor pane. * * @return the accessible context associated with this editor pane */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) { if (getEditorKit() instanceof HTMLEditorKit) accessibleContext = new AccessibleJEditorPaneHTML(); else accessibleContext = new AccessibleJEditorPane(); } return accessibleContext; } public final String getContentType() { return getEditorKit().getContentType(); } /** * Returns the EditorKit. If there is no EditorKit set this method * calls createDefaultEditorKit() and setEditorKit() first. */ public EditorKit getEditorKit() { if (editorKit == null) setEditorKit(createDefaultEditorKit()); return editorKit; } /** * Returns the class name of the EditorKit associated with the given * content type. * * @since 1.3 * @param type the content type * @return the class name of the EditorKit associated with this content type */ public static String getEditorKitClassNameForContentType(String type) { return (String) registerMap.get(type); } /** * Returns the EditorKit to use for the given content type. If an * EditorKit has been explicitly set via * <code>setEditorKitForContentType</code> * then it will be returned. Otherwise an attempt will be made to create * an EditorKit from the default recognzied content types or any * EditorKits that have been registered. If none can be created, a * PlainEditorKit is created. * * @see #registerEditorKitForContentType(String, String) * @see #registerEditorKitForContentType(String, String, ClassLoader) * @param type the content type * @return an appropriate EditorKit for the given content type */ public EditorKit getEditorKitForContentType(String type) { // First check if an EditorKit has been explicitly set. EditorKit e = (EditorKit) editorMap.get(type); // Then check to see if we can create one. if (e == null) e = createEditorKitForContentType(type); // Otherwise default to PlainEditorKit. if (e == null) e = new PlainEditorKit(); return e; } /** * Returns the preferred size for the JEditorPane. This is implemented to * return the super's preferred size, unless one of * {@link #getScrollableTracksViewportHeight()} or * {@link #getScrollableTracksViewportWidth()} returns <code>true</code>, * in which case the preferred width and/or height is replaced by the UI's * minimum size. * * @return the preferred size for the JEditorPane */ public Dimension getPreferredSize() { Dimension pref = super.getPreferredSize(); if (getScrollableTracksViewportWidth()) pref.width = getUI().getMinimumSize(this).width; if (getScrollableTracksViewportHeight()) pref.height = getUI().getMinimumSize(this).height; return pref; } /** * Returns <code>true</code> when a Viewport should force the height of * this component to match the viewport height. This is implemented to return * <code>true</code> when the parent is an instance of JViewport and * the viewport height > the UI's minimum height. * * @return <code>true</code> when a Viewport should force the height of * this component to match the viewport height */ public boolean getScrollableTracksViewportHeight() { // Tests show that this returns true when the parent is a JViewport // and has a height > minimum UI height. Container parent = getParent(); return parent instanceof JViewport && parent.getHeight() > getUI().getMinimumSize(this).height; } /** * Returns <code>true</code> when a Viewport should force the width of * this component to match the viewport width. This is implemented to return * <code>true</code> when the parent is an instance of JViewport and * the viewport width > the UI's minimum width. * * @return <code>true</code> when a Viewport should force the width of * this component to match the viewport width */ public boolean getScrollableTracksViewportWidth() { // Tests show that this returns true when the parent is a JViewport // and has a width > minimum UI width. Container parent = getParent(); return parent != null && parent instanceof JViewport && parent.getWidth() > getUI().getMinimumSize(this).width; } public URL getPage() { return page; } protected InputStream getStream(URL page) throws IOException { return page.openStream(); } public String getText() { return super.getText(); } public String getUIClassID() { return "EditorPaneUI"; } public boolean isFocusCycleRoot() { return focus_root; } protected String paramString() { return "JEditorPane"; } /** * This method initializes from a stream. */ public void read(InputStream in, Object desc) throws IOException { EditorKit kit = getEditorKit(); if (kit instanceof HTMLEditorKit && desc instanceof HTMLDocument) { Document doc = (Document) desc; try { kit.read(in, doc, 0); } catch (BadLocationException ex) { assert false : "BadLocationException must not be thrown here."; } } else { Reader inRead = new InputStreamReader(in); super.read(inRead, desc); } } /** * Establishes a binding between type and classname. This enables * us to create an EditorKit later for the given content type. * * @param type the content type * @param classname the name of the class that is associated with this * content type */ public static void registerEditorKitForContentType(String type, String classname) { registerMap.put(type, classname); } /** * Establishes the default bindings of type to classname. */ public static void registerEditorKitForContentType(String type, String classname, ClassLoader loader) { // TODO: Implement this properly. } /** * Replaces the currently selected content with new content represented * by the given string. */ public void replaceSelection(String content) { // TODO: Implement this properly. super.replaceSelection(content); } /** * Scrolls the view to the given reference location (that is, the value * returned by the UL.getRef method for the URL being displayed). */ public void scrollToReference(String reference) { // TODO: Implement this properly. } public final void setContentType(String type) { if (editorKit != null && editorKit.getContentType().equals(type)) return; EditorKit kit = getEditorKitForContentType(type); if (kit != null) setEditorKit(kit); } public void setEditorKit(EditorKit newValue) { if (editorKit == newValue) return; if (editorKit != null) editorKit.deinstall(this); EditorKit oldValue = editorKit; editorKit = newValue; if (editorKit != null) { editorKit.install(this); setDocument(editorKit.createDefaultDocument()); } firePropertyChange("editorKit", oldValue, newValue); invalidate(); repaint(); // Reset the accessibleContext since this depends on the editorKit. accessibleContext = null; } /** * Explicitly sets an EditorKit to be used for the given content type. * @param type the content type * @param k the EditorKit to use for the given content type */ public void setEditorKitForContentType(String type, EditorKit k) { editorMap.put(type, k); } /** * Sets the current URL being displayed. */ public void setPage(String url) throws IOException { setPage(new URL(url)); } /** * Sets the current URL being displayed. */ public void setPage(URL page) throws IOException { if (page == null) throw new IOException("invalid url"); try { this.page = page; getEditorKit().read(page.openStream(), getDocument(), 0); } catch (BadLocationException e) { // Ignored. '0' is always a valid offset. } } /** * Sets the text of the JEditorPane. The argument <code>t</code> * is expected to be in the format of the current EditorKit. This removes * the content of the current document and uses the EditorKit to read in the * new text. This allows the EditorKit to handle the String rather than just * inserting in plain text. * * @param t the text to display in this JEditorPane */ public void setText(String t) { try { // Remove the current content. Document doc = getDocument(); doc.remove(0, doc.getLength()); if (t == null || t.equals("")) return; // Let the EditorKit read the text into the Document. getEditorKit().read(new StringReader(t), doc, 0); } catch (BadLocationException ble) { // TODO: Don't know what to do here. } catch (IOException ioe) { // TODO: Don't know what to do here. } } /** * Add a <code>HyperlinkListener</code> object to this editor pane. * * @param listener the listener to add */ public void addHyperlinkListener(HyperlinkListener listener) { listenerList.add(HyperlinkListener.class, listener); } /** * Removes a <code>HyperlinkListener</code> object to this editor pane. * * @param listener the listener to remove */ public void removeHyperlinkListener(HyperlinkListener listener) { listenerList.remove(HyperlinkListener.class, listener); } /** * Returns all added <code>HyperlinkListener</code> objects. * * @return array of listeners * * @since 1.4 */ public HyperlinkListener[] getHyperlinkListeners() { return (HyperlinkListener[]) getListeners(HyperlinkListener.class); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?