diff options
author | Mark Wielaard <mark@gcc.gnu.org> | 2006-01-17 18:09:40 +0000 |
---|---|---|
committer | Mark Wielaard <mark@gcc.gnu.org> | 2006-01-17 18:09:40 +0000 |
commit | 2127637945ea6b763966398130e0770fa993c860 (patch) | |
tree | c976ca91e3ef0bda3b34b37c0195145638d8d08e /libjava/classpath/javax/swing/JEditorPane.java | |
parent | bcb36c3e02e3bd2843aad1b9888513dfb5d6e337 (diff) | |
download | gcc-2127637945ea6b763966398130e0770fa993c860.zip gcc-2127637945ea6b763966398130e0770fa993c860.tar.gz gcc-2127637945ea6b763966398130e0770fa993c860.tar.bz2 |
Imported GNU Classpath 0.20
Imported GNU Classpath 0.20
* Makefile.am (AM_CPPFLAGS): Add classpath/include.
* java/nio/charset/spi/CharsetProvider.java: New override file.
* java/security/Security.java: Likewise.
* sources.am: Regenerated.
* Makefile.in: Likewise.
From-SVN: r109831
Diffstat (limited to 'libjava/classpath/javax/swing/JEditorPane.java')
-rw-r--r-- | libjava/classpath/javax/swing/JEditorPane.java | 147 |
1 files changed, 139 insertions, 8 deletions
diff --git a/libjava/classpath/javax/swing/JEditorPane.java b/libjava/classpath/javax/swing/JEditorPane.java index 39f7c1f..3560ffd 100644 --- a/libjava/classpath/javax/swing/JEditorPane.java +++ b/libjava/classpath/javax/swing/JEditorPane.java @@ -43,8 +43,10 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; +import java.io.StringReader; import java.net.MalformedURLException; import java.net.URL; +import java.util.HashMap; import javax.accessibility.AccessibleContext; import javax.accessibility.AccessibleHyperlink; @@ -88,6 +90,7 @@ import javax.swing.text.html.HTMLEditorKit; * * @author original author unknown * @author Roman Kennke (roman@kennke.org) + * @author Anthony Balkissoon abalkiss at redhat dot com */ public class JEditorPane extends JTextComponent { @@ -168,6 +171,14 @@ public class JEditorPane extends JTextComponent { /** + * Creates a new JEditorPaneAccessibleHypertextSupport object. + */ + public JEditorPaneAccessibleHypertextSupport() + { + super(); + } + + /** * The accessible representation of a HTML link. * * @author Roman Kennke (kennke@aicas.com) @@ -499,9 +510,16 @@ public class JEditorPane extends JTextComponent 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()); } @@ -512,24 +530,69 @@ public class JEditorPane extends JTextComponent public JEditorPane(String type, String text) { + init(); setEditorKit(createEditorKitForContentType(type)); setText(text); } public JEditorPane(URL url) throws IOException { - this(); + 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) { - return new PlainEditorKit(); + // 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; } /** @@ -578,14 +641,44 @@ public class JEditorPane extends JTextComponent 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 "text/plain"; + 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) { - return editorKit; + // 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; } /** @@ -669,12 +762,17 @@ public class JEditorPane extends JTextComponent } /** - * Establishes the default bindings of type to classname. + * 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) { - // TODO: Implement this properly. + registerMap.put(type, classname); } /** @@ -694,6 +792,7 @@ public class JEditorPane extends JTextComponent public void replaceSelection(String content) { // TODO: Implement this properly. + super.replaceSelection(content); } /** @@ -741,9 +840,14 @@ public class JEditorPane extends JTextComponent 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) { - // FIXME: editorKitCache.put(type, kit); + editorMap.put(type, k); } /** @@ -773,9 +877,36 @@ public class JEditorPane extends JTextComponent } } + /** + * 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) { - super.setText(t); + try + { + // Remove the current content. + Document doc = getDocument(); + doc.remove(0, doc.getLength()); + if (t == null || t == "") + 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. + } } /** |