09db1879784d6bfa9d1bd0fbc3b4246edac48648
[pithos] / src / gr / ebs / gss / client / CellTreeView.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.rest.GetCommand;
22 import gr.ebs.gss.client.rest.resource.FolderResource;
23 import gr.ebs.gss.client.rest.resource.FolderResource;
24 import gr.ebs.gss.client.rest.resource.UserResource;
25
26 import java.util.Arrays;
27
28 import com.google.gwt.cell.client.AbstractCell;
29 import com.google.gwt.cell.client.Cell;
30 import com.google.gwt.core.client.GWT;
31 import com.google.gwt.resources.client.ClientBundle;
32 import com.google.gwt.resources.client.ImageResource;
33 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
34 import com.google.gwt.user.cellview.client.CellTree;
35 import com.google.gwt.user.cellview.client.TreeNode;
36 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
37 import com.google.gwt.user.client.DeferredCommand;
38 import com.google.gwt.user.client.IncrementalCommand;
39 import com.google.gwt.user.client.ui.Composite;
40 import com.google.gwt.user.client.ui.Tree;
41 import com.google.gwt.view.client.AsyncDataProvider;
42 import com.google.gwt.view.client.HasData;
43 import com.google.gwt.view.client.ListDataProvider;
44 import com.google.gwt.view.client.Range;
45 import com.google.gwt.view.client.SingleSelectionModel;
46 import com.google.gwt.view.client.TreeViewModel;
47 import com.google.gwt.view.client.TreeViewModel.DefaultNodeInfo;
48
49
50 /**
51  * @author kman
52  *
53  */
54 public class CellTreeView extends Composite{
55         public static final boolean DONE = false;
56         Images images;
57         private final ListDataProvider<FolderResource> rootNodes = new ListDataProvider<FolderResource>();
58         SingleSelectionModel<FolderResource> selectionModel = new SingleSelectionModel<FolderResource>();
59         /**
60          * Specifies the images that will be bundled for this Composite and other
61          * inherited images that will be included in the same bundle.
62          */
63         public interface Images extends ClientBundle, Tree.Resources, FolderContextMenu.Images {
64
65                 @Source("gr/ebs/gss/resources/folder_home.png")
66                 ImageResource home();
67
68                 @Source("gr/ebs/gss/resources/folder_yellow.png")
69                 ImageResource folderYellow();
70
71                 @Source("gr/ebs/gss/resources/mimetypes/document.png")
72                 ImageResource document();
73
74                 @Source("gr/ebs/gss/resources/internet.png")
75                 ImageResource othersShared();
76
77                 @Source("gr/ebs/gss/resources/edit_user.png")
78                 ImageResource myShared();
79
80                 @Source("gr/ebs/gss/resources/folder_user.png")
81                 ImageResource sharedFolder();
82
83                 @Source("gr/ebs/gss/resources/trashcan_empty.png")
84                 ImageResource trash();
85         }
86         
87         /**
88          * 
89          */
90         public CellTreeView(final Images _images) {
91                 images = _images;
92                 TreeViewModel model = new CustomTreeModel();
93
94             /*
95              * Create the tree using the model. We use <code>null</code> as the default
96              * value of the root node. The default value will be passed to
97              * CustomTreeModel#getNodeInfo();
98              */
99             CellTree tree = new CellTree(model, null);
100             tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
101
102             
103             initWidget(tree);
104             DeferredCommand.addCommand(new IncrementalCommand() {
105
106                         @Override
107                         public boolean execute() {
108                                 return fetchRootFolders();
109                         }
110                 });
111         }
112         public boolean fetchRootFolders() {
113                 UserResource userResource = GSS.get().getCurrentUserResource();
114                 if (userResource == null)
115                         return !DONE;
116
117                 final String path = userResource.getFilesPath();
118                 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, null) {
119
120                         @Override
121                         public void onComplete() {
122                                 FolderResource rootResource = getResult();
123                                 rootNodes.setList(Arrays.asList((FolderResource)rootResource));
124                                 
125                         }
126
127                         @Override
128                         public void onError(Throwable t) {
129                                 GWT.log("Error fetching root folder", t);
130                                 GSS.get().displayError("Unable to fetch root folder");
131                         }
132
133                 };
134                 DeferredCommand.addCommand(gf);
135                 return DONE;
136         }
137
138         public Images getImages() {
139                 return images;
140         }
141         
142         
143         class CustomTreeModel implements TreeViewModel{
144                 private final Cell<FolderResource> departmentCell = new AbstractCell<FolderResource>(){
145
146                         @Override
147                         public void render(com.google.gwt.cell.client.Cell.Context arg0, FolderResource arg1, SafeHtmlBuilder arg2) {
148                                 arg2.appendEscaped(arg1.getName());
149                                 
150                         }
151                         
152                 };
153                 
154                 
155                 @Override
156                 public <T> NodeInfo<?> getNodeInfo(T value) {
157                         if(value==null){
158                                 return new DefaultNodeInfo<FolderResource>(rootNodes, departmentCell,
159                                             selectionModel, null);
160                         }
161                         else if (value instanceof FolderResource) {
162                         // Second level.
163                                 FolderDataProvider dataProvider = new FolderDataProvider(
164                             ((FolderResource) value).getUri());
165                         return new DefaultNodeInfo<FolderResource>(dataProvider, departmentCell,
166                             selectionModel, null);
167                         }
168                         // TODO Auto-generated method stub
169                         return null;
170                 }
171
172                 @Override
173                 public boolean isLeaf(Object value) {
174                         /*if(value instanceof FolderResource)
175                                 return ((FolderResource)value).getFolders().size()==0;*/
176                         return false;
177                 }
178                 
179         }
180         
181         class FolderDataProvider extends AsyncDataProvider<FolderResource>{
182                 private final String department;
183
184                   public FolderDataProvider(String department) {
185                     super(null);
186                     this.department = department;
187                   }
188
189                   /*@Override
190                   public void addDataDisplay(HasData<FolderResource> display) {
191                     super.addDataDisplay(display);
192
193                     // Request the count anytime a view is added.
194                     requestFactory.employeeRequest().countEmployeesByDepartment(department).fire(
195                         new Receiver<Long>() {
196                           @Override
197                           public void onSuccess(Long response) {
198                             updateRowCount(response.intValue(), true);
199                           }
200                         });
201                   }*/
202
203                   @Override
204                   protected void onRangeChanged(final HasData<FolderResource> view) {
205                     Range range = view.getVisibleRange();
206                     GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, department, null) {
207
208                                 @Override
209                                 public void onComplete() {
210                                         FolderResource rootResource = getResult();
211                                         
212                                         
213                                         if(rootResource.getFolders().size()!=0){
214                                                 updateRowCount(rootResource.getFolders().size(), true);
215                                                 updateRowData(0,rootResource.getFolders());
216                                         }
217                                         else{
218                                                 updateRowCount(rootResource.getFolders().size(), true);
219                                                 updateRowData(0,rootResource.getFolders());
220                                                 removeDataDisplay(view);
221                                         }
222                                 }
223
224                                 @Override
225                                 public void onError(Throwable t) {
226                                         GWT.log("Error fetching root folder", t);
227                                         GSS.get().displayError("Unable to fetch root folder");
228                                 }
229
230                         };
231                         DeferredCommand.addCommand(gf);
232                         
233                   }
234                 
235         }
236         
237         
238 }