195 }
196 }
197
198 protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList)
199 throws IOException
200 {
201 ByteArrayOutputStream bos = new ByteArrayOutputStream();
202 for (int i = 0; i < fileList.size(); i++)
203 {
204 byte[] bytes = fileList.get(i).getBytes();
205 if (i != 0) bos.write(0);
206 bos.write(bytes, 0, bytes.length);
207 }
208 return bos;
209 }
210
211 /**
212 * Translates either a byte array or an input stream which contain
213 * platform-specific image data in the given format into an Image.
214 */
215 protected Image platformImageBytesOrStreamToImage(InputStream inputStream,
216 byte[] bytes,
217 long format)
218 throws IOException {
219 String mimeType = null;
220 if (format == PNG_ATOM.getAtom()) {
221 mimeType = "image/png";
222 } else if (format == JFIF_ATOM.getAtom()) {
223 mimeType = "image/jpeg";
224 } else {
225 // Check if an image MIME format.
226 try {
227 String nat = getNativeForFormat(format);
228 DataFlavor df = new DataFlavor(nat);
229 String primaryType = df.getPrimaryType();
230 if ("image".equals(primaryType)) {
231 mimeType = df.getPrimaryType() + "/" + df.getSubType();
232 }
233 } catch (Exception e) {
234 // Not an image MIME format.
235 }
236 }
237 if (mimeType != null) {
238 return standardImageBytesOrStreamToImage(inputStream, bytes, mimeType);
239 } else {
240 String nativeFormat = getNativeForFormat(format);
241 throw new IOException("Translation from " + nativeFormat +
242 " is not supported.");
243 }
244 }
245
246 protected String[] dragQueryFile(byte[] bytes) {
247 XToolkit.awtLock();
248 try {
249 return XlibWrapper.XTextPropertyToStringList(bytes,
250 XAtom.get("STRING").getAtom());
251 } finally {
252 XToolkit.awtUnlock();
253 }
254 }
255
256 protected URI[] dragQueryURIs(InputStream stream,
257 byte[] bytes,
258 long format,
313 try {
314 DataFlavor df = new DataFlavor(nat);
315 if (primaryType.equals(df.getPrimaryType())) {
316 return true;
317 }
318 } catch (Exception e) {
319 // Not a MIME format.
320 }
321
322 return false;
323 }
324
325 /*
326 * The XDnD protocol prescribes that the Atoms used as targets for data
327 * transfer should have string names that represent the corresponding MIME
328 * types.
329 * To meet this requirement we check if the passed native format constitutes
330 * a valid MIME and return a list of flavors to which the data in this MIME
331 * type can be translated by the Data Transfer subsystem.
332 */
333 public List getPlatformMappingsForNative(String nat) {
334 List flavors = new ArrayList();
335
336 if (nat == null) {
337 return flavors;
338 }
339
340 DataFlavor df = null;
341
342 try {
343 df = new DataFlavor(nat);
344 } catch (Exception e) {
345 // The string doesn't constitute a valid MIME type.
346 return flavors;
347 }
348
349 Object value = df;
350 final String primaryType = df.getPrimaryType();
351 final String baseType = primaryType + "/" + df.getSubType();
352
353 // For text formats we map natives to MIME strings instead of data
354 // flavors to enable dynamic text native-to-flavor mapping generation.
355 // See SystemFlavorMap.getFlavorsForNative() for details.
356 if ("text".equals(primaryType)) {
357 value = primaryType + "/" + df.getSubType();
358 } else if ("image".equals(primaryType)) {
359 Iterator readers = ImageIO.getImageReadersByMIMEType(baseType);
360 if (readers.hasNext()) {
361 flavors.add(DataFlavor.imageFlavor);
362 }
363 }
364
365 flavors.add(value);
366
367 return flavors;
368 }
369
370 private static ImageTypeSpecifier defaultSpecifier = null;
371
372 private ImageTypeSpecifier getDefaultImageTypeSpecifier() {
373 if (defaultSpecifier == null) {
374 ColorModel model = ColorModel.getRGBdefault();
375 WritableRaster raster =
376 model.createCompatibleWritableRaster(10, 10);
377
378 BufferedImage bufferedImage =
421 String[] mimeTypes = ImageIO.getWriterMIMETypes();
422 if (mimeTypes != null) {
423 for (int i = 0; i < mimeTypes.length; i++) {
424 Iterator writers =
425 ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
426
427 while (writers.hasNext()) {
428 ImageWriter imageWriter = (ImageWriter)writers.next();
429 ImageWriterSpi writerSpi =
430 imageWriter.getOriginatingProvider();
431
432 if (writerSpi != null &&
433 writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
434 natives.add(mimeTypes[i]);
435 break;
436 }
437 }
438 }
439 }
440 } else if (DataTransferer.isFlavorCharsetTextType(df)) {
441 final Iterator iter = DataTransferer.standardEncodings();
442
443 // stringFlavor is semantically equivalent to the standard
444 // "text/plain" MIME type.
445 if (DataFlavor.stringFlavor.equals(df)) {
446 baseType = "text/plain";
447 }
448
449 while (iter.hasNext()) {
450 String encoding = (String)iter.next();
451 if (!encoding.equals(charset)) {
452 natives.add(baseType + ";charset=" + encoding);
453 }
454 }
455
456 // Add a MIME format without specified charset.
457 if (!natives.contains(baseType)) {
458 natives.add(baseType);
459 }
460 }
461
462 return natives;
463 }
464 }
|
195 }
196 }
197
198 protected ByteArrayOutputStream convertFileListToBytes(ArrayList<String> fileList)
199 throws IOException
200 {
201 ByteArrayOutputStream bos = new ByteArrayOutputStream();
202 for (int i = 0; i < fileList.size(); i++)
203 {
204 byte[] bytes = fileList.get(i).getBytes();
205 if (i != 0) bos.write(0);
206 bos.write(bytes, 0, bytes.length);
207 }
208 return bos;
209 }
210
211 /**
212 * Translates either a byte array or an input stream which contain
213 * platform-specific image data in the given format into an Image.
214 */
215 protected Image platformImageBytesToImage(
216 byte[] bytes, long format) throws IOException
217 {
218 String mimeType = null;
219 if (format == PNG_ATOM.getAtom()) {
220 mimeType = "image/png";
221 } else if (format == JFIF_ATOM.getAtom()) {
222 mimeType = "image/jpeg";
223 } else {
224 // Check if an image MIME format.
225 try {
226 String nat = getNativeForFormat(format);
227 DataFlavor df = new DataFlavor(nat);
228 String primaryType = df.getPrimaryType();
229 if ("image".equals(primaryType)) {
230 mimeType = df.getPrimaryType() + "/" + df.getSubType();
231 }
232 } catch (Exception e) {
233 // Not an image MIME format.
234 }
235 }
236 if (mimeType != null) {
237 return standardImageBytesToImage(bytes, mimeType);
238 } else {
239 String nativeFormat = getNativeForFormat(format);
240 throw new IOException("Translation from " + nativeFormat +
241 " is not supported.");
242 }
243 }
244
245 protected String[] dragQueryFile(byte[] bytes) {
246 XToolkit.awtLock();
247 try {
248 return XlibWrapper.XTextPropertyToStringList(bytes,
249 XAtom.get("STRING").getAtom());
250 } finally {
251 XToolkit.awtUnlock();
252 }
253 }
254
255 protected URI[] dragQueryURIs(InputStream stream,
256 byte[] bytes,
257 long format,
312 try {
313 DataFlavor df = new DataFlavor(nat);
314 if (primaryType.equals(df.getPrimaryType())) {
315 return true;
316 }
317 } catch (Exception e) {
318 // Not a MIME format.
319 }
320
321 return false;
322 }
323
324 /*
325 * The XDnD protocol prescribes that the Atoms used as targets for data
326 * transfer should have string names that represent the corresponding MIME
327 * types.
328 * To meet this requirement we check if the passed native format constitutes
329 * a valid MIME and return a list of flavors to which the data in this MIME
330 * type can be translated by the Data Transfer subsystem.
331 */
332 public List <DataFlavor> getPlatformMappingsForNative(String nat) {
333 List <DataFlavor> flavors = new ArrayList();
334
335 if (nat == null) {
336 return flavors;
337 }
338
339 DataFlavor df = null;
340
341 try {
342 df = new DataFlavor(nat);
343 } catch (Exception e) {
344 // The string doesn't constitute a valid MIME type.
345 return flavors;
346 }
347
348 DataFlavor value = df;
349 final String primaryType = df.getPrimaryType();
350 final String baseType = primaryType + "/" + df.getSubType();
351
352 // For text formats we map natives to MIME strings instead of data
353 // flavors to enable dynamic text native-to-flavor mapping generation.
354 // See SystemFlavorMap.getFlavorsForNative() for details.
355 if ("image".equals(primaryType)) {
356 Iterator readers = ImageIO.getImageReadersByMIMEType(baseType);
357 if (readers.hasNext()) {
358 flavors.add(DataFlavor.imageFlavor);
359 }
360 }
361
362 flavors.add(value);
363
364 return flavors;
365 }
366
367 private static ImageTypeSpecifier defaultSpecifier = null;
368
369 private ImageTypeSpecifier getDefaultImageTypeSpecifier() {
370 if (defaultSpecifier == null) {
371 ColorModel model = ColorModel.getRGBdefault();
372 WritableRaster raster =
373 model.createCompatibleWritableRaster(10, 10);
374
375 BufferedImage bufferedImage =
418 String[] mimeTypes = ImageIO.getWriterMIMETypes();
419 if (mimeTypes != null) {
420 for (int i = 0; i < mimeTypes.length; i++) {
421 Iterator writers =
422 ImageIO.getImageWritersByMIMEType(mimeTypes[i]);
423
424 while (writers.hasNext()) {
425 ImageWriter imageWriter = (ImageWriter)writers.next();
426 ImageWriterSpi writerSpi =
427 imageWriter.getOriginatingProvider();
428
429 if (writerSpi != null &&
430 writerSpi.canEncodeImage(getDefaultImageTypeSpecifier())) {
431 natives.add(mimeTypes[i]);
432 break;
433 }
434 }
435 }
436 }
437 } else if (DataTransferer.isFlavorCharsetTextType(df)) {
438 // stringFlavor is semantically equivalent to the standard
439 // "text/plain" MIME type.
440 if (DataFlavor.stringFlavor.equals(df)) {
441 baseType = "text/plain";
442 }
443
444 for (String encoding : DataTransferer.standardEncodings()) {
445 if (!encoding.equals(charset)) {
446 natives.add(baseType + ";charset=" + encoding);
447 }
448 }
449
450 // Add a MIME format without specified charset.
451 if (!natives.contains(baseType)) {
452 natives.add(baseType);
453 }
454 }
455
456 return natives;
457 }
458 }
|