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 org.eclipse.swt.dnd.ByteArrayTransfer;
15 import org.eclipse.swt.dnd.DND;
16 import org.eclipse.swt.dnd.TextTransfer;
17 import org.eclipse.swt.dnd.TransferData;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Session;
20 import org.simantics.db.common.ResourceArray;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.service.SerialisationSupport;
23 import org.simantics.ui.SimanticsUI;
26 * @author Tuukka Lehtonen
28 public class ResourceReferenceTransfer extends ByteArrayTransfer {
30 private static final String TYPENAME = "ResourceReferenceTransfer";
32 private static final int TYPEID = registerType(TYPENAME);
34 private static final ResourceReferenceTransfer INSTANCE = createInstance((String) null);
36 private String purpose;
37 private Session session;
39 public static ResourceReferenceTransfer getInstance() {
43 public static ResourceReferenceTransfer createInstance(Session session) {
44 ResourceReferenceTransfer result = new ResourceReferenceTransfer(null);
45 result.session = session;
49 public static ResourceReferenceTransfer createInstance(String purpose) {
50 ResourceReferenceTransfer result = new ResourceReferenceTransfer(purpose);
54 ResourceReferenceTransfer(String purpose) {
55 this.purpose = purpose;
58 private Session getSession() {
59 // NOTE return SimanticsUI is a big HAXXX
60 return session == null ? SimanticsUI.getSession() : session;
63 public void javaToNative(Object object, TransferData transferData) {
64 if (!checkResourceReference(object) || !isSupportedType(transferData)) {
65 DND.error(DND.ERROR_INVALID_DATA);
67 ResourceArray[] resources = null;
68 if (object instanceof Resource)
69 resources = new ResourceArray[] {new ResourceArray((Resource) object)};
70 if (object instanceof Resource[])
71 resources = new ResourceArray[] {new ResourceArray((Resource[]) object)};
72 if (object instanceof ResourceArray)
73 resources = new ResourceArray[] {(ResourceArray) object};
74 if (object instanceof ResourceArray[])
75 resources = (ResourceArray[]) object;
77 DND.error(DND.ERROR_INVALID_DATA);
78 //throw new IllegalArgumentException();
80 // write data to a byte array and then ask super to convert to
82 Session s = getSession();
83 SerialisationSupport support = s.getService(SerialisationSupport.class);
84 ResourceTransferData rtd = new ResourceTransferData(s, resources);
85 rtd.setPurpose(purpose);
89 buffer = ResourceTransferUtils.createStringTransferable(support, rtd);
90 } catch (DatabaseException e) {
91 throw new RuntimeException(e);
93 TextTransfer.getInstance().javaToNative(buffer, transferData);
96 public Object nativeToJava(TransferData transferData) {
97 if (isSupportedType(transferData)) {
98 String buffer = (String) TextTransfer.getInstance().nativeToJava(transferData);
102 Session s = getSession();
103 SerialisationSupport support = s.getService(SerialisationSupport.class);
104 ResourceTransferData rtd;
106 rtd = ResourceTransferUtils.readStringTransferable(support, buffer);
107 } catch (IllegalArgumentException e) {
108 throw new RuntimeException(e);
109 } catch (DatabaseException e) {
110 throw new RuntimeException(e);
112 // FIXME: the transferred "purpose" data cannot be passed further
113 // because this method is currently assumed to return
114 // ResourceArray[] instead of ResourceTransferData
115 return rtd.toResourceArrayArray();
120 protected String[] getTypeNames() {
121 return new String[] { TYPENAME };
124 protected int[] getTypeIds() {
125 return new int[] { TYPEID };
128 boolean checkResourceReference(Object object) {
129 System.out.println("o = " + object);
130 if (object == null) return false;
131 if ( !(object instanceof ResourceArray[]) || ((ResourceArray[]) object).length == 0) {
134 // ResourceReference[] ResourceReferences = (ResourceReference[]) object;
135 // for (int i = 0; i < ResourceReferences.length; i++) {
136 // // Check each reference if there is something to check
141 protected boolean validate(Object object) {
142 return checkResourceReference(object);