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