1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.ui.dnd;
14 import java.awt.datatransfer.DataFlavor;
15 import java.awt.datatransfer.FlavorTable;
16 import java.awt.datatransfer.SystemFlavorMap;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.resource.JFaceResources;
22 import org.eclipse.jface.util.Policy;
23 import org.eclipse.swt.dnd.ByteArrayTransfer;
24 import org.eclipse.swt.dnd.TransferData;
26 import sun.awt.datatransfer.DataTransferer;
28 public class LocalObjectTransfer extends ByteArrayTransfer {
30 // First attempt to create a UUID for the type name to make sure that
31 // different Eclipse applications use different "types" of
32 // <code>LocalSelectionTransfer</code>
33 public static final String TYPE_NAME = SystemFlavorMap.encodeJavaMIMEType(LocalObjectTransferable.TYPE_NAME);
35 private static int getMapping() {
37 for(Map.Entry<DataFlavor, String> entry : SystemFlavorMap.getDefaultFlavorMap().getNativesForFlavors(new DataFlavor[] { LocalObjectTransferable.FLAVOR }).entrySet()) {
38 for(long l : DataTransferer.getInstance().getFormatsForFlavorAsArray(entry.getKey(), (FlavorTable)SystemFlavorMap.getDefaultFlavorMap())) {
43 throw new Error("No mapping.");
46 // In Linux (Ubuntu 14.04) using SystemFlavorMap to get the type id gives different result than what is received with drop data (TransferData.type)
47 private static final int TYPEID = registerType(TYPE_NAME);//getMapping();
49 private static final LocalObjectTransfer INSTANCE = new LocalObjectTransfer();
51 Object transferredObject;
54 * Only the singleton instance of this class may be used.
56 protected LocalObjectTransfer() {
61 * Returns the singleton.
63 * @return the singleton
65 public static LocalObjectTransfer getTransfer() {
70 * Tests whether native drop data matches this transfer type.
72 * @param result result of converting the native drop data to Java
73 * @return true if the native drop data does not match this transfer type.
76 private boolean isInvalidNativeType(Object result) {
77 return !(result instanceof byte[])
78 || !TYPE_NAME.equals(new String((byte[]) result));
82 * Returns the type id used to identify this transfer.
84 * @return the type id used to identify this transfer.
86 protected int[] getTypeIds() {
87 return new int[] { TYPEID };
91 * Returns the type name used to identify this transfer.
93 * @return the type name used to identify this transfer.
95 protected String[] getTypeNames() {
96 return new String[] { TYPE_NAME };
100 * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(Object,
102 * Only encode the transfer type name since the selection is read and
103 * written in the same process.
105 * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
107 public void javaToNative(Object object, TransferData transferData) {
108 //System.out.println("javaToNative " + object);
109 transferredObject = object;
110 byte[] check = TYPE_NAME.getBytes();
111 super.javaToNative(check, transferData);
115 * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData).
116 * Test if the native drop data matches this transfer type.
118 * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData)
120 public Object nativeToJava(TransferData transferData) {
121 //System.out.println("nativeToJava " + transferredObject);
122 Object result = super.nativeToJava(transferData);
123 if (isInvalidNativeType(result)) {
124 Policy.getLog().log(new Status(
128 JFaceResources.getString("LocalObjectTransfer.errorMessage"), null)); //$NON-NLS-1$
130 return transferredObject;
133 public void clear() {
134 //System.out.println("LocalObjectTransfer clear");
135 transferredObject = null;
138 public Object getObject() {
139 //System.out.println("LocalObjectTransfer get " + transferredObject);
140 return transferredObject;
143 public void setObject(Object object) {
144 //System.out.println("LocalObjectTransfer set " + object);
145 transferredObject = object;