1. /*
  2. * @(#)RTFEditorKit.java 1.9 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package javax.swing.text.rtf;
  11. import java.awt.*;
  12. import java.io.*;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import javax.swing.Action;
  16. import javax.swing.text.*;
  17. import javax.swing.*;
  18. /**
  19. * This is the default implementation of RTF editing
  20. * functionality. The RTF support was not written by the
  21. * Swing team. In the future we hope to improve the support
  22. * provided.
  23. *
  24. * @author Timothy Prinzing (of this class, not the package!)
  25. * @version 1.9 02/02/00
  26. */
  27. public class RTFEditorKit extends StyledEditorKit {
  28. /**
  29. * Constructs an RTFEditorKit.
  30. */
  31. public RTFEditorKit() {
  32. super();
  33. }
  34. /**
  35. * Create a copy of the editor kit. This
  36. * allows an implementation to serve as a prototype
  37. * for others, so that they can be quickly created.
  38. *
  39. * @return the copy
  40. */
  41. public Object clone() {
  42. return new RTFEditorKit();
  43. }
  44. /**
  45. * Get the MIME type of the data that this
  46. * kit represents support for. This kit supports
  47. * the type <code>text/rtf</code>.
  48. *
  49. * @return the type
  50. */
  51. public String getContentType() {
  52. return "text/rtf";
  53. }
  54. /**
  55. * Insert content from the given stream which is expected
  56. * to be in a format appropriate for this kind of content
  57. * handler.
  58. *
  59. * @param in The stream to read from
  60. * @param doc The destination for the insertion.
  61. * @param pos The location in the document to place the
  62. * content.
  63. * @exception IOException on any I/O error
  64. * @exception BadLocationException if pos represents an invalid
  65. * location within the document.
  66. */
  67. public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException {
  68. if (doc instanceof StyledDocument) {
  69. // PENDING(prinz) this needs to be fixed to
  70. // insert to the given position.
  71. RTFReader rdr = new RTFReader((StyledDocument) doc);
  72. rdr.readFromStream(in);
  73. rdr.close();
  74. } else {
  75. // treat as text/plain
  76. super.read(in, doc, pos);
  77. }
  78. }
  79. /**
  80. * Write content from a document to the given stream
  81. * in a format appropriate for this kind of content handler.
  82. *
  83. * @param out The stream to write to
  84. * @param doc The source for the write.
  85. * @param pos The location in the document to fetch the
  86. * content.
  87. * @param len The amount to write out.
  88. * @exception IOException on any I/O error
  89. * @exception BadLocationException if pos represents an invalid
  90. * location within the document.
  91. */
  92. public void write(OutputStream out, Document doc, int pos, int len)
  93. throws IOException, BadLocationException {
  94. // PENDING(prinz) this needs to be fixed to
  95. // use the given document range.
  96. RTFGenerator.writeDocument(doc, out);
  97. }
  98. /**
  99. * Insert content from the given stream, which will be
  100. * treated as plain text.
  101. *
  102. * @param in The stream to read from
  103. * @param doc The destination for the insertion.
  104. * @param pos The location in the document to place the
  105. * content.
  106. * @exception IOException on any I/O error
  107. * @exception BadLocationException if pos represents an invalid
  108. * location within the document.
  109. */
  110. public void read(Reader in, Document doc, int pos)
  111. throws IOException, BadLocationException {
  112. if (doc instanceof StyledDocument) {
  113. RTFReader rdr = new RTFReader((StyledDocument) doc);
  114. rdr.readFromReader(in);
  115. rdr.close();
  116. } else {
  117. // treat as text/plain
  118. super.read(in, doc, pos);
  119. }
  120. }
  121. /**
  122. * Write content from a document to the given stream
  123. * as plain text.
  124. *
  125. * @param out The stream to write to
  126. * @param doc The source for the write.
  127. * @param pos The location in the document to fetch the
  128. * content.
  129. * @param len The amount to write out.
  130. * @exception IOException on any I/O error
  131. * @exception BadLocationException if pos represents an invalid
  132. * location within the document.
  133. */
  134. public void write(Writer out, Document doc, int pos, int len)
  135. throws IOException, BadLocationException {
  136. throw new IOException("RTF is an 8-bit format");
  137. }
  138. }