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.FileResource;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.MyFolderResource;
29 import gr.ebs.gss.client.rest.resource.OtherUserResource;
30 import gr.ebs.gss.client.rest.resource.OthersFolderResource;
31 import gr.ebs.gss.client.rest.resource.OthersResource;
32 import gr.ebs.gss.client.rest.resource.RestResource;
33 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
34 import gr.ebs.gss.client.rest.resource.SharedFolderResource;
35 import gr.ebs.gss.client.rest.resource.SharedResource;
36 import gr.ebs.gss.client.rest.resource.TrashFolderResource;
37 import gr.ebs.gss.client.rest.resource.TrashResource;
38 import gwtquery.plugins.draggable.client.DraggableOptions;
39 import gwtquery.plugins.draggable.client.DraggableOptions.CursorAt;
40 import gwtquery.plugins.draggable.client.DraggableOptions.DragFunction;
41 import gwtquery.plugins.draggable.client.DraggableOptions.RevertOption;
42 import gwtquery.plugins.draggable.client.events.DragContext;
43 import gwtquery.plugins.droppable.client.DroppableOptions;
44 import gwtquery.plugins.droppable.client.DroppableOptions.DroppableFunction;
45 import gwtquery.plugins.droppable.client.events.DragAndDropContext;
46 import gwtquery.plugins.droppable.client.gwt.DragAndDropNodeInfo;
48 import java.util.ArrayList;
49 import java.util.Arrays;
50 import java.util.HashMap;
51 import java.util.List;
53 import static com.google.gwt.query.client.GQuery.$;
55 import com.google.gwt.cell.client.AbstractCell;
56 import com.google.gwt.cell.client.Cell;
57 import com.google.gwt.cell.client.ValueUpdater;
58 import com.google.gwt.core.client.GWT;
59 import com.google.gwt.dom.client.Style.Cursor;
60 import com.google.gwt.safehtml.client.SafeHtmlTemplates;
61 import com.google.gwt.safehtml.client.SafeHtmlTemplates.Template;
62 import com.google.gwt.safehtml.shared.SafeHtml;
63 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
64 import com.google.gwt.user.client.DeferredCommand;
65 import com.google.gwt.user.client.ui.AbstractImagePrototype;
66 import com.google.gwt.view.client.AsyncDataProvider;
67 import com.google.gwt.view.client.HasData;
68 import com.google.gwt.view.client.ListDataProvider;
69 import com.google.gwt.view.client.ProvidesKey;
70 import com.google.gwt.view.client.SingleSelectionModel;
71 import com.google.gwt.view.client.TreeViewModel;
79 public class CellTreeViewModel implements TreeViewModel{
80 private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
82 SingleSelectionModel<RestResource> selectionModel;
83 Map<String, MyFolderDataProvider> mymap = new HashMap<String, MyFolderDataProvider>();
84 static interface Templates extends SafeHtmlTemplates {
85 Templates INSTANCE = GWT.create(Templates.class);
87 @Template(" <div id='dragHelper' class='{0}'></div>")
88 SafeHtml outerHelper(String cssClassName);
91 static void configureDragOperation(DraggableOptions options) {
93 // set a custom element as drag helper. The content of the helper will be
94 // set when the drag will start
95 options.setHelper($(Templates.INSTANCE.outerHelper(
97 // opacity of the drag helper
98 options.setOpacity((float) 0.9);
99 // cursor during the drag operation
100 options.setCursor(Cursor.MOVE);
101 // the cell being greater than the helper, force the position of the
102 // helper on the mouse cursor.
103 options.setCursorAt(new CursorAt(10, 10, null, null));
104 // append the helper to the body element
105 options.setAppendTo("body");
106 // set the revert option
107 options.setRevert(RevertOption.ON_INVALID_DROP);
108 // use a Function to fill the content of the helper
109 // we could also add a DragStartEventHandler on the DragAndDropTreeCell and
110 // DragAndDropCellList.
111 options.setOnDragStart(new DragFunction() {
112 public void f(DragContext context) {
113 RestResourceWrapper memberInfo = context.getDraggableData();
114 context.getHelper().setInnerHTML(memberInfo.getName());
123 public CellTreeViewModel(final Images _images,SingleSelectionModel<RestResource> selectionModel ) {
126 this.selectionModel=selectionModel;
129 private final Cell<RestResource> departmentCell = new AbstractCell<RestResource>("contextmenu"){
132 public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
135 if(arg1 instanceof TrashFolderResource){
136 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
137 FolderResource res = ((RestResourceWrapper)arg1).getResource();
140 else if(arg1 instanceof RestResourceWrapper){
141 FolderResource res = ((RestResourceWrapper)arg1).getResource();
143 html = AbstractImagePrototype.create(images.sharedFolder()).getHTML();
144 else if(res.getParentName()==null){
145 html = AbstractImagePrototype.create(images.home()).getHTML();
148 html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
149 name = res.getName();
152 else if(arg1 instanceof TrashResource){
153 html = AbstractImagePrototype.create(images.trash()).getHTML();
157 else if(arg1 instanceof SharedResource){
158 html = AbstractImagePrototype.create(images.myShared()).getHTML();
161 else if(arg1 instanceof OthersResource){
162 html = AbstractImagePrototype.create(images.othersShared()).getHTML();
163 name = "Other's Shared";
165 else if(arg1 instanceof OtherUserResource){
166 html = AbstractImagePrototype.create(images.permUser()).getHTML();
167 name = ((OtherUserResource)arg1).getName();
169 arg2.appendHtmlConstant(html);
170 arg2.appendHtmlConstant("<span class='papala'>");
171 arg2.appendEscaped(name);
172 arg2.appendHtmlConstant("</span>");
175 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) {
176 if(event.getType().equals("contextmenu")){
177 selectionModel.setSelected(value, true);
178 GSS.get().getTreeView().showPopup(event.getClientX(), event.getClientY());
186 public <T> NodeInfo<?> getNodeInfo(final T value) {
189 return new DragAndDropNodeInfo<RestResource>(getRootNodes(), departmentCell,
190 selectionModel, null);
192 else if (value instanceof MyFolderResource) {
194 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
195 ((MyFolderResource) value),MyFolderResource.class);
196 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
197 selectionModel, new ResourceValueUpdater());
198 //nodeInfos.put(((MyFolderResource) value).getUri(), n);
199 mymap.put(((MyFolderResource) value).getUri(), dataProvider);
200 DroppableOptions options = n.getDroppableOptions();
201 options.setDroppableHoverClass("droppableHover");
202 // use a DroppableFunction here. We can also add a DropHandler in the tree
204 options.setOnDrop(new DroppableFunction() {
206 public void f(DragAndDropContext context) {
208 DnDFolderPopupMenu popup ;
209 if(context.getDraggableData() instanceof FileResource){
210 popup = new DnDFolderPopupMenu(images, ((MyFolderResource) context.getDroppableData()).getResource(), Arrays.asList(context.getDraggableData()));
213 popup = new DnDFolderPopupMenu(images, ((MyFolderResource) context.getDroppableData()).getResource(), context.getDraggableData());
214 int left = context.getDroppable().getAbsoluteLeft() + 40;
215 int top = context.getDroppable().getAbsoluteTop() + 20;
216 popup.setPopupPosition(left, top);
221 // permission cell are not draggable
222 //n.setCellDroppableOnly();
223 configureDragOperation(n.getDraggableOptions());
226 else if (value instanceof SharedResource) {
228 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
229 ((SharedResource) value), SharedFolderResource.class);
230 return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
231 selectionModel, new ResourceValueUpdater());
233 else if (value instanceof TrashResource) {
235 ListDataProvider<RestResource> trashProvider = new ListDataProvider<RestResource>();
236 List<RestResource> r = new ArrayList<RestResource>();
237 for(FolderResource f : GSS.get().getTreeView().getTrash().getFolders()){
238 r.add(new TrashFolderResource(f));
240 trashProvider.setList(r);
241 return new DragAndDropNodeInfo<RestResource>(trashProvider, departmentCell,
242 selectionModel, new ResourceValueUpdater());
244 else if (value instanceof OthersResource) {
246 OthersDataProvider dataProvider = new OthersDataProvider(
247 ((OthersResource) value), SharedFolderResource.class);
248 return new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
249 selectionModel, null);
251 else if (value instanceof SharedFolderResource) {
253 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
254 ((SharedFolderResource) value),SharedFolderResource.class);
255 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
256 selectionModel, new ResourceValueUpdater());
257 //nodeInfos.put(((SharedFolderResource) value).getUri(), n);
258 DroppableOptions options = n.getDroppableOptions();
259 options.setDroppableHoverClass("droppableHover");
260 // use a DroppableFunction here. We can also add a DropHandler in the tree
262 options.setOnDrop(new DroppableFunction() {
264 public void f(DragAndDropContext context) {
269 // permission cell are not draggable
270 n.setCellDroppableOnly();
273 else if (value instanceof OthersFolderResource) {
275 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
276 ((OthersFolderResource) value),OthersFolderResource.class);
277 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
278 selectionModel, new ResourceValueUpdater());
279 //nodeInfos.put(((OthersFolderResource) value).getUri(), n);
280 DroppableOptions options = n.getDroppableOptions();
281 options.setDroppableHoverClass("droppableHover");
282 // use a DroppableFunction here. We can also add a DropHandler in the tree
284 options.setOnDrop(new DroppableFunction() {
286 public void f(DragAndDropContext context) {
291 // permission cell are not draggable
292 n.setCellDroppableOnly();
295 else if (value instanceof OtherUserResource) {
297 MyFolderDataProvider dataProvider = new MyFolderDataProvider(
298 ((OtherUserResource) value),OthersFolderResource.class);
299 DragAndDropNodeInfo<RestResource> n = new DragAndDropNodeInfo<RestResource>(dataProvider, departmentCell,
300 selectionModel, new ResourceValueUpdater());
301 //nodeInfos.put(((OtherUserResource) value).getUri(), n);
302 DroppableOptions options = n.getDroppableOptions();
303 options.setDroppableHoverClass("droppableHover");
304 // use a DroppableFunction here. We can also add a DropHandler in the tree
306 options.setOnDrop(new DroppableFunction() {
308 public void f(DragAndDropContext context) {
313 // permission cell are not draggable
314 n.setCellDroppableOnly();
317 // TODO Auto-generated method stub
322 public boolean isLeaf(Object value) {
323 if(value instanceof RestResourceWrapper)
324 return ((RestResourceWrapper)value).getResource().getFolders().size()==0;
325 else if(value instanceof TrashResource)
326 return ((TrashResource)value).getFolders().size()==0;
327 else if(value instanceof SharedResource)
328 return ((SharedResource)value).getFolders().size()==0;
329 else if(value instanceof OthersResource)
330 return ((OthersResource)value).getOtherUsers().size()==0;
331 else if(value instanceof OtherUserResource)
332 return ((OtherUserResource)value).getFolders().size()==0;
336 class ResourceValueUpdater implements ValueUpdater<RestResource>{
339 public void update(final RestResource value) {
340 if(value instanceof MyFolderResource){
341 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
344 public void onComplete() {
345 FolderResource rootResource = getResult();
346 ((MyFolderResource)value).getResource().setFiles(rootResource.getFiles());
347 if(GSS.get().getTreeView().getSelection().getUri().equals(value.getUri()))
348 selectionModel.setSelected(value, true);
349 GWT.log("UPDATYING");
350 GSS.get().onResourceUpdate(value);
355 public void onError(Throwable t) {
356 GWT.log("Error fetching root folder", t);
357 GSS.get().displayError("Unable to fetch root folder");
361 DeferredCommand.addCommand(gf);
363 if(value instanceof TrashResource){
364 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
366 public void onComplete() {
367 //trash = getResult();
368 for(RestResource r : getRootNodes().getList()){
369 if(r instanceof TrashResource)
370 getRootNodes().getList().set(getRootNodes().getList().indexOf(r),GSS.get().getTreeView().getTrash());
372 GSS.get().getTreeView().updateNodeChildren(GSS.get().getTreeView().getTrash());
376 public void onError(Throwable t) {
377 if(t instanceof RestException){
378 int statusCode = ((RestException)t).getHttpStatusCode();
379 // On IE status code 1223 may be returned instead of 204.
380 if(statusCode == 204 || statusCode == 1223){
381 //trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
385 GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
386 //GSS.get().getTreeView().getTrash() = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
396 class MyFolderDataProvider extends AsyncDataProvider<RestResource>{
397 private RestResource restResource;
398 private Class resourceClass;
399 public MyFolderDataProvider(RestResource department, Class resourceClass) {
400 super(new ProvidesKey<RestResource>() {
403 public Object getKey(RestResource item) {
404 return item.getUri();
406 this.restResource = department;
407 this.resourceClass=resourceClass;
408 //CellTreeView.this.mymap.put(department.getUri(), MyFolderDataProvider.this);
412 protected void onRangeChanged(final HasData<RestResource> view) {
417 * Retrieve the restResource.
419 * @return the restResource
421 public RestResource getRestResource() {
427 * Modify the restResource.
429 * @param restResource the restResource to set
431 public void setRestResource(RestResource restResource) {
432 this.restResource = restResource;
435 public void refresh(final RefreshHandler refresh){
436 GWT.log("******************************************");
437 GWT.log("[REFRESHING]:"+restResource.getUri());
438 GWT.log("******************************************");
439 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, restResource.getUri(), null) {
442 public void onComplete() {
443 if(restResource instanceof RestResourceWrapper)
444 ((RestResourceWrapper)restResource).setResource(getResult());//restResource = getResult();
446 //if(CellTreeView.this.mymap.get(restResource.getUri())!=null)
447 //CellTreeView.this.mymap.get(restResource.getUri()).setRestResource(restResource);
448 String[] folderPaths = null;
449 if(resourceClass.equals(MyFolderResource.class))
450 folderPaths=((MyFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
451 else if(resourceClass.equals(SharedFolderResource.class) && restResource instanceof SharedResource)
452 folderPaths=((SharedResource) restResource).getSubfolderPaths().toArray(new String[] {});
453 else if(resourceClass.equals(SharedFolderResource.class)){
454 folderPaths=((SharedFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
455 GWT.log("------------>"+folderPaths);
457 else if(resourceClass.equals(TrashFolderResource.class))
458 folderPaths=((TrashFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
459 else if(resourceClass.equals(OthersFolderResource.class) && restResource instanceof OtherUserResource)
460 folderPaths=((OtherUserResource) restResource).getSubfolderPaths().toArray(new String[] {});
461 else if(resourceClass.equals(OthersFolderResource.class))
462 folderPaths=((OthersFolderResource) restResource).getResource().getSubfolderPaths().toArray(new String[] {});
463 MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
467 public void onComplete() {
468 List<RestResource> res = new ArrayList<RestResource>();
469 for(FolderResource r : getResult()){
473 else if(resourceClass.equals(MyFolderResource.class))
474 res.add(new MyFolderResource(r));
475 else if(resourceClass.equals(SharedFolderResource.class)){
476 GWT.log("ADDING:"+r.getUri());
477 res.add(new SharedFolderResource(r));
479 else if(resourceClass.equals(TrashFolderResource.class))
480 res.add(new TrashFolderResource(r));
481 else if(resourceClass.equals(OthersFolderResource.class))
482 res.add(new OthersFolderResource(r));
484 updateRowCount(res.size(), true);
485 updateRowData(0,res);
491 public void onError(Throwable t) {
492 GSS.get().displayError("Unable to fetch subfolders");
493 GWT.log("Unable to fetch subfolders", t);
497 public void onError(String p, Throwable throwable) {
498 GWT.log("Path:"+p, throwable);
502 DeferredCommand.addCommand(gf2);
507 public void onError(Throwable t) {
508 GWT.log("Error fetching root folder", t);
509 GSS.get().displayError("Unable to fetch root folder");
513 DeferredCommand.addCommand(gf);
518 class OthersDataProvider extends AsyncDataProvider<RestResource>{
519 private RestResource restResource;
520 private Class resourceClass;
521 public OthersDataProvider(RestResource department, Class resourceClass) {
522 super(new ProvidesKey<RestResource>() {
525 public Object getKey(RestResource item) {
526 return item.getUri();
528 this.restResource = department;
529 this.resourceClass=resourceClass;
530 //CellTreeView.this.mymap.put(department.getUri(), OthersDataProvider.this);
534 protected void onRangeChanged(final HasData<RestResource> view) {
539 * Retrieve the restResource.
541 * @return the restResource
543 public RestResource getRestResource() {
549 * Modify the restResource.
551 * @param restResource the restResource to set
553 public void setRestResource(RestResource restResource) {
554 this.restResource = restResource;
557 public void refresh(final RefreshHandler refresh){
558 GWT.log("******************************************");
559 GWT.log("[REFRESHING]:"+restResource.getUri());
560 GWT.log("******************************************");
561 GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
562 restResource.getUri(), null) {
565 public void onComplete() {
566 final OthersResource others = getResult();
567 MultipleGetCommand<OtherUserResource> gogo = new MultipleGetCommand<OtherUserResource>(OtherUserResource.class,
568 others.getOthers().toArray(new String[] {}), null) {
571 public void onComplete() {
572 List<OtherUserResource> res = getResult();
573 updateRowCount(res.size(), true);
574 List<RestResource> r = new ArrayList<RestResource>();
580 public void onError(Throwable t) {
581 GWT.log("Error fetching Others Root folder", t);
582 GSS.get().displayError("Unable to fetch Others Root folder");
586 public void onError(String p, Throwable throwable) {
587 GWT.log("Path:"+p, throwable);
590 DeferredCommand.addCommand(gogo);
594 public void onError(Throwable t) {
595 GWT.log("Error fetching Others Root folder", t);
596 GSS.get().displayError("Unable to fetch Others Root folder");
599 DeferredCommand.addCommand(go);
606 * Retrieve the rootNodes.
608 * @return the rootNodes
610 public ListDataProvider<RestResource> getRootNodes() {
616 * Retrieve the mymap.
620 public Map<String, MyFolderDataProvider> getMymap() {