root / src / gr / grnet / pithos / web / client / foldertree / FolderTreeViewModel.java @ 04f50dbe
History | View | Annotate | Download (10.6 kB)
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 gr.grnet.pithos.web.client.FolderContextMenu; |
39 |
import gr.grnet.pithos.web.client.Pithos; |
40 |
import gr.grnet.pithos.web.client.foldertree.FolderTreeView.Templates; |
41 |
import gr.grnet.pithos.web.client.rest.GetRequest; |
42 |
import gr.grnet.pithos.web.client.rest.RestException; |
43 |
|
44 |
import java.util.ArrayList; |
45 |
import java.util.HashMap; |
46 |
import java.util.HashSet; |
47 |
import java.util.Iterator; |
48 |
import java.util.Map; |
49 |
|
50 |
import com.google.gwt.cell.client.AbstractCell; |
51 |
import com.google.gwt.cell.client.Cell; |
52 |
import com.google.gwt.core.client.GWT; |
53 |
import com.google.gwt.core.client.Scheduler; |
54 |
import com.google.gwt.event.dom.client.ContextMenuEvent; |
55 |
import com.google.gwt.http.client.Response; |
56 |
import com.google.gwt.http.client.URL; |
57 |
import com.google.gwt.safehtml.shared.SafeHtml; |
58 |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; |
59 |
import com.google.gwt.user.client.Command; |
60 |
import com.google.gwt.user.client.ui.AbstractImagePrototype; |
61 |
import com.google.gwt.user.client.ui.HTML; |
62 |
import com.google.gwt.user.client.ui.UIObject; |
63 |
import com.google.gwt.view.client.AsyncDataProvider; |
64 |
import com.google.gwt.view.client.HasData; |
65 |
import com.google.gwt.view.client.ListDataProvider; |
66 |
import com.google.gwt.view.client.SingleSelectionModel; |
67 |
import com.google.gwt.view.client.TreeViewModel; |
68 |
|
69 |
public class FolderTreeViewModel implements TreeViewModel { |
70 |
|
71 |
protected Pithos app;
|
72 |
|
73 |
private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) { |
74 |
|
75 |
@Override
|
76 |
public void render(@SuppressWarnings("unused") Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) { |
77 |
String html;
|
78 |
SafeHtml name; |
79 |
if (folder.isHome()) {
|
80 |
html = AbstractImagePrototype.create(FolderTreeView.images.home()).getHTML(); |
81 |
name = Templates.INSTANCE.nameSpan("Pithos"); |
82 |
} |
83 |
else if (folder.isTrash()) { |
84 |
html = AbstractImagePrototype.create(FolderTreeView.images.trash()).getHTML(); |
85 |
name = Templates.INSTANCE.nameSpan("Trash"); |
86 |
} |
87 |
else {
|
88 |
html = AbstractImagePrototype.create(folder.isShared() ? FolderTreeView.images.myShared() : FolderTreeView.images.folderYellow()).getHTML(); |
89 |
name = Templates.INSTANCE.nameSpan(folder.getName());
|
90 |
} |
91 |
safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant(" ");
|
92 |
safeHtmlBuilder.append(name); |
93 |
} |
94 |
|
95 |
@Override
|
96 |
public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Folder folder, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<Folder> valueUpdater) { |
97 |
if (event.getType().equals(ContextMenuEvent.getType().getName())) {
|
98 |
FolderTreeViewModel.this.selectionModel.setSelected(folder, true);
|
99 |
FolderContextMenu menu = new FolderContextMenu(app, FolderTreeView.images, app.getSelectedTree(), folder);
|
100 |
menu.setPopupPosition(event.getClientX(), event.getClientY()); |
101 |
menu.show(); |
102 |
} |
103 |
} |
104 |
}; |
105 |
|
106 |
protected ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>(); |
107 |
|
108 |
private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>(); |
109 |
|
110 |
protected SingleSelectionModel<Folder> selectionModel; |
111 |
|
112 |
public FolderTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) { |
113 |
app = _app; |
114 |
this.selectionModel = selectionModel;
|
115 |
} |
116 |
|
117 |
@Override
|
118 |
public <T> NodeInfo<?> getNodeInfo(T value) {
|
119 |
if (value == null) { |
120 |
Folder f = new Folder("Loading ..."); |
121 |
rootDataProvider.getList().add(f); |
122 |
return new DefaultNodeInfo<Folder>(rootDataProvider, folderCell, selectionModel, null); |
123 |
} |
124 |
final Folder f = (Folder) value;
|
125 |
if (dataProviderMap.get(f) == null) { |
126 |
dataProviderMap.put(f, new ListDataProvider<Folder>());
|
127 |
} |
128 |
final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
|
129 |
//This prevents the loading indicator
|
130 |
// dataProvider.getList().clear();
|
131 |
// dataProvider.getList().addAll(f.getSubfolders());
|
132 |
fetchFolder(f, dataProvider, false, null); |
133 |
return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null); |
134 |
} |
135 |
|
136 |
@Override
|
137 |
public boolean isLeaf(Object o) { |
138 |
if (o instanceof Folder) { |
139 |
Folder f = (Folder) o; |
140 |
return f.getSubfolders().isEmpty();
|
141 |
} |
142 |
return false; |
143 |
} |
144 |
|
145 |
protected void fetchFolder(final Iterator<Folder> iter, final Command callback) { |
146 |
if (iter.hasNext()) {
|
147 |
final Folder f = iter.next();
|
148 |
|
149 |
String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()); |
150 |
GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
|
151 |
@Override
|
152 |
public void onSuccess(@SuppressWarnings("unused") Folder _result) { |
153 |
fetchFolder(iter, callback); |
154 |
} |
155 |
|
156 |
@Override
|
157 |
public void onError(Throwable t) { |
158 |
GWT.log("Error getting folder", t);
|
159 |
if (t instanceof RestException) |
160 |
app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
161 |
else
|
162 |
app.displayError("System error fetching folder: " + t.getMessage());
|
163 |
} |
164 |
|
165 |
@Override
|
166 |
protected void onUnauthorized(Response response) { |
167 |
app.sessionExpired(); |
168 |
} |
169 |
}; |
170 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
171 |
Scheduler.get().scheduleDeferred(getFolder); |
172 |
} |
173 |
else if (callback != null) |
174 |
callback.execute(); |
175 |
} |
176 |
|
177 |
public void initialize(final AccountResource account, final Command callback) { |
178 |
Iterator<Folder> iter = account.getContainers().iterator();
|
179 |
fetchFolder(iter, new Command() {
|
180 |
@Override
|
181 |
public void execute() { |
182 |
rootDataProvider.getList().clear(); |
183 |
Folder t = null;
|
184 |
for (Folder c : account.getContainers()) {
|
185 |
if (c.isHome())
|
186 |
rootDataProvider.getList().add(0, c); //Pithos is always first |
187 |
else if (c.isTrash()) |
188 |
t = c; //Keep trash for adding in the end
|
189 |
else
|
190 |
rootDataProvider.getList().add(c); |
191 |
} |
192 |
if (t != null) |
193 |
rootDataProvider.getList().add(t); |
194 |
selectionModel.setSelected(rootDataProvider.getList().get(0), true); |
195 |
if (callback != null) |
196 |
callback.execute(); |
197 |
} |
198 |
}); |
199 |
} |
200 |
|
201 |
public Folder getSelection() {
|
202 |
return selectionModel.getSelectedObject();
|
203 |
} |
204 |
|
205 |
public void updateFolder(final Folder folder, boolean showfiles, final Command callback) { |
206 |
if (dataProviderMap.get(folder) == null) { |
207 |
dataProviderMap.put(folder, new ListDataProvider<Folder>());
|
208 |
} |
209 |
final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
|
210 |
fetchFolder(folder, dataProvider, showfiles, new Command() {
|
211 |
|
212 |
@Override
|
213 |
public void execute() { |
214 |
app.getFolderTreeView().openFolder(folder); |
215 |
if (callback != null) |
216 |
callback.execute(); |
217 |
} |
218 |
}); |
219 |
} |
220 |
|
221 |
public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles, final Command callback) { |
222 |
String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()); |
223 |
GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
|
224 |
@Override
|
225 |
public void onSuccess(final Folder _result) { |
226 |
if (showfiles)
|
227 |
app.showFiles(_result); |
228 |
Iterator<Folder> iter = _result.getSubfolders().iterator();
|
229 |
fetchFolder(iter, new Command() {
|
230 |
@Override
|
231 |
public void execute() { |
232 |
dataProvider.getList().clear(); |
233 |
dataProvider.getList().addAll(_result.getSubfolders()); |
234 |
// app.getFolderTreeView().updateChildren(f);
|
235 |
if (callback != null) |
236 |
callback.execute(); |
237 |
} |
238 |
}); |
239 |
} |
240 |
|
241 |
@Override
|
242 |
public void onError(Throwable t) { |
243 |
GWT.log("Error getting folder", t);
|
244 |
if (t instanceof RestException) |
245 |
app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
246 |
else
|
247 |
app.displayError("System error fetching folder: " + t.getMessage());
|
248 |
} |
249 |
|
250 |
@Override
|
251 |
protected void onUnauthorized(Response response) { |
252 |
app.sessionExpired(); |
253 |
} |
254 |
}; |
255 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
256 |
Scheduler.get().scheduleDeferred(getFolder); |
257 |
} |
258 |
} |