temporary commit
[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                                 node.setChildOpen(i, false, true);
60                                 node.setChildOpen(i, true, true);
61                                 return;
62                         }
63                         else if(node.isChildOpen(i)){
64                                 TreeNode n = node.setChildOpen(i, true);
65                                 if(n!=null)
66                                         refreshNodeContainingResource(n,resource);
67                         }
68                 }
69                 
70         }
71         
72         void refreshNodeContainingResource(TreeNode node, String uri){
73                 int count = node.getChildCount();
74                 for(int i=0;i<count;i++){
75                         if(node.isChildOpen(i)){
76                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(uri)){
77                                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
78                                                 return;
79                                         node.setChildOpen(i, false, true);
80                                         node.setChildOpen(i, true, true);
81                                         return;
82                                 }
83                                 else{
84                                         TreeNode n = node.setChildOpen(i, true);
85                                         if(n!=null)
86                                                 refreshNodeContainingResource(n,uri);
87                                 }
88                         }
89                 }
90         }
91         public void openNodeContainingResource(RestResource resource){
92                 TreeNode node = tree.getRootTreeNode();
93                 openNodeContainingResource(node,resource);
94         }
95         private void openNodeContainingResource(TreeNode node, RestResource resource){
96                 int count = node.getChildCount();
97                 for(int i=0;i<count;i++){
98                                 if(node.getChildValue(i).equals(resource)){
99                                         //node.setChildOpen(i, false, true);
100                                         node.setChildOpen(i, true, true);
101                                         return;
102                                 }
103                                 else{
104                                         if(node.isChildOpen(i)){
105                                                 TreeNode n = node.setChildOpen(i, true);
106                                                 if(n!=null)
107                                                         openNodeContainingResource(n,resource);
108                                         }
109                                 }
110                         
111                 }
112         }
113         
114         public void openNodeContainingResource(RestResource resource, RefreshHandler handler){
115                 TreeNode node = tree.getRootTreeNode();
116                 openNodeContainingResource(node,resource,handler);
117         }
118         private void openNodeContainingResource(TreeNode node, RestResource resource, RefreshHandler handler){
119                 int count = node.getChildCount();
120                 for(int i=0;i<count;i++){
121                                 if(node.getChildValue(i).equals(resource)){
122                                         //node.setChildOpen(i, false, true);
123                                         node.setChildOpen(i, true, true);
124                                         handler.onRefresh();
125                                         return;
126                                 }
127                                 else{
128                                         if(node.isChildOpen(i)){
129                                                 TreeNode n = node.setChildOpen(i, true);
130                                                 if(n!=null)
131                                                         openNodeContainingResource(n,resource, handler);
132                                         }
133                                 }
134                         
135                 }
136         }
137         
138         
139         
140         public boolean doesNodeContainsResource(TreeNode node, RestResource resource){
141                 int count = node.getChildCount();
142                 for(int i=0;i<count;i++){
143                         if(node.isChildOpen(i)){
144                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).equals(resource)){
145                                         return true;
146                                 }
147                                 else if(node.isChildOpen(i)){
148                                         TreeNode n = node.setChildOpen(i, true);
149                                         if(n!=null)
150                                                 return doesNodeContainsResource(n,resource);
151                                 }
152                         }
153                 }
154                 return false;
155         }
156         
157         public boolean doesNodeContainsResource(TreeNode node, String resource){
158                 int count = node.getChildCount();
159                 for(int i=0;i<count;i++){
160                         
161                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
162                                         return true;
163                                 }
164                                 else if(node.isChildOpen(i)){
165                                         TreeNode n = node.setChildOpen(i, true);
166                                         if(n!=null)
167                                                 return doesNodeContainsResource(n,resource);
168                                 }
169                         
170                 }
171                 return false;
172         }
173         
174         public TreeNode getNodeContainingResource(TreeNode node, RestResource resource){
175                 int count = node.getChildCount();
176                 for(int i=0;i<count;i++){
177                         
178                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource.getUri())){
179                                         return node;
180                                 }
181                                 else if(node.isChildOpen(i)){
182                                         TreeNode n = node.setChildOpen(i, true);
183                                         if(n!=null)
184                                                 return getNodeContainingResource(n,resource);
185                                 }
186                         
187                 }
188                 return null;
189         }
190         
191         public TreeNode getNodeContainingResource(TreeNode node, String resource){
192                 if(node==null)
193                         return null;
194                 int count = node.getChildCount();
195                 for(int i=0;i<count;i++){
196                         
197                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
198                                         return node;
199                                 }
200                                 else if(node.isChildOpen(i)){
201                                         TreeNode n = node.setChildOpen(i, true);
202                                         if(n!=null)
203                                                 return getNodeContainingResource(n,resource);
204                                 }
205                         
206                 }
207                 return null;
208         }
209         
210         public TreeNode getNodeContainingResource2(TreeNode node, String resource){
211                 if(node==null)
212                         return null;
213                 int count = node.getChildCount();
214                 for(int i=0;i<count;i++){
215                         
216                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
217                                         return node.setChildOpen(i, node.isChildOpen(i));
218                                 }
219                                 else if(node.isChildOpen(i)){
220                                         TreeNode n = node.setChildOpen(i, true);
221                                         if(n!=null)
222                                                 return getNodeContainingResource2(n,resource);
223                                 }
224                         
225                 }
226                 return null;
227         }
228         public boolean doesSharedNodeContainsResource( String resource){
229                 if(tree.getRootTreeNode().isChildOpen(2)){
230                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
231                         return doesNodeContainsResource(node, resource);
232                 }
233                 return false;
234         }
235         
236         public boolean doesSharedNodeContainsResourceIn1stLevel( String resource){
237                 if(tree.getRootTreeNode().isChildOpen(2)){
238                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
239                         int count = node.getChildCount();
240                         for(int i=0;i<count;i++){
241                                 
242                                         if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
243                                                 return true;
244                                         }
245                                 
246                         }
247                         return false;
248                 }
249                 return false;
250         }
251         
252         public boolean doesSharedNodeContainsResourceIn2ndLevel( String resource){
253                 if(tree.getRootTreeNode().isChildOpen(2)){
254                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
255                         int count = node.getChildCount();
256                         for(int i=0;i<count;i++){
257                                 if(node.isChildOpen(i)){
258                                         TreeNode child = node.setChildOpen(i, true);
259                                         for(int j=0;j<child.getChildCount();j++){
260                                                 if(child.getChildValue(j) instanceof RestResource && ((RestResource)child.getChildValue(j)).getUri().equals(resource)){
261                                                         return true;
262                                                 }
263                                         }
264                                 }
265                                         
266                                 
267                         }
268                         return false;
269                 }
270                 return false;
271         }
272         
273         public boolean doesRootNodeContainsResource( String resource){
274                 if(tree.getRootTreeNode().isChildOpen(0)){
275                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
276                         return doesNodeContainsResource(node, resource);
277                 }
278                 return false;
279         }
280         
281         public boolean doesSharedNodeContainsResource( RestResource resource){
282                 if(tree.getRootTreeNode().isChildOpen(2)){
283                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
284                         return doesNodeContainsResource(node, resource);
285                 }
286                 return false;
287         }
288         
289         public boolean doesRootNodeContainsResource( RestResource resource){
290                 if(tree.getRootTreeNode().isChildOpen(0)){
291                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
292                         return doesNodeContainsResource(node, resource);
293                 }
294                 return false;
295         }
296 }