Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / web / client / CellTreeViewUtils.java @ 1206:292dec4eae08

History | View | Annotate | Download (8.9 kB)

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 org.gss_project.gss.web.client;
20

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