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