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.rest.GetCommand;
22 import gr.ebs.gss.client.rest.resource.FolderResource;
23 import gr.ebs.gss.client.rest.resource.FolderResource;
24 import gr.ebs.gss.client.rest.resource.UserResource;
26 import java.util.Arrays;
28 import com.google.gwt.cell.client.AbstractCell;
29 import com.google.gwt.cell.client.Cell;
30 import com.google.gwt.core.client.GWT;
31 import com.google.gwt.resources.client.ClientBundle;
32 import com.google.gwt.resources.client.ImageResource;
33 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
34 import com.google.gwt.user.cellview.client.CellTree;
35 import com.google.gwt.user.cellview.client.TreeNode;
36 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
37 import com.google.gwt.user.client.DeferredCommand;
38 import com.google.gwt.user.client.IncrementalCommand;
39 import com.google.gwt.user.client.ui.Composite;
40 import com.google.gwt.user.client.ui.Tree;
41 import com.google.gwt.view.client.AsyncDataProvider;
42 import com.google.gwt.view.client.HasData;
43 import com.google.gwt.view.client.ListDataProvider;
44 import com.google.gwt.view.client.Range;
45 import com.google.gwt.view.client.SingleSelectionModel;
46 import com.google.gwt.view.client.TreeViewModel;
47 import com.google.gwt.view.client.TreeViewModel.DefaultNodeInfo;
54 public class CellTreeView extends Composite{
55 public static final boolean DONE = false;
57 private final ListDataProvider<FolderResource> rootNodes = new ListDataProvider<FolderResource>();
58 SingleSelectionModel<FolderResource> selectionModel = new SingleSelectionModel<FolderResource>();
60 * Specifies the images that will be bundled for this Composite and other
61 * inherited images that will be included in the same bundle.
63 public interface Images extends ClientBundle, Tree.Resources, FolderContextMenu.Images {
65 @Source("gr/ebs/gss/resources/folder_home.png")
68 @Source("gr/ebs/gss/resources/folder_yellow.png")
69 ImageResource folderYellow();
71 @Source("gr/ebs/gss/resources/mimetypes/document.png")
72 ImageResource document();
74 @Source("gr/ebs/gss/resources/internet.png")
75 ImageResource othersShared();
77 @Source("gr/ebs/gss/resources/edit_user.png")
78 ImageResource myShared();
80 @Source("gr/ebs/gss/resources/folder_user.png")
81 ImageResource sharedFolder();
83 @Source("gr/ebs/gss/resources/trashcan_empty.png")
84 ImageResource trash();
90 public CellTreeView(final Images _images) {
92 TreeViewModel model = new CustomTreeModel();
95 * Create the tree using the model. We use <code>null</code> as the default
96 * value of the root node. The default value will be passed to
97 * CustomTreeModel#getNodeInfo();
99 CellTree tree = new CellTree(model, null);
100 tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
104 DeferredCommand.addCommand(new IncrementalCommand() {
107 public boolean execute() {
108 return fetchRootFolders();
112 public boolean fetchRootFolders() {
113 UserResource userResource = GSS.get().getCurrentUserResource();
114 if (userResource == null)
117 final String path = userResource.getFilesPath();
118 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, null) {
121 public void onComplete() {
122 FolderResource rootResource = getResult();
123 rootNodes.setList(Arrays.asList((FolderResource)rootResource));
128 public void onError(Throwable t) {
129 GWT.log("Error fetching root folder", t);
130 GSS.get().displayError("Unable to fetch root folder");
134 DeferredCommand.addCommand(gf);
138 public Images getImages() {
143 class CustomTreeModel implements TreeViewModel{
144 private final Cell<FolderResource> departmentCell = new AbstractCell<FolderResource>(){
147 public void render(com.google.gwt.cell.client.Cell.Context arg0, FolderResource arg1, SafeHtmlBuilder arg2) {
148 arg2.appendEscaped(arg1.getName());
156 public <T> NodeInfo<?> getNodeInfo(T value) {
158 return new DefaultNodeInfo<FolderResource>(rootNodes, departmentCell,
159 selectionModel, null);
161 else if (value instanceof FolderResource) {
163 FolderDataProvider dataProvider = new FolderDataProvider(
164 ((FolderResource) value).getUri());
165 return new DefaultNodeInfo<FolderResource>(dataProvider, departmentCell,
166 selectionModel, null);
168 // TODO Auto-generated method stub
173 public boolean isLeaf(Object value) {
174 /*if(value instanceof FolderResource)
175 return ((FolderResource)value).getFolders().size()==0;*/
181 class FolderDataProvider extends AsyncDataProvider<FolderResource>{
182 private final String department;
184 public FolderDataProvider(String department) {
186 this.department = department;
190 public void addDataDisplay(HasData<FolderResource> display) {
191 super.addDataDisplay(display);
193 // Request the count anytime a view is added.
194 requestFactory.employeeRequest().countEmployeesByDepartment(department).fire(
195 new Receiver<Long>() {
197 public void onSuccess(Long response) {
198 updateRowCount(response.intValue(), true);
204 protected void onRangeChanged(final HasData<FolderResource> view) {
205 Range range = view.getVisibleRange();
206 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, department, null) {
209 public void onComplete() {
210 FolderResource rootResource = getResult();
213 if(rootResource.getFolders().size()!=0){
214 updateRowCount(rootResource.getFolders().size(), true);
215 updateRowData(0,rootResource.getFolders());
218 updateRowCount(rootResource.getFolders().size(), true);
219 updateRowData(0,rootResource.getFolders());
220 removeDataDisplay(view);
225 public void onError(Throwable t) {
226 GWT.log("Error fetching root folder", t);
227 GSS.get().displayError("Unable to fetch root folder");
231 DeferredCommand.addCommand(gf);