2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and
31 * documentation are those of the authors and should not be
32 * interpreted as representing official policies, either expressed
33 * or implied, of GRNET S.A.
36 package gr.grnet.pithos.web.client.foldertree;
38 import com.google.gwt.core.client.GWT;
39 import com.google.gwt.core.client.Scheduler;
40 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
41 import com.google.gwt.view.client.ListDataProvider;
42 import com.google.gwt.view.client.SelectionChangeEvent;
43 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
44 import com.google.gwt.view.client.SingleSelectionModel;
45 import com.google.gwt.view.client.TreeViewModel;
46 import gr.grnet.pithos.web.client.GSS;
47 import gr.grnet.pithos.web.client.foldertree.FolderTreeView.FolderCell;
48 import gr.grnet.pithos.web.client.rest.GetRequest;
49 import gr.grnet.pithos.web.client.rest.RestException;
50 import java.util.Iterator;
53 public class FolderTreeViewModel implements TreeViewModel {
55 private ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>();
57 private SingleSelectionModel<Folder> selectionModel;
59 public FolderTreeViewModel(SingleSelectionModel<Folder> selectionModel) {
60 this.selectionModel = selectionModel;
64 public <T> NodeInfo<?> getNodeInfo(T value) {
66 Folder f = new Folder("Loading ...");
67 rootDataProvider.getList().add(f);
68 return new DefaultNodeInfo<Folder>(rootDataProvider, new FolderCell(), selectionModel, null);
71 final Folder f = (Folder) value;
72 final ListDataProvider<Folder> dataProvider = new ListDataProvider<Folder>();
74 Scheduler.get().scheduleDeferred(new ScheduledCommand() {
76 public void execute() {
77 final GSS app = GSS.get();
78 String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
79 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
81 public void onSuccess(Folder result) {
82 Iterator<Folder> iter = result.getSubfolders().iterator();
83 fetchFolder(iter, dataProvider, result.getSubfolders());
87 public void onError(Throwable t) {
88 GWT.log("Error getting folder", t);
89 if (t instanceof RestException)
90 GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
92 GSS.get().displayError("System error fetching folder: " + t.getMessage());
95 getFolder.setHeader("X-Auth-Token", app.getToken());
96 Scheduler.get().scheduleDeferred(getFolder);
99 return new DefaultNodeInfo<Folder>(dataProvider, new FolderCell(), selectionModel, null);
104 public boolean isLeaf(Object o) {
105 if (o instanceof Folder) {
106 Folder f = (Folder) o;
107 return f.getSubfolders().isEmpty();
112 private void fetchFolder(final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Set<Folder> folders) {
113 if (iter.hasNext()) {
114 final Folder f = iter.next();
117 String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
118 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
120 public void onSuccess(Folder result) {
121 fetchFolder(iter, dataProvider, folders);
125 public void onError(Throwable t) {
126 GWT.log("Error getting folder", t);
127 if (t instanceof RestException)
128 GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
130 GSS.get().displayError("System error fetching folder: " + t.getMessage());
133 getFolder.setHeader("X-Auth-Token", app.getToken());
134 Scheduler.get().scheduleDeferred(getFolder);
137 dataProvider.getList().clear();
138 dataProvider.getList().addAll(folders);
139 if (dataProvider.equals(rootDataProvider))
140 selectionModel.setSelected(dataProvider.getList().get(0), true);
144 public void initialize(AccountResource account) {
145 Iterator<Folder> iter = account.getContainers().iterator();
146 fetchFolder(iter, rootDataProvider, account.getContainers());
149 public Folder getSelection() {
150 return selectionModel.getSelectedObject();