]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dnd/LocalObjectTransfer.java
Fixed compilation problems and SWTAWTComponent focus problem with Java9+
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / LocalObjectTransfer.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.ui.dnd;
13
14 import java.awt.datatransfer.SystemFlavorMap;
15
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;
22
23 public class LocalObjectTransfer extends ByteArrayTransfer {
24
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);
29         
30 //      private static int getMapping() {
31 //
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())) {
34 //                              return (int)l;
35 //                      }
36 //              }
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()) {
39 //                      return (int)l;
40 //              }
41 //              
42 //              throw new Error("No mapping.");
43 //              
44 //      }
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();
47     
48         private static final LocalObjectTransfer INSTANCE = new LocalObjectTransfer();
49
50     Object transferredObject;
51
52     /**
53      * Only the singleton instance of this class may be used. 
54      */
55     protected LocalObjectTransfer() {
56         // do nothing
57     }
58
59     /**
60      * Returns the singleton.
61      * 
62      * @return the singleton
63      */
64     public static LocalObjectTransfer getTransfer() {
65         return INSTANCE;
66     }
67
68     /**
69      * Tests whether native drop data matches this transfer type.
70      * 
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.
73      *  false otherwise.
74      */
75     private boolean isInvalidNativeType(Object result) {
76         return !(result instanceof byte[])
77                 || !TYPE_NAME.equals(new String((byte[]) result));
78     }
79
80     /**
81      * Returns the type id used to identify this transfer.
82      * 
83      * @return the type id used to identify this transfer.
84      */
85     protected int[] getTypeIds() {
86         return new int[] { TYPEID };
87     }
88
89     /**
90      * Returns the type name used to identify this transfer.
91      * 
92      * @return the type name used to identify this transfer.
93      */
94     protected String[] getTypeNames() {
95         return new String[] { TYPE_NAME };
96     }
97
98     /**
99      * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(Object,
100      * TransferData).
101      * Only encode the transfer type name since the selection is read and
102      * written in the same process.
103      * 
104      * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
105      */
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);
111     }
112
113     /**
114      * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData).
115      * Test if the native drop data matches this transfer type.
116      * 
117      * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData)
118      */
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(
124                             IStatus.ERROR,
125                             Policy.JFACE,
126                             IStatus.ERROR,
127                             JFaceResources.getString("LocalObjectTransfer.errorMessage"), null)); //$NON-NLS-1$
128         }
129         return transferredObject;
130     }
131     
132     public void clear() {
133         //System.out.println("LocalObjectTransfer clear");
134         transferredObject = null;
135     }
136     
137     public Object getObject() {
138         //System.out.println("LocalObjectTransfer get " + transferredObject);
139         return transferredObject;
140     }
141     
142     public void setObject(Object object) {
143         //System.out.println("LocalObjectTransfer set " + object);
144         transferredObject = object;
145     }
146     
147 }