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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.ui.dnd;
\r
14 import java.util.ArrayList;
\r
15 import java.util.Collection;
\r
16 import java.util.List;
\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
28 * Transferable that contains a set of resource path references.
\r
30 * @author Toni Kalajainen
\r
32 public class ResourceTransferData extends ArrayList<ResourceArray> {
\r
34 private static final long serialVersionUID = -6980850210973439012L;
\r
35 private String purpose;
\r
37 public ResourceTransferData() {
\r
41 public ResourceTransferData(Collection<? extends ResourceArray> c) {
\r
42 super(c); // TODO Auto-generated constructor stub
\r
45 public ResourceTransferData(ResourceArray...arrays) {
\r
50 public ResourceTransferData(Session s, Collection<? extends ResourceArray> c) {
\r
51 super(c); // TODO Auto-generated constructor stub
\r
54 public ResourceTransferData(Session s, ResourceArray...arrays) {
\r
59 public ResourceTransferData(SessionReference s, Collection<? extends ResourceArray> c) {
\r
60 super(c); // TODO Auto-generated constructor stub
\r
63 public ResourceTransferData(SessionReference s, ResourceArray...arrays) {
\r
68 public ResourceTransferData(SessionReference s) {
\r
72 public ResourceTransferData(Session s) {
\r
76 public String getPurpose() {
\r
80 public void setPurpose(String purpose) {
\r
81 this.purpose = purpose;
\r
84 public void addAll(ResourceArray[] array)
\r
86 for (ResourceArray a : array)
\r
90 public ResourceArray[] toResourceArrayArray()
\r
92 return toArray(ResourceArray.NONE);
\r
95 //////////// SERIALIZATION //////////////
\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
102 public void readFromMemento(SerialisationSupport serializer, IMemento memento)
\r
103 throws DatabaseException
\r
105 // String sessionReference = memento.getString("SessionReference");
\r
107 String purpose = memento.getString(PURPOSE_KEY);
\r
109 List<ResourceArray> res = new ArrayList<ResourceArray>();
\r
110 for (IMemento child : memento.getChildren(RES_KEY))
\r
112 ResourceArray array = readResourceArrayFromMemento(serializer, child);
\r
117 // setSessionReference(sessionReference);
\r
118 setPurpose(purpose);
\r
121 public static ResourceArray readResourceArrayFromMemento(SerialisationSupport serializer, IMemento memento)
\r
122 throws DatabaseException
\r
124 List<Resource> result = new ArrayList<Resource>();
\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
133 return new ResourceArray( result );
\r
136 public void writeToMemento(SerialisationSupport serializer, IMemento memento)
\r
137 throws DatabaseException
\r
140 if (purpose != null)
\r
141 writePurpose(memento);
\r
142 for (ResourceArray array : this)
\r
144 String id = ""+index++;
\r
145 StringMemento child = (StringMemento) memento.createChild(RES_KEY, id);
\r
146 writeResourceArrayToMemento(serializer, array, child);
\r
150 private void writePurpose(IMemento memento) {
\r
151 assert purpose != null;
\r
152 memento.putString(PURPOSE_KEY, purpose);
\r
155 public static void writeResourceArrayToMemento(SerialisationSupport serializer, ResourceArray array, IMemento sm)
\r
156 throws DatabaseException
\r
158 for (int i=0; i<array.resources.length; i++)
\r
160 Resource r = array.resources[i];
\r
161 String rai = "" + serializer.getRandomAccessId(r);
\r
162 sm.putString(""+i, rai);
\r