KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > datatransfer > StringSelection


1 /*
2  * @(#)StringSelection.java 1.21 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
8 package java.awt.datatransfer;
9
10 import java.io.*;
11
12 /**
13  * A <code>Transferable</code> which implements the capability required
14  * to transfer a <code>String</code>.
15  *
16  * This <code>Transferable</code> properly supports
17  * <code>DataFlavor.stringFlavor</code>
18  * and all equivalent flavors. Support for
19  * <code>DataFlavor.plainTextFlavor</code>
20  * and all equivalent flavors is <b>deprecated</b>. No other
21  * <code>DataFlavor</code>s are supported.
22  *
23  * @see java.awt.datatransfer.DataFlavor#stringFlavor
24  * @see java.awt.datatransfer.DataFlavor#plainTextFlavor
25  */

26 public class StringSelection implements Transferable JavaDoc, ClipboardOwner JavaDoc {
27
28     private static final int STRING = 0;
29     private static final int PLAIN_TEXT = 1;
30
31     private static final DataFlavor JavaDoc[] flavors = {
32         DataFlavor.stringFlavor,
33     DataFlavor.plainTextFlavor // deprecated
34
};
35
36     private String JavaDoc data;
37                            
38     /**
39      * Creates a <code>Transferable</code> capable of transferring
40      * the specified <code>String</code>.
41      */

42     public StringSelection(String JavaDoc data) {
43         this.data = data;
44     }
45
46     /**
47      * Returns an array of flavors in which this <code>Transferable</code>
48      * can provide the data. <code>DataFlavor.stringFlavor</code>
49      * is properly supported.
50      * Support for <code>DataFlavor.plainTextFlavor</code> is
51      * <b>deprecated</b>.
52      *
53      * @return an array of length two, whose elements are <code>DataFlavor.
54      * stringFlavor</code> and <code>DataFlavor.plainTextFlavor</code>
55      */

56     public DataFlavor JavaDoc[] getTransferDataFlavors() {
57         // returning flavors itself would allow client code to modify
58
// our internal behavior
59
return (DataFlavor JavaDoc[])flavors.clone();
60     }
61
62     /**
63      * Returns whether the requested flavor is supported by this
64      * <code>Transferable</code>.
65      *
66      * @param flavor the requested flavor for the data
67      * @return true if <code>flavor</code> is equal to
68      * <code>DataFlavor.stringFlavor</code> or
69      * <code>DataFlavor.plainTextFlavor</code>; false if <code>flavor</code>
70      * is not one of the above flavors
71      * @throws NullPointerException if flavor is <code>null</code>
72      */

73     public boolean isDataFlavorSupported(DataFlavor JavaDoc flavor) {
74     // JCK Test StringSelection0003: if 'flavor' is null, throw NPE
75
for (int i = 0; i < flavors.length; i++) {
76         if (flavor.equals(flavors[i])) {
77             return true;
78         }
79     }
80     return false;
81     }
82
83     /**
84      * Returns the <code>Transferable</code>'s data in the requested
85      * <code>DataFlavor</code> if possible. If the desired flavor is
86      * <code>DataFlavor.stringFlavor</code>, or an equivalent flavor,
87      * the <code>String</code> representing the selection is
88      * returned. If the desired flavor is
89      * <code>DataFlavor.plainTextFlavor</code>,
90      * or an equivalent flavor, a <code>Reader</code> is returned.
91      * <b>Note:</b> The behavior of this method for
92      * </code>DataFlavor.plainTextFlavor</code>
93      * and equivalent <code>DataFlavor</code>s is inconsistent with the
94      * definition of <code>DataFlavor.plainTextFlavor</code>.
95      *
96      * @param flavor the requested flavor for the data
97      * @return the data in the requested flavor, as outlined above
98      * @throws UnsupportedFlavorException if the requested data flavor is
99      * not equivalent to either <code>DataFlavor.stringFlavor</code>
100      * or <code>DataFlavor.plainTextFlavor</code>
101      * @throws IOException if an IOException occurs while retrieving the data.
102      * By default, StringSelection never throws this exception, but a
103      * subclass may.
104      * @throws NullPointerException if flavor is <code>null</code>
105      * @see java.io.Reader
106      */

107     public Object JavaDoc getTransferData(DataFlavor JavaDoc flavor)
108         throws UnsupportedFlavorException JavaDoc, IOException
109     {
110     // JCK Test StringSelection0007: if 'flavor' is null, throw NPE
111
if (flavor.equals(flavors[STRING])) {
112         return (Object JavaDoc)data;
113     } else if (flavor.equals(flavors[PLAIN_TEXT])) {
114         return new StringReader(data == null ? "" : data);
115     } else {
116         throw new UnsupportedFlavorException JavaDoc(flavor);
117     }
118     }
119
120     public void lostOwnership(Clipboard JavaDoc clipboard, Transferable JavaDoc contents) {
121     }
122 }
123
Popular Tags