KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > plaf > basic > BasicTransferable


1 /*
2  * @(#)BasicTransferable.java 1.9 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.plaf.basic;
8
9 import java.io.*;
10 import java.awt.datatransfer.*;
11 import javax.swing.plaf.UIResource JavaDoc;
12
13 /**
14  * A transferable implementation for the default data transfer of some Swing
15  * components.
16  *
17  * @author Timothy Prinzing
18  * @version 1.9 12/19/03
19  */

20 class BasicTransferable implements Transferable, UIResource JavaDoc {
21     
22     protected String JavaDoc plainData;
23     protected String JavaDoc htmlData;
24
25     private static DataFlavor[] htmlFlavors;
26     private static DataFlavor[] stringFlavors;
27     private static DataFlavor[] plainFlavors;
28
29     static {
30     try {
31         htmlFlavors = new DataFlavor[3];
32         htmlFlavors[0] = new DataFlavor("text/html;class=java.lang.String");
33         htmlFlavors[1] = new DataFlavor("text/html;class=java.io.Reader");
34         htmlFlavors[2] = new DataFlavor("text/html;charset=unicode;class=java.io.InputStream");
35
36         plainFlavors = new DataFlavor[3];
37         plainFlavors[0] = new DataFlavor("text/plain;class=java.lang.String");
38         plainFlavors[1] = new DataFlavor("text/plain;class=java.io.Reader");
39         plainFlavors[2] = new DataFlavor("text/plain;charset=unicode;class=java.io.InputStream");
40
41         stringFlavors = new DataFlavor[2];
42             stringFlavors[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class=java.lang.String");
43         stringFlavors[1] = DataFlavor.stringFlavor;
44  
45     } catch (ClassNotFoundException JavaDoc cle) {
46         System.err.println("error initializing javax.swing.plaf.basic.BasicTranserable");
47     }
48     }
49     
50     public BasicTransferable(String JavaDoc plainData, String JavaDoc htmlData) {
51     this.plainData = plainData;
52     this.htmlData = htmlData;
53     }
54
55
56     /**
57      * Returns an array of DataFlavor objects indicating the flavors the data
58      * can be provided in. The array should be ordered according to preference
59      * for providing the data (from most richly descriptive to least descriptive).
60      * @return an array of data flavors in which this data can be transferred
61      */

62     public DataFlavor[] getTransferDataFlavors() {
63     DataFlavor[] richerFlavors = getRicherFlavors();
64     int nRicher = (richerFlavors != null) ? richerFlavors.length : 0;
65     int nHTML = (isHTMLSupported()) ? htmlFlavors.length : 0;
66     int nPlain = (isPlainSupported()) ? plainFlavors.length: 0;
67     int nString = (isPlainSupported()) ? stringFlavors.length : 0;
68     int nFlavors = nRicher + nHTML + nPlain + nString;
69     DataFlavor[] flavors = new DataFlavor[nFlavors];
70     
71     // fill in the array
72
int nDone = 0;
73     if (nRicher > 0) {
74         System.arraycopy(richerFlavors, 0, flavors, nDone, nRicher);
75         nDone += nRicher;
76     }
77     if (nHTML > 0) {
78         System.arraycopy(htmlFlavors, 0, flavors, nDone, nHTML);
79         nDone += nHTML;
80     }
81     if (nPlain > 0) {
82         System.arraycopy(plainFlavors, 0, flavors, nDone, nPlain);
83         nDone += nPlain;
84     }
85     if (nString > 0) {
86         System.arraycopy(stringFlavors, 0, flavors, nDone, nString);
87         nDone += nString;
88     }
89     return flavors;
90     }
91
92     /**
93      * Returns whether or not the specified data flavor is supported for
94      * this object.
95      * @param flavor the requested flavor for the data
96      * @return boolean indicating whether or not the data flavor is supported
97      */

98     public boolean isDataFlavorSupported(DataFlavor flavor) {
99     DataFlavor[] flavors = getTransferDataFlavors();
100         for (int i = 0; i < flavors.length; i++) {
101         if (flavors[i].equals(flavor)) {
102             return true;
103         }
104     }
105     return false;
106     }
107
108     /**
109      * Returns an object which represents the data to be transferred. The class
110      * of the object returned is defined by the representation class of the flavor.
111      *
112      * @param flavor the requested flavor for the data
113      * @see DataFlavor#getRepresentationClass
114      * @exception IOException if the data is no longer available
115      * in the requested flavor.
116      * @exception UnsupportedFlavorException if the requested data flavor is
117      * not supported.
118      */

119     public Object JavaDoc getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
120     DataFlavor[] richerFlavors = getRicherFlavors();
121     if (isRicherFlavor(flavor)) {
122         return getRicherData(flavor);
123     } else if (isHTMLFlavor(flavor)) {
124         String JavaDoc data = getHTMLData();
125         data = (data == null) ? "" : data;
126         if (String JavaDoc.class.equals(flavor.getRepresentationClass())) {
127         return data;
128         } else if (Reader.class.equals(flavor.getRepresentationClass())) {
129         return new StringReader(data);
130         } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
131         return new StringBufferInputStream(data);
132         }
133         // fall through to unsupported
134
} else if (isPlainFlavor(flavor)) {
135         String JavaDoc data = getPlainData();
136         data = (data == null) ? "" : data;
137         if (String JavaDoc.class.equals(flavor.getRepresentationClass())) {
138         return data;
139         } else if (Reader.class.equals(flavor.getRepresentationClass())) {
140         return new StringReader(data);
141         } else if (InputStream.class.equals(flavor.getRepresentationClass())) {
142         return new StringBufferInputStream(data);
143         }
144         // fall through to unsupported
145

146     } else if (isStringFlavor(flavor)) {
147         String JavaDoc data = getPlainData();
148         data = (data == null) ? "" : data;
149         return data;
150     }
151     throw new UnsupportedFlavorException(flavor);
152     }
153
154     // --- richer subclass flavors ----------------------------------------------
155

156     protected boolean isRicherFlavor(DataFlavor flavor) {
157     DataFlavor[] richerFlavors = getRicherFlavors();
158     int nFlavors = (richerFlavors != null) ? richerFlavors.length : 0;
159     for (int i = 0; i < nFlavors; i++) {
160         if (richerFlavors[i].equals(flavor)) {
161         return true;
162         }
163     }
164     return false;
165     }
166     
167     /**
168      * Some subclasses will have flavors that are more descriptive than HTML
169      * or plain text. If this method returns a non-null value, it will be
170      * placed at the start of the array of supported flavors.
171      */

172     protected DataFlavor[] getRicherFlavors() {
173     return null;
174     }
175
176     protected Object JavaDoc getRicherData(DataFlavor flavor) throws UnsupportedFlavorException {
177     return null;
178     }
179
180     // --- html flavors ----------------------------------------------------------
181

182     /**
183      * Returns whether or not the specified data flavor is an HTML flavor that
184      * is supported.
185      * @param flavor the requested flavor for the data
186      * @return boolean indicating whether or not the data flavor is supported
187      */

188     protected boolean isHTMLFlavor(DataFlavor flavor) {
189     DataFlavor[] flavors = htmlFlavors;
190         for (int i = 0; i < flavors.length; i++) {
191         if (flavors[i].equals(flavor)) {
192             return true;
193         }
194     }
195     return false;
196     }
197
198     /**
199      * Should the HTML flavors be offered? If so, the method
200      * getHTMLData should be implemented to provide something reasonable.
201      */

202     protected boolean isHTMLSupported() {
203     return htmlData != null;
204     }
205
206     /**
207      * Fetch the data in a text/html format
208      */

209     protected String JavaDoc getHTMLData() {
210     return htmlData;
211     }
212
213     // --- plain text flavors ----------------------------------------------------
214

215     /**
216      * Returns whether or not the specified data flavor is an plain flavor that
217      * is supported.
218      * @param flavor the requested flavor for the data
219      * @return boolean indicating whether or not the data flavor is supported
220      */

221     protected boolean isPlainFlavor(DataFlavor flavor) {
222     DataFlavor[] flavors = plainFlavors;
223         for (int i = 0; i < flavors.length; i++) {
224         if (flavors[i].equals(flavor)) {
225             return true;
226         }
227     }
228     return false;
229     }
230
231     /**
232      * Should the plain text flavors be offered? If so, the method
233      * getPlainData should be implemented to provide something reasonable.
234      */

235     protected boolean isPlainSupported() {
236     return plainData != null;
237     }
238
239     /**
240      * Fetch the data in a text/plain format.
241      */

242     protected String JavaDoc getPlainData() {
243     return plainData;
244     }
245
246     // --- string flavorss --------------------------------------------------------
247

248     /**
249      * Returns whether or not the specified data flavor is a String flavor that
250      * is supported.
251      * @param flavor the requested flavor for the data
252      * @return boolean indicating whether or not the data flavor is supported
253      */

254     protected boolean isStringFlavor(DataFlavor flavor) {
255     DataFlavor[] flavors = stringFlavors;
256         for (int i = 0; i < flavors.length; i++) {
257         if (flavors[i].equals(flavor)) {
258             return true;
259         }
260     }
261     return false;
262     }
263
264
265 }
266
Popular Tags