2 * Copyright 2011-2012 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.mysharedtree;
38 import gr.grnet.pithos.web.client.FolderContextMenu;
39 import gr.grnet.pithos.web.client.Pithos;
40 import gr.grnet.pithos.web.client.foldertree.AccountResource;
41 import gr.grnet.pithos.web.client.foldertree.File;
42 import gr.grnet.pithos.web.client.foldertree.Folder;
43 import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
44 import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView.Templates;
45 import gr.grnet.pithos.web.client.rest.GetRequest;
46 import gr.grnet.pithos.web.client.rest.RestException;
48 import java.util.ArrayList;
49 import java.util.HashMap;
50 import java.util.HashSet;
51 import java.util.Iterator;
52 import java.util.List;
56 import com.google.gwt.cell.client.AbstractCell;
57 import com.google.gwt.cell.client.Cell;
58 import com.google.gwt.cell.client.TextCell;
59 import com.google.gwt.cell.client.ValueUpdater;
60 import com.google.gwt.core.client.GWT;
61 import com.google.gwt.core.client.Scheduler;
62 import com.google.gwt.event.dom.client.ContextMenuEvent;
63 import com.google.gwt.http.client.Response;
64 import com.google.gwt.http.client.URL;
65 import com.google.gwt.safehtml.shared.SafeHtml;
66 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
67 import com.google.gwt.text.shared.SafeHtmlRenderer;
68 import com.google.gwt.user.client.Command;
69 import com.google.gwt.user.client.ui.AbstractImagePrototype;
70 import com.google.gwt.view.client.ListDataProvider;
71 import com.google.gwt.view.client.SelectionChangeEvent;
72 import com.google.gwt.view.client.SelectionChangeEvent.Handler;
73 import com.google.gwt.view.client.SingleSelectionModel;
74 import com.google.gwt.view.client.TreeViewModel;
76 public class MysharedTreeViewModel implements TreeViewModel {
80 private Cell<Folder> folderCell = new AbstractCell<Folder>() {
83 public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
84 String html = AbstractImagePrototype.create(MysharedTreeView.images.folderYellow()).getHTML();
85 safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant(" ");
86 safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
90 public void onBrowserEvent(Context context, com.google.gwt.dom.client.Element parent, final Folder folder, com.google.gwt.dom.client.NativeEvent event, ValueUpdater<Folder> valueUpdater) {
91 if (event.getType().equals(ContextMenuEvent.getType().getName())) {
92 final int x = event.getClientX();
93 final int y = event.getClientY();
94 MysharedTreeViewModel.this.selectionModel.setSelected(folder, true);
95 app.scheduleFolderHeadCommand(folder, new Command() {
98 public void execute() {
99 FolderContextMenu menu = new FolderContextMenu(app, MysharedTreeView.images, app.getSelectedTree(), folder);
100 menu.setPopupPosition(x, y);
108 protected ListDataProvider<Folder> firstLevelDataProvider = new ListDataProvider<Folder>();
110 private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>();
112 protected SingleSelectionModel<Folder> selectionModel;
114 public MysharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
116 this.selectionModel = selectionModel;
120 public <T> NodeInfo<?> getNodeInfo(T value) {
122 fetchSharedContainers(null);
123 return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, selectionModel, null);
125 final Folder f = (Folder) value;
126 if (dataProviderMap.get(f) == null) {
127 dataProviderMap.put(f, new ListDataProvider<Folder>());
129 final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
130 fetchFolder(f, dataProvider, false);
131 return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
134 private void fetchSharedContainers(final Command callback) {
135 String path = "?format=json&shared=";
136 GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), app.getUsername(), path) {
138 public void onSuccess(final AccountResource _result) {
139 Iterator<Folder> iter = _result.getContainers().listIterator();
140 fetchFolder(iter, new Command() {
143 public void execute() {
144 firstLevelDataProvider.getList().clear();
146 for (Folder c : _result.getContainers()) {
148 firstLevelDataProvider.getList().add(0, c); //Pithos is always first
149 else if (!c.isTrash())
150 firstLevelDataProvider.getList().add(c);
152 if (callback != null)
159 public void onError(Throwable t) {
160 GWT.log("Error getting account", t);
162 if (t instanceof RestException)
163 app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
165 app.displayError("System error fetching user data: " + t.getMessage());
169 protected void onUnauthorized(Response response) {
170 app.sessionExpired();
173 getAccount.setHeader("X-Auth-Token", app.getToken());
174 Scheduler.get().scheduleDeferred(getAccount);
178 public boolean isLeaf(Object o) {
181 else if (o instanceof Folder) {
182 Folder f = (Folder) o;
183 return f.getSubfolders().isEmpty();
186 return firstLevelDataProvider.getList().isEmpty();
190 private native void log(String msg) /*-{
191 $wnd.console.log(msg);
194 protected void fetchFolder(final Iterator<Folder> iter, final Command callback) {
195 if (iter.hasNext()) {
196 final Folder f = iter.next();
198 String path = "/" + f.getContainer() + "?format=json&shared=&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
199 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
201 public void onSuccess(Folder _result) {
202 fetchFolder(iter, callback);
206 public void onError(Throwable t) {
207 GWT.log("Error getting folder", t);
209 if (t instanceof RestException)
210 app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
212 app.displayError("System error fetching folder: " + t.getMessage());
216 protected void onUnauthorized(Response response) {
217 app.sessionExpired();
220 getFolder.setHeader("X-Auth-Token", app.getToken());
221 Scheduler.get().scheduleDeferred(getFolder);
223 else if (callback != null)
227 public Folder getSelection() {
228 return selectionModel.getSelectedObject();
231 public void updateFolder(Folder folder, boolean showfiles) {
232 if (dataProviderMap.get(folder) == null) {
233 dataProviderMap.put(folder, new ListDataProvider<Folder>());
235 final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
236 fetchFolder(folder, dataProvider, showfiles);
239 public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles) {
240 String path = "/" + f.getContainer() + "?format=json&shared=&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
241 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
243 public void onSuccess(final Folder _result) {
245 app.showFiles(_result);
246 Iterator<Folder> iter = new ArrayList<Folder>(_result.getSubfolders()).listIterator();
247 fetchFolder(iter, new Command() {
249 public void execute() {
250 dataProvider.getList().clear();
251 dataProvider.getList().addAll(_result.getSubfolders());
252 app.getMySharedTreeView().updateChildren(f);
258 public void onError(Throwable t) {
259 GWT.log("Error getting folder", t);
261 if (t instanceof RestException)
262 app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
264 app.displayError("System error fetching folder: " + t.getMessage());
268 protected void onUnauthorized(Response response) {
269 app.sessionExpired();
272 getFolder.setHeader("X-Auth-Token", app.getToken());
273 Scheduler.get().scheduleDeferred(getFolder);
276 public void initialize(Command callback) {
277 fetchSharedContainers(callback);