fix bug for node copy and move refresh when using dnd
[pithos] / src / gr / ebs / gss / client / CellTreeViewUtils.java
1 /*
2  * Copyright 2011 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.CellTreeView.RefreshHandler;
22 import gr.ebs.gss.client.CellTreeViewModel.MyFolderDataProvider;
23 import gr.ebs.gss.client.rest.resource.MyFolderResource;
24 import gr.ebs.gss.client.rest.resource.RestResource;
25 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
26
27 import com.google.gwt.core.client.GWT;
28 import com.google.gwt.user.cellview.client.CellTree;
29 import com.google.gwt.user.cellview.client.TreeNode;
30
31
32 /**
33  * @author kman
34  *
35  */
36 public class CellTreeViewUtils {
37         CellTree tree;
38         /**
39          * 
40          */
41         public CellTreeViewUtils(CellTree tree) {
42                 this.tree = tree;
43         }
44         
45         void refreshNodeContainingResource(RestResource r){
46                 TreeNode node = tree.getRootTreeNode();
47                 refreshNodeContainingResource(node,r);
48         }
49         
50         void refreshNodeContainingResource(String  uri){
51                 TreeNode node = tree.getRootTreeNode();
52                 refreshNodeContainingResource(node,uri);
53         }
54         
55         private void refreshNodeContainingResource(TreeNode node, RestResource resource){
56                 int count = node.getChildCount();
57                 for(int i=0;i<count;i++){
58                         if(node.getChildValue(i).equals(resource)){
59                                 if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
60                                         return;
61                                 node.setChildOpen(i, false, true);
62                                 node.setChildOpen(i, true, true);
63                                 return;
64                         }
65                         else if(node.isChildOpen(i)){
66                                 TreeNode n = node.setChildOpen(i, true);
67                                 if(n!=null)
68                                         refreshNodeContainingResource(n,resource);
69                         }
70                 }
71                 
72         }
73         
74         void refreshNodeContainingResource(TreeNode node, String uri){
75                 int count = node.getChildCount();
76                 for(int i=0;i<count;i++){
77                         if(node.isChildOpen(i)){
78                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(uri)){
79                                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
80                                                 return;
81                                         node.setChildOpen(i, false, true);
82                                         node.setChildOpen(i, true, true);
83                                         return;
84                                 }
85                                 else{
86                                         TreeNode n = node.setChildOpen(i, true);
87                                         if(n!=null)
88                                                 refreshNodeContainingResource(n,uri);
89                                 }
90                         }
91                 }
92         }
93         public void openNodeContainingResource(RestResource resource){
94                 TreeNode node = tree.getRootTreeNode();
95                 openNodeContainingResource(node,resource);
96         }
97         private void openNodeContainingResource(TreeNode node, RestResource resource){
98                 int count = node.getChildCount();
99                 for(int i=0;i<count;i++){
100                         
101                                 if(node.getChildValue(i).equals(resource)){
102                                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
103                                                 return;
104                                         node.setChildOpen(i, true, true);
105                                         return;
106                                 }
107                                 else{
108                                         if(node.isChildOpen(i)){
109                                                 TreeNode n = node.setChildOpen(i, true);
110                                                 if(n!=null)
111                                                         openNodeContainingResource(n,resource);
112                                         }
113                                 }
114                         
115                 }
116         }
117         
118         public void openNodeContainingResource(RestResource resource, RefreshHandler handler){
119                 TreeNode node = tree.getRootTreeNode();
120                 openNodeContainingResource(node,resource,handler);
121         }
122         private void openNodeContainingResource(TreeNode node, RestResource resource, RefreshHandler handler){
123                 int count = node.getChildCount();
124                 for(int i=0;i<count;i++){
125                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
126                                 return;
127                                 if(node.getChildValue(i).equals(resource)){
128                                         //node.setChildOpen(i, false, true);
129                                         node.setChildOpen(i, true, true);
130                                         handler.onRefresh();
131                                         return;
132                                 }
133                                 else{
134                                         if(node.isChildOpen(i)){
135                                                 TreeNode n = node.setChildOpen(i, true);
136                                                 if(n!=null)
137                                                         openNodeContainingResource(n,resource, handler);
138                                         }
139                                 }
140                         
141                 }
142         }
143         
144         
145         
146         public boolean doesNodeContainsResource(TreeNode node, RestResource resource){
147                 int count = node.getChildCount();
148                 for(int i=0;i<count;i++){
149                         if(node.isChildOpen(i)){
150                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).equals(resource)){
151                                         return true;
152                                 }
153                                 else if(node.isChildOpen(i)){
154                                         TreeNode n = node.setChildOpen(i, true);
155                                         if(n!=null)
156                                                 return doesNodeContainsResource(n,resource);
157                                 }
158                         }
159                 }
160                 return false;
161         }
162         
163         public boolean doesNodeContainsResource(TreeNode node, String resource){
164                 int count = node.getChildCount();
165                 for(int i=0;i<count;i++){
166                         
167                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
168                                         return true;
169                                 }
170                                 else if(node.isChildOpen(i)){
171                                         TreeNode n = node.setChildOpen(i, true);
172                                         if(n!=null)
173                                                 return doesNodeContainsResource(n,resource);
174                                 }
175                         
176                 }
177                 return false;
178         }
179         
180         public TreeNode getNodeContainingResource(TreeNode node, RestResource resource){
181                 int count = node.getChildCount();
182                 for(int i=0;i<count;i++){
183                         
184                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource.getUri())){
185                                         return node;
186                                 }
187                                 else if(node.isChildOpen(i)){
188                                         TreeNode n = node.setChildOpen(i, true);
189                                         if(n!=null)
190                                                 return getNodeContainingResource(n,resource);
191                                 }
192                         
193                 }
194                 return null;
195         }
196         
197         public TreeNode getNodeContainingResource(TreeNode node, String resource){
198                 if(node==null)
199                         return null;
200                 int count = node.getChildCount();
201                 for(int i=0;i<count;i++){
202                         
203                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
204                                         return node;
205                                 }
206                                 else if(node.isChildOpen(i)){
207                                         TreeNode n = node.setChildOpen(i, true);
208                                         if(n!=null){
209                                                 TreeNode result = getNodeContainingResource2(n,resource);
210                                                 if(result !=null)
211                                                         return result;
212                                         }
213                                 }
214                         
215                 }
216                 return null;
217         }
218         
219         public TreeNode getNodeContainingResource2(TreeNode node, String resource){
220                 if(node==null)
221                         return null;
222                 int count = node.getChildCount();
223                 for(int i=0;i<count;i++){
224                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
225                                         return node.setChildOpen(i, node.isChildOpen(i));
226                                 }
227                                 else if(node.isChildOpen(i)){
228                                         TreeNode n = node.setChildOpen(i, true);
229                                         if(n!=null){
230                                                 TreeNode result = getNodeContainingResource2(n,resource);
231                                                 if(result !=null)
232                                                         return result;
233                                         }
234                                 }
235                         
236                 }
237                 return null;
238         }
239         public boolean doesSharedNodeContainsResource( String resource){
240                 if(tree.getRootTreeNode().isChildOpen(2)){
241                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
242                         return doesNodeContainsResource(node, resource);
243                 }
244                 return false;
245         }
246         
247         public boolean doesSharedNodeContainsResourceIn1stLevel( String resource){
248                 if(tree.getRootTreeNode().isChildOpen(2)){
249                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
250                         int count = node.getChildCount();
251                         for(int i=0;i<count;i++){
252                                 
253                                         if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
254                                                 return true;
255                                         }
256                                 
257                         }
258                         return false;
259                 }
260                 return false;
261         }
262         
263         public boolean doesSharedNodeContainsResourceIn2ndLevel( String resource){
264                 if(tree.getRootTreeNode().isChildOpen(2)){
265                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
266                         int count = node.getChildCount();
267                         for(int i=0;i<count;i++){
268                                 if(node.isChildOpen(i)){
269                                         TreeNode child = node.setChildOpen(i, true);
270                                         for(int j=0;j<child.getChildCount();j++){
271                                                 if(child.getChildValue(j) instanceof RestResource && ((RestResource)child.getChildValue(j)).getUri().equals(resource)){
272                                                         return true;
273                                                 }
274                                         }
275                                 }
276                                         
277                                 
278                         }
279                         return false;
280                 }
281                 return false;
282         }
283         
284         public boolean doesRootNodeContainsResource( String resource){
285                 if(tree.getRootTreeNode().isChildOpen(0)){
286                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
287                         return doesNodeContainsResource(node, resource);
288                 }
289                 return false;
290         }
291         
292         public boolean doesSharedNodeContainsResource( RestResource resource){
293                 if(tree.getRootTreeNode().isChildOpen(2)){
294                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
295                         return doesNodeContainsResource(node, resource);
296                 }
297                 return false;
298         }
299         
300         public boolean doesRootNodeContainsResource( RestResource resource){
301                 if(tree.getRootTreeNode().isChildOpen(0)){
302                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
303                         return doesNodeContainsResource(node, resource);
304                 }
305                 return false;
306         }
307 }