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.io.IOException;
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;
25 * @author Tuukka Lehtonen
27 public class SubgraphTransfer extends ByteArrayTransfer {
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 };
34 public static final SubgraphTransfer INSTANCE = createInstance();
38 public static SubgraphTransfer getInstance() {
42 public static SubgraphTransfer createInstance() {
43 SubgraphTransfer result = new SubgraphTransfer();
47 public void javaToNative(Object object, TransferData transferData) {
48 if (!(object instanceof TransferableGraph1) || !isSupportedType(transferData))
49 DND.error(DND.ERROR_INVALID_DATA);
51 Serializer s = Bindings.getSerializerUnchecked(TransferableGraph1.class);
53 byte[] data = s.serialize(object);
54 super.javaToNative(data, transferData);
55 } catch (IOException e) {
56 ErrorLogger.defaultLogError(e);
60 public Object nativeToJava(TransferData transferData) {
61 if (!isSupportedType(transferData)) return null;
63 byte[] data = (byte[]) super.nativeToJava(transferData);
65 Serializer s = Bindings.getSerializerUnchecked(TransferableGraph1.class);
66 return s.deserialize(data);
67 } catch (IOException e) {
72 protected String[] getTypeNames() {
76 protected int[] getTypeIds() {
80 protected boolean validate(Object object) {
81 return object instanceof TransferableGraph1;