2 * Copyright 2008, 2009 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.tree;
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.PopupTree;
23 import gr.ebs.gss.client.Folders.Images;
24 import gr.ebs.gss.client.dnd.DnDTreeItem;
25 import gr.ebs.gss.client.rest.GetCommand;
26 import gr.ebs.gss.client.rest.MultipleGetCommand;
27 import gr.ebs.gss.client.rest.resource.FolderResource;
28 import gr.ebs.gss.client.rest.resource.UserResource;
30 import java.util.List;
32 import com.google.gwt.core.client.GWT;
33 import com.google.gwt.user.client.DeferredCommand;
34 import com.google.gwt.user.client.IncrementalCommand;
35 import com.google.gwt.user.client.ui.TreeItem;
40 public class FolderSubtree extends Subtree {
43 * A constant that denotes the completion of an IncrementalCommand.
45 public static final boolean DONE = false;
47 private DnDTreeItem rootItem;
49 public FolderSubtree(PopupTree aTree, final Images _images) {
50 super(aTree, _images);
52 DeferredCommand.addCommand(new IncrementalCommand() {
54 public boolean execute() {
55 return fetchRootFolder();
60 public boolean fetchRootFolder() {
61 UserResource userResource = GSS.get().getCurrentUserResource();
62 if (userResource == null)
65 final String path = userResource.getFilesPath();
66 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path) {
69 public void onComplete() {
70 FolderResource rootResource = getResult();
71 rootItem = new DnDTreeItem(imageItemHTML(images.home(), rootResource.getName()), rootResource.getName(), false);
72 rootItem.setUserObject(rootResource);
74 tree.addItem(rootItem);
75 rootItem.doDroppable();
76 GSS.get().getFolders().select(rootItem);
77 updateSubFoldersLazily(rootItem, rootResource.getFolders(), images.folderYellow(), images.sharedFolder());
78 rootItem.setState(true);
82 public void onError(Throwable t) {
83 GWT.log("Error fetching root folder", t);
84 GSS.get().displayError("Unable to fetch root folder");
86 rootItem = new DnDTreeItem(imageItemHTML(images.home(), "ERROR"), "ERROR", false);
88 tree.addItem(rootItem);
93 DeferredCommand.addCommand(gf);
97 public void updateSubfolders(final DnDTreeItem folderItem) {
98 if (folderItem.getFolderResource() == null) {
99 GWT.log("folder resource is null", null);
102 updateNodes(folderItem);
105 private void updateNodes(final DnDTreeItem folderItem) {
106 String parentName = "";
107 if (folderItem.getParentItem() != null)
108 parentName = ((DnDTreeItem) folderItem.getParentItem()).getFolderResource().getName() + "->";
109 parentName = parentName + folderItem.getFolderResource().getName();
110 MultipleGetCommand<FolderResource> gf = new MultipleGetCommand<FolderResource>(FolderResource.class,
111 folderItem.getFolderResource().getSubfolderPaths().toArray(new String[] {})) {
114 public void onComplete() {
115 List<FolderResource> res = getResult();
116 folderItem.getFolderResource().setFolders(res);
117 updateSubFoldersLazily(folderItem, res, images.folderYellow(), images.sharedFolder());
118 for (int i = 0; i < folderItem.getChildCount(); i++) {
119 DnDTreeItem anItem = (DnDTreeItem) folderItem.getChild(i);
120 updateSubFoldersLazily(anItem, anItem.getFolderResource().getFolders(), images.folderYellow(), images.sharedFolder());
121 anItem.setState(false);
126 public void onError(Throwable t) {
127 GSS.get().displayError("Unable to fetch subfolders");
128 GWT.log("Unable to fetch subfolders", t);
132 public void onError(String p, Throwable throwable) {
133 GWT.log("Path:"+p, throwable);
137 DeferredCommand.addCommand(gf);
140 public void updateFolderAndSubfolders(final DnDTreeItem folderItem) {
141 final String path = folderItem.getFolderResource().getUri();
142 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path) {
145 public void onComplete() {
146 FolderResource rootResource = getResult();
147 if (!folderItem.equals(rootItem)) {
148 folderItem.undoDraggable();
149 if(rootResource.isShared())
150 folderItem.updateWidget(imageItemHTML(images.sharedFolder(), rootResource.getName()));
152 folderItem.updateWidget(imageItemHTML(images.folderYellow(), rootResource.getName()));
153 folderItem.setUserObject(rootResource);
154 folderItem.doDraggable();
156 folderItem.undoDroppable();
157 folderItem.setUserObject(rootResource);
158 folderItem.updateWidget(imageItemHTML(images.home(), rootResource.getName()));
159 folderItem.doDroppable();
161 updateSubfolders(folderItem);
165 public void onError(Throwable t) {
166 GWT.log("Error fetching folder", t);
167 GSS.get().displayError("Unable to fetch folder:" + folderItem.getFolderResource().getName());
170 DeferredCommand.addCommand(gf);
174 * Retrieve the rootItem.
176 * @return the rootItem
178 public TreeItem getRootItem() {