]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.ui/src/org/simantics/ui/dnd/ResourceTransferData.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / dnd / ResourceTransferData.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 java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
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;
26
27 /**
28  * Transferable that contains a set of resource path references.
29  *
30  * @author Toni Kalajainen
31  */
32 public class ResourceTransferData extends ArrayList<ResourceArray> {
33
34     private static final long serialVersionUID = -6980850210973439012L;
35     private String purpose;
36
37     public ResourceTransferData() {
38         super();
39     }
40
41     public ResourceTransferData(Collection<? extends ResourceArray> c) {
42         super(c);        // TODO Auto-generated constructor stub
43     }
44
45     public ResourceTransferData(ResourceArray...arrays) {
46         super();
47         addAll(arrays);
48     }
49
50     public ResourceTransferData(Session s, Collection<? extends ResourceArray> c) {
51         super(c);        // TODO Auto-generated constructor stub
52     }
53
54     public ResourceTransferData(Session s, ResourceArray...arrays) {
55         super();
56         addAll(arrays);
57     }
58
59     public ResourceTransferData(SessionReference s, Collection<? extends ResourceArray> c) {
60         super(c);        // TODO Auto-generated constructor stub
61     }
62
63     public ResourceTransferData(SessionReference s, ResourceArray...arrays) {
64         super();
65         addAll(arrays);
66     }
67
68     public ResourceTransferData(SessionReference s) {
69         super();
70     }
71
72     public ResourceTransferData(Session s) {
73         super();
74     }
75
76     public String getPurpose() {
77         return purpose;
78     }
79
80     public void setPurpose(String purpose) {
81         this.purpose = purpose;
82     }
83
84     public void addAll(ResourceArray[] array)
85     {
86         for (ResourceArray a : array)
87             add(a);
88     }
89
90     public ResourceArray[] toResourceArrayArray()
91     {
92         return toArray(ResourceArray.NONE);
93     }
94
95     //////////// SERIALIZATION //////////////
96
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";
101
102     public void readFromMemento(SerialisationSupport serializer, IMemento memento)
103     throws DatabaseException
104     {
105 //        String sessionReference = memento.getString("SessionReference");
106
107         String purpose = memento.getString(PURPOSE_KEY);
108
109         List<ResourceArray> res = new ArrayList<ResourceArray>();
110         for (IMemento child : memento.getChildren(RES_KEY))
111         {
112             ResourceArray array = readResourceArrayFromMemento(serializer, child);
113             res.add(array);
114         }
115
116         addAll(res);
117 //        setSessionReference(sessionReference);
118         setPurpose(purpose);
119     }
120
121     public static ResourceArray readResourceArrayFromMemento(SerialisationSupport serializer, IMemento memento)
122     throws DatabaseException
123     {
124         List<Resource> result = new ArrayList<Resource>();
125         int index = 0;
126         while (memento.getString(""+index)!=null) {
127             String randomAccessId = memento.getString(""+index);
128             long id = Long.parseLong(randomAccessId);
129             Resource r = serializer.getResource(id);
130             result.add(r);
131             index++;
132         }
133         return new ResourceArray( result );
134     }
135
136     public void writeToMemento(SerialisationSupport serializer, IMemento memento)
137     throws DatabaseException
138     {
139         int index = 0;
140         if (purpose != null)
141             writePurpose(memento);
142         for (ResourceArray array : this)
143         {
144             String id = ""+index++;
145             StringMemento child = (StringMemento) memento.createChild(RES_KEY, id);
146             writeResourceArrayToMemento(serializer, array, child);
147         }
148     }
149
150     private void writePurpose(IMemento memento) {
151         assert purpose != null;
152         memento.putString(PURPOSE_KEY, purpose);
153     }
154
155     public static void writeResourceArrayToMemento(SerialisationSupport serializer, ResourceArray array, IMemento sm)
156     throws DatabaseException
157     {
158         for (int i=0; i<array.resources.length; i++)
159         {
160             Resource r = array.resources[i];
161             String rai = "" + serializer.getRandomAccessId(r);
162             sm.putString(""+i, rai);
163         }
164     }
165 }