53320f3a9367e7eaa8240b21109a27b5feaf694d
[pithos] / web_client / src / gr / grnet / pithos / web / client / foldertree / FolderTreeViewModel.java
1 /*
2  * Copyright 2011 GRNET S.A. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above
9  *      copyright notice, this list of conditions and the following
10  *      disclaimer.
11  *
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.
16  *
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.
29  *
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.
34  */
35
36 package gr.grnet.pithos.web.client.foldertree;
37
38 import com.google.gwt.cell.client.AbstractCell;
39 import com.google.gwt.cell.client.Cell;
40 import com.google.gwt.core.client.GWT;
41 import com.google.gwt.core.client.Scheduler;
42 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
43 import com.google.gwt.event.dom.client.ContextMenuEvent;
44 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
45 import com.google.gwt.user.client.ui.AbstractImagePrototype;
46 import com.google.gwt.view.client.ListDataProvider;
47 import com.google.gwt.view.client.SingleSelectionModel;
48 import com.google.gwt.view.client.TreeViewModel;
49 import gr.grnet.pithos.web.client.FolderContextMenu;
50 import gr.grnet.pithos.web.client.GSS;
51 import gr.grnet.pithos.web.client.foldertree.FolderTreeView.Templates;
52 import gr.grnet.pithos.web.client.rest.GetRequest;
53 import gr.grnet.pithos.web.client.rest.RestException;
54 import java.util.Iterator;
55 import java.util.Set;
56
57 public class FolderTreeViewModel implements TreeViewModel {
58
59     private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) {
60
61        @Override
62         public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
63             String html = AbstractImagePrototype.create(FolderTreeView.images.folderYellow()).getHTML();
64             safeHtmlBuilder.appendHtmlConstant(html);
65             safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
66         }
67
68         @Override
69         public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, Folder folder, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<Folder> valueUpdater) {
70             if (event.getType().equals(ContextMenuEvent.getType().getName())) {
71                 Folder target = (Folder) context.getKey();
72                 FolderTreeViewModel.this.selectionModel.setSelected(target, true);
73                 FolderContextMenu menu = new FolderContextMenu(FolderTreeView.images, target);
74                 menu.setPopupPosition(event.getClientX(), event.getClientY());
75                 menu.show();
76             }
77         }
78     };
79
80     private ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>();
81
82     private SingleSelectionModel<Folder> selectionModel;
83
84     public FolderTreeViewModel(SingleSelectionModel<Folder> selectionModel) {
85         this.selectionModel = selectionModel;
86     }
87
88     @Override
89     public <T> NodeInfo<?> getNodeInfo(T value) {
90         if (value == null) {
91             Folder f = new Folder("Loading ...");
92             rootDataProvider.getList().add(f);
93             return new DefaultNodeInfo<Folder>(rootDataProvider, folderCell, selectionModel, null);
94         }
95         else {
96             final Folder f = (Folder) value;
97             final ListDataProvider<Folder> dataProvider = new ListDataProvider<Folder>();
98             dataProvider.flush();
99             Scheduler.get().scheduleDeferred(new ScheduledCommand() {
100                 @Override
101                 public void execute() {
102                     final GSS app = GSS.get();
103                     String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
104                     GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
105                         @Override
106                         public void onSuccess(Folder result) {
107                             Iterator<Folder> iter = result.getSubfolders().iterator();
108                             fetchFolder(iter, dataProvider, result.getSubfolders());
109                         }
110
111                         @Override
112                         public void onError(Throwable t) {
113                             GWT.log("Error getting folder", t);
114                             if (t instanceof RestException)
115                                 GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
116                             else
117                                 GSS.get().displayError("System error fetching folder: " + t.getMessage());
118                         }
119                     };
120                     getFolder.setHeader("X-Auth-Token", app.getToken());
121                     Scheduler.get().scheduleDeferred(getFolder);
122                 }
123             });
124             return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
125         }
126     }
127
128     @Override
129     public boolean isLeaf(Object o) {
130         if (o instanceof Folder) {
131             Folder f = (Folder) o;
132             return f.getSubfolders().isEmpty();
133         }
134         return false;
135     }
136
137     private void fetchFolder(final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Set<Folder> folders) {
138         if (iter.hasNext()) {
139             final Folder f = iter.next();
140
141             GSS app = GSS.get();
142             String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
143             GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
144                 @Override
145                 public void onSuccess(Folder result) {
146                     fetchFolder(iter, dataProvider, folders);
147                 }
148
149                 @Override
150                 public void onError(Throwable t) {
151                     GWT.log("Error getting folder", t);
152                     if (t instanceof RestException)
153                         GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
154                     else
155                         GSS.get().displayError("System error fetching folder: " + t.getMessage());
156                 }
157             };
158             getFolder.setHeader("X-Auth-Token", app.getToken());
159             Scheduler.get().scheduleDeferred(getFolder);
160         }
161         else {
162             dataProvider.getList().clear();
163             dataProvider.getList().addAll(folders);
164             if (dataProvider.equals(rootDataProvider))
165                 selectionModel.setSelected(dataProvider.getList().get(0), true);
166         }
167     }
168
169     public void initialize(AccountResource account) {
170         Iterator<Folder> iter = account.getContainers().iterator();
171         fetchFolder(iter, rootDataProvider, account.getContainers());
172     }
173
174     public Folder getSelection() {
175         return selectionModel.getSelectedObject();
176     }
177 }