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 if(value instanceof TrashResource)
267 return ((TrashResource)value).getFolders().size()==0;
272 class ResourceValueUpdater implements ValueUpdater<RestResource>{
275 public void update(final RestResource value) {
276 if(value instanceof MyFolderResource){
277 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
280 public void onComplete() {
281 FolderResource rootResource = getResult();
282 ((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
283 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
284 selectionModel.setSelected(value, true);
285 GWT.log("UPDATYING");
286 GSS.get().onResourceUpdate(value);
291 public void onError(Throwable t) {
292 GWT.log("Error fetching root folder", t);
293 GSS.get().displayError("Unable to fetch root folder");
297 DeferredCommand.addCommand(gf);
299 if(value instanceof TrashResource){
300 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
302 public void onComplete() {
303 //trash = getResult();
304 for(RestResource r : getRootNodes().getList()){
305 if(r instanceof TrashResource)
306 getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
308 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
312 public void onError(Throwable t) {
313 if(t instanceof RestException){
314 int statusCode = ((RestException)t).getHttpStatusCode();
315 // On IE status code 1223 may be returned instead of 204.
316 if(statusCode == 204 || statusCode == 1223){
317 //trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
321 GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
322 //GSS.get().getTreeView().getTrash() = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
332 class MyFolderDataProvider extends AsyncDataProvider<RestResource>{
333 private RestResource restResource;
334 private Class resourceClass;
335 public MyFolderDataProvider(RestResource department, Class resourceClass) {
336 super(new ProvidesKey<RestResource>() {
339 public Object getKey(RestResource item) {
340 return item.getUri();
342 this.restResource = department;
343 this.resourceClass=resourceClass;
344 //CellTreeView.this.mymap.put(department.getUri(), MyFolderDataProvider.this);
348 protected void onRangeChanged(final HasData<RestResource> view) {
353 * Retrieve the restResource.
355 * @return the restResource
357 public RestResource getRestResource() {
363 * Modify the restResource.
365 * @param restResource the restResource to set
367 public void setRestResource(RestResource restResource) {
368 this.restResource = restResource;
371 public void refresh(final RefreshHandler refresh){
372 GWT.log("******************************************");
373 GWT.log("[REFRESHING]:"+restResource.getUri());
374 GWT.log("******************************************");
375 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(), null) {
378 public void onComplete() {
379 if(restResource instanceof RestResourceWrapper)
380 ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
382 //if(CellTreeView.this.mymap.get(restResource.getUri())!=null)
383 //CellTreeView.this.mymap.get(restResource.getUri()).setRestResource(restResource);
384 String[] folderPaths = null;
385 if(resourceClass.equals(MyFolderResource.class))
386 folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
387 else if(resourceClass.equals(SharedFolderResource.class) && restResource instanceof SharedResource)
388 folderPaths=((SharedResource) restResource).getSubfolderPaths().toArray(new String[] {});
389 else if(resourceClass.equals(SharedFolderResource.class)){
390 folderPaths=((SharedFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
391 GWT.log("------------>"+folderPaths);
393 else if(resourceClass.equals(TrashFolderResource.class))
394 folderPaths=((TrashFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
395 else if(resourceClass.equals(OthersFolderResource.class) && restResource instanceof OtherUserResource)
396 folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
397 else if(resourceClass.equals(OthersFolderResource.class))
398 folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
399 MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
403 public void onComplete() {
404 List<RestResource> res = new ArrayList<RestResource>();
405 for(FolderResource r : getResult()){
409 else if(resourceClass.equals(MyFolderResource.class))
410 res.add(new MyFolderResource(r));
411 else if(resourceClass.equals(SharedFolderResource.class)){
412 GWT.log("ADDING:"+r.getUri());
413 res.add(new SharedFolderResource(r));
415 else if(resourceClass.equals(TrashFolderResource.class))
416 res.add(new TrashFolderResource(r));
417 else if(resourceClass.equals(OthersFolderResource.class))
418 res.add(new OthersFolderResource(r));
420 updateRowCount(res.size(), true);
421 updateRowData(0,res);
427 public void onError(Throwable t) {
428 GSS.get().displayError("Unable to fetch subfolders");
429 GWT.log("Unable to fetch subfolders", t);
433 public void onError(String p, Throwable throwable) {
434 GWT.log("Path:"+p, throwable);
438 DeferredCommand.addCommand(gf2);
443 public void onError(Throwable t) {
444 GWT.log("Error fetching root folder", t);
445 GSS.get().displayError("Unable to fetch root folder");
449 DeferredCommand.addCommand(gf);
454 class OthersDataProvider extends AsyncDataProvider<RestResource>{
455 private RestResource restResource;
456 private Class resourceClass;
457 public OthersDataProvider(RestResource department, Class resourceClass) {
458 super(new ProvidesKey<RestResource>() {
461 public Object getKey(RestResource item) {
462 return item.getUri();
464 this.restResource = department;
465 this.resourceClass=resourceClass;
466 //CellTreeView.this.mymap.put(department.getUri(), OthersDataProvider.this);
470 protected void onRangeChanged(final HasData<RestResource> view) {
475 * Retrieve the restResource.
477 * @return the restResource
479 public RestResource getRestResource() {
485 * Modify the restResource.
487 * @param restResource the restResource to set
489 public void setRestResource(RestResource restResource) {
490 this.restResource = restResource;
493 public void refresh(final RefreshHandler refresh){
494 GWT.log("******************************************");
495 GWT.log("[REFRESHING]:"+restResource.getUri());
496 GWT.log("******************************************");
497 GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
498 restResource.getUri(), null) {
501 public void onComplete() {
502 final OthersResource others = getResult();
503 MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
504 others.getOthers().toArray(new String[] {}), null) {
507 public void onComplete() {
508 List<OtherUserResource> res = getResult();
509 updateRowCount(res.size(), true);
510 List<RestResource> r = new ArrayList<RestResource>();
516 public void onError(Throwable t) {
517 GWT.log("Error fetching Others Root folder", t);
518 GSS.get().displayError("Unable to fetch Others Root folder");
522 public void onError(String p, Throwable throwable) {
523 GWT.log("Path:"+p, throwable);
526 DeferredCommand.addCommand(gogo);
530 public void onError(Throwable t) {
531 GWT.log("Error fetching Others Root folder", t);
532 GSS.get().displayError("Unable to fetch Others Root folder");
535 DeferredCommand.addCommand(go);
542 * Retrieve the rootNodes.
544 * @return the rootNodes
546 public ListDataProvider<RestResource> getRootNodes() {
552 * Retrieve the mymap.
556 public Map<String, MyFolderDataProvider> getMymap() {