]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dnd/ResourceTransferData.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / ResourceTransferData.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.ui.dnd;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.List;\r
17 \r
18 import org.eclipse.ui.IMemento;\r
19 import org.simantics.db.Resource;\r
20 import org.simantics.db.Session;\r
21 import org.simantics.db.SessionReference;\r
22 import org.simantics.db.common.ResourceArray;\r
23 import org.simantics.db.exception.DatabaseException;\r
24 import org.simantics.db.service.SerialisationSupport;\r
25 import org.simantics.utils.ui.workbench.StringMemento;\r
26 \r
27 /**\r
28  * Transferable that contains a set of resource path references.\r
29  *\r
30  * @author Toni Kalajainen\r
31  */\r
32 public class ResourceTransferData extends ArrayList<ResourceArray> {\r
33 \r
34     private static final long serialVersionUID = -6980850210973439012L;\r
35     private String purpose;\r
36 \r
37     public ResourceTransferData() {\r
38         super();\r
39     }\r
40 \r
41     public ResourceTransferData(Collection<? extends ResourceArray> c) {\r
42         super(c);        // TODO Auto-generated constructor stub\r
43     }\r
44 \r
45     public ResourceTransferData(ResourceArray...arrays) {\r
46         super();\r
47         addAll(arrays);\r
48     }\r
49 \r
50     public ResourceTransferData(Session s, Collection<? extends ResourceArray> c) {\r
51         super(c);        // TODO Auto-generated constructor stub\r
52     }\r
53 \r
54     public ResourceTransferData(Session s, ResourceArray...arrays) {\r
55         super();\r
56         addAll(arrays);\r
57     }\r
58 \r
59     public ResourceTransferData(SessionReference s, Collection<? extends ResourceArray> c) {\r
60         super(c);        // TODO Auto-generated constructor stub\r
61     }\r
62 \r
63     public ResourceTransferData(SessionReference s, ResourceArray...arrays) {\r
64         super();\r
65         addAll(arrays);\r
66     }\r
67 \r
68     public ResourceTransferData(SessionReference s) {\r
69         super();\r
70     }\r
71 \r
72     public ResourceTransferData(Session s) {\r
73         super();\r
74     }\r
75 \r
76     public String getPurpose() {\r
77         return purpose;\r
78     }\r
79 \r
80     public void setPurpose(String purpose) {\r
81         this.purpose = purpose;\r
82     }\r
83 \r
84     public void addAll(ResourceArray[] array)\r
85     {\r
86         for (ResourceArray a : array)\r
87             add(a);\r
88     }\r
89 \r
90     public ResourceArray[] toResourceArrayArray()\r
91     {\r
92         return toArray(ResourceArray.NONE);\r
93     }\r
94 \r
95     //////////// SERIALIZATION //////////////\r
96 \r
97     /** Key to resource array of memento child */\r
98     private static final String RES_KEY = "res";\r
99     /** Key to purpose string of a memento */\r
100     private static final String PURPOSE_KEY = "purpose";\r
101 \r
102     public void readFromMemento(SerialisationSupport serializer, IMemento memento)\r
103     throws DatabaseException\r
104     {\r
105 //        String sessionReference = memento.getString("SessionReference");\r
106 \r
107         String purpose = memento.getString(PURPOSE_KEY);\r
108 \r
109         List<ResourceArray> res = new ArrayList<ResourceArray>();\r
110         for (IMemento child : memento.getChildren(RES_KEY))\r
111         {\r
112             ResourceArray array = readResourceArrayFromMemento(serializer, child);\r
113             res.add(array);\r
114         }\r
115 \r
116         addAll(res);\r
117 //        setSessionReference(sessionReference);\r
118         setPurpose(purpose);\r
119     }\r
120 \r
121     public static ResourceArray readResourceArrayFromMemento(SerialisationSupport serializer, IMemento memento)\r
122     throws DatabaseException\r
123     {\r
124         List<Resource> result = new ArrayList<Resource>();\r
125         int index = 0;\r
126         while (memento.getString(""+index)!=null) {\r
127             String randomAccessId = memento.getString(""+index);\r
128             long id = Long.parseLong(randomAccessId);\r
129             Resource r = serializer.getResource(id);\r
130             result.add(r);\r
131             index++;\r
132         }\r
133         return new ResourceArray( result );\r
134     }\r
135 \r
136     public void writeToMemento(SerialisationSupport serializer, IMemento memento)\r
137     throws DatabaseException\r
138     {\r
139         int index = 0;\r
140         if (purpose != null)\r
141             writePurpose(memento);\r
142         for (ResourceArray array : this)\r
143         {\r
144             String id = ""+index++;\r
145             StringMemento child = (StringMemento) memento.createChild(RES_KEY, id);\r
146             writeResourceArrayToMemento(serializer, array, child);\r
147         }\r
148     }\r
149 \r
150     private void writePurpose(IMemento memento) {\r
151         assert purpose != null;\r
152         memento.putString(PURPOSE_KEY, purpose);\r
153     }\r
154 \r
155     public static void writeResourceArrayToMemento(SerialisationSupport serializer, ResourceArray array, IMemento sm)\r
156     throws DatabaseException\r
157     {\r
158         for (int i=0; i<array.resources.length; i++)\r
159         {\r
160             Resource r = array.resources[i];\r
161             String rai = "" + serializer.getRandomAccessId(r);\r
162             sm.putString(""+i, rai);\r
163         }\r
164     }\r
165 }\r