*/
package gr.ebs.gss.client;
+import gr.ebs.gss.client.CellTreeViewModel.MyFolderDataProvider;
import gr.ebs.gss.client.rest.GetCommand;
-import gr.ebs.gss.client.rest.MultipleGetCommand;
import gr.ebs.gss.client.rest.RestException;
import gr.ebs.gss.client.rest.resource.FolderResource;
+import gr.ebs.gss.client.rest.resource.MyFolderResource;
+import gr.ebs.gss.client.rest.resource.OtherUserResource;
+import gr.ebs.gss.client.rest.resource.OthersFolderResource;
import gr.ebs.gss.client.rest.resource.OthersResource;
import gr.ebs.gss.client.rest.resource.RestResource;
+import gr.ebs.gss.client.rest.resource.RestResourceWrapper;
import gr.ebs.gss.client.rest.resource.SharedResource;
+import gr.ebs.gss.client.rest.resource.TrashFolderResource;
import gr.ebs.gss.client.rest.resource.TrashResource;
import gr.ebs.gss.client.rest.resource.UserResource;
+import gwtquery.plugins.draggable.client.DragAndDropManager;
+import gwtquery.plugins.droppable.client.events.DragAndDropContext;
+import gwtquery.plugins.droppable.client.events.OverDroppableEvent;
+import gwtquery.plugins.droppable.client.events.OverDroppableEvent.OverDroppableEventHandler;
+import gwtquery.plugins.droppable.client.gwt.DragAndDropCellTree;
-import java.util.ArrayList;
import java.util.Arrays;
-import java.util.List;
-import com.google.gwt.cell.client.AbstractCell;
-import com.google.gwt.cell.client.Cell;
-import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
+import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.event.logical.shared.OpenEvent;
+import com.google.gwt.event.logical.shared.OpenHandler;
+import com.google.gwt.query.client.plugins.GQueryUi;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.resources.client.ImageResource.ImageOptions;
-import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.cellview.client.TreeNode;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.IncrementalCommand;
-import com.google.gwt.user.client.ui.AbstractImagePrototype;
+import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Tree;
-import com.google.gwt.user.client.ui.TreeItem;
-import com.google.gwt.view.client.AbstractDataProvider;
-import com.google.gwt.view.client.AsyncDataProvider;
-import com.google.gwt.view.client.HasData;
-import com.google.gwt.view.client.ListDataProvider;
-import com.google.gwt.view.client.Range;
+import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;
import com.google.gwt.view.client.TreeViewModel;
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
-import com.google.gwt.view.client.TreeViewModel.DefaultNodeInfo;
import com.google.gwt.view.client.TreeViewModel.NodeInfo;
public class CellTreeView extends Composite{
public static final boolean DONE = false;
Images images;
- private final ListDataProvider<RestResource> rootNodes = new ListDataProvider<RestResource>();
- SingleSelectionModel<RestResource> selectionModel = new SingleSelectionModel<RestResource>();
+
+ SingleSelectionModel<RestResource> selectionModel = new SingleSelectionModel<RestResource>(new ProvidesKey<RestResource>() {
+
+ @Override
+ public Object getKey(RestResource item) {
+ return item.getClass().getName()+":"+item.getUri();
+ }});
FolderContextMenu menu;
- FolderResource myFolders=null;
+ MyFolderResource myFolders=null;
TrashResource trash = null;
SharedResource myshared = null;
OthersResource others = null;
- TreeViewModel model = new CustomTreeModel();
+ CellTreeViewModel model;
+ CellTreeViewUtils utils;
public interface Images extends ClientBundle,Tree.Resources, FolderContextMenu.Images {
@Source("gr/ebs/gss/resources/trashcan_empty.png")
ImageResource trash();
}
- final CellTree tree;
+ DragAndDropCellTree tree;
/*public interface BasicResources extends CellTree.BasicResources{
@ImageOptions(flipRtl = true)
@Source("cellTreeLoadingBasic.gif")
*/
public CellTreeView(final Images _images) {
images = _images;
-
-
+ model = new CellTreeViewModel(images,selectionModel);
/*
* Create the tree using the model. We use <code>null</code> as the default
* value of the root node. The default value will be passed to
* CustomTreeModel#getNodeInfo();
*/
CellTree.Resources res = GWT.create(BasicResources.class);
- tree = new CellTree(model,null, res){
+ tree = new DragAndDropCellTree(model,null, res){
@Override
public void onBrowserEvent(Event event) {
// TODO Auto-generated method stub
super.onBrowserEvent(event);
- //GWT.log(event.getType());
}
};
+ utils=new CellTreeViewUtils(tree);
tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
+ /*tree.addOpenHandler(new OpenHandler<TreeNode>() {
+
+ @Override
+ public void onOpen(OpenEvent<TreeNode> event) {
+ Window.alert("[NODE OPENED]"+event.getTarget());
+
+ }
+ });
+ tree.addOverDroppableHandler(new OverDroppableEventHandler() {
+
+ @Override
+ public void onOverDroppable(OverDroppableEvent event) {
+ GWT.log("OVER:"+event);
+
+ }
+ });
+ tree.addAttachHandler(new AttachEvent.Handler() {
+
+ @Override
+ public void onAttachOrDetach(AttachEvent event) {
+ GWT.log("ATTACH:"+event.getSource());
+
+ }
+ });*/
Handler selectionHandler = new SelectionChangeEvent.Handler() {
@Override
public void onSelectionChange(com.google.gwt.view.client.SelectionChangeEvent event) {
//((DefaultNodeInfo<RestResource>)nodeInfo).getValueUpdater().update(selectionModel.getSelectedObject());
//if(nodeInfo.getProvidesKey() instanceof FolderDataProvider)
//((FolderDataProvider)nodeInfo.getProvidesKey()).onRangeChanged(null);
- if(nodeInfo.getValueUpdater()==null)
- GWT.log("VALUE UPDATER IS NULL");
+ if(nodeInfo==null || nodeInfo.getValueUpdater()==null){}
+
else
nodeInfo.getValueUpdater().update(selectionModel.getSelectedObject());
GSS.get().setCurrentSelection(selectionModel.getSelectedObject());
sinkEvents(Event.ONCONTEXTMENU);
sinkEvents(Event.ONMOUSEUP);
initWidget(tree);
+
DeferredCommand.addCommand(new IncrementalCommand() {
@Override
public void updateNode(RestResource resource){
NodeInfo<RestResource> nodeInfo = (NodeInfo<RestResource>) getModel().getNodeInfo(resource);
- if(nodeInfo!=null){
- if(nodeInfo.getValueUpdater()==null)
- GWT.log("VALUE UPDATER IS NULL");
+
+ if(nodeInfo!=null){
+ if(nodeInfo.getValueUpdater()==null){}
else
nodeInfo.getValueUpdater().update(resource);
}
}
+ public void updateNodeChildren(final RestResource resource){
+
+ if(resource instanceof RestResourceWrapper){
+ boolean updated=false;
+ if(((RestResourceWrapper)resource).getResource().getFolders().size()==0){
+ if(model.getMymap().get(((RestResourceWrapper)resource).getResource().getParentURI())!=null){
+ model.getMymap().get(((RestResourceWrapper)resource).getResource().getParentURI()).refresh(null);
+ updated=true;
+ }
+ if(model.getOthersmap().get(((RestResourceWrapper)resource).getResource().getParentURI())!=null){
+ model.getOthersmap().get(((RestResourceWrapper)resource).getResource().getParentURI()).refresh(null);
+ updated=true;
+ }
+ if(model.getSharedmap().get(((RestResourceWrapper)resource).getResource().getParentURI())!=null){
+ model.getSharedmap().get(((RestResourceWrapper)resource).getResource().getParentURI()).refresh(null);
+ updated=true;
+ }
+ if(updated){
+ if(utils.doesSharedNodeContainsResourceIn1stLevel(resource.getUri())){
+ updateMySharedNode();
+ }
+ return;
+ }
+ }
+ }
+
+ utils.refreshNodeContainingResource(resource);
+ if(utils.doesSharedNodeContainsResourceIn1stLevel(resource.getUri())){
+ updateMySharedNode();
+ }
+
+ }
+ public void updateNodeChildrenForRemove(final String resource){
+ GWT.log("********************************");
+ GWT.log("[UPDATENODECHILDREN]"+resource);
+ GWT.log("********************************");
+ boolean updated=false;
+ TreeNode node=null;
+ if(tree.getRootTreeNode().isChildOpen(0)){
+ node = utils.getNodeContainingResource2(tree.getRootTreeNode().setChildOpen(0,true), resource);
+ }
+ GWT.log("CHECK NODE1:"+node+" "+resource);
+ if(node==null)
+ if(tree.getRootTreeNode().isChildOpen(2)){
+ GWT.log("CHECK NODE2:"+node);
+ node = utils.getNodeContainingResource2(tree.getRootTreeNode().setChildOpen(2,true), resource);
+ }
+ if(node==null)
+ if(tree.getRootTreeNode().isChildOpen(3)){
+ GWT.log("CHECK NODE3:"+node);
+ node = utils.getNodeContainingResource2(tree.getRootTreeNode().setChildOpen(3,true), resource);
+ }
+
+ if(node != null && node.getValue() instanceof RestResourceWrapper){
+ GWT.log("*********************"+((RestResourceWrapper) node.getValue()).getResource().getFolders().size());
+ if(((RestResourceWrapper) node.getValue()).getResource().getFolders().size()==1||((RestResourceWrapper) node.getValue()).getResource().getFolders().size()==0)
+ updateNodeChildren(((RestResourceWrapper) node.getValue()).getResource().getParentURI());
+ else
+ updateNodeChildren(((RestResource) node.getValue()).getUri());
+ return;
+ /*GWT.log("INSIDE:"+node);
+ RestResourceWrapper rw = (RestResourceWrapper) node.getValue();
+ //if(rw.getResource().getFolders().size()==0){
+ if(model.getMymap().get(rw.getResource().getUri())!=null){
+ model.getMymap().get(rw.getResource().getUri()).refresh(null);
+ updated=true;
+ }
+ if(model.getOthersmap().get(rw.getResource().getUri())!=null){
+ model.getOthersmap().get(rw.getResource().getUri()).refresh(null);
+ updated=true;
+ }
+ if(model.getSharedmap().get(rw.getResource().getUri())!=null){
+ model.getSharedmap().get(rw.getResource().getUri()).refresh(null);
+ updated=true;
+ }
+ if(updated){
+ if(doesSharedNodeContainsResourceIn1stLevel(resource)){
+ updateMySharedNode();
+ }
+ return;
+ }
+ }
+ //}*/
+
+ }
+ updateNodeChildren(resource);
+ }
+ public void updateNodeChildren(final String resource){
+
+
+ GWT.log("REFRESH THE OTHER WAY");
+ utils.refreshNodeContainingResource(resource);
+ if(utils.doesSharedNodeContainsResourceIn1stLevel(resource)){
+ updateMySharedNode();
+ }
+ }
+
protected void showPopup(final int x, final int y) {
if (selectionModel.getSelectedObject() == null)
return;
@Override
public void onComplete() {
- myFolders = getResult();
+ myFolders = new MyFolderResource(getResult());
+ //selectionModel.setSelected(myFolders, true);
//rootNodes.setList(Arrays.asList((RestResource)rootResource));
//tree.getRootTreeNode().setChildOpen(0, true);
}
}
if(myFolders==null||trash==null||myshared==null||others==null)
return !DONE;
- rootNodes.setList(Arrays.asList((RestResource)myFolders,(RestResource)trash,(RestResource)myshared,(RestResource)others));
+ model.getRootNodes().setList(Arrays.asList((RestResource)myFolders,(RestResource)trash,(RestResource)myshared,(RestResource)others));
tree.getRootTreeNode().setChildOpen(0, true);
+ selectionModel.setSelected(myFolders, true);
return DONE;
}
}
- class CustomTreeModel implements TreeViewModel{
- private final Cell<RestResource> departmentCell = new AbstractCell<RestResource>("contextmenu"){
-
+ public void updateTrashNode(){
+ DeferredCommand.addCommand(new GetCommand<TrashResource>(TrashResource.class, GSS.get().getCurrentUserResource().getTrashPath(), null) {
@Override
- public void render(com.google.gwt.cell.client.Cell.Context arg0, RestResource arg1, SafeHtmlBuilder arg2) {
- String html=null;
- String name=null;
- if(arg1 instanceof FolderResource){
- FolderResource res = (FolderResource) arg1;
- if(res.isShared())
- html = AbstractImagePrototype.create(images.sharedFolder()).getHTML();
- else if(res.getParentName()==null){
- html = AbstractImagePrototype.create(images.home()).getHTML();
- }
- else
- html = AbstractImagePrototype.create(images.folderYellow()).getHTML();
- name = res.getName();
-
- }
- if(arg1 instanceof TrashResource){
- html = AbstractImagePrototype.create(images.trash()).getHTML();
- name="Trash";
- }
- if(arg1 instanceof SharedResource){
- html = AbstractImagePrototype.create(images.myShared()).getHTML();
- name="My Shared";
- }
- if(arg1 instanceof OthersResource){
- html = AbstractImagePrototype.create(images.othersShared()).getHTML();
- name = "Other's Shared";
- }
- arg2.appendHtmlConstant(html);
- arg2.appendHtmlConstant("<span class='papala'>");
- arg2.appendEscaped(name);
- arg2.appendHtmlConstant("</span>");
+ public void onComplete() {
+ trash = getResult();
+ model.getRootNodes().getList().set(1, trash);
+ //model.getRootNodes().refresh();
}
-
- public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, FolderResource value, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<FolderResource> valueUpdater) {
- GWT.log("-->"+event.getType());
- if(event.getType().equals("contextmenu")){
- selectionModel.setSelected(value, true);
- showPopup(event.getClientX(), event.getClientY());
+
+ @Override
+ public void onError(Throwable t) {
+ if(t instanceof RestException){
+ int statusCode = ((RestException)t).getHttpStatusCode();
+ // On IE status code 1223 may be returned instead of 204.
+ if(statusCode == 204 || statusCode == 1223){
+ trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
+ model.getRootNodes().getList().set(1, trash);
+ //model.getRootNodes().refresh();
+ }
+ else{
+ GWT.log("", t);
+ GSS.get().displayError("Unable to fetch trash folder:"+t.getMessage());
+ trash = new TrashResource(GSS.get().getCurrentUserResource().getTrashPath());
+ model.getRootNodes().getList().set(1, trash);
+ //model.getRootNodes().refresh();
}
- };
-
- };
-
-
- @Override
- public <T> NodeInfo<?> getNodeInfo(T value) {
-
- if(value==null){
- return new DefaultNodeInfo<RestResource>(rootNodes, departmentCell,
- selectionModel, null);
- }
- else if (value instanceof FolderResource) {
- // Second level.
- FolderDataProvider dataProvider = new FolderDataProvider(
- ((FolderResource) value).getUri());
- return new DefaultNodeInfo<RestResource>(dataProvider, departmentCell,
- selectionModel, new ResourceValueUpdater());
- }
- else if (value instanceof SharedResource) {
- // Second level.
- FolderDataProvider dataProvider = new FolderDataProvider(
- ((SharedResource) value).getUri());
- return new DefaultNodeInfo<RestResource>(dataProvider, departmentCell,
- selectionModel, null);
}
- // TODO Auto-generated method stub
- return null;
}
-
- @Override
- public boolean isLeaf(Object value) {
- if(value instanceof FolderResource)
- return ((FolderResource)value).getFolders().size()==0;
- if(value instanceof TrashResource)
- return ((TrashResource)value).getFolders().size()==0;
- return false;
- }
-
+ });
}
- class ResourceValueUpdater implements ValueUpdater<RestResource>{
-
- @Override
- public void update(final RestResource value) {
- if(value instanceof FolderResource){
- GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, value.getUri(), null) {
-
- @Override
- public void onComplete() {
- FolderResource rootResource = getResult();
- ((FolderResource)value).setFiles(rootResource.getFiles());
- if(getSelection().getUri().equals(value.getUri()))
- selectionModel.setSelected(value, true);
- GSS.get().onResourceUpdate(value);
- }
-
- @Override
- public void onError(Throwable t) {
- GWT.log("Error fetching root folder", t);
- GSS.get().displayError("Unable to fetch root folder");
- }
- };
- DeferredCommand.addCommand(gf);
- }
-
- }
-
- }
- class FolderDataProvider extends AsyncDataProvider<RestResource>{
- private final String department;
-
- public FolderDataProvider(String department) {
- super(null);
- this.department = department;
- }
+ public void updateRootNode(){
+ final String path = GSS.get().getCurrentUserResource().getFilesPath();
+ GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, null) {
- @Override
- protected void onRangeChanged(final HasData<RestResource> view) {
- GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, department, null) {
+ @Override
+ public void onComplete() {
+ myFolders = new MyFolderResource(getResult());
+ model.getRootNodes().getList().set(0, myFolders);
+ model.getRootNodes().refresh();
+ tree.getRootTreeNode().setChildOpen(0, true);
+
+ }
- @Override
- public void onComplete() {
- FolderResource rootResource = getResult();
- MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
- rootResource.getSubfolderPaths().toArray(new String[] {}), null) {
-
- @Override
- public void onComplete() {
- List<RestResource> res = new ArrayList<RestResource>();
- res.addAll(getResult());
- updateRowCount(res.size(), true);
- updateRowData(0,res);
- }
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("Error fetching root folder", t);
+ GSS.get().displayError("Unable to fetch root folder");
+ }
- @Override
- public void onError(Throwable t) {
- GSS.get().displayError("Unable to fetch subfolders");
- GWT.log("Unable to fetch subfolders", t);
- }
+ };
+ DeferredCommand.addCommand(gf);
+ }
+
+ public void updateMySharedNode(){
+ GetCommand<SharedResource> gs = new GetCommand<SharedResource>(SharedResource.class, GSS.get().getCurrentUserResource().getSharedPath(), null) {
- @Override
- public void onError(String p, Throwable throwable) {
- GWT.log("Path:"+p, throwable);
- }
+ @Override
+ public void onComplete() {
+ myshared=getResult();
+ model.getRootNodes().getList().set(2, myshared);
+ }
- };
- DeferredCommand.addCommand(gf2);
-
- }
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("Error fetching Shared Root folder", t);
+ GSS.get().displayError("Unable to fetch Shared Root folder");
+ }
+ };
+ DeferredCommand.addCommand(gs);
+ }
+
+ public void updateOtherNode(){
+ GetCommand<OthersResource> go = new GetCommand<OthersResource>(OthersResource.class,
+ GSS.get().getCurrentUserResource().getOthersPath(), null) {
- @Override
- public void onError(Throwable t) {
- GWT.log("Error fetching root folder", t);
- GSS.get().displayError("Unable to fetch root folder");
- }
+ @Override
+ public void onComplete() {
+ others = getResult();
+ model.getRootNodes().getList().set(3, others);
+ }
- };
- DeferredCommand.addCommand(gf);
-
-
- }
-
+ @Override
+ public void onError(Throwable t) {
+ GWT.log("Error fetching Others Root folder", t);
+ GSS.get().displayError("Unable to fetch Others Root folder");
+ }
+ };
+ DeferredCommand.addCommand(go);
}
return selectionModel.getSelectedObject();
}
+ public void clearSelection(){
+ if(GSS.get().getCurrentSelection().equals(getSelection()))
+ GSS.get().setCurrentSelection(null);
+ selectionModel.setSelected(getSelection(), false);
+ }
+
/**
* Retrieve the myFolders.
*
* @return the myFolders
*/
- public FolderResource getMyFolders() {
+ public MyFolderResource getMyFolders() {
return myFolders;
}
public TreeViewModel getModel() {
return model;
}
- /*
- private TreeNode getUserItem(TreeNode parent, RestResource folder) {
- if(parent==null)
- parent = tree.getRootTreeNode();
- TreeNode tmp = null;
- if (parent.getValue() instanceof RestResource &&
- (parent.getValue().equals(folder) ||
- ((FolderResource) parent.getValue()).getUri().equals(folder.getUri())))
- return parent;
- for (int i = 0; i < parent.getChildCount(); i++) {
- boolean op = parent.isChildOpen(i);
- TreeNode child = parent.setChildOpen(index, open, fireEvents)(i);
- if (child.getUserObject() instanceof FolderResource) {
- FolderResource dto = (FolderResource) child.getUserObject();
- if (dto.equals(folder) || dto.getUri().equals(folder.getUri()))
- return child;
+
+
+
+ /**
+ * Retrieve the utils.
+ *
+ * @return the utils
+ */
+ public CellTreeViewUtils getUtils() {
+ return utils;
+ }
+
+ public interface RefreshHandler{
+ void onRefresh();
+ }
+
+
+ public boolean isTrashOrTrashFolderSelected(){
+ return (getSelection() instanceof TrashResource) || (getSelection() instanceof TrashFolderResource);
+ }
+
+ public OtherUserResource getOtherUserResourceOfOtherFolder(OthersFolderResource res){
+ TreeNode n = utils.getNodeContainingResource(tree.getRootTreeNode().setChildOpen(3, true), res);
+
+ if(n!=null){
+ if(n.getValue() instanceof OtherUserResource)
+ return (OtherUserResource) n.getValue();
+ TreeNode parent = n.getParent();
+
+ while (parent!=null){
+ if(parent.getValue() instanceof OtherUserResource)
+ return (OtherUserResource) parent.getValue();
+ parent = parent.getParent();
}
- tmp = getUserItem(child, folder);
- if (tmp != null)
- return tmp;
}
return null;
- }*/
+ }
+
+
}