]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dnd/SubgraphTransfer.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / SubgraphTransfer.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.io.IOException;
15
16 import org.eclipse.swt.dnd.ByteArrayTransfer;
17 import org.eclipse.swt.dnd.DND;
18 import org.eclipse.swt.dnd.TransferData;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.serialization.Serializer;
21 import org.simantics.graph.representation.TransferableGraph1;
22 import org.simantics.utils.ui.ErrorLogger;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class SubgraphTransfer extends ByteArrayTransfer {
28
29     private static final String          TYPENAME  = "graph";
30     private static final int             TYPEID    = registerType(TYPENAME);
31     private static final int[]           TYPEIDS   = new int[] { TYPEID };
32     private static final String[]        TYPENAMES = new String[] { TYPENAME };
33
34     public static final SubgraphTransfer INSTANCE = createInstance();
35
36     SubgraphTransfer() {}
37
38     public static SubgraphTransfer getInstance() {
39         return INSTANCE;
40     }
41
42     public static SubgraphTransfer createInstance() {
43         SubgraphTransfer result = new SubgraphTransfer();
44         return result;
45     }
46
47     public void javaToNative(Object object, TransferData transferData) {
48         if (!(object instanceof TransferableGraph1) || !isSupportedType(transferData)) 
49             DND.error(DND.ERROR_INVALID_DATA);
50
51         Serializer s = Bindings.getSerializerUnchecked(TransferableGraph1.class);
52         try {
53             byte[] data = s.serialize(object);
54             super.javaToNative(data, transferData);
55         } catch (IOException e) {
56             ErrorLogger.defaultLogError(e);
57         }
58     }
59
60     public Object nativeToJava(TransferData transferData) {
61         if (!isSupportedType(transferData)) return null;
62
63         byte[] data = (byte[]) super.nativeToJava(transferData);
64         try {
65             Serializer s = Bindings.getSerializerUnchecked(TransferableGraph1.class);
66             return s.deserialize(data);
67         } catch (IOException e) {
68             return null;
69         }
70     }
71
72     protected String[] getTypeNames() {
73         return TYPENAMES;
74     }
75
76     protected int[] getTypeIds() {
77         return TYPEIDS;
78     }
79
80     protected boolean validate(Object object) {
81         return object instanceof TransferableGraph1;
82     }
83
84 }