root / src / gr / grnet / pithos / web / client / mysharedtree / MysharedTreeViewModel.java @ 7e8e9ff0
History | View | Annotate | Download (11 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2012 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.mysharedtree; |
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.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; |
47 |
|
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; |
53 |
import java.util.Map; |
54 |
import java.util.Set; |
55 |
|
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; |
75 |
|
76 |
public class MysharedTreeViewModel implements TreeViewModel { |
77 |
|
78 |
protected Pithos app;
|
79 |
|
80 |
private Cell<Folder> folderCell = new AbstractCell<Folder>() { |
81 |
|
82 |
@Override
|
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()));
|
87 |
} |
88 |
|
89 |
@Override
|
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() {
|
96 |
|
97 |
@Override
|
98 |
public void execute() { |
99 |
FolderContextMenu menu = new FolderContextMenu(app, MysharedTreeView.images, app.getSelectedTree(), folder);
|
100 |
menu.setPopupPosition(x, y); |
101 |
menu.show(); |
102 |
} |
103 |
}); |
104 |
} |
105 |
} |
106 |
}; |
107 |
|
108 |
protected ListDataProvider<Folder> firstLevelDataProvider = new ListDataProvider<Folder>(); |
109 |
|
110 |
private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>(); |
111 |
|
112 |
protected SingleSelectionModel<Folder> selectionModel; |
113 |
|
114 |
public MysharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) { |
115 |
app = _app; |
116 |
this.selectionModel = selectionModel;
|
117 |
} |
118 |
|
119 |
@Override
|
120 |
public <T> NodeInfo<?> getNodeInfo(T value) {
|
121 |
if (value == null) { |
122 |
fetchSharedContainers(null);
|
123 |
return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, selectionModel, null); |
124 |
} |
125 |
final Folder f = (Folder) value;
|
126 |
if (dataProviderMap.get(f) == null) { |
127 |
dataProviderMap.put(f, new ListDataProvider<Folder>());
|
128 |
} |
129 |
final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
|
130 |
fetchFolder(f, dataProvider, false);
|
131 |
return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null); |
132 |
} |
133 |
|
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) {
|
137 |
@Override
|
138 |
public void onSuccess(final AccountResource _result) { |
139 |
Iterator<Folder> iter = _result.getContainers().listIterator();
|
140 |
fetchFolder(iter, new Command() {
|
141 |
|
142 |
@Override
|
143 |
public void execute() { |
144 |
firstLevelDataProvider.getList().clear(); |
145 |
Folder t = null;
|
146 |
for (Folder c : _result.getContainers()) {
|
147 |
if (c.isHome())
|
148 |
firstLevelDataProvider.getList().add(0, c); //Pithos is always first |
149 |
else if (!c.isTrash()) |
150 |
firstLevelDataProvider.getList().add(c); |
151 |
} |
152 |
if (callback != null) |
153 |
callback.execute(); |
154 |
} |
155 |
}); |
156 |
} |
157 |
|
158 |
@Override
|
159 |
public void onError(Throwable t) { |
160 |
GWT.log("Error getting account", t);
|
161 |
app.setError(t); |
162 |
if (t instanceof RestException) |
163 |
app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
|
164 |
else
|
165 |
app.displayError("System error fetching user data: " + t.getMessage());
|
166 |
} |
167 |
|
168 |
@Override
|
169 |
protected void onUnauthorized(Response response) { |
170 |
app.sessionExpired(); |
171 |
} |
172 |
}; |
173 |
getAccount.setHeader("X-Auth-Token", app.getToken());
|
174 |
Scheduler.get().scheduleDeferred(getAccount); |
175 |
} |
176 |
|
177 |
@Override
|
178 |
public boolean isLeaf(Object o) { |
179 |
if (o == null) |
180 |
return false; |
181 |
else if (o instanceof Folder) { |
182 |
Folder f = (Folder) o; |
183 |
return f.getSubfolders().isEmpty();
|
184 |
} |
185 |
else {
|
186 |
return firstLevelDataProvider.getList().isEmpty();
|
187 |
} |
188 |
} |
189 |
|
190 |
private native void log(String msg) /*-{ |
191 |
$wnd.console.log(msg);
|
192 |
}-*/;
|
193 |
|
194 |
protected void fetchFolder(final Iterator<Folder> iter, final Command callback) { |
195 |
if (iter.hasNext()) {
|
196 |
final Folder f = iter.next();
|
197 |
|
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) {
|
200 |
@Override
|
201 |
public void onSuccess(Folder _result) { |
202 |
fetchFolder(iter, callback); |
203 |
} |
204 |
|
205 |
@Override
|
206 |
public void onError(Throwable t) { |
207 |
GWT.log("Error getting folder", t);
|
208 |
app.setError(t); |
209 |
if (t instanceof RestException) |
210 |
app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
211 |
else
|
212 |
app.displayError("System error fetching folder: " + t.getMessage());
|
213 |
} |
214 |
|
215 |
@Override
|
216 |
protected void onUnauthorized(Response response) { |
217 |
app.sessionExpired(); |
218 |
} |
219 |
}; |
220 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
221 |
Scheduler.get().scheduleDeferred(getFolder); |
222 |
} |
223 |
else if (callback != null) |
224 |
callback.execute(); |
225 |
} |
226 |
|
227 |
public Folder getSelection() {
|
228 |
return selectionModel.getSelectedObject();
|
229 |
} |
230 |
|
231 |
public void updateFolder(Folder folder, boolean showfiles) { |
232 |
if (dataProviderMap.get(folder) == null) { |
233 |
dataProviderMap.put(folder, new ListDataProvider<Folder>());
|
234 |
} |
235 |
final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
|
236 |
fetchFolder(folder, dataProvider, showfiles); |
237 |
} |
238 |
|
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) {
|
242 |
@Override
|
243 |
public void onSuccess(final Folder _result) { |
244 |
if (showfiles)
|
245 |
app.showFiles(_result); |
246 |
Iterator<Folder> iter = new ArrayList<Folder>(_result.getSubfolders()).listIterator(); |
247 |
fetchFolder(iter, new Command() {
|
248 |
@Override
|
249 |
public void execute() { |
250 |
dataProvider.getList().clear(); |
251 |
dataProvider.getList().addAll(_result.getSubfolders()); |
252 |
app.getMySharedTreeView().updateChildren(f); |
253 |
} |
254 |
}); |
255 |
} |
256 |
|
257 |
@Override
|
258 |
public void onError(Throwable t) { |
259 |
GWT.log("Error getting folder", t);
|
260 |
app.setError(t); |
261 |
if (t instanceof RestException) |
262 |
app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
263 |
else
|
264 |
app.displayError("System error fetching folder: " + t.getMessage());
|
265 |
} |
266 |
|
267 |
@Override
|
268 |
protected void onUnauthorized(Response response) { |
269 |
app.sessionExpired(); |
270 |
} |
271 |
}; |
272 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
273 |
Scheduler.get().scheduleDeferred(getFolder); |
274 |
} |
275 |
|
276 |
public void initialize(Command callback) { |
277 |
fetchSharedContainers(callback); |
278 |
} |
279 |
} |