📄 jeditorpane.java
字号:
HTMLDocument doc = (HTMLDocument) getDocument(); HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A); int count = 0; while (linkIter.isValid()) { if (linkIter.getStartOffset() <= c && linkIter.getEndOffset() > c) break; count++; linkIter.next(); } if (linkIter.isValid()) return count; else return -1; } /** * Returns the link text of the link at index <code>i</code>, or * <code>null</code>, if there is no link at the specified position. * * @param i the index of the link * * @return the link text of the link at index <code>i</code>, or * <code>null</code>, if there is no link at the specified * position */ public String getLinkText(int i) { HTMLDocument doc = (HTMLDocument) getDocument(); HTMLDocument.Iterator linkIter = doc.getIterator(HTML.Tag.A); int count = 0; while (linkIter.isValid()) { count++; if (count == i) break; linkIter.next(); } if (linkIter.isValid()) { int offset = linkIter.getStartOffset(); // TODO: I fetch the element for the link via getCharacterElement(). // I am not sure that this is correct, maybe we must use // getParagraphElement()? Element el = doc.getCharacterElement(offset); try { String text = doc.getText(el.getStartOffset(), el.getEndOffset() - el.getStartOffset()); return text; } catch (BadLocationException ex) { throw (AssertionError) new AssertionError("BadLocationException must not be thrown " + "here.") .initCause(ex); } } else return null; } } /** * An EditorKit used for plain text. This is the default editor kit for * JEditorPanes. * * @author Roman Kennke (kennke@aicas.com) */ 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; public JEditorPane() { setEditorKit(createDefaultEditorKit()); } public JEditorPane(String url) throws IOException { this(new URL(url)); } public JEditorPane(String type, String text) { setEditorKit(createEditorKitForContentType(type)); setText(text); } public JEditorPane(URL url) throws IOException { this(); setPage(url); } protected EditorKit createDefaultEditorKit() { return new PlainEditorKit(); } public static EditorKit createEditorKitForContentType(String type) { return new PlainEditorKit(); } /** * 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; } public static String getEditorKitClassNameForContentType(String type) { return "text/plain"; } public EditorKit getEditorKitForContentType(String type) { return editorKit; } /** * Returns the preferred size for the JEditorPane. */ public Dimension getPreferredSize() { return super.getPreferredSize(); } public boolean getScrollableTracksViewportHeight() { /* Container parent = getParent(); return (parent instanceof JViewport && parent.isValid());*/ return isValid(); } public boolean getScrollableTracksViewportWidth() { /*Container parent = getParent(); return (parent instanceof JViewport && parent.isValid());*/ return isValid(); } 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 the default bindings of type to classname. */ public static void registerEditorKitForContentType(String type, String classname) { // TODO: Implement this properly. } /** * 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. } /** * 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; } public void setEditorKitForContentType(String type, EditorKit k) { // FIXME: editorKitCache.put(type, kit); } /** * 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. } } public void setText(String t) { super.setText(t); } /** * 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -