Automated merge with https://gss.googlecode.com/hg/
[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.rest.resource.RestResource;
23 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
24
25 import com.google.gwt.user.cellview.client.CellTree;
26 import com.google.gwt.user.cellview.client.TreeNode;
27
28
29 /**
30  * @author kman
31  *
32  */
33 public class CellTreeViewUtils {
34         CellTree tree;
35         /**
36          * 
37          */
38         public CellTreeViewUtils(CellTree tree) {
39                 this.tree = tree;
40         }
41         
42         void refreshNodeContainingResource(RestResource r){
43                 TreeNode node = tree.getRootTreeNode();
44                 refreshNodeContainingResource(node,r);
45         }
46         
47         void refreshNodeContainingResource(String  uri){
48                 TreeNode node = tree.getRootTreeNode();
49                 refreshNodeContainingResource(node,uri);
50         }
51         
52         private void refreshNodeContainingResource(TreeNode node, RestResource resource){
53                 int count = node.getChildCount();
54                 for(int i=0;i<count;i++){
55                         if(node.isChildOpen(i)){
56                                 if(node.getChildValue(i).equals(resource)){
57                                         node.setChildOpen(i, false, true);
58                                         node.setChildOpen(i, true, true);
59                                         return;
60                                 }
61                                 else{
62                                         TreeNode n = node.setChildOpen(i, true);
63                                         if(n!=null)
64                                                 refreshNodeContainingResource(n,resource);
65                                 }
66                         }
67                 }
68         }
69         
70         private void refreshNodeContainingResource(TreeNode node, String uri){
71                 int count = node.getChildCount();
72                 for(int i=0;i<count;i++){
73                         if(node.isChildOpen(i)){
74                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(uri)){
75                                         if(node.getChildValue(i) instanceof RestResourceWrapper && ((RestResourceWrapper)node.getChildValue(i)).getResource().getFolders().size()==0)
76                                                 return;
77                                         node.setChildOpen(i, false, true);
78                                         node.setChildOpen(i, true, true);
79                                         return;
80                                 }
81                                 else{
82                                         TreeNode n = node.setChildOpen(i, true);
83                                         if(n!=null)
84                                                 refreshNodeContainingResource(n,uri);
85                                 }
86                         }
87                 }
88         }
89         public void openNodeContainingResource(RestResource resource){
90                 TreeNode node = tree.getRootTreeNode();
91                 openNodeContainingResource(node,resource);
92         }
93         private void openNodeContainingResource(TreeNode node, RestResource resource){
94                 int count = node.getChildCount();
95                 for(int i=0;i<count;i++){
96                                 if(node.getChildValue(i).equals(resource)){
97                                         //node.setChildOpen(i, false, true);
98                                         node.setChildOpen(i, true, true);
99                                         return;
100                                 }
101                                 else{
102                                         if(node.isChildOpen(i)){
103                                                 TreeNode n = node.setChildOpen(i, true);
104                                                 if(n!=null)
105                                                         openNodeContainingResource(n,resource);
106                                         }
107                                 }
108                         
109                 }
110         }
111         
112         public void openNodeContainingResource(RestResource resource, RefreshHandler handler){
113                 TreeNode node = tree.getRootTreeNode();
114                 openNodeContainingResource(node,resource,handler);
115         }
116         private void openNodeContainingResource(TreeNode node, RestResource resource, RefreshHandler handler){
117                 int count = node.getChildCount();
118                 for(int i=0;i<count;i++){
119                                 if(node.getChildValue(i).equals(resource)){
120                                         //node.setChildOpen(i, false, true);
121                                         node.setChildOpen(i, true, true);
122                                         handler.onRefresh();
123                                         return;
124                                 }
125                                 else{
126                                         if(node.isChildOpen(i)){
127                                                 TreeNode n = node.setChildOpen(i, true);
128                                                 if(n!=null)
129                                                         openNodeContainingResource(n,resource, handler);
130                                         }
131                                 }
132                         
133                 }
134         }
135         
136         
137         
138         public boolean doesNodeContainsResource(TreeNode node, RestResource resource){
139                 int count = node.getChildCount();
140                 for(int i=0;i<count;i++){
141                         if(node.isChildOpen(i)){
142                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).equals(resource)){
143                                         return true;
144                                 }
145                                 else if(node.isChildOpen(i)){
146                                         TreeNode n = node.setChildOpen(i, true);
147                                         if(n!=null)
148                                                 return doesNodeContainsResource(n,resource);
149                                 }
150                         }
151                 }
152                 return false;
153         }
154         
155         public boolean doesNodeContainsResource(TreeNode node, String resource){
156                 int count = node.getChildCount();
157                 for(int i=0;i<count;i++){
158                         
159                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().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 TreeNode getNodeContainingResource(TreeNode node, RestResource 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.getUri())){
177                                         return node;
178                                 }
179                                 else if(node.isChildOpen(i)){
180                                         TreeNode n = node.setChildOpen(i, true);
181                                         if(n!=null)
182                                                 return getNodeContainingResource(n,resource);
183                                 }
184                         
185                 }
186                 return null;
187         }
188         
189         public TreeNode getNodeContainingResource(TreeNode node, String resource){
190                 if(node==null)
191                         return null;
192                 int count = node.getChildCount();
193                 for(int i=0;i<count;i++){
194                         
195                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
196                                         return node;
197                                 }
198                                 else if(node.isChildOpen(i)){
199                                         TreeNode n = node.setChildOpen(i, true);
200                                         if(n!=null)
201                                                 return getNodeContainingResource(n,resource);
202                                 }
203                         
204                 }
205                 return null;
206         }
207         
208         public TreeNode getNodeContainingResource2(TreeNode node, String resource){
209                 if(node==null)
210                         return null;
211                 int count = node.getChildCount();
212                 for(int i=0;i<count;i++){
213                         
214                                 if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
215                                         return node.setChildOpen(i, node.isChildOpen(i));
216                                 }
217                                 else if(node.isChildOpen(i)){
218                                         TreeNode n = node.setChildOpen(i, true);
219                                         if(n!=null)
220                                                 return getNodeContainingResource2(n,resource);
221                                 }
222                         
223                 }
224                 return null;
225         }
226         public boolean doesSharedNodeContainsResource( String resource){
227                 if(tree.getRootTreeNode().isChildOpen(2)){
228                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
229                         return doesNodeContainsResource(node, resource);
230                 }
231                 return false;
232         }
233         
234         public boolean doesSharedNodeContainsResourceIn1stLevel( String resource){
235                 if(tree.getRootTreeNode().isChildOpen(2)){
236                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
237                         int count = node.getChildCount();
238                         for(int i=0;i<count;i++){
239                                 
240                                         if(node.getChildValue(i) instanceof RestResource && ((RestResource)node.getChildValue(i)).getUri().equals(resource)){
241                                                 return true;
242                                         }
243                                 
244                         }
245                         return false;
246                 }
247                 return false;
248         }
249         
250         public boolean doesRootNodeContainsResource( String resource){
251                 if(tree.getRootTreeNode().isChildOpen(0)){
252                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
253                         return doesNodeContainsResource(node, resource);
254                 }
255                 return false;
256         }
257         
258         public boolean doesSharedNodeContainsResource( RestResource resource){
259                 if(tree.getRootTreeNode().isChildOpen(2)){
260                         TreeNode node = tree.getRootTreeNode().setChildOpen(2, true);
261                         return doesNodeContainsResource(node, resource);
262                 }
263                 return false;
264         }
265         
266         public boolean doesRootNodeContainsResource( RestResource resource){
267                 if(tree.getRootTreeNode().isChildOpen(0)){
268                         TreeNode node = tree.getRootTreeNode().setChildOpen(0, true);
269                         return doesNodeContainsResource(node, resource);
270                 }
271                 return false;
272         }
273 }