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.CellTreeViewModel.MyFolderDataProvider;
22 import gr.ebs.gss.client.rest.GetCommand;
23 import gr.ebs.gss.client.rest.RestException;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25 import gr.ebs.gss.client.rest.resource.MyFolderResource;
26 import gr.ebs.gss.client.rest.resource.OtherUserResource;
27 import gr.ebs.gss.client.rest.resource.OthersFolderResource;
28 import gr.ebs.gss.client.rest.resource.OthersResource;
29 import gr.ebs.gss.client.rest.resource.RestResource;
30 import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
31 import gr.ebs.gss.client.rest.resource.SharedResource;
32 import gr.ebs.gss.client.rest.resource.TrashFolderResource;
33 import gr.ebs.gss.client.rest.resource.TrashResource;
34 import gr.ebs.gss.client.rest.resource.UserResource;
35 import gwtquery.plugins.draggable.client.DragAndDropManager;
36 import gwtquery.plugins.droppable.client.events.DragAndDropContext;
37 import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTree;
39 import java.util.Arrays;
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.query.client.plugins.GQueryUi;
43 import com.google.gwt.resources.client.ClientBundle;
44 import com.google.gwt.resources.client.ImageResource;
45 import com.google.gwt.resources.client.ImageResource.ImageOptions;
46 import com.google.gwt.user.cellview.client.CellTree;
47 import com.google.gwt.user.cellview.client.TreeNode;
48 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
49 import com.google.gwt.user.client.DeferredCommand;
50 import com.google.gwt.user.client.Event;
51 import com.google.gwt.user.client.IncrementalCommand;
52 import com.google.gwt.user.client.Window;
53 import com.google.gwt.user.client.ui.Composite;
54 import com.google.gwt.user.client.ui.Tree;
55 import com.google.gwt.view.client.ProvidesKey;
56 import com.google.gwt.view.client.SelectionChangeEvent;
57 import com.google.gwt.view.client.SingleSelectionModel;
58 import com.google.gwt.view.client.TreeViewModel;
59 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
60 import com.google.gwt.view.client.TreeViewModel.NodeInfo;
67 public class CellTreeView extends Composite{
68 public static final boolean DONE = false;
71 SingleSelectionModel<RestResource> selectionModel = new SingleSelectionModel<RestResource>(new ProvidesKey<RestResource>() {
74 public Object getKey(RestResource item) {
75 return item.getClass().getName()+":"+item.getUri();
77 FolderContextMenu menu;
80 MyFolderResource myFolders=null;
81 TrashResource trash = null;
82 SharedResource myshared = null;
83 OthersResource others = null;
85 CellTreeViewModel model;
86 CellTreeViewUtils utils;
88 public interface Images extends ClientBundle,Tree.Resources, FolderContextMenu.Images {
90 @Source("gr/ebs/gss/resources/folder_home.png")
93 @Source("gr/ebs/gss/resources/folder_yellow.png")
94 ImageResource folderYellow();
96 @Source("gr/ebs/gss/resources/mimetypes/document.png")
97 ImageResource document();
99 @Source("gr/ebs/gss/resources/internet.png")
100 ImageResource othersShared();
102 @Source("gr/ebs/gss/resources/edit_user.png")
103 ImageResource myShared();
105 @Source("gr/ebs/gss/resources/folder_user.png")
106 ImageResource sharedFolder();
108 @Source("gr/ebs/gss/resources/trashcan_empty.png")
109 ImageResource trash();
111 DragAndDropCellTree tree;
112 /*public interface BasicResources extends CellTree.BasicResources{
113 @ImageOptions(flipRtl = true)
114 @Source("cellTreeLoadingBasic.gif")
115 ImageResource cellTreeLoading();
117 @Source({"GssCellTreeBasic.css"})
118 CellTree.Style cellTreeStyle();
120 public interface BasicResources extends CellTree.Resources {
122 @ImageOptions(flipRtl = true)
123 @Source("cellTreeClosedItem.gif")
124 ImageResource cellTreeClosedItem();
126 @ImageOptions(flipRtl = true)
127 @Source("cellTreeLoadingBasic.gif")
128 ImageResource cellTreeLoading();
130 @ImageOptions(flipRtl = true)
131 @Source("cellTreeOpenItem.gif")
132 ImageResource cellTreeOpenItem();
134 //@Source({CellTree.Style.DEFAULT_CSS,"GssCellTreeBasic.css"})
135 @Source({"GssCellTreeBasic.css"})
136 CellTree.Style cellTreeStyle();
141 public CellTreeView(final Images _images) {
143 model = new CellTreeViewModel(images,selectionModel);
145 * Create the tree using the model. We use <code>null</code> as the default
146 * value of the root node. The default value will be passed to
147 * CustomTreeModel#getNodeInfo();
149 CellTree.Resources res = GWT.create(BasicResources.class);
150 tree = new DragAndDropCellTree(model,null, res){
152 public void onBrowserEvent(Event event) {
153 // TODO Auto-generated method stub
154 super.onBrowserEvent(event);
157 utils=new CellTreeViewUtils(tree);
158 tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
160 Handler selectionHandler = new SelectionChangeEvent.Handler() {
162 public void onSelectionChange(com.google.gwt.view.client.SelectionChangeEvent event) {
163 NodeInfo<RestResource> nodeInfo = (NodeInfo<RestResource>) getModel().getNodeInfo(selectionModel.getSelectedObject());
165 //GWT.log("SLECTED NODE INFO:"+nodeInfo+" "+tree.getRootTreeNode().getChildValue(1));
166 //((DefaultNodeInfo<RestResource>)nodeInfo).getValueUpdater().update(selectionModel.getSelectedObject());
167 //if(nodeInfo.getProvidesKey() instanceof FolderDataProvider)
168 //((FolderDataProvider)nodeInfo.getProvidesKey()).onRangeChanged(null);
169 if(nodeInfo==null || nodeInfo.getValueUpdater()==null){}
172 nodeInfo.getValueUpdater().update(selectionModel.getSelectedObject());
173 GSS.get().setCurrentSelection(selectionModel.getSelectedObject());
174 GSS.get().showFileList(true);
175 //tree.fireEvent(new OpenEvent<RestResource>(selectionModel.getSelectedObject()));
179 //OpenEvent<RestResource>.fire(tree, selectionModel.getSelectedObject());
180 selectionModel.addSelectionChangeHandler(selectionHandler);
181 sinkEvents(Event.ONCONTEXTMENU);
182 sinkEvents(Event.ONMOUSEUP);
185 DeferredCommand.addCommand(new IncrementalCommand() {
188 public boolean execute() {
189 return fetchRootFolders();
194 public void updateNode(RestResource resource){
195 NodeInfo<RestResource> nodeInfo = (NodeInfo<RestResource>) getModel().getNodeInfo(resource);
198 if(nodeInfo.getValueUpdater()==null){}
200 nodeInfo.getValueUpdater().update(resource);
204 public void updateNodeChildren(final RestResource resource){
206 if(resource instanceof RestResourceWrapper){
207 boolean updated=false;
208 if(((RestResourceWrapper)resource).getResource().getFolders().size()==0){
209 if(model.getMymap().get(((RestResourceWrapper)resource).getResource().getParentURI())!=null){
210 model.getMymap().get(((RestResourceWrapper)resource).getResource().getParentURI()).refresh(null);
213 if(model.getOthersmap().get(((RestResourceWrapper)resource).getResource().getParentURI())!=null){
214 model.getOthersmap().get(((RestResourceWrapper)resource).getResource().getParentURI()).refresh(null);
217 if(model.getSharedmap().get(((RestResourceWrapper)resource).getResource().getParentURI())!=null){
218 model.getSharedmap().get(((RestResourceWrapper)resource).getResource().getParentURI()).refresh(null);
222 if(utils.doesSharedNodeContainsResourceIn1stLevel(resource.getUri())){
223 updateMySharedNode();
230 utils.refreshNodeContainingResource(resource);
231 if(utils.doesSharedNodeContainsResourceIn1stLevel(resource.getUri())){
232 updateMySharedNode();
236 public void updateNodeChildrenForRemove(final String resource){
237 GWT.log("********************************");
238 GWT.log("[UPDATENODECHILDREN]"+resource);
239 GWT.log("********************************");
240 boolean updated=false;
242 if(tree.getRootTreeNode().isChildOpen(0)){
243 node = utils.getNodeContainingResource2(tree.getRootTreeNode().setChildOpen(0,true), resource);
245 GWT.log("CHECK NODE1:"+node+" "+resource);
247 if(tree.getRootTreeNode().isChildOpen(2)){
248 GWT.log("CHECK NODE2:"+node);
249 node = utils.getNodeContainingResource2(tree.getRootTreeNode().setChildOpen(2,true), resource);
252 if(tree.getRootTreeNode().isChildOpen(3)){
253 GWT.log("CHECK NODE3:"+node);
254 node = utils.getNodeContainingResource2(tree.getRootTreeNode().setChildOpen(3,true), resource);
257 if(node != null && node.getValue() instanceof RestResourceWrapper){
258 GWT.log("*********************"+((RestResourceWrapper) node.getValue()).getResource().getFolders().size());
259 if(((RestResourceWrapper) node.getValue()).getResource().getFolders().size()==1)
260 updateNodeChildren(((RestResourceWrapper) node.getValue()).getResource().getParentURI());
262 updateNodeChildren(((RestResource) node.getValue()).getUri());
264 /*GWT.log("INSIDE:"+node);
265 RestResourceWrapper rw = (RestResourceWrapper) node.getValue();
266 //if(rw.getResource().getFolders().size()==0){
267 if(model.getMymap().get(rw.getResource().getUri())!=null){
268 model.getMymap().get(rw.getResource().getUri()).refresh(null);
271 if(model.getOthersmap().get(rw.getResource().getUri())!=null){
272 model.getOthersmap().get(rw.getResource().getUri()).refresh(null);
275 if(model.getSharedmap().get(rw.getResource().getUri())!=null){
276 model.getSharedmap().get(rw.getResource().getUri()).refresh(null);
280 if(doesSharedNodeContainsResourceIn1stLevel(resource)){
281 updateMySharedNode();
289 updateNodeChildren(resource);
291 public void updateNodeChildren(final String resource){
294 GWT.log("REFRESH THE OTHER WAY");
295 utils.refreshNodeContainingResource(resource);
296 if(utils.doesSharedNodeContainsResourceIn1stLevel(resource)){
297 updateMySharedNode();
301 protected void showPopup(final int x, final int y) {
302 if (selectionModel.getSelectedObject() == null)
306 menu = new FolderContextMenu(images);
307 menu.setPopupPosition(x, y);
310 private boolean init=false;
311 public boolean fetchRootFolders() {
312 UserResource userResource = GSS.get().getCurrentUserResource();
313 if (userResource == null)
316 final String path = userResource.getFilesPath();
317 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, null) {
320 public void onComplete() {
321 myFolders = new MyFolderResource(getResult());
322 //selectionModel.setSelected(myFolders, true);
323 //rootNodes.setList(Arrays.asList((RestResource)rootResource));
324 //tree.getRootTreeNode().setChildOpen(0, true);
328 public void onError(Throwable t) {
329 GWT.log("Error fetching root folder", t);
330 GSS.get().displayError("Unable to fetch root folder");
334 DeferredCommand.addCommand(gf);
335 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
337 public void onComplete() {
342 public void onError(Throwable t) {
343 if(t instanceof RestException){
344 int statusCode = ((RestException)t).getHttpStatusCode();
345 // On IE status code 1223 may be returned instead of 204.
346 if(statusCode == 204 || statusCode == 1223){
347 trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
351 GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
352 trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
357 GetCommand<SharedResource> gs = new GetCommand<SharedResource>(SharedResource.class, userResource.getSharedPath(), null) {
360 public void onComplete() {
361 myshared=getResult();
365 public void onError(Throwable t) {
366 GWT.log("Error fetching Shared Root folder", t);
367 GSS.get().displayError("Unable to fetch Shared Root folder");
370 DeferredCommand.addCommand(gs);
371 GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
372 userResource.getOthersPath(), null) {
375 public void onComplete() {
376 others = getResult();
377 GSS.get().removeGlassPanel();
381 public void onError(Throwable t) {
382 GWT.log("Error fetching Others Root folder", t);
383 GSS.get().displayError("Unable to fetch Others Root folder");
386 DeferredCommand.addCommand(go);
388 if(myFolders==null||trash==null||myshared==null||others==null)
390 model.getRootNodes().setList(Arrays.asList((RestResource)myFolders,(RestResource)trash,(RestResource)myshared,(RestResource)others));
391 tree.getRootTreeNode().setChildOpen(0, true);
392 selectionModel.setSelected(myFolders, true);
396 public Images getImages() {
401 public void updateTrashNode(){
402 DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
404 public void onComplete() {
406 model.getRootNodes().getList().set(1, trash);
407 //model.getRootNodes().refresh();
411 public void onError(Throwable t) {
412 if(t instanceof RestException){
413 int statusCode = ((RestException)t).getHttpStatusCode();
414 // On IE status code 1223 may be returned instead of 204.
415 if(statusCode == 204 || statusCode == 1223){
416 trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
417 model.getRootNodes().getList().set(1, trash);
418 //model.getRootNodes().refresh();
422 GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
423 trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
424 model.getRootNodes().getList().set(1, trash);
425 //model.getRootNodes().refresh();
432 public void updateRootNode(){
433 final String path = GSS.get().getCurrentUserResource().getFilesPath();
434 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, null) {
437 public void onComplete() {
438 myFolders = new MyFolderResource(getResult());
439 model.getRootNodes().getList().set(0, myFolders);
440 model.getRootNodes().refresh();
441 tree.getRootTreeNode().setChildOpen(0, true);
446 public void onError(Throwable t) {
447 GWT.log("Error fetching root folder", t);
448 GSS.get().displayError("Unable to fetch root folder");
452 DeferredCommand.addCommand(gf);
455 public void updateMySharedNode(){
456 GetCommand<SharedResource> gs = new GetCommand<SharedResource>(SharedResource.class, GSS.get().getCurrentUserResource().getSharedPath(), null) {
459 public void onComplete() {
460 myshared=getResult();
461 model.getRootNodes().getList().set(2, myshared);
465 public void onError(Throwable t) {
466 GWT.log("Error fetching Shared Root folder", t);
467 GSS.get().displayError("Unable to fetch Shared Root folder");
470 DeferredCommand.addCommand(gs);
473 public void updateOtherNode(){
474 GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
475 GSS.get().getCurrentUserResource().getOthersPath(), null) {
478 public void onComplete() {
479 others = getResult();
480 model.getRootNodes().getList().set(3, others);
484 public void onError(Throwable t) {
485 GWT.log("Error fetching Others Root folder", t);
486 GSS.get().displayError("Unable to fetch Others Root folder");
489 DeferredCommand.addCommand(go);
493 public RestResource getSelection(){
494 return selectionModel.getSelectedObject();
499 * Retrieve the myFolders.
501 * @return the myFolders
503 public MyFolderResource getMyFolders() {
509 * Retrieve the myshared.
511 * @return the myshared
513 public SharedResource getMyshared() {
519 * Retrieve the trash.
523 public TrashResource getTrash() {
529 * Retrieve the others.
533 public OthersResource getOthers() {
539 * Retrieve the model.
543 public TreeViewModel getModel() {
550 * Retrieve the utils.
554 public CellTreeViewUtils getUtils() {
558 public interface RefreshHandler{
563 public boolean isTrashOrTrashFolderSelected(){
564 return (getSelection() instanceof TrashResource) || (getSelection() instanceof TrashFolderResource);
567 public OtherUserResource getOtherUserResourceOfOtherFolder(OthersFolderResource res){
568 TreeNode n = utils.getNodeContainingResource(tree.getRootTreeNode().setChildOpen(3, true), res);
571 if(n.getValue() instanceof OtherUserResource)
572 return (OtherUserResource) n.getValue();
573 TreeNode parent = n.getParent();
575 while (parent!=null){
576 if(parent.getValue() instanceof OtherUserResource)
577 return (OtherUserResource) parent.getValue();
578 parent = parent.getParent();