]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dnd/ResourceReferenceTransfer.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / ResourceReferenceTransfer.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 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.Simantics;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.common.ResourceArray;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.service.SerialisationSupport;
24
25 /**
26  * @author Tuukka Lehtonen
27  */
28 public class ResourceReferenceTransfer extends ByteArrayTransfer {
29
30     private static final String                    TYPENAME = "ResourceReferenceTransfer";
31
32     private static final int                       TYPEID   = registerType(TYPENAME);
33
34     private static final ResourceReferenceTransfer INSTANCE = createInstance((String) null);
35
36     private String                                 purpose;
37     private Session                                session;
38
39     public static ResourceReferenceTransfer getInstance() {
40         return INSTANCE;
41     }
42
43     public static ResourceReferenceTransfer createInstance(Session session) {
44         ResourceReferenceTransfer result = new ResourceReferenceTransfer(null);
45         result.session = session;
46         return result;
47     }
48
49     public static ResourceReferenceTransfer createInstance(String purpose) {
50         ResourceReferenceTransfer result = new ResourceReferenceTransfer(purpose);
51         return result;
52     }
53
54     ResourceReferenceTransfer(String purpose) {
55         this.purpose = purpose;
56     }
57
58     private Session getSession() {
59         // NOTE return SimanticsUI is a big HAXXX
60         return session == null ? Simantics.getSession() : session;
61     }
62
63     public void javaToNative(Object object, TransferData transferData) {
64         if (!checkResourceReference(object) || !isSupportedType(transferData)) {
65             DND.error(DND.ERROR_INVALID_DATA);
66         }
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;
76         if (resources==null)
77             DND.error(DND.ERROR_INVALID_DATA);
78         //throw new IllegalArgumentException();
79
80         // write data to a byte array and then ask super to convert to
81         // pMedium
82         Session s = getSession();
83         SerialisationSupport support = s.getService(SerialisationSupport.class);
84         ResourceTransferData rtd = new ResourceTransferData(s, resources);
85         rtd.setPurpose(purpose);
86
87         Object buffer;
88         try {
89             buffer = ResourceTransferUtils.createStringTransferable(support, rtd);
90         } catch (DatabaseException e) {
91             throw new RuntimeException(e);
92         }
93         TextTransfer.getInstance().javaToNative(buffer, transferData);
94     }
95
96     public Object nativeToJava(TransferData transferData) {
97         if (isSupportedType(transferData)) {
98             String buffer = (String) TextTransfer.getInstance().nativeToJava(transferData);
99             if (buffer == null)
100                 return null;
101
102             Session s = getSession();
103             SerialisationSupport support = s.getService(SerialisationSupport.class);
104             ResourceTransferData rtd;
105             try {
106                 rtd = ResourceTransferUtils.readStringTransferable(support, buffer);
107             } catch (IllegalArgumentException e) {
108                 throw new RuntimeException(e);
109             } catch (DatabaseException e) {
110                 throw new RuntimeException(e);
111             }
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();
116         }
117         return null;
118     }
119
120     protected String[] getTypeNames() {
121         return new String[] { TYPENAME };
122     }
123
124     protected int[] getTypeIds() {
125         return new int[] { TYPEID };
126     }
127
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) {
132             return false;
133         }
134 //        ResourceReference[] ResourceReferences = (ResourceReference[]) object;
135 //        for (int i = 0; i < ResourceReferences.length; i++) {
136 //            // Check each reference if there is something to check
137 //        }
138         return true;
139     }
140
141     protected boolean validate(Object object) {
142         return checkResourceReference(object);
143     }
144
145 }