1. /*
  2. * @(#)RTFEditorKit.java 1.13 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.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.13 12/19/03
  23. */
  24. public class RTFEditorKit extends StyledEditorKit {
  25. /**
  26. * Constructs an RTFEditorKit.
  27. */
  28. public RTFEditorKit() {
  29. super();
  30. }
  31. /**
  32. * Get the MIME type of the data that this
  33. * kit represents support for. This kit supports
  34. * the type <code>text/rtf</code>.
  35. *
  36. * @return the type
  37. */
  38. public String getContentType() {
  39. return "text/rtf";
  40. }
  41. /**
  42. * Insert content from the given stream which is expected
  43. * to be in a format appropriate for this kind of content
  44. * handler.
  45. *
  46. * @param in The stream to read from
  47. * @param doc The destination for the insertion.
  48. * @param pos The location in the document to place the
  49. * content.
  50. * @exception IOException on any I/O error
  51. * @exception BadLocationException if pos represents an invalid
  52. * location within the document.
  53. */
  54. public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException {
  55. if (doc instanceof StyledDocument) {
  56. // PENDING(prinz) this needs to be fixed to
  57. // insert to the given position.
  58. RTFReader rdr = new RTFReader((StyledDocument) doc);
  59. rdr.readFromStream(in);
  60. rdr.close();
  61. } else {
  62. // treat as text/plain
  63. super.read(in, doc, pos);
  64. }
  65. }
  66. /**
  67. * Write content from a document to the given stream
  68. * in a format appropriate for this kind of content handler.
  69. *
  70. * @param out The stream to write to
  71. * @param doc The source for the write.
  72. * @param pos The location in the document to fetch the
  73. * content.
  74. * @param len The amount to write out.
  75. * @exception IOException on any I/O error
  76. * @exception BadLocationException if pos represents an invalid
  77. * location within the document.
  78. */
  79. public void write(OutputStream out, Document doc, int pos, int len)
  80. throws IOException, BadLocationException {
  81. // PENDING(prinz) this needs to be fixed to
  82. // use the given document range.
  83. RTFGenerator.writeDocument(doc, out);
  84. }
  85. /**
  86. * Insert content from the given stream, which will be
  87. * treated as plain text.
  88. *
  89. * @param in The stream to read from
  90. * @param doc The destination for the insertion.
  91. * @param pos The location in the document to place the
  92. * content.
  93. * @exception IOException on any I/O error
  94. * @exception BadLocationException if pos represents an invalid
  95. * location within the document.
  96. */
  97. public void read(Reader in, Document doc, int pos)
  98. throws IOException, BadLocationException {
  99. if (doc instanceof StyledDocument) {
  100. RTFReader rdr = new RTFReader((StyledDocument) doc);
  101. rdr.readFromReader(in);
  102. rdr.close();
  103. } else {
  104. // treat as text/plain
  105. super.read(in, doc, pos);
  106. }
  107. }
  108. /**
  109. * Write content from a document to the given stream
  110. * as plain text.
  111. *
  112. * @param out The stream to write to
  113. * @param doc The source for the write.
  114. * @param pos The location in the document to fetch the
  115. * content.
  116. * @param len The amount to write out.
  117. * @exception IOException on any I/O error
  118. * @exception BadLocationException if pos represents an invalid
  119. * location within the document.
  120. */
  121. public void write(Writer out, Document doc, int pos, int len)
  122. throws IOException, BadLocationException {
  123. throw new IOException("RTF is an 8-bit format");
  124. }
  125. }