1 | /*
|
---|
2 | * 07/28/2008
|
---|
3 | *
|
---|
4 | * RtfTransferable.java - Used during drag-and-drop to represent RTF text.
|
---|
5 | *
|
---|
6 | * This library is distributed under a modified BSD license. See the included
|
---|
7 | * RSyntaxTextArea.License.txt file for details.
|
---|
8 | */
|
---|
9 | package org.fife.ui.rsyntaxtextarea;
|
---|
10 |
|
---|
11 | import java.awt.datatransfer.*;
|
---|
12 | import java.io.ByteArrayInputStream;
|
---|
13 | import java.io.IOException;
|
---|
14 | import java.io.StringReader;
|
---|
15 |
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Object used during copy/paste and DnD operations to represent RTF text.
|
---|
19 | * It can return the text being moved as either RTF or plain text. This
|
---|
20 | * class is basically the same as
|
---|
21 | * <code>java.awt.datatransfer.StringSelection</code>, except that it can also
|
---|
22 | * return the text as RTF.
|
---|
23 | *
|
---|
24 | * @author Robert Futrell
|
---|
25 | * @version 1.0
|
---|
26 | */
|
---|
27 | class RtfTransferable implements Transferable {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The RTF data, in bytes (the RTF is 7-bit ascii).
|
---|
31 | */
|
---|
32 | private byte[] data;
|
---|
33 |
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * The "flavors" the text can be returned as.
|
---|
37 | */
|
---|
38 | private final DataFlavor[] FLAVORS = {
|
---|
39 | new DataFlavor("text/rtf", "RTF"),
|
---|
40 | DataFlavor.stringFlavor,
|
---|
41 | DataFlavor.plainTextFlavor // deprecated
|
---|
42 | };
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Constructor.
|
---|
47 | *
|
---|
48 | * @param data The RTF data.
|
---|
49 | */
|
---|
50 | public RtfTransferable(byte[] data) {
|
---|
51 | this.data = data;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | public Object getTransferData(DataFlavor flavor)
|
---|
56 | throws UnsupportedFlavorException, IOException {
|
---|
57 | if (flavor.equals(FLAVORS[0])) { // RTF
|
---|
58 | return new ByteArrayInputStream(data==null ? new byte[0] : data);
|
---|
59 | }
|
---|
60 | else if (flavor.equals(FLAVORS[1])) { // stringFlavor
|
---|
61 | return data==null ? "" : RtfToText.getPlainText(data);
|
---|
62 | }
|
---|
63 | else if (flavor.equals(FLAVORS[2])) { // plainTextFlavor (deprecated)
|
---|
64 | String text = ""; // Valid if data==null
|
---|
65 | if (data!=null) {
|
---|
66 | text = RtfToText.getPlainText(data);
|
---|
67 | }
|
---|
68 | return new StringReader(text);
|
---|
69 | }
|
---|
70 | else {
|
---|
71 | throw new UnsupportedFlavorException(flavor);
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | public DataFlavor[] getTransferDataFlavors() {
|
---|
77 | return (DataFlavor[])FLAVORS.clone();
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | public boolean isDataFlavorSupported(DataFlavor flavor) {
|
---|
82 | for (int i=0; i<FLAVORS.length; i++) {
|
---|
83 | if (flavor.equals(FLAVORS[i])) {
|
---|
84 | return true;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | } |
---|