2 * Copyright 2011 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client;
21 import gr.ebs.gss.client.CellTreeView.Images;
22 import gr.ebs.gss.client.CellTreeView.RefreshHandler;
23 import gr.ebs.gss.client.rest.GetCommand;
24 import gr.ebs.gss.client.rest.MultipleGetCommand;
25 import gr.ebs.gss.client.rest.RestException;
26 import gr.ebs.gss.client.rest.resource.FolderResource;
27 import gr.ebs.gss.client.rest.resource.MyFolderResource;
28 import gr.ebs.gss.client.rest.resource.OtherUserResource;
29 import gr.ebs.gss.client.rest.resource.OthersFolderResource;
30 import gr.ebs.gss.client.rest.resource.OthersResource;
31 import gr.ebs.gss.client.rest.resource.RestResource;
32 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
33 import gr.ebs.gss.client.rest.resource.SharedFolderResource;
34 import gr.ebs.gss.client.rest.resource.SharedResource;
35 import gr.ebs.gss.client.rest.resource.TrashFolderResource;
36 import gr.ebs.gss.client.rest.resource.TrashResource;
37 import gwtquery.plugins.droppable.client.DroppableOptions;
38 import gwtquery.plugins.droppable.client.DroppableOptions.DroppableFunction;
39 import gwtquery.plugins.droppable.client.events.DragAndDropContext;
40 import gwtquery.plugins.droppable.client.gwt.DragAndDropNodeInfo;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
47 import com.google.gwt.cell.client.AbstractCell;
48 import com.google.gwt.cell.client.Cell;
49 import com.google.gwt.cell.client.ValueUpdater;
50 import com.google.gwt.core.client.GWT;
51 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
52 import com.google.gwt.user.client.DeferredCommand;
53 import com.google.gwt.user.client.ui.AbstractImagePrototype;
54 import com.google.gwt.view.client.AsyncDataProvider;
55 import com.google.gwt.view.client.HasData;
56 import com.google.gwt.view.client.ListDataProvider;
57 import com.google.gwt.view.client.ProvidesKey;
58 import com.google.gwt.view.client.SingleSelectionModel;
59 import com.google.gwt.view.client.TreeViewModel;
67 public class CellTreeViewModel implements TreeViewModel{
68 private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
70 SingleSelectionModel<RestResource> selectionModel;
71 Map<String, MyFolderDataProvider> mymap = new HashMap<String, MyFolderDataProvider>();
75 public CellTreeViewModel(final Images _images,SingleSelectionModel<RestResource> selectionModel ) {
78 this.selectionModel=selectionModel;
81 private final Cell<RestResource> departmentCell = new AbstractCell<RestResource>("contextmenu"){
84 public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
87 if(arg1 instanceof TrashFolderResource){
88 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
89 FolderResource res = ((RestResourceWrapper)arg1).getResource();
92 else if(arg1 instanceof RestResourceWrapper){
93 FolderResource res = ((RestResourceWrapper)arg1).getResource();
95 html = AbstractImagePrototype.create(images.sharedFolder()).getHTML();
96 else if(res.getParentName()==null){
97 html = AbstractImagePrototype.create(images.home()).getHTML();
100 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
101 name = res.getName();
104 else if(arg1 instanceof TrashResource){
105 html = AbstractImagePrototype.create(images.trash()).getHTML();
109 else if(arg1 instanceof SharedResource){
110 html = AbstractImagePrototype.create(images.myShared()).getHTML();
113 else if(arg1 instanceof OthersResource){
114 html = AbstractImagePrototype.create(images.othersShared()).getHTML();
115 name = "Other's Shared";
117 else if(arg1 instanceof OtherUserResource){
118 html = AbstractImagePrototype.create(images.permUser()).getHTML();
119 name = ((OtherUserResource)arg1).getName();
121 arg2.appendHtmlConstant(html);
122 arg2.appendHtmlConstant("<span class='papala'>");
123 arg2.appendEscaped(name);
124 arg2.appendHtmlConstant("</span>");
127 public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, RestResource value, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<RestResource> valueUpdater) {
128 if(event.getType().equals("contextmenu")){
129 selectionModel.setSelected(value, true);
130 GSS.get().getTreeView().showPopup(event.getClientX(), event.getClientY());
138 public <T> NodeInfo<?> getNodeInfo(T value) {
141 return new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
142 selectionModel, null);
144 else if (value instanceof MyFolderResource) {
146 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
147 ((MyFolderResource) value),MyFolderResource.class);
148 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
149 selectionModel, new ResourceValueUpdater());
150 //nodeInfos.put(((MyFolderResource) value).getUri(), n);
151 mymap.put(((MyFolderResource) value).getUri(), dataProvider);
152 DroppableOptions options = n.getDroppableOptions();
153 options.setDroppableHoverClass("droppableHover");
154 // use a DroppableFunction here. We can also add a DropHandler in the tree
156 options.setOnDrop(new DroppableFunction() {
158 public void f(DragAndDropContext context) {
163 // permission cell are not draggable
164 n.setCellDroppableOnly();
167 else if (value instanceof SharedResource) {
169 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
170 ((SharedResource) value), SharedFolderResource.class);
171 return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
172 selectionModel, new ResourceValueUpdater());
174 else if (value instanceof TrashResource) {
176 ListDataProvider<RestResource> trashProvider = new ListDataProvider<RestResource>();
177 List<RestResource> r = new ArrayList<RestResource>();
178 for(FolderResource f : GSS.get().getTreeView().getTrash().getFolders()){
179 r.add(new TrashFolderResource(f));
181 trashProvider.setList(r);
182 return new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
183 selectionModel, new ResourceValueUpdater());
185 else if (value instanceof OthersResource) {
187 OthersDataProvider dataProvider = new OthersDataProvider(
188 ((OthersResource) value), SharedFolderResource.class);
189 return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
190 selectionModel, null);
192 else if (value instanceof SharedFolderResource) {
194 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
195 ((SharedFolderResource) value),SharedFolderResource.class);
196 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
197 selectionModel, new ResourceValueUpdater());
198 //nodeInfos.put(((SharedFolderResource) value).getUri(), n);
199 DroppableOptions options = n.getDroppableOptions();
200 options.setDroppableHoverClass("droppableHover");
201 // use a DroppableFunction here. We can also add a DropHandler in the tree
203 options.setOnDrop(new DroppableFunction() {
205 public void f(DragAndDropContext context) {
210 // permission cell are not draggable
211 n.setCellDroppableOnly();
214 else if (value instanceof OthersFolderResource) {
216 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
217 ((OthersFolderResource) value),OthersFolderResource.class);
218 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
219 selectionModel, new ResourceValueUpdater());
220 //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
221 DroppableOptions options = n.getDroppableOptions();
222 options.setDroppableHoverClass("droppableHover");
223 // use a DroppableFunction here. We can also add a DropHandler in the tree
225 options.setOnDrop(new DroppableFunction() {
227 public void f(DragAndDropContext context) {
232 // permission cell are not draggable
233 n.setCellDroppableOnly();
236 else if (value instanceof OtherUserResource) {
238 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
239 ((OtherUserResource) value),OthersFolderResource.class);
240 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
241 selectionModel, new ResourceValueUpdater());
242 //nodeInfos.put(((OtherUserResource) value).getUri(), n);
243 DroppableOptions options = n.getDroppableOptions();
244 options.setDroppableHoverClass("droppableHover");
245 // use a DroppableFunction here. We can also add a DropHandler in the tree
247 options.setOnDrop(new DroppableFunction() {
249 public void f(DragAndDropContext context) {
254 // permission cell are not draggable
255 n.setCellDroppableOnly();
258 // TODO Auto-generated method stub
263 public boolean isLeaf(Object value) {
264 if(value instanceof RestResourceWrapper)
265 return ((RestResourceWrapper)value).getResource().getFolders().size()==0;
266 else if(value instanceof TrashResource)
267 return ((TrashResource)value).getFolders().size()==0;
268 else if(value instanceof SharedResource)
269 return ((SharedResource)value).getFolders().size()==0;
270 else if(value instanceof OthersResource)
271 return ((OthersResource)value).getOtherUsers().size()==0;
272 else if(value instanceof OtherUserResource)
273 return ((OtherUserResource)value).getFolders().size()==0;
277 class ResourceValueUpdater implements ValueUpdater<RestResource>{
280 public void update(final RestResource value) {
281 if(value instanceof MyFolderResource){
282 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
285 public void onComplete() {
286 FolderResource rootResource = getResult();
287 ((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
288 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
289 selectionModel.setSelected(value, true);
290 GWT.log("UPDATYING");
291 GSS.get().onResourceUpdate(value);
296 public void onError(Throwable t) {
297 GWT.log("Error fetching root folder", t);
298 GSS.get().displayError("Unable to fetch root folder");
302 DeferredCommand.addCommand(gf);
304 if(value instanceof TrashResource){
305 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
307 public void onComplete() {
308 //trash = getResult();
309 for(RestResource r : getRootNodes().getList()){
310 if(r instanceof TrashResource)
311 getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
313 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
317 public void onError(Throwable t) {
318 if(t instanceof RestException){
319 int statusCode = ((RestException)t).getHttpStatusCode();
320 // On IE status code 1223 may be returned instead of 204.
321 if(statusCode == 204 || statusCode == 1223){
322 //trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
326 GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
327 //GSS.get().getTreeView().getTrash() = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
337 class MyFolderDataProvider extends AsyncDataProvider<RestResource>{
338 private RestResource restResource;
339 private Class resourceClass;
340 public MyFolderDataProvider(RestResource department, Class resourceClass) {
341 super(new ProvidesKey<RestResource>() {
344 public Object getKey(RestResource item) {
345 return item.getUri();
347 this.restResource = department;
348 this.resourceClass=resourceClass;
349 //CellTreeView.this.mymap.put(department.getUri(), MyFolderDataProvider.this);
353 protected void onRangeChanged(final HasData<RestResource> view) {
358 * Retrieve the restResource.
360 * @return the restResource
362 public RestResource getRestResource() {
368 * Modify the restResource.
370 * @param restResource the restResource to set
372 public void setRestResource(RestResource restResource) {
373 this.restResource = restResource;
376 public void refresh(final RefreshHandler refresh){
377 GWT.log("******************************************");
378 GWT.log("[REFRESHING]:"+restResource.getUri());
379 GWT.log("******************************************");
380 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(), null) {
383 public void onComplete() {
384 if(restResource instanceof RestResourceWrapper)
385 ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
387 //if(CellTreeView.this.mymap.get(restResource.getUri())!=null)
388 //CellTreeView.this.mymap.get(restResource.getUri()).setRestResource(restResource);
389 String[] folderPaths = null;
390 if(resourceClass.equals(MyFolderResource.class))
391 folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
392 else if(resourceClass.equals(SharedFolderResource.class) && restResource instanceof SharedResource)
393 folderPaths=((SharedResource) restResource).getSubfolderPaths().toArray(new String[] {});
394 else if(resourceClass.equals(SharedFolderResource.class)){
395 folderPaths=((SharedFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
396 GWT.log("------------>"+folderPaths);
398 else if(resourceClass.equals(TrashFolderResource.class))
399 folderPaths=((TrashFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
400 else if(resourceClass.equals(OthersFolderResource.class) && restResource instanceof OtherUserResource)
401 folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
402 else if(resourceClass.equals(OthersFolderResource.class))
403 folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
404 MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
408 public void onComplete() {
409 List<RestResource> res = new ArrayList<RestResource>();
410 for(FolderResource r : getResult()){
414 else if(resourceClass.equals(MyFolderResource.class))
415 res.add(new MyFolderResource(r));
416 else if(resourceClass.equals(SharedFolderResource.class)){
417 GWT.log("ADDING:"+r.getUri());
418 res.add(new SharedFolderResource(r));
420 else if(resourceClass.equals(TrashFolderResource.class))
421 res.add(new TrashFolderResource(r));
422 else if(resourceClass.equals(OthersFolderResource.class))
423 res.add(new OthersFolderResource(r));
425 updateRowCount(res.size(), true);
426 updateRowData(0,res);
432 public void onError(Throwable t) {
433 GSS.get().displayError("Unable to fetch subfolders");
434 GWT.log("Unable to fetch subfolders", t);
438 public void onError(String p, Throwable throwable) {
439 GWT.log("Path:"+p, throwable);
443 DeferredCommand.addCommand(gf2);
448 public void onError(Throwable t) {
449 GWT.log("Error fetching root folder", t);
450 GSS.get().displayError("Unable to fetch root folder");
454 DeferredCommand.addCommand(gf);
459 class OthersDataProvider extends AsyncDataProvider<RestResource>{
460 private RestResource restResource;
461 private Class resourceClass;
462 public OthersDataProvider(RestResource department, Class resourceClass) {
463 super(new ProvidesKey<RestResource>() {
466 public Object getKey(RestResource item) {
467 return item.getUri();
469 this.restResource = department;
470 this.resourceClass=resourceClass;
471 //CellTreeView.this.mymap.put(department.getUri(), OthersDataProvider.this);
475 protected void onRangeChanged(final HasData<RestResource> view) {
480 * Retrieve the restResource.
482 * @return the restResource
484 public RestResource getRestResource() {
490 * Modify the restResource.
492 * @param restResource the restResource to set
494 public void setRestResource(RestResource restResource) {
495 this.restResource = restResource;
498 public void refresh(final RefreshHandler refresh){
499 GWT.log("******************************************");
500 GWT.log("[REFRESHING]:"+restResource.getUri());
501 GWT.log("******************************************");
502 GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
503 restResource.getUri(), null) {
506 public void onComplete() {
507 final OthersResource others = getResult();
508 MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
509 others.getOthers().toArray(new String[] {}), null) {
512 public void onComplete() {
513 List<OtherUserResource> res = getResult();
514 updateRowCount(res.size(), true);
515 List<RestResource> r = new ArrayList<RestResource>();
521 public void onError(Throwable t) {
522 GWT.log("Error fetching Others Root folder", t);
523 GSS.get().displayError("Unable to fetch Others Root folder");
527 public void onError(String p, Throwable throwable) {
528 GWT.log("Path:"+p, throwable);
531 DeferredCommand.addCommand(gogo);
535 public void onError(Throwable t) {
536 GWT.log("Error fetching Others Root folder", t);
537 GSS.get().displayError("Unable to fetch Others Root folder");
540 DeferredCommand.addCommand(go);
547 * Retrieve the rootNodes.
549 * @return the rootNodes
551 public ListDataProvider<RestResource> getRootNodes() {
557 * Retrieve the mymap.
561 public Map<String, MyFolderDataProvider> getMymap() {