Make use of GetUserCommand in the PropertiesCommand
[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.dnd.DnDTreeItem;
22 import gr.ebs.gss.client.rest.GetCommand;
23 import gr.ebs.gss.client.rest.MultipleGetCommand;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25 import gr.ebs.gss.client.rest.resource.FolderResource;
26 import gr.ebs.gss.client.rest.resource.UserResource;
27
28 import java.util.Arrays;
29 import java.util.List;
30
31 import com.google.gwt.cell.client.AbstractCell;
32 import com.google.gwt.cell.client.Cell;
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.event.dom.client.ContextMenuEvent;
35 import com.google.gwt.event.dom.client.ContextMenuHandler;
36 import com.google.gwt.resources.client.ClientBundle;
37 import com.google.gwt.resources.client.ImageResource;
38 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
39 import com.google.gwt.user.cellview.client.CellTree;
40 import com.google.gwt.user.cellview.client.TreeNode;
41 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
42 import com.google.gwt.user.client.DeferredCommand;
43 import com.google.gwt.user.client.Event;
44 import com.google.gwt.user.client.IncrementalCommand;
45 import com.google.gwt.user.client.ui.AbstractImagePrototype;
46 import com.google.gwt.user.client.ui.Composite;
47 import com.google.gwt.user.client.ui.Tree;
48 import com.google.gwt.user.client.ui.TreeItem;
49 import com.google.gwt.view.client.AsyncDataProvider;
50 import com.google.gwt.view.client.HasData;
51 import com.google.gwt.view.client.ListDataProvider;
52 import com.google.gwt.view.client.Range;
53 import com.google.gwt.view.client.SingleSelectionModel;
54 import com.google.gwt.view.client.TreeViewModel;
55 import com.google.gwt.view.client.TreeViewModel.DefaultNodeInfo;
56
57
58 /**
59  * @author kman
60  *
61  */
62 public class CellTreeView extends Composite{
63         public static final boolean DONE = false;
64         Images images;
65         private final ListDataProvider<FolderResource> rootNodes = new ListDataProvider<FolderResource>();
66         SingleSelectionModel<FolderResource> selectionModel = new SingleSelectionModel<FolderResource>();
67         FolderContextMenu menu;
68         /**
69          * Specifies the images that will be bundled for this Composite and other
70          * inherited images that will be included in the same bundle.
71          */
72         public interface Images extends ClientBundle, Tree.Resources, FolderContextMenu.Images {
73
74                 @Source("gr/ebs/gss/resources/folder_home.png")
75                 ImageResource home();
76
77                 @Source("gr/ebs/gss/resources/folder_yellow.png")
78                 ImageResource folderYellow();
79
80                 @Source("gr/ebs/gss/resources/mimetypes/document.png")
81                 ImageResource document();
82
83                 @Source("gr/ebs/gss/resources/internet.png")
84                 ImageResource othersShared();
85
86                 @Source("gr/ebs/gss/resources/edit_user.png")
87                 ImageResource myShared();
88
89                 @Source("gr/ebs/gss/resources/folder_user.png")
90                 ImageResource sharedFolder();
91
92                 @Source("gr/ebs/gss/resources/trashcan_empty.png")
93                 ImageResource trash();
94         }
95         final CellTree tree;
96         /**
97          * 
98          */
99         public CellTreeView(final Images _images) {
100                 images = _images;
101                 TreeViewModel model = new CustomTreeModel();
102
103             /*
104              * Create the tree using the model. We use <code>null</code> as the default
105              * value of the root node. The default value will be passed to
106              * CustomTreeModel#getNodeInfo();
107              */
108                 CellTree.Resources res = GWT.create(CellTree.BasicResources.class);
109             tree = new CellTree(model,null, res){
110                 @Override
111                 public void onBrowserEvent(Event event) {
112                         // TODO Auto-generated method stub
113                         super.onBrowserEvent(event);
114                         GWT.log(event.getType());
115                 }
116             };
117             tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
118             
119             sinkEvents(Event.ONCONTEXTMENU);
120                 sinkEvents(Event.ONMOUSEUP);
121             initWidget(tree);
122             DeferredCommand.addCommand(new IncrementalCommand() {
123
124                         @Override
125                         public boolean execute() {
126                                 return fetchRootFolders();
127                         }
128                 });
129         }
130         
131         protected void showPopup(final int x, final int y) {
132                 if (selectionModel.getSelectedObject() == null)
133                         return;
134                 if (menu != null)
135                         menu.hide();
136                 menu = new FolderContextMenu(images);
137                 menu.setPopupPosition(x, y);
138                 menu.show();
139         }
140         
141         public boolean fetchRootFolders() {
142                 UserResource userResource = GSS.get().getCurrentUserResource();
143                 if (userResource == null)
144                         return !DONE;
145
146                 final String path = userResource.getFilesPath();
147                 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, null) {
148
149                         @Override
150                         public void onComplete() {
151                                 FolderResource rootResource = getResult();
152                                 rootNodes.setList(Arrays.asList((FolderResource)rootResource));
153                                 tree.getRootTreeNode().setChildOpen(0, true);
154                                 
155                         }
156
157                         @Override
158                         public void onError(Throwable t) {
159                                 GWT.log("Error fetching root folder", t);
160                                 GSS.get().displayError("Unable to fetch root folder");
161                         }
162
163                 };
164                 DeferredCommand.addCommand(gf);
165                 return DONE;
166         }
167
168         public Images getImages() {
169                 return images;
170         }
171         
172         
173         class CustomTreeModel implements TreeViewModel{
174                 private final Cell<FolderResource> departmentCell = new AbstractCell<FolderResource>("contextmenu"){
175                         
176                         @Override
177                         public void render(com.google.gwt.cell.client.Cell.Context arg0, FolderResource arg1, SafeHtmlBuilder arg2) {
178                                 if(arg1.isShared())
179                                         arg2.appendHtmlConstant(AbstractImagePrototype.create(images.sharedFolder()).getHTML());
180                                 else if(arg1.getParentName()==null){
181                                         arg2.appendHtmlConstant(AbstractImagePrototype.create(images.home()).getHTML());
182                                 }
183                                 else
184                                         arg2.appendHtmlConstant(AbstractImagePrototype.create(images.folderYellow()).getHTML());
185                                 arg2.appendEscaped(arg1.getName());
186                                 
187                         }
188                         
189                         public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, FolderResource value, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<FolderResource> valueUpdater) {
190                                 GWT.log("-->"+event.getType());
191                                 if(event.getType().equals("contextmenu")){
192                                         selectionModel.setSelected(value, true);
193                                         showPopup(event.getClientX(), event.getClientY());
194                                 }
195                         };
196                         
197                 };
198                 
199                 
200                 @Override
201                 public <T> NodeInfo<?> getNodeInfo(T value) {
202                         
203                         if(value==null){
204                                 return new DefaultNodeInfo<FolderResource>(rootNodes, departmentCell,
205                                             selectionModel, null);
206                         }
207                         else if (value instanceof FolderResource) {
208                         // Second level.
209                                 FolderDataProvider dataProvider = new FolderDataProvider(
210                             ((FolderResource) value).getUri());
211                         return new DefaultNodeInfo<FolderResource>(dataProvider, departmentCell,
212                             selectionModel, null);
213                         }
214                         // TODO Auto-generated method stub
215                         return null;
216                 }
217
218                 @Override
219                 public boolean isLeaf(Object value) {
220                         if(value instanceof FolderResource)
221                                 return ((FolderResource)value).getFolders().size()==0;
222                         return false;
223                 }
224                 
225         }
226         
227         class FolderDataProvider extends AsyncDataProvider<FolderResource>{
228                 private final String department;
229
230                   public FolderDataProvider(String department) {
231                     super(null);
232                     this.department = department;
233                   }
234
235                   /*@Override
236                   public void addDataDisplay(HasData<FolderResource> display) {
237                     super.addDataDisplay(display);
238
239                     // Request the count anytime a view is added.
240                     requestFactory.employeeRequest().countEmployeesByDepartment(department).fire(
241                         new Receiver<Long>() {
242                           @Override
243                           public void onSuccess(Long response) {
244                             updateRowCount(response.intValue(), true);
245                           }
246                         });
247                   }*/
248
249                   @Override
250                   protected void onRangeChanged(final HasData<FolderResource> view) {
251                     Range range = view.getVisibleRange();
252                     GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, department, null) {
253
254                                 @Override
255                                 public void onComplete() {
256                                         FolderResource rootResource = getResult();
257                                         MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
258                                                                 rootResource.getSubfolderPaths().toArray(new String[] {}), null) {
259
260                                                 @Override
261                                                 public void onComplete() {
262                                                         List<FolderResource> res = getResult();
263                                                         updateRowCount(res.size(), true);
264                                                         updateRowData(0,res);
265                                                 }
266
267                                                 @Override
268                                                 public void onError(Throwable t) {
269                                                         GSS.get().displayError("Unable to fetch subfolders");
270                                                         GWT.log("Unable to fetch subfolders", t);
271                                                 }
272
273                                                 @Override
274                                                 public void onError(String p, Throwable throwable) {
275                                                         GWT.log("Path:"+p, throwable);
276                                                 }
277
278                                         };
279                                         DeferredCommand.addCommand(gf2);
280                                         
281                                 }
282
283                                 @Override
284                                 public void onError(Throwable t) {
285                                         GWT.log("Error fetching root folder", t);
286                                         GSS.get().displayError("Unable to fetch root folder");
287                                 }
288
289                         };
290                         DeferredCommand.addCommand(gf);
291                     
292                         
293                   }
294                 
295         }
296         
297         
298 }