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.dnd.DnDTreeItem;
22 import gr.ebs.gss.client.rest.GetCommand;
23 import gr.ebs.gss.client.rest.MultipleGetCommand;
24 import gr.ebs.gss.client.rest.resource.FolderResource;
25 import gr.ebs.gss.client.rest.resource.FolderResource;
26 import gr.ebs.gss.client.rest.resource.UserResource;
28 import java.util.Arrays;
29 import java.util.List;
31 import com.google.gwt.cell.client.AbstractCell;
32 import com.google.gwt.cell.client.Cell;
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.event.dom.client.ContextMenuEvent;
35 import com.google.gwt.event.dom.client.ContextMenuHandler;
36 import com.google.gwt.resources.client.ClientBundle;
37 import com.google.gwt.resources.client.ImageResource;
38 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
39 import com.google.gwt.user.cellview.client.CellTree;
40 import com.google.gwt.user.cellview.client.TreeNode;
41 import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
42 import com.google.gwt.user.client.DeferredCommand;
43 import com.google.gwt.user.client.Event;
44 import com.google.gwt.user.client.IncrementalCommand;
45 import com.google.gwt.user.client.ui.AbstractImagePrototype;
46 import com.google.gwt.user.client.ui.Composite;
47 import com.google.gwt.user.client.ui.Tree;
48 import com.google.gwt.user.client.ui.TreeItem;
49 import com.google.gwt.view.client.AsyncDataProvider;
50 import com.google.gwt.view.client.HasData;
51 import com.google.gwt.view.client.ListDataProvider;
52 import com.google.gwt.view.client.Range;
53 import com.google.gwt.view.client.SingleSelectionModel;
54 import com.google.gwt.view.client.TreeViewModel;
55 import com.google.gwt.view.client.TreeViewModel.DefaultNodeInfo;
62 public class CellTreeView extends Composite{
63 public static final boolean DONE = false;
65 private final ListDataProvider<FolderResource> rootNodes = new ListDataProvider<FolderResource>();
66 SingleSelectionModel<FolderResource> selectionModel = new SingleSelectionModel<FolderResource>();
67 FolderContextMenu menu;
69 * Specifies the images that will be bundled for this Composite and other
70 * inherited images that will be included in the same bundle.
72 public interface Images extends ClientBundle, Tree.Resources, FolderContextMenu.Images {
74 @Source("gr/ebs/gss/resources/folder_home.png")
77 @Source("gr/ebs/gss/resources/folder_yellow.png")
78 ImageResource folderYellow();
80 @Source("gr/ebs/gss/resources/mimetypes/document.png")
81 ImageResource document();
83 @Source("gr/ebs/gss/resources/internet.png")
84 ImageResource othersShared();
86 @Source("gr/ebs/gss/resources/edit_user.png")
87 ImageResource myShared();
89 @Source("gr/ebs/gss/resources/folder_user.png")
90 ImageResource sharedFolder();
92 @Source("gr/ebs/gss/resources/trashcan_empty.png")
93 ImageResource trash();
99 public CellTreeView(final Images _images) {
101 TreeViewModel model = new CustomTreeModel();
104 * Create the tree using the model. We use <code>null</code> as the default
105 * value of the root node. The default value will be passed to
106 * CustomTreeModel#getNodeInfo();
108 CellTree.Resources res = GWT.create(CellTree.BasicResources.class);
109 tree = new CellTree(model,null, res){
111 public void onBrowserEvent(Event event) {
112 // TODO Auto-generated method stub
113 super.onBrowserEvent(event);
114 GWT.log(event.getType());
117 tree.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
119 sinkEvents(Event.ONCONTEXTMENU);
120 sinkEvents(Event.ONMOUSEUP);
122 DeferredCommand.addCommand(new IncrementalCommand() {
125 public boolean execute() {
126 return fetchRootFolders();
131 protected void showPopup(final int x, final int y) {
132 if (selectionModel.getSelectedObject() == null)
136 menu = new FolderContextMenu(images);
137 menu.setPopupPosition(x, y);
141 public boolean fetchRootFolders() {
142 UserResource userResource = GSS.get().getCurrentUserResource();
143 if (userResource == null)
146 final String path = userResource.getFilesPath();
147 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, path, null) {
150 public void onComplete() {
151 FolderResource rootResource = getResult();
152 rootNodes.setList(Arrays.asList((FolderResource)rootResource));
153 tree.getRootTreeNode().setChildOpen(0, true);
158 public void onError(Throwable t) {
159 GWT.log("Error fetching root folder", t);
160 GSS.get().displayError("Unable to fetch root folder");
164 DeferredCommand.addCommand(gf);
168 public Images getImages() {
173 class CustomTreeModel implements TreeViewModel{
174 private final Cell<FolderResource> departmentCell = new AbstractCell<FolderResource>("contextmenu"){
177 public void render(com.google.gwt.cell.client.Cell.Context arg0, FolderResource arg1, SafeHtmlBuilder arg2) {
179 arg2.appendHtmlConstant(AbstractImagePrototype.create(images.sharedFolder()).getHTML());
180 else if(arg1.getParentName()==null){
181 arg2.appendHtmlConstant(AbstractImagePrototype.create(images.home()).getHTML());
184 arg2.appendHtmlConstant(AbstractImagePrototype.create(images.folderYellow()).getHTML());
185 arg2.appendEscaped(arg1.getName());
189 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) {
190 GWT.log("-->"+event.getType());
191 if(event.getType().equals("contextmenu")){
192 selectionModel.setSelected(value, true);
193 showPopup(event.getClientX(), event.getClientY());
201 public <T> NodeInfo<?> getNodeInfo(T value) {
204 return new DefaultNodeInfo<FolderResource>(rootNodes, departmentCell,
205 selectionModel, null);
207 else if (value instanceof FolderResource) {
209 FolderDataProvider dataProvider = new FolderDataProvider(
210 ((FolderResource) value).getUri());
211 return new DefaultNodeInfo<FolderResource>(dataProvider, departmentCell,
212 selectionModel, null);
214 // TODO Auto-generated method stub
219 public boolean isLeaf(Object value) {
220 if(value instanceof FolderResource)
221 return ((FolderResource)value).getFolders().size()==0;
227 class FolderDataProvider extends AsyncDataProvider<FolderResource>{
228 private final String department;
230 public FolderDataProvider(String department) {
232 this.department = department;
236 public void addDataDisplay(HasData<FolderResource> display) {
237 super.addDataDisplay(display);
239 // Request the count anytime a view is added.
240 requestFactory.employeeRequest().countEmployeesByDepartment(department).fire(
241 new Receiver<Long>() {
243 public void onSuccess(Long response) {
244 updateRowCount(response.intValue(), true);
250 protected void onRangeChanged(final HasData<FolderResource> view) {
251 Range range = view.getVisibleRange();
252 GetCommand<FolderResource> gf = new GetCommand<FolderResource>(FolderResource.class, department, null) {
255 public void onComplete() {
256 FolderResource rootResource = getResult();
257 MultipleGetCommand<FolderResource> gf2 = new MultipleGetCommand<FolderResource>(FolderResource.class,
258 rootResource.getSubfolderPaths().toArray(new String[] {}), null) {
261 public void onComplete() {
262 List<FolderResource> res = getResult();
263 updateRowCount(res.size(), true);
264 updateRowData(0,res);
268 public void onError(Throwable t) {
269 GSS.get().displayError("Unable to fetch subfolders");
270 GWT.log("Unable to fetch subfolders", t);
274 public void onError(String p, Throwable throwable) {
275 GWT.log("Path:"+p, throwable);
279 DeferredCommand.addCommand(gf2);
284 public void onError(Throwable t) {
285 GWT.log("Error fetching root folder", t);
286 GSS.get().displayError("Unable to fetch root folder");
290 DeferredCommand.addCommand(gf);