J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | package javax.swing.text.rtf; |
| 26 | |
| 27 | import java.awt.*; |
| 28 | import java.io.*; |
| 29 | import java.net.MalformedURLException; |
| 30 | import java.net.URL; |
| 31 | import javax.swing.Action; |
| 32 | import javax.swing.text.*; |
| 33 | import javax.swing.*; |
| 34 | |
| 35 | /** |
| 36 | * This is the default implementation of RTF editing |
| 37 | * functionality. The RTF support was not written by the |
| 38 | * Swing team. In the future we hope to improve the support |
| 39 | * provided. |
| 40 | * |
| 41 | * @author Timothy Prinzing (of this class, not the package!) |
| 42 | */ |
| 43 | public class RTFEditorKit extends StyledEditorKit { |
| 44 | |
| 45 | /** |
| 46 | * Constructs an RTFEditorKit. |
| 47 | */ |
| 48 | public RTFEditorKit() { |
| 49 | super(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the MIME type of the data that this |
| 54 | * kit represents support for. This kit supports |
| 55 | * the type <code>text/rtf</code>. |
| 56 | * |
| 57 | * @return the type |
| 58 | */ |
| 59 | public String getContentType() { |
| 60 | return "text/rtf"; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Insert content from the given stream which is expected |
| 65 | * to be in a format appropriate for this kind of content |
| 66 | * handler. |
| 67 | * |
| 68 | * @param in The stream to read from |
| 69 | * @param doc The destination for the insertion. |
| 70 | * @param pos The location in the document to place the |
| 71 | * content. |
| 72 | * @exception IOException on any I/O error |
| 73 | * @exception BadLocationException if pos represents an invalid |
| 74 | * location within the document. |
| 75 | */ |
| 76 | public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException { |
| 77 | |
| 78 | if (doc instanceof StyledDocument) { |
| 79 | // PENDING(prinz) this needs to be fixed to |
| 80 | // insert to the given position. |
| 81 | RTFReader rdr = new RTFReader((StyledDocument) doc); |
| 82 | rdr.readFromStream(in); |
| 83 | rdr.close(); |
| 84 | } else { |
| 85 | // treat as text/plain |
| 86 | super.read(in, doc, pos); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Write content from a document to the given stream |
| 92 | * in a format appropriate for this kind of content handler. |
| 93 | * |
| 94 | * @param out The stream to write to |
| 95 | * @param doc The source for the write. |
| 96 | * @param pos The location in the document to fetch the |
| 97 | * content. |
| 98 | * @param len The amount to write out. |
| 99 | * @exception IOException on any I/O error |
| 100 | * @exception BadLocationException if pos represents an invalid |
| 101 | * location within the document. |
| 102 | */ |
| 103 | public void write(OutputStream out, Document doc, int pos, int len) |
| 104 | throws IOException, BadLocationException { |
| 105 | |
| 106 | // PENDING(prinz) this needs to be fixed to |
| 107 | // use the given document range. |
| 108 | RTFGenerator.writeDocument(doc, out); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Insert content from the given stream, which will be |
| 113 | * treated as plain text. |
| 114 | * |
| 115 | * @param in The stream to read from |
| 116 | * @param doc The destination for the insertion. |
| 117 | * @param pos The location in the document to place the |
| 118 | * content. |
| 119 | * @exception IOException on any I/O error |
| 120 | * @exception BadLocationException if pos represents an invalid |
| 121 | * location within the document. |
| 122 | */ |
| 123 | public void read(Reader in, Document doc, int pos) |
| 124 | throws IOException, BadLocationException { |
| 125 | |
| 126 | if (doc instanceof StyledDocument) { |
| 127 | RTFReader rdr = new RTFReader((StyledDocument) doc); |
| 128 | rdr.readFromReader(in); |
| 129 | rdr.close(); |
| 130 | } else { |
| 131 | // treat as text/plain |
| 132 | super.read(in, doc, pos); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Write content from a document to the given stream |
| 138 | * as plain text. |
| 139 | * |
| 140 | * @param out The stream to write to |
| 141 | * @param doc The source for the write. |
| 142 | * @param pos The location in the document to fetch the |
| 143 | * content. |
| 144 | * @param len The amount to write out. |
| 145 | * @exception IOException on any I/O error |
| 146 | * @exception BadLocationException if pos represents an invalid |
| 147 | * location within the document. |
| 148 | */ |
| 149 | public void write(Writer out, Document doc, int pos, int len) |
| 150 | throws IOException, BadLocationException { |
| 151 | |
| 152 | throw new IOException("RTF is an 8-bit format"); |
| 153 | } |
| 154 | |
| 155 | } |