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.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
18 import org.eclipse.ui.IMemento;
19 import org.simantics.db.Resource;
20 import org.simantics.db.Session;
21 import org.simantics.db.SessionReference;
22 import org.simantics.db.common.ResourceArray;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.service.SerialisationSupport;
25 import org.simantics.utils.ui.workbench.StringMemento;
28 * Transferable that contains a set of resource path references.
30 * @author Toni Kalajainen
32 public class ResourceTransferData extends ArrayList<ResourceArray> {
34 private static final long serialVersionUID = -6980850210973439012L;
35 private String purpose;
37 public ResourceTransferData() {
41 public ResourceTransferData(Collection<? extends ResourceArray> c) {
42 super(c); // TODO Auto-generated constructor stub
45 public ResourceTransferData(ResourceArray...arrays) {
50 public ResourceTransferData(Session s, Collection<? extends ResourceArray> c) {
51 super(c); // TODO Auto-generated constructor stub
54 public ResourceTransferData(Session s, ResourceArray...arrays) {
59 public ResourceTransferData(SessionReference s, Collection<? extends ResourceArray> c) {
60 super(c); // TODO Auto-generated constructor stub
63 public ResourceTransferData(SessionReference s, ResourceArray...arrays) {
68 public ResourceTransferData(SessionReference s) {
72 public ResourceTransferData(Session s) {
76 public String getPurpose() {
80 public void setPurpose(String purpose) {
81 this.purpose = purpose;
84 public void addAll(ResourceArray[] array)
86 for (ResourceArray a : array)
90 public ResourceArray[] toResourceArrayArray()
92 return toArray(ResourceArray.NONE);
95 //////////// SERIALIZATION //////////////
97 /** Key to resource array of memento child */
98 private static final String RES_KEY = "res";
99 /** Key to purpose string of a memento */
100 private static final String PURPOSE_KEY = "purpose";
102 public void readFromMemento(SerialisationSupport serializer, IMemento memento)
103 throws DatabaseException
105 // String sessionReference = memento.getString("SessionReference");
107 String purpose = memento.getString(PURPOSE_KEY);
109 List<ResourceArray> res = new ArrayList<ResourceArray>();
110 for (IMemento child : memento.getChildren(RES_KEY))
112 ResourceArray array = readResourceArrayFromMemento(serializer, child);
117 // setSessionReference(sessionReference);
121 public static ResourceArray readResourceArrayFromMemento(SerialisationSupport serializer, IMemento memento)
122 throws DatabaseException
124 List<Resource> result = new ArrayList<Resource>();
126 while (memento.getString(""+index)!=null) {
127 String randomAccessId = memento.getString(""+index);
128 long id = Long.parseLong(randomAccessId);
129 Resource r = serializer.getResource(id);
133 return new ResourceArray( result );
136 public void writeToMemento(SerialisationSupport serializer, IMemento memento)
137 throws DatabaseException
141 writePurpose(memento);
142 for (ResourceArray array : this)
144 String id = ""+index++;
145 StringMemento child = (StringMemento) memento.createChild(RES_KEY, id);
146 writeResourceArrayToMemento(serializer, array, child);
150 private void writePurpose(IMemento memento) {
151 assert purpose != null;
152 memento.putString(PURPOSE_KEY, purpose);
155 public static void writeResourceArrayToMemento(SerialisationSupport serializer, ResourceArray array, IMemento sm)
156 throws DatabaseException
158 for (int i=0; i<array.resources.length; i++)
160 Resource r = array.resources[i];
161 String rai = "" + serializer.getRandomAccessId(r);
162 sm.putString(""+i, rai);