Fixed folder deletion with all subfolders and files
[pithos-web-client] / src / gr / grnet / pithos / web / client / CellTreeViewUtils.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above
13  *      copyright notice, this list of conditions and the following
14  *      disclaimer in the documentation and/or other materials
15  *      provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * The views and conclusions contained in the software and
31  * documentation are those of the authors and should not be
32  * interpreted as representing official policies, either expressed
33  * or implied, of GRNET S.A.
34  */
35 package gr.grnet.pithos.web.client;
36
37 import gr.grnet.pithos.web.client.CellTreeView.RefreshHandler;
38 import gr.grnet.pithos.web.client.rest.resource.RestResource;
39 import gr.grnet.pithos.web.client.rest.resource.RestResourceWrapper;
40
41 import com.google.gwt.user.cellview.client.CellTree;
42 import com.google.gwt.user.cellview.client.TreeNode;
43
44
45 public class CellTreeViewUtils {
46         CellTree tree;
47         /**
48          * 
49          */
50         public CellTreeViewUtils(CellTree tree) {
51                 this.tree = tree;
52         }
53         
54         void refreshNodeContainingResource(RestResource r){
55                 TreeNode node = tree.getRootTreeNode();
56                 refreshNodeContainingResource(node,r);
57         }
58         
59         void refreshNodeContainingResource(String  uri){
60                 TreeNode node = tree.getRootTreeNode();
61                 refreshNodeContainingResource(node,uri);
62         }
63         
64         private void refreshNodeContainingResource(TreeNode node, RestResource resource){
65                 int count = node.getChildCount();
66                 for(int i=0;i<count;i++){
67                         if(node.getChildValue(i).equals(resource)){
68                                 if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
69                                         return;
70                                 node.setChildOpen(i, false, true);
71                                 node.setChildOpen(i, true, true);
72                                 return;
73                         }
74                         else if(node.isChildOpen(i)){
75                                 TreeNode n = node.setChildOpen(i, true);
76                                 if(n!=null)
77                                         refreshNodeContainingResource(n,resource);
78                         }
79                 }
80                 
81         }
82         
83         void refreshNodeContainingResource(TreeNode node, String uri){
84                 int count = node.getChildCount();
85                 for(int i=0;i<count;i++){
86                         if(node.isChildOpen(i)){
87                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(uri)){
88                                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
89                                                 return;
90                                         node.setChildOpen(i, false, true);
91                                         node.setChildOpen(i, true, true);
92                                         return;
93                                 }
94                                 else{
95                                         TreeNode n = node.setChildOpen(i, true);
96                                         if(n!=null)
97                                                 refreshNodeContainingResource(n,uri);
98                                 }
99                         }
100                 }
101         }
102         public void openNodeContainingResource(RestResource resource){
103                 TreeNode node = tree.getRootTreeNode();
104                 openNodeContainingResource(node,resource);
105         }
106         private void openNodeContainingResource(TreeNode node, RestResource resource){
107                 int count = node.getChildCount();
108                 for(int i=0;i<count;i++){
109                         
110                                 if(node.getChildValue(i).equals(resource)){
111                                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
112                                                 return;
113                                         node.setChildOpen(i, true, true);
114                                         return;
115                                 }
116                                 else{
117                                         if(node.isChildOpen(i)){
118                                                 TreeNode n = node.setChildOpen(i, true);
119                                                 if(n!=null)
120                                                         openNodeContainingResource(n,resource);
121                                         }
122                                 }
123                         
124                 }
125         }
126         
127         public void openNodeContainingResource(RestResource resource, RefreshHandler handler){
128                 TreeNode node = tree.getRootTreeNode();
129                 openNodeContainingResource(node,resource,handler);
130         }
131         private void openNodeContainingResource(TreeNode node, RestResource resource, RefreshHandler handler){
132                 int count = node.getChildCount();
133                 for(int i=0;i<count;i++){
134                                 if(node.getChildValue(i).equals(resource)){
135                                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
136                                                 return;
137                                         //node.setChildOpen(i, false, true);
138                                         node.setChildOpen(i, true, true);
139                                         handler.onRefresh();
140                                         return;
141                                 }
142                                 else{
143                                         if(node.isChildOpen(i)){
144                                                 TreeNode n = node.setChildOpen(i, true);
145                                                 if(n!=null)
146                                                         openNodeContainingResource(n,resource, handler);
147                                         }
148                                 }
149                         
150                 }
151         }
152         
153         
154         
155         public boolean doesNodeContainsResource(TreeNode node, RestResource resource){
156                 int count = node.getChildCount();
157                 for(int i=0;i<count;i++){
158                         if(node.isChildOpen(i)){
159                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).equals(resource)){
160                                         return true;
161                                 }
162                                 else if(node.isChildOpen(i)){
163                                         TreeNode n = node.setChildOpen(i, true);
164                                         if(n!=null)
165                                                 return doesNodeContainsResource(n,resource);
166                                 }
167                         }
168                 }
169                 return false;
170         }
171         
172         public boolean doesNodeContainsResource(TreeNode node, String resource){
173                 int count = node.getChildCount();
174                 for(int i=0;i<count;i++){
175                         
176                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
177                                         return true;
178                                 }
179                                 else if(node.isChildOpen(i)){
180                                         TreeNode n = node.setChildOpen(i, true);
181                                         if(n!=null)
182                                                 return doesNodeContainsResource(n,resource);
183                                 }
184                         
185                 }
186                 return false;
187         }
188         
189         public TreeNode getNodeContainingResource(TreeNode node, RestResource resource){
190                 int count = node.getChildCount();
191                 for(int i=0;i<count;i++){
192                         
193                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource.getUri())){
194                                         return node;
195                                 }
196                                 else if(node.isChildOpen(i)){
197                                         TreeNode n = node.setChildOpen(i, true);
198                                         if(n!=null)
199                                                 return getNodeContainingResource(n,resource);
200                                 }
201                         
202                 }
203                 return null;
204         }
205         
206         public TreeNode getNodeContainingResource(TreeNode node, String resource){
207                 if(node==null)
208                         return null;
209                 int count = node.getChildCount();
210                 for(int i=0;i<count;i++){
211                         
212                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
213                                         return node;
214                                 }
215                                 else if(node.isChildOpen(i)){
216                                         TreeNode n = node.setChildOpen(i, true);
217                                         if(n!=null){
218                                                 TreeNode result = getNodeContainingResource2(n,resource);
219                                                 if(result !=null)
220                                                         return result;
221                                         }
222                                 }
223                         
224                 }
225                 return null;
226         }
227         
228         public TreeNode getNodeContainingResource2(TreeNode node, String resource){
229                 if(node==null)
230                         return null;
231                 int count = node.getChildCount();
232                 for(int i=0;i<count;i++){
233                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
234                                         return node.setChildOpen(i, node.isChildOpen(i));
235                                 }
236                                 else if(node.isChildOpen(i)){
237                                         TreeNode n = node.setChildOpen(i, true);
238                                         if(n!=null){
239                                                 TreeNode result = getNodeContainingResource2(n,resource);
240                                                 if(result !=null)
241                                                         return result;
242                                         }
243                                 }
244                         
245                 }
246                 return null;
247         }
248         public boolean doesSharedNodeContainsResource( String resource){
249                 if(tree.getRootTreeNode().isChildOpen(2)){
250                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
251                         return doesNodeContainsResource(node, resource);
252                 }
253                 return false;
254         }
255         
256         public boolean doesSharedNodeContainsResourceIn1stLevel( String resource){
257                 if(tree.getRootTreeNode().isChildOpen(2)){
258                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
259                         int count = node.getChildCount();
260                         for(int i=0;i<count;i++){
261                                 
262                                         if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
263                                                 return true;
264                                         }
265                                 
266                         }
267                         return false;
268                 }
269                 return false;
270         }
271         
272         public boolean doesSharedNodeContainsResourceIn2ndLevel( String resource){
273                 if(tree.getRootTreeNode().isChildOpen(2)){
274                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
275                         int count = node.getChildCount();
276                         for(int i=0;i<count;i++){
277                                 if(node.isChildOpen(i)){
278                                         TreeNode child = node.setChildOpen(i, true);
279                                         for(int j=0;j<child.getChildCount();j++){
280                                                 if(child.getChildValue(j) instanceof RestResource && ((RestResource)child.getChildValue(j)).getUri().equals(resource)){
281                                                         return true;
282                                                 }
283                                         }
284                                 }
285                                         
286                                 
287                         }
288                         return false;
289                 }
290                 return false;
291         }
292         
293         public boolean doesRootNodeContainsResource( String resource){
294                 if(tree.getRootTreeNode().isChildOpen(0)){
295                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
296                         return doesNodeContainsResource(node, resource);
297                 }
298                 return false;
299         }
300         
301         public boolean doesSharedNodeContainsResource( RestResource resource){
302                 if(tree.getRootTreeNode().isChildOpen(2)){
303                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
304                         return doesNodeContainsResource(node, resource);
305                 }
306                 return false;
307         }
308         
309         public boolean doesRootNodeContainsResource( RestResource resource){
310                 if(tree.getRootTreeNode().isChildOpen(0)){
311                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
312                         return doesNodeContainsResource(node, resource);
313                 }
314                 return false;
315         }
316 }