]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dnd/LocalObjectTransfer.java
Use searchByTypeShallow in ModelingUtils.createMissingGUIDs
[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.DataFlavor;
15 import java.awt.datatransfer.FlavorTable;
16 import java.awt.datatransfer.SystemFlavorMap;
17 import java.util.Map;
18
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;
25
26 import sun.awt.datatransfer.DataTransferer;
27
28 public class LocalObjectTransfer extends ByteArrayTransfer {
29
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);
34         
35         private static int getMapping() {
36
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())) {
39                                 return (int)l;
40                         }
41                 }
42                 
43                 throw new Error("No mapping.");
44                 
45         }
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();
48     
49         private static final LocalObjectTransfer INSTANCE = new LocalObjectTransfer();
50
51     Object transferredObject;
52
53     /**
54      * Only the singleton instance of this class may be used. 
55      */
56     protected LocalObjectTransfer() {
57         // do nothing
58     }
59
60     /**
61      * Returns the singleton.
62      * 
63      * @return the singleton
64      */
65     public static LocalObjectTransfer getTransfer() {
66         return INSTANCE;
67     }
68
69     /**
70      * Tests whether native drop data matches this transfer type.
71      * 
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.
74      *  false otherwise.
75      */
76     private boolean isInvalidNativeType(Object result) {
77         return !(result instanceof byte[])
78                 || !TYPE_NAME.equals(new String((byte[]) result));
79     }
80
81     /**
82      * Returns the type id used to identify this transfer.
83      * 
84      * @return the type id used to identify this transfer.
85      */
86     protected int[] getTypeIds() {
87         return new int[] { TYPEID };
88     }
89
90     /**
91      * Returns the type name used to identify this transfer.
92      * 
93      * @return the type name used to identify this transfer.
94      */
95     protected String[] getTypeNames() {
96         return new String[] { TYPE_NAME };
97     }
98
99     /**
100      * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(Object,
101      * TransferData).
102      * Only encode the transfer type name since the selection is read and
103      * written in the same process.
104      * 
105      * @see org.eclipse.swt.dnd.ByteArrayTransfer#javaToNative(java.lang.Object, org.eclipse.swt.dnd.TransferData)
106      */
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);
112     }
113
114     /**
115      * Overrides org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData).
116      * Test if the native drop data matches this transfer type.
117      * 
118      * @see org.eclipse.swt.dnd.ByteArrayTransfer#nativeToJava(TransferData)
119      */
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(
125                             IStatus.ERROR,
126                             Policy.JFACE,
127                             IStatus.ERROR,
128                             JFaceResources.getString("LocalObjectTransfer.errorMessage"), null)); //$NON-NLS-1$
129         }
130         return transferredObject;
131     }
132     
133     public void clear() {
134         //System.out.println("LocalObjectTransfer clear");
135         transferredObject = null;
136     }
137     
138     public Object getObject() {
139         //System.out.println("LocalObjectTransfer get " + transferredObject);
140         return transferredObject;
141     }
142     
143     public void setObject(Object object) {
144         //System.out.println("LocalObjectTransfer set " + object);
145         transferredObject = object;
146     }
147     
148 }