]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textpanel/StyledTextClipboard.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textpanel / StyledTextClipboard.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.textpanel;\r
14 \r
15 import java.awt.datatransfer.Clipboard;\r
16 import java.awt.datatransfer.ClipboardOwner;\r
17 import java.awt.datatransfer.Transferable;\r
18 import java.awt.datatransfer.DataFlavor;\r
19 import java.awt.datatransfer.UnsupportedFlavorException;\r
20 import java.io.IOException;\r
21 import java.io.InputStream;\r
22 \r
23 import java.awt.Toolkit;\r
24 \r
25 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
26 \r
27 import com.ibm.richtext.styledtext.MConstText;\r
28 import com.ibm.richtext.styledtext.StyledText;\r
29 \r
30 /**\r
31 * Wrapper for java.awt.datatransfer.Clipboard\r
32 * Packages an MConstText in a transferable, and puts it on the clipboard.\r
33 */\r
34 \r
35 class StyledTextClipboard implements ClipboardOwner {\r
36 \r
37     static final String COPYRIGHT =\r
38                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
39     // This class has a workaround for a bug in the Windows system clipboard.\r
40     // The system clipboard will only return String content, even\r
41     // though it has a reference to the contents.  So if our\r
42     // clipboard is the system clipboard, we'll keep a reference\r
43     // to the content and use that instead of what the Clipboard returns.\r
44 \r
45     private static Clipboard SYSTEM = null;\r
46     static {\r
47         try {\r
48             SYSTEM = Toolkit.getDefaultToolkit().getSystemClipboard();\r
49         }\r
50         catch(Throwable th) {\r
51         }\r
52     }\r
53 \r
54     private static StyledTextClipboard fgSystemClipboard = null;\r
55 \r
56     public static StyledTextClipboard getClipboardFor(Clipboard clipboard) {\r
57 \r
58         if (clipboard == SYSTEM && SYSTEM != null) {\r
59             synchronized(SYSTEM) {\r
60                 if (fgSystemClipboard == null) {\r
61                     fgSystemClipboard = new StyledTextClipboard(SYSTEM, true);\r
62                 }\r
63             }\r
64             return fgSystemClipboard;\r
65         }\r
66         else {\r
67             return new StyledTextClipboard(clipboard, false);\r
68         }\r
69     }\r
70 \r
71     private Clipboard fClipboard;\r
72     private boolean fUseLocalContents;\r
73     private Transferable fContents = null;\r
74 \r
75     private StyledTextClipboard(Clipboard clipboard, boolean useLocalContents) {\r
76 \r
77         if (clipboard == null) {\r
78             fClipboard = new Clipboard("TextPanel clipboard");\r
79         }\r
80         else {\r
81             fClipboard = clipboard;\r
82         }\r
83 \r
84         fUseLocalContents = useLocalContents;\r
85     }\r
86 \r
87     public void lostOwnership(Clipboard clipboard,\r
88                               Transferable contents) {\r
89         if (contents == fContents) {\r
90             this.fContents = null;\r
91         }\r
92     }\r
93 \r
94     public void setContents(MConstText newContents) {\r
95 \r
96         TransferableText contents = new TransferableText(newContents);\r
97         if (fClipboard == SYSTEM) {\r
98             fContents = contents;\r
99         }\r
100         fClipboard.setContents(contents, this);\r
101     }\r
102 \r
103     private Transferable getClipboardContents() {\r
104 \r
105         if (fUseLocalContents && fContents != null) {\r
106             return fContents;\r
107         }\r
108 \r
109         return fClipboard.getContents(this);\r
110     }\r
111 \r
112     /**\r
113      * Has contents - faster than getContents for finding out whether the\r
114      * clipboard has text.\r
115      */\r
116     public boolean hasContents() {\r
117 \r
118         Transferable contents = getClipboardContents();\r
119 \r
120         if (contents == null) {\r
121             return false;\r
122         }\r
123 \r
124         return contents.isDataFlavorSupported(MConstText.styledTextFlavor) ||\r
125                contents.isDataFlavorSupported(DataFlavor.stringFlavor) ||\r
126                contents.isDataFlavorSupported(DataFlavor.plainTextFlavor);\r
127     }\r
128 \r
129     private String getString(InputStream inStream) throws IOException {\r
130 \r
131         String value = new String();\r
132         int bytesRead;\r
133 \r
134         do {\r
135             byte inBytes[] = new byte[inStream.available()];\r
136             bytesRead = inStream.read(inBytes);\r
137 \r
138             if (bytesRead != -1)\r
139                 value = value + new String(inBytes);\r
140 \r
141         } while (bytesRead != -1);\r
142 \r
143         return value;\r
144     }\r
145 \r
146     /**\r
147      * If the Clipboard has text content, return it as an\r
148      * MConstText.  Otherwise return null.\r
149      * @param defaultStyle the style to apply to unstyled\r
150      *      text (such as a String).  If the clipboard\r
151      *      has styled text this parameter is not used.\r
152      */\r
153     public MConstText getContents(AttributeMap defaultStyle) {\r
154 \r
155         Transferable contents = getClipboardContents();\r
156 \r
157         if (contents == null) {\r
158             return null;\r
159         }\r
160 \r
161         DataFlavor flavors[] = contents.getTransferDataFlavors();\r
162 \r
163         // search flavors for our flavor, String flavor and raw text flavor\r
164 \r
165         Exception ex = null;\r
166 \r
167         try {\r
168            int i;\r
169 \r
170             for (i=0; i < flavors.length; i++) {\r
171                 if (flavors[i].equals(MConstText.styledTextFlavor))\r
172                     break;\r
173             }\r
174 \r
175             if (i < flavors.length) {\r
176 \r
177                 Object data = contents.getTransferData(MConstText.styledTextFlavor);\r
178                 if (data == null)\r
179                     System.out.println("Data is null.");\r
180                 return (MConstText) data;\r
181             }\r
182 \r
183             for (i=0; i < flavors.length; i++) {\r
184                 if (flavors[i].equals(DataFlavor.stringFlavor))\r
185                     break;\r
186             }\r
187 \r
188             if (i < flavors.length) {\r
189 \r
190                 Object data = contents.getTransferData(DataFlavor.stringFlavor);\r
191                 return new StyledText((String) data, defaultStyle);\r
192             }\r
193 \r
194             for (i=0; i < flavors.length; i++) {\r
195                 if (flavors[i].equals(DataFlavor.plainTextFlavor))\r
196                     break;\r
197             }\r
198 \r
199             if (i < flavors.length) {\r
200 \r
201                 Object data = contents.getTransferData(DataFlavor.plainTextFlavor);\r
202 \r
203                 String textString = getString((InputStream) data);\r
204                 return new StyledText(textString, defaultStyle);\r
205             }\r
206         }\r
207         catch(UnsupportedFlavorException e) {\r
208             ex = e;\r
209         }\r
210         catch(IOException e) {\r
211             ex = e;\r
212         }\r
213 \r
214         System.out.println("Exception when retrieving data.  Exception:" + ex);\r
215         return null;\r
216     }\r
217 }\r