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 static com.google.gwt.query.client.GQuery.$;
22 import gr.ebs.gss.client.CellTreeView.Images;
23 import gr.ebs.gss.client.CellTreeView.RefreshHandler;
24 import gr.ebs.gss.client.rest.GetCommand;
25 import gr.ebs.gss.client.rest.MultipleGetCommand;
26 import gr.ebs.gss.client.rest.RestException;
27 import gr.ebs.gss.client.rest.resource.FileResource;
28 import gr.ebs.gss.client.rest.resource.FolderResource;
29 import gr.ebs.gss.client.rest.resource.MyFolderResource;
30 import gr.ebs.gss.client.rest.resource.OtherUserResource;
31 import gr.ebs.gss.client.rest.resource.OthersFolderResource;
32 import gr.ebs.gss.client.rest.resource.OthersResource;
33 import gr.ebs.gss.client.rest.resource.RestResource;
34 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
35 import gr.ebs.gss.client.rest.resource.SharedFolderResource;
36 import gr.ebs.gss.client.rest.resource.SharedResource;
37 import gr.ebs.gss.client.rest.resource.TrashFolderResource;
38 import gr.ebs.gss.client.rest.resource.TrashResource;
39 import gwtquery.plugins.draggable.client.DragAndDropManager;
40 import gwtquery.plugins.draggable.client.DraggableOptions;
41 import gwtquery.plugins.draggable.client.StopDragException;
42 import gwtquery.plugins.draggable.client.DraggableOptions.CursorAt;
43 import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
44 import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
45 import gwtquery.plugins.draggable.client.events.DragContext;
46 import gwtquery.plugins.droppable.client.DroppableOptions;
47 import gwtquery.plugins.droppable.client.DroppableOptions.DroppableFunction;
48 import gwtquery.plugins.droppable.client.events.DragAndDropContext;
49 import gwtquery.plugins.droppable.client.gwt.DragAndDropNodeInfo;
51 import java.util.ArrayList;
52 import java.util.Arrays;
53 import java.util.HashMap;
54 import java.util.List;
57 import com.google.gwt.cell.client.AbstractCell;
58 import com.google.gwt.cell.client.Cell;
59 import com.google.gwt.cell.client.ValueUpdater;
60 import com.google.gwt.core.client.GWT;
61 import com.google.gwt.dom.client.Style.Cursor;
62 import com.google.gwt.query.client.plugins.GQueryUi;
63 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
64 import com.google.gwt.safehtml.shared.SafeHtml;
65 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
66 import com.google.gwt.user.client.DeferredCommand;
67 import com.google.gwt.user.client.Window;
68 import com.google.gwt.user.client.ui.AbstractImagePrototype;
69 import com.google.gwt.view.client.AsyncDataProvider;
70 import com.google.gwt.view.client.HasData;
71 import com.google.gwt.view.client.ListDataProvider;
72 import com.google.gwt.view.client.ProvidesKey;
73 import com.google.gwt.view.client.SingleSelectionModel;
74 import com.google.gwt.view.client.TreeViewModel;
82 public class CellTreeViewModel implements TreeViewModel{
84 private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
85 private Map<String,FolderResource> folderCache=new HashMap<String, FolderResource>();
87 SingleSelectionModel<RestResource> selectionModel;
88 Map<String, MyFolderDataProvider> mymap = new HashMap<String, MyFolderDataProvider>();
89 Map<String, MyFolderDataProvider> sharedmap = new HashMap<String, MyFolderDataProvider>();
90 Map<String, MyFolderDataProvider> othersmap = new HashMap<String, MyFolderDataProvider>();
91 static interface Templates extends SafeHtmlTemplates {
92 Templates INSTANCE = GWT.create(Templates.class);
94 @Template(" <div id='dragHelper' class='{0}'></div>")
95 SafeHtml outerHelper(String cssClassName);
98 static void configureDragOperation(final DraggableOptions options) {
100 // set a custom element as drag helper. The content of the helper will be
101 // set when the drag will start
102 options.setHelper($(Templates.INSTANCE.outerHelper(
103 "drag").asString()));
104 // opacity of the drag helper
105 options.setOpacity((float) 0.9);
106 // cursor during the drag operation
107 options.setCursor(Cursor.MOVE);
108 // the cell being greater than the helper, force the position of the
109 // helper on the mouse cursor.
110 options.setCursorAt(new CursorAt(10, 10, null, null));
111 // append the helper to the body element
112 options.setAppendTo("body");
113 // set the revert option
114 options.setRevert(RevertOption.ON_INVALID_DROP);
115 options.setOnBeforeDragStart(new DragFunction() {
118 public void f(DragContext context) {
119 RestResource value = context.getDraggableData();
120 if(value instanceof TrashResource || value instanceof SharedResource || value instanceof OthersResource || value instanceof OtherUserResource){
121 throw new StopDragException();
126 // use a Function to fill the content of the helper
127 // we could also add a DragStartEventHandler on the DragAndDropTreeCell and
128 // DragAndDropCellList.
130 options.setOnDragStart(new DragFunction() {
131 public void f(DragContext context) {
132 RestResourceWrapper memberInfo = context.getDraggableData();
133 context.getHelper().setInnerHTML(memberInfo.getName());
142 public CellTreeViewModel(final Images _images,SingleSelectionModel<RestResource> selectionModel ) {
145 this.selectionModel=selectionModel;
148 private final Cell<RestResource> departmentCell = new AbstractCell<RestResource>("contextmenu"){
151 public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
155 if(arg1 instanceof TrashFolderResource){
156 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
157 FolderResource res = ((RestResourceWrapper)arg1).getResource();
158 name = res.getName();
159 id = res.getParentName() +"."+name;
161 else if(arg1 instanceof RestResourceWrapper){
162 FolderResource res = ((RestResourceWrapper)arg1).getResource();
164 html = AbstractImagePrototype.create(images.sharedFolder()).getHTML();
165 else if(res.getParentName()==null){
166 html = AbstractImagePrototype.create(images.home()).getHTML();
169 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
170 name = res.getName();
171 if(res.getParentName() != null){
172 id = res.getParentName()+"."+name;
178 else if(arg1 instanceof TrashResource){
179 html = AbstractImagePrototype.create(images.trash()).getHTML();
184 else if(arg1 instanceof SharedResource){
185 html = AbstractImagePrototype.create(images.myShared()).getHTML();
189 else if(arg1 instanceof OthersResource){
190 html = AbstractImagePrototype.create(images.othersShared()).getHTML();
191 name = "Other's Shared";
194 else if(arg1 instanceof OtherUserResource){
195 html = AbstractImagePrototype.create(images.permUser()).getHTML();
196 name = ((OtherUserResource)arg1).getName();
199 arg2.appendHtmlConstant(html);
200 arg2.appendHtmlConstant("<span id='"+id +"'"+ " class='papala'>");
201 arg2.appendEscaped(name);
202 arg2.appendHtmlConstant("</span>");
205 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) {
206 if(event.getType().equals("contextmenu")){
207 selectionModel.setSelected(value, true);
208 GSS.get().getTreeView().showPopup(event.getClientX(), event.getClientY());
216 public <T> NodeInfo<?> getNodeInfo(final T value) {
219 DragAndDropNodeInfo n = new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
220 selectionModel, null);
221 configureFolderDrop(n);
222 configureDragOperation(n.getDraggableOptions());
225 else if (value instanceof MyFolderResource) {
227 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
228 ((MyFolderResource) value),MyFolderResource.class);
229 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
230 selectionModel, new ResourceValueUpdater());
232 //nodeInfos.put(((MyFolderResource) value).getUri(), n);
233 mymap.put(((MyFolderResource) value).getUri(), dataProvider);
235 // permission cell are not draggable
236 //n.setCellDroppableOnly();
237 configureFolderDrop(n);
238 configureDragOperation(n.getDraggableOptions());
242 else if (value instanceof SharedResource) {
244 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
245 ((SharedResource) value), SharedFolderResource.class);
246 sharedmap.put(((SharedResource) value).getUri(), dataProvider);
247 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
248 selectionModel, new ResourceValueUpdater());
249 configureFolderDrop(n);
250 configureDragOperation(n.getDraggableOptions());
253 else if (value instanceof TrashResource) {
255 ListDataProvider<RestResource> trashProvider = new ListDataProvider<RestResource>();
256 List<RestResource> r = new ArrayList<RestResource>();
257 for(FolderResource f : GSS.get().getTreeView().getTrash().getFolders()){
258 r.add(new TrashFolderResource(f));
260 trashProvider.setList(r);
261 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
262 selectionModel, new ResourceValueUpdater());
263 configureFolderDrop(n);
264 configureDragOperation(n.getDraggableOptions());
267 else if (value instanceof OthersResource) {
269 OthersDataProvider dataProvider = new OthersDataProvider(
270 ((OthersResource) value), SharedFolderResource.class);
271 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
272 selectionModel, null);
273 configureFolderDrop(n);
274 configureDragOperation(n.getDraggableOptions());
277 else if (value instanceof SharedFolderResource) {
279 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
280 ((SharedFolderResource) value),SharedFolderResource.class);
281 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
282 selectionModel, new ResourceValueUpdater());
283 sharedmap.put(((SharedFolderResource) value).getUri(), dataProvider);
284 configureFolderDrop(n);
285 configureDragOperation(n.getDraggableOptions());
288 else if (value instanceof OthersFolderResource) {
290 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
291 ((OthersFolderResource) value),OthersFolderResource.class);
292 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
293 selectionModel, new ResourceValueUpdater());
294 //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
295 othersmap.put(((SharedFolderResource) value).getUri(), dataProvider);
296 configureFolderDrop(n);
297 configureDragOperation(n.getDraggableOptions());
300 else if (value instanceof OtherUserResource) {
302 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
303 ((OtherUserResource) value),OthersFolderResource.class);
304 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
305 selectionModel, new ResourceValueUpdater());
306 configureFolderDrop(n);
307 configureDragOperation(n.getDraggableOptions());
310 // TODO Auto-generated method stub
314 private void configureFolderDrop(DragAndDropNodeInfo<RestResource> n){
315 DroppableOptions options = n.getDroppableOptions();
316 options.setDroppableHoverClass("droppableHover");
317 // use a DroppableFunction here. We can also add a DropHandler in the tree
320 options.setOnOver(new DroppableFunction() {
323 public void f(final DragAndDropContext context) {
324 if(context.getDroppableData()!=null && context.getDroppableData() instanceof RestResource){
326 GSS.get().getTreeView().getUtils().openNodeContainingResource((RestResource) context.getDroppableData(), new RefreshHandler() {
329 public void onRefresh() {
331 DragAndDropManager.getInstance().update(context);//initialize(context, GQueryUi.Event.create(com.google.gwt.user.client.Event.getCurrentEvent()));
342 options.setOnDrop(new DroppableFunction() {
344 public void f(DragAndDropContext context) {
346 DnDFolderPopupMenu popup ;
347 if(context.getDraggableData() instanceof FileResource){
348 popup = new DnDFolderPopupMenu(images, ((MyFolderResource) context.getDroppableData()).getResource(), Arrays.asList(context.getDraggableData()));
351 popup = new DnDFolderPopupMenu(images, ((MyFolderResource) context.getDroppableData()).getResource(), context.getDraggableData());
352 int left = context.getDroppable().getAbsoluteLeft() + 40;
353 int top = context.getDroppable().getAbsoluteTop() + 20;
354 popup.setPopupPosition(left, top);
362 public boolean isLeaf(Object value) {
363 if(value instanceof RestResourceWrapper)
364 return ((RestResourceWrapper)value).getResource().getFolders().size()==0;
365 else if(value instanceof TrashResource)
366 return ((TrashResource)value).getFolders().size()==0;
367 else if(value instanceof SharedResource)
368 return ((SharedResource)value).getFolders().size()==0;
369 else if(value instanceof OthersResource)
370 return ((OthersResource)value).getOtherUsers().size()==0;
371 else if(value instanceof OtherUserResource)
372 return ((OtherUserResource)value).getFolders().size()==0;
376 class ResourceValueUpdater implements ValueUpdater<RestResource>{
379 public void update(final RestResource value) {
380 if(value instanceof MyFolderResource){
381 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
384 public void onComplete() {
385 FolderResource rootResource = getResult();
386 //((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
387 ((MyFolderResource)value).setResource(rootResource);
388 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
389 selectionModel.setSelected(value, true);
390 GSS.get().onResourceUpdate(value);
395 public void onError(Throwable t) {
396 GWT.log("Error fetching root folder", t);
397 GSS.get().displayError("Unable to fetch root folder");
401 DeferredCommand.addCommand(gf);
403 else if(value instanceof TrashResource){
404 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
406 public void onComplete() {
407 //trash = getResult();
408 for(RestResource r : getRootNodes().getList()){
409 if(r instanceof TrashResource)
410 getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
412 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
416 public void onError(Throwable t) {
417 if(t instanceof RestException){
418 int statusCode = ((RestException)t).getHttpStatusCode();
419 // On IE status code 1223 may be returned instead of 204.
420 if(statusCode == 204 || statusCode == 1223){
421 //trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
425 GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
426 //GSS.get().getTreeView().getTrash() = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
432 else if(value instanceof OthersFolderResource){
439 class MyFolderDataProvider extends AsyncDataProvider<RestResource>{
440 private RestResource restResource;
441 private Class resourceClass;
442 public MyFolderDataProvider(RestResource department, Class resourceClass) {
443 super(new ProvidesKey<RestResource>() {
446 public Object getKey(RestResource item) {
447 return item.getUri();
449 this.restResource = department;
450 this.resourceClass=resourceClass;
451 //getMymap().put(department.getUri(), MyFolderDataProvider.this);
455 protected void onRangeChanged(final HasData<RestResource> view) {
460 * Retrieve the restResource.
462 * @return the restResource
464 public RestResource getRestResource() {
470 * Modify the restResource.
472 * @param restResource the restResource to set
474 public void setRestResource(RestResource restResource) {
475 this.restResource = restResource;
477 List<RestResource> res =null;
478 public void refresh(final RefreshHandler refresh){
479 FolderResource cache = null;
480 if(restResource instanceof RestResourceWrapper && !((RestResourceWrapper)restResource).getResource().isNeedsExpanding())
481 cache = ((RestResourceWrapper)restResource).getResource();
482 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(),cache ) {
485 public void onComplete() {
486 if(restResource instanceof RestResourceWrapper){
487 ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
488 ((RestResourceWrapper)restResource).getResource().setNeedsExpanding(false);
490 if(usedCachedVersion()&&res!=null){
492 updateRowCount(res.size(), true);
493 updateRowData(0,res);
496 String[] folderPaths = null;
497 if(resourceClass.equals(MyFolderResource.class))
498 folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
499 else if(resourceClass.equals(SharedFolderResource.class) && restResource instanceof SharedResource)
500 folderPaths=((SharedResource) restResource).getSubfolderPaths().toArray(new String[] {});
501 else if(resourceClass.equals(SharedFolderResource.class)){
502 folderPaths=((SharedFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
503 GWT.log("------------>"+folderPaths);
505 else if(resourceClass.equals(TrashFolderResource.class))
506 folderPaths=((TrashFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
507 else if(resourceClass.equals(OthersFolderResource.class) && restResource instanceof OtherUserResource)
508 folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
509 else if(resourceClass.equals(OthersFolderResource.class))
510 folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
511 MultipleGetCommand.Cached[] cached = null;
512 if(restResource instanceof RestResourceWrapper)
513 cached=((RestResourceWrapper)restResource).getResource().getCache();
514 MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
515 folderPaths, cached) {
518 public void onComplete() {
519 res = new ArrayList<RestResource>();
520 for(FolderResource r : getResult()){
524 else if(resourceClass.equals(MyFolderResource.class))
525 res.add(new MyFolderResource(r));
526 else if(resourceClass.equals(SharedFolderResource.class)){
527 res.add(new SharedFolderResource(r));
529 else if(resourceClass.equals(TrashFolderResource.class))
530 res.add(new TrashFolderResource(r));
531 else if(resourceClass.equals(OthersFolderResource.class))
532 res.add(new OthersFolderResource(r));
534 if(restResource instanceof RestResourceWrapper)
535 ((RestResourceWrapper)restResource).getResource().setFolders(getResult());
536 updateRowCount(res.size(), true);
537 updateRowData(0,res);
543 public void onError(Throwable t) {
544 GSS.get().displayError("Unable to fetch subfolders");
545 GWT.log("Unable to fetch subfolders", t);
549 public void onError(String p, Throwable throwable) {
550 GWT.log("Path:"+p, throwable);
554 DeferredCommand.addCommand(gf2);
559 public void onError(Throwable t) {
561 GWT.log("Error fetching root folder", t);
562 GSS.get().displayError("Unable to fetch root folder");
566 DeferredCommand.addCommand(gf);
571 class OthersDataProvider extends AsyncDataProvider<RestResource>{
572 private RestResource restResource;
573 private Class resourceClass;
574 public OthersDataProvider(RestResource department, Class resourceClass) {
575 super(new ProvidesKey<RestResource>() {
578 public Object getKey(RestResource item) {
579 return item.getUri();
581 this.restResource = department;
582 this.resourceClass=resourceClass;
583 //CellTreeView.this.mymap.put(department.getUri(), OthersDataProvider.this);
587 protected void onRangeChanged(final HasData<RestResource> view) {
592 * Retrieve the restResource.
594 * @return the restResource
596 public RestResource getRestResource() {
602 * Modify the restResource.
604 * @param restResource the restResource to set
606 public void setRestResource(RestResource restResource) {
607 this.restResource = restResource;
610 public void refresh(final RefreshHandler refresh){
611 GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
612 restResource.getUri(), null) {
615 public void onComplete() {
616 final OthersResource others = getResult();
617 MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
618 others.getOthers().toArray(new String[] {}), null) {
621 public void onComplete() {
622 List<OtherUserResource> res = getResult();
623 updateRowCount(res.size(), true);
624 List<RestResource> r = new ArrayList<RestResource>();
630 public void onError(Throwable t) {
631 GWT.log("Error fetching Others Root folder", t);
632 GSS.get().displayError("Unable to fetch Others Root folder");
636 public void onError(String p, Throwable throwable) {
637 GWT.log("Path:"+p, throwable);
640 DeferredCommand.addCommand(gogo);
644 public void onError(Throwable t) {
645 GWT.log("Error fetching Others Root folder", t);
646 GSS.get().displayError("Unable to fetch Others Root folder");
649 DeferredCommand.addCommand(go);
656 * Retrieve the rootNodes.
658 * @return the rootNodes
660 public ListDataProvider<RestResource> getRootNodes() {
666 * Retrieve the mymap.
670 public Map<String, MyFolderDataProvider> getMymap() {
676 * Retrieve the sharedmap.
678 * @return the sharedmap
680 public Map<String, MyFolderDataProvider> getSharedmap() {
686 * Retrieve the othersmap.
688 * @return the othersmap
690 public Map<String, MyFolderDataProvider> getOthersmap() {