1. /*
  2. * @(#)EditorKit.java 1.19 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.swing.text;
  8. import java.io.*;
  9. import javax.swing.Action;
  10. import javax.swing.JEditorPane;
  11. /**
  12. * Establishes the set of things needed by a text component
  13. * to be a reasonably functioning editor for some <em>type</em>
  14. * of text content. The EditorKit acts as a factory for some
  15. * kind of policy. For example, an implementation
  16. * of html and rtf can be provided that is replaceable
  17. * with other implementations.
  18. * <p>
  19. * A kit can safely store editing state as an instance
  20. * of the kit will be dedicated to a text component.
  21. * New kits will normally be created by cloning a
  22. * prototype kit. The kit will have it's
  23. * <code>setComponent</code> method called to establish
  24. * it's relationship with a JTextComponent.
  25. *
  26. * @author Timothy Prinzing
  27. * @version 1.19 12/19/03
  28. */
  29. public abstract class EditorKit implements Cloneable, Serializable {
  30. /**
  31. * Construct an EditorKit.
  32. */
  33. public EditorKit() {
  34. }
  35. /**
  36. * Creates a copy of the editor kit. This is implemented
  37. * to use Object.clone</em>. If the kit cannot be cloned,
  38. * null is returned.
  39. *
  40. * @return the copy
  41. */
  42. public Object clone() {
  43. Object o;
  44. try {
  45. o = super.clone();
  46. } catch (CloneNotSupportedException cnse) {
  47. o = null;
  48. }
  49. return o;
  50. }
  51. /**
  52. * Called when the kit is being installed into the
  53. * a JEditorPane.
  54. *
  55. * @param c the JEditorPane
  56. */
  57. public void install(JEditorPane c) {
  58. }
  59. /**
  60. * Called when the kit is being removed from the
  61. * JEditorPane. This is used to unregister any
  62. * listeners that were attached.
  63. *
  64. * @param c the JEditorPane
  65. */
  66. public void deinstall(JEditorPane c) {
  67. }
  68. /**
  69. * Gets the MIME type of the data that this
  70. * kit represents support for.
  71. *
  72. * @return the type
  73. */
  74. public abstract String getContentType();
  75. /**
  76. * Fetches a factory that is suitable for producing
  77. * views of any models that are produced by this
  78. * kit.
  79. *
  80. * @return the factory
  81. */
  82. public abstract ViewFactory getViewFactory();
  83. /**
  84. * Fetches the set of commands that can be used
  85. * on a text component that is using a model and
  86. * view produced by this kit.
  87. *
  88. * @return the set of actions
  89. */
  90. public abstract Action[] getActions();
  91. /**
  92. * Fetches a caret that can navigate through views
  93. * produced by the associated ViewFactory.
  94. *
  95. * @return the caret
  96. */
  97. public abstract Caret createCaret();
  98. /**
  99. * Creates an uninitialized text storage model
  100. * that is appropriate for this type of editor.
  101. *
  102. * @return the model
  103. */
  104. public abstract Document createDefaultDocument();
  105. /**
  106. * Inserts content from the given stream which is expected
  107. * to be in a format appropriate for this kind of content
  108. * handler.
  109. *
  110. * @param in The stream to read from
  111. * @param doc The destination for the insertion.
  112. * @param pos The location in the document to place the
  113. * content >= 0.
  114. * @exception IOException on any I/O error
  115. * @exception BadLocationException if pos represents an invalid
  116. * location within the document.
  117. */
  118. public abstract void read(InputStream in, Document doc, int pos)
  119. throws IOException, BadLocationException;
  120. /**
  121. * Writes content from a document to the given stream
  122. * in a format appropriate for this kind of content handler.
  123. *
  124. * @param out The stream to write to
  125. * @param doc The source for the write.
  126. * @param pos The location in the document to fetch the
  127. * content from >= 0.
  128. * @param len The amount to write out >= 0.
  129. * @exception IOException on any I/O error
  130. * @exception BadLocationException if pos represents an invalid
  131. * location within the document.
  132. */
  133. public abstract void write(OutputStream out, Document doc, int pos, int len)
  134. throws IOException, BadLocationException;
  135. /**
  136. * Inserts content from the given stream which is expected
  137. * to be in a format appropriate for this kind of content
  138. * handler.
  139. * <p>
  140. * Since actual text editing is unicode based, this would
  141. * generally be the preferred way to read in the data.
  142. * Some types of content are stored in an 8-bit form however,
  143. * and will favor the InputStream.
  144. *
  145. * @param in The stream to read from
  146. * @param doc The destination for the insertion.
  147. * @param pos The location in the document to place the
  148. * content >= 0.
  149. * @exception IOException on any I/O error
  150. * @exception BadLocationException if pos represents an invalid
  151. * location within the document.
  152. */
  153. public abstract void read(Reader in, Document doc, int pos)
  154. throws IOException, BadLocationException;
  155. /**
  156. * Writes content from a document to the given stream
  157. * in a format appropriate for this kind of content handler.
  158. * <p>
  159. * Since actual text editing is unicode based, this would
  160. * generally be the preferred way to write the data.
  161. * Some types of content are stored in an 8-bit form however,
  162. * and will favor the OutputStream.
  163. *
  164. * @param out The stream to write to
  165. * @param doc The source for the write.
  166. * @param pos The location in the document to fetch the
  167. * content >= 0.
  168. * @param len The amount to write out >= 0.
  169. * @exception IOException on any I/O error
  170. * @exception BadLocationException if pos represents an invalid
  171. * location within the document.
  172. */
  173. public abstract void write(Writer out, Document doc, int pos, int len)
  174. throws IOException, BadLocationException;
  175. }