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