--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.ui.dnd;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.List;\r
+\r
+import org.eclipse.ui.IMemento;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.SessionReference;\r
+import org.simantics.db.common.ResourceArray;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.utils.ui.workbench.StringMemento;\r
+\r
+/**\r
+ * Transferable that contains a set of resource path references.\r
+ *\r
+ * @author Toni Kalajainen\r
+ */\r
+public class ResourceTransferData extends ArrayList<ResourceArray> {\r
+\r
+ private static final long serialVersionUID = -6980850210973439012L;\r
+ private String purpose;\r
+\r
+ public ResourceTransferData() {\r
+ super();\r
+ }\r
+\r
+ public ResourceTransferData(Collection<? extends ResourceArray> c) {\r
+ super(c); // TODO Auto-generated constructor stub\r
+ }\r
+\r
+ public ResourceTransferData(ResourceArray...arrays) {\r
+ super();\r
+ addAll(arrays);\r
+ }\r
+\r
+ public ResourceTransferData(Session s, Collection<? extends ResourceArray> c) {\r
+ super(c); // TODO Auto-generated constructor stub\r
+ }\r
+\r
+ public ResourceTransferData(Session s, ResourceArray...arrays) {\r
+ super();\r
+ addAll(arrays);\r
+ }\r
+\r
+ public ResourceTransferData(SessionReference s, Collection<? extends ResourceArray> c) {\r
+ super(c); // TODO Auto-generated constructor stub\r
+ }\r
+\r
+ public ResourceTransferData(SessionReference s, ResourceArray...arrays) {\r
+ super();\r
+ addAll(arrays);\r
+ }\r
+\r
+ public ResourceTransferData(SessionReference s) {\r
+ super();\r
+ }\r
+\r
+ public ResourceTransferData(Session s) {\r
+ super();\r
+ }\r
+\r
+ public String getPurpose() {\r
+ return purpose;\r
+ }\r
+\r
+ public void setPurpose(String purpose) {\r
+ this.purpose = purpose;\r
+ }\r
+\r
+ public void addAll(ResourceArray[] array)\r
+ {\r
+ for (ResourceArray a : array)\r
+ add(a);\r
+ }\r
+\r
+ public ResourceArray[] toResourceArrayArray()\r
+ {\r
+ return toArray(ResourceArray.NONE);\r
+ }\r
+\r
+ //////////// SERIALIZATION //////////////\r
+\r
+ /** Key to resource array of memento child */\r
+ private static final String RES_KEY = "res";\r
+ /** Key to purpose string of a memento */\r
+ private static final String PURPOSE_KEY = "purpose";\r
+\r
+ public void readFromMemento(SerialisationSupport serializer, IMemento memento)\r
+ throws DatabaseException\r
+ {\r
+// String sessionReference = memento.getString("SessionReference");\r
+\r
+ String purpose = memento.getString(PURPOSE_KEY);\r
+\r
+ List<ResourceArray> res = new ArrayList<ResourceArray>();\r
+ for (IMemento child : memento.getChildren(RES_KEY))\r
+ {\r
+ ResourceArray array = readResourceArrayFromMemento(serializer, child);\r
+ res.add(array);\r
+ }\r
+\r
+ addAll(res);\r
+// setSessionReference(sessionReference);\r
+ setPurpose(purpose);\r
+ }\r
+\r
+ public static ResourceArray readResourceArrayFromMemento(SerialisationSupport serializer, IMemento memento)\r
+ throws DatabaseException\r
+ {\r
+ List<Resource> result = new ArrayList<Resource>();\r
+ int index = 0;\r
+ while (memento.getString(""+index)!=null) {\r
+ String randomAccessId = memento.getString(""+index);\r
+ long id = Long.parseLong(randomAccessId);\r
+ Resource r = serializer.getResource(id);\r
+ result.add(r);\r
+ index++;\r
+ }\r
+ return new ResourceArray( result );\r
+ }\r
+\r
+ public void writeToMemento(SerialisationSupport serializer, IMemento memento)\r
+ throws DatabaseException\r
+ {\r
+ int index = 0;\r
+ if (purpose != null)\r
+ writePurpose(memento);\r
+ for (ResourceArray array : this)\r
+ {\r
+ String id = ""+index++;\r
+ StringMemento child = (StringMemento) memento.createChild(RES_KEY, id);\r
+ writeResourceArrayToMemento(serializer, array, child);\r
+ }\r
+ }\r
+\r
+ private void writePurpose(IMemento memento) {\r
+ assert purpose != null;\r
+ memento.putString(PURPOSE_KEY, purpose);\r
+ }\r
+\r
+ public static void writeResourceArrayToMemento(SerialisationSupport serializer, ResourceArray array, IMemento sm)\r
+ throws DatabaseException\r
+ {\r
+ for (int i=0; i<array.resources.length; i++)\r
+ {\r
+ Resource r = array.resources[i];\r
+ String rai = "" + serializer.getRandomAccessId(r);\r
+ sm.putString(""+i, rai);\r
+ }\r
+ }\r
+}\r