KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > rtf > RTFEditorKit


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
9 import java.awt.*;
10 import java.io.*;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13 import javax.swing.Action JavaDoc;
14 import javax.swing.text.*;
15 import javax.swing.*;
16
17 /**
18  * This is the default implementation of RTF editing
19  * functionality. The RTF support was not written by the
20  * Swing team. In the future we hope to improve the support
21  * provided.
22  *
23  * @author Timothy Prinzing (of this class, not the package!)
24  * @version 1.13 12/19/03
25  */

26 public class RTFEditorKit extends StyledEditorKit {
27
28     /**
29      * Constructs an RTFEditorKit.
30      */

31     public RTFEditorKit() {
32     super();
33     }
34
35     /**
36      * Get the MIME type of the data that this
37      * kit represents support for. This kit supports
38      * the type <code>text/rtf</code>.
39      *
40      * @return the type
41      */

42     public String JavaDoc getContentType() {
43     return "text/rtf";
44     }
45
46     /**
47      * Insert content from the given stream which is expected
48      * to be in a format appropriate for this kind of content
49      * handler.
50      *
51      * @param in The stream to read from
52      * @param doc The destination for the insertion.
53      * @param pos The location in the document to place the
54      * content.
55      * @exception IOException on any I/O error
56      * @exception BadLocationException if pos represents an invalid
57      * location within the document.
58      */

59     public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException {
60
61     if (doc instanceof StyledDocument) {
62         // PENDING(prinz) this needs to be fixed to
63
// insert to the given position.
64
RTFReader JavaDoc rdr = new RTFReader JavaDoc((StyledDocument) doc);
65         rdr.readFromStream(in);
66         rdr.close();
67     } else {
68         // treat as text/plain
69
super.read(in, doc, pos);
70     }
71     }
72
73     /**
74      * Write content from a document to the given stream
75      * in a format appropriate for this kind of content handler.
76      *
77      * @param out The stream to write to
78      * @param doc The source for the write.
79      * @param pos The location in the document to fetch the
80      * content.
81      * @param len The amount to write out.
82      * @exception IOException on any I/O error
83      * @exception BadLocationException if pos represents an invalid
84      * location within the document.
85      */

86     public void write(OutputStream out, Document doc, int pos, int len)
87     throws IOException, BadLocationException {
88
89         // PENDING(prinz) this needs to be fixed to
90
// use the given document range.
91
RTFGenerator.writeDocument(doc, out);
92     }
93
94     /**
95      * Insert content from the given stream, which will be
96      * treated as plain text.
97      *
98      * @param in The stream to read from
99      * @param doc The destination for the insertion.
100      * @param pos The location in the document to place the
101      * content.
102      * @exception IOException on any I/O error
103      * @exception BadLocationException if pos represents an invalid
104      * location within the document.
105      */

106     public void read(Reader in, Document doc, int pos)
107     throws IOException, BadLocationException {
108
109     if (doc instanceof StyledDocument) {
110         RTFReader JavaDoc rdr = new RTFReader JavaDoc((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     /**
120      * Write content from a document to the given stream
121      * as plain text.
122      *
123      * @param out The stream to write to
124      * @param doc The source for the write.
125      * @param pos The location in the document to fetch the
126      * content.
127      * @param len The amount to write out.
128      * @exception IOException on any I/O error
129      * @exception BadLocationException if pos represents an invalid
130      * location within the document.
131      */

132     public void write(Writer out, Document doc, int pos, int len)
133     throws IOException, BadLocationException {
134
135     throw new IOException("RTF is an 8-bit format");
136     }
137
138 }
139
Popular Tags