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.SystemFlavorMap;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.jface.util.Policy;
20 import org.eclipse.swt.dnd.ByteArrayTransfer;
21 import org.eclipse.swt.dnd.TransferData;
23 public class LocalObjectTransfer extends ByteArrayTransfer {
25 // First attempt to create a UUID for the type name to make sure that
26 // different Eclipse applications use different "types" of
27 // <code>LocalSelectionTransfer</code>
28 public static final String TYPE_NAME = SystemFlavorMap.encodeJavaMIMEType(LocalObjectTransferable.TYPE_NAME);
30 // private static int getMapping() {
32 // for(Map.Entry<DataFlavor, String> entry : SystemFlavorMap.getDefaultFlavorMap().getNativesForFlavors(new DataFlavor[] { LocalObjectTransferable.FLAVOR }).entrySet()) {
33 // for(long l : DataTransferer.getInstance().getFormatsForFlavorAsArray(entry.getKey(), (FlavorTable)SystemFlavorMap.getDefaultFlavorMap())) {
37 // DataFlavor[] flavors = SystemFlavorMap.getDefaultFlavorMap().getNativesForFlavors(new DataFlavor[] { LocalObjectTransferable.FLAVOR }).keySet().toArray(new DataFlavor[0]);
38 // for(long l : DataTransferer.getInstance().getFormatsForFlavors(flavors, (FlavorTable)SystemFlavorMap.getDefaultFlavorMap()).keySet()) {
42 // throw new Error("No mapping.");
45 // In Linux (Ubuntu 14.04) using SystemFlavorMap to get the type id gives different result than what is received with drop data (TransferData.type)
46 private static final int TYPEID = registerType(TYPE_NAME);//getMapping();
48 private static final LocalObjectTransfer INSTANCE = new LocalObjectTransfer();
50 Object transferredObject;
53 * Only the singleton instance of this class may be used.
55 protected LocalObjectTransfer() {
60 * Returns the singleton.
62 * @return the singleton
64 public static LocalObjectTransfer getTransfer() {
69 * Tests whether native drop data matches this transfer type.
71 * @param result result of converting the native drop data to Java
72 * @return true if the native drop data does not match this transfer type.
75 private boolean isInvalidNativeType(Object result) {
76 return !(result instanceof byte[])
77 || !TYPE_NAME.equals(new String((byte[]) result));
81 * Returns the type id used to identify this transfer.
83 * @return the type id used to identify this transfer.
85 protected int[] getTypeIds() {
86 return new int[] { TYPEID };
90 * Returns the type name used to identify this transfer.
92 * @return the type name used to identify this transfer.
94 protected String[] getTypeNames() {
95 return new String[] { TYPE_NAME };
99 * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(Object,
101 * Only encode the transfer type name since the selection is read and
102 * written in the same process.
104 * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
106 public void javaToNative(Object object, TransferData transferData) {
107 //System.out.println("javaToNative " + object);
108 transferredObject = object;
109 byte[] check = TYPE_NAME.getBytes();
110 super.javaToNative(check, transferData);
114 * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData).
115 * Test if the native drop data matches this transfer type.
117 * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData)
119 public Object nativeToJava(TransferData transferData) {
120 //System.out.println("nativeToJava " + transferredObject);
121 Object result = super.nativeToJava(transferData);
122 if (isInvalidNativeType(result)) {
123 Policy.getLog().log(new Status(
127 JFaceResources.getString("LocalObjectTransfer.errorMessage"), null)); //$NON-NLS-1$
129 return transferredObject;
132 public void clear() {
133 //System.out.println("LocalObjectTransfer clear");
134 transferredObject = null;
137 public Object getObject() {
138 //System.out.println("LocalObjectTransfer get " + transferredObject);
139 return transferredObject;
142 public void setObject(Object object) {
143 //System.out.println("LocalObjectTransfer set " + object);
144 transferredObject = object;