-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClip.java
More file actions
333 lines (237 loc) · 7.74 KB
/
Copy pathClip.java
File metadata and controls
333 lines (237 loc) · 7.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// Clip.java
// Andrew Davison, ad@fivedots.coe.psu.ac.th, July 2016
/* Office Clipboard support functions:
* use the system clipboard
* set/get text
-- uses my TextTransferable class
* get/set a BufferedImage
-- uses my ImageTransferable class
* store a file on the clipboard (as bytes)
-- uses my FileTransferable class
* flavor functions
* get functions that try to retrieve data of a
particular type from the clipboard
See JClip.java for clipboard functions using only Java,
not the Office API.
A good clipboard tool: ClCl
http://www.nakka.com/soft/clcl/index_eng.html
*/
import java.io.*;
import java.awt.image.*;
import javax.imageio.*;
import com.sun.star.uno.*;
import com.sun.star.frame.*;
import com.sun.star.text.*;
import com.sun.star.datatransfer.*;
import com.sun.star.datatransfer.clipboard.*;
import com.sun.star.lang.EventObject;
public class Clip
{
private static final int MAX_TRIES = 3;
private static XSystemClipboard cb = null;
// used to store clipboard ref, so only one is created
// by the methods here
public static XSystemClipboard getClip()
{
if (cb == null) {
cb = SystemClipboard.create(Lo.getContext());
/*
cb.addClipboardListener( new XClipboardListener()
{
public void disposing(EventObject e) { }
public void changedContents(ClipboardEvent e)
{ System.out.println(">>Clipboard has been updated"); }
});
*/
}
return cb;
} // end of getClip()
public static boolean addContents(XTransferable trf)
{
int i = 0;
while (i < MAX_TRIES) {
try {
getClip().setContents(trf, null);
return true;
}
catch (IllegalStateException e) {
System.out.println("Problem accessing clipboard...");
Lo.wait(50);
}
i++;
}
System.out.println("Unable to add contents");
return false;
} // end of addContents()
public static boolean setText(String str)
{ return addContents( new TextTransferable(str)); }
public static String getText()
{ return (String) getData("text/plain;charset=utf-16"); }
public static Object getData(String mimeStr)
/* return the data associated with the specified mime type
string, or return null */
{
XTransferable trf = getClip().getContents();
if (trf == null) {
System.out.println("No transferable found");
return null;
}
try {
DataFlavor df = findFlavor(trf, mimeStr);
if (df != null)
return trf.getTransferData(df);
else
System.out.println("Mime type \"" + mimeStr + "\" not found");
}
catch (com.sun.star.uno.Exception e) {
System.out.println("Could not read clipboard: " + e);
}
return null;
} // end of getData()
public static boolean setImage(BufferedImage im)
{ return addContents(new ImageTransferable(im)); }
public static BufferedImage getImage()
{
XTransferable trf = getClip().getContents();
if (trf == null) {
System.out.println("No transferable found");
return null;
}
DataFlavor df = findImageFlavor(trf);
if (df == null)
return null;
try {
return Images.bytes2im( (byte[])trf.getTransferData(df) );
}
catch (com.sun.star.uno.Exception e) {
System.out.println("Could not retrieve image from clipboard: " + e);
return null;
}
} // end of getImage()
public static BufferedImage readImage()
// included for backward compatibility with old code
{ return getImage(); }
public static boolean setFile(String fnm)
{ return addContents(new FileTransferable(fnm)); }
public static byte[] getFile(String fnm)
{
String mimeStr = Info.getMIMEType(fnm);
System.out.println("MIME Type: " + mimeStr);
return (byte[]) getData(mimeStr);
}
// ----------------- flavor-related functions ---------------------
public static DataFlavor findFlavor(XTransferable trf, String mimeStr)
/* search through the flavors associated with the transferable
looking for the specified mime type string
*/
{
DataFlavor[] dfs = trf.getTransferDataFlavors();
for (int i = 0; i < dfs.length; i++) {
if (dfs[i].MimeType.startsWith(mimeStr)) {
// System.out.println("Mime type is: \"" + dfs[i].MimeType + "\"");
return dfs[i];
}
}
System.out.println("Clip does not support mime type: " + mimeStr);
return null;
} // end of findFlavor()
public static DataFlavor findImageFlavor(XTransferable trf)
// find the first image-related flavor for this transferable, or null
{
DataFlavor[] dfs = trf.getTransferDataFlavors();
for (int i = 0; i < dfs.length; i++) {
if (Info.isImageMime(dfs[i].MimeType)) {
System.out.println("Found mime type: \"" + dfs[i].MimeType + "\"");
return dfs[i];
}
}
System.out.println("Clip does not support an image mime type");
return null;
} // end of findImageFlavor()
public static String getFirstMime(XTransferable trf)
/* return the MIME type string in the first flavor for
this transferable
*/
{
DataFlavor[] dfs = trf.getTransferDataFlavors();
if (dfs.length > 0) {
System.out.println("Using first Mime type: " + dfs[0].MimeType);
return dfs[0].MimeType;
}
else {
System.out.println("No Mime type found");
return null;
}
} // end of getFirstMime()
public static void listFlavors()
/* print the MIME type strings for all the flavors supported
by the current clip */
{
XTransferable trf = getClip().getContents();
if (trf == null)
System.out.println("No transferable found");
else
listFlavors(trf);
} // end of listFlavors()
public static void listFlavors(XTransferable trf)
/* print the MIME type strings for all the flavors supported
by the transferable */
{
DataFlavor[] dfs = trf.getTransferDataFlavors();
System.out.println("No of flavors: " + dfs.length);
for (int i = 0; i < dfs.length; i++)
System.out.println((i+1) + ". " + dfs[i].MimeType);
System.out.println();
} // end of listFlavors()
// ----------- get data from the clipboard -------------
public static String getHTML()
{
byte[] data = (byte[]) getData("text/html");
if (data == null)
return null;
else
return new String(data);
} // end of getHTML()
public static String getRTF()
// text-based text format used by MS
{
byte[] data = (byte[]) getData("text/richtext");
if (data == null)
return null;
else
return new String(data);
} // end of getRTF()
public static String getSylk()
// text-based spreadsheet format used by MS
{
byte[] data = (byte[]) getData(
"application/x-openoffice-sylk;windows_formatname=\"Sylk\"");
if (data == null)
return null;
else
return new String(data);
} // end of getSylk()
public static String getXMLDraw()
// a 'flat' XML version of the draw/slide copy
{
byte[] data = (byte[]) getData(
"application/x-openoffice-drawing;windows_formatname=\"Drawing Format\"");
if (data == null)
return null;
else
return new String(data);
} // end of getXMLDraw()
public static byte[] getEmbedSource()
// fragment in binary ODF format
{
return (byte[]) getData(
"application/x-openoffice-embed-source-xml;windows_formatname=\"Star Embed Source (XML)\"");
} // end of getEmbedSource()
public static byte[] getBitmap()
// bitmap image, suitable for use by BitmapTable service;
// see Images.getBitmap()
{
return (byte[]) getData(
"application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"");
} // end of getBitmap()
} // end of Clip class