root / src / gr / grnet / pithos / web / client / foldertree / FolderTreeViewModel.java @ fea6cde7
History | View | Annotate | Download (10.4 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.Iterator; |
47 |
import java.util.Map; |
48 |
|
49 |
import com.google.gwt.cell.client.AbstractCell; |
50 |
import com.google.gwt.cell.client.Cell; |
51 |
import com.google.gwt.core.client.GWT; |
52 |
import com.google.gwt.core.client.Scheduler; |
53 |
import com.google.gwt.event.dom.client.ContextMenuEvent; |
54 |
import com.google.gwt.http.client.Response; |
55 |
import com.google.gwt.http.client.URL; |
56 |
import com.google.gwt.safehtml.shared.SafeHtml; |
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 FolderTreeViewModel implements TreeViewModel { |
65 |
|
66 |
protected Pithos app;
|
67 |
|
68 |
private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) { |
69 |
|
70 |
@Override
|
71 |
public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) { |
72 |
String html;
|
73 |
SafeHtml name; |
74 |
if (folder.isHome()) {
|
75 |
html = AbstractImagePrototype.create(FolderTreeView.images.home()).getHTML(); |
76 |
name = Templates.INSTANCE.nameSpan("Pithos"); |
77 |
} |
78 |
else if (folder.isTrash()) { |
79 |
html = AbstractImagePrototype.create(FolderTreeView.images.trash()).getHTML(); |
80 |
name = Templates.INSTANCE.nameSpan("Trash"); |
81 |
} |
82 |
else {
|
83 |
html = AbstractImagePrototype.create(folder.isShared() ? FolderTreeView.images.myShared() : FolderTreeView.images.folderYellow()).getHTML(); |
84 |
name = Templates.INSTANCE.nameSpan(folder.getName());
|
85 |
} |
86 |
safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant(" ");
|
87 |
safeHtmlBuilder.append(name); |
88 |
} |
89 |
|
90 |
@Override
|
91 |
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) { |
92 |
if (event.getType().equals(ContextMenuEvent.getType().getName())) {
|
93 |
FolderTreeViewModel.this.selectionModel.setSelected(folder, true);
|
94 |
FolderContextMenu menu = new FolderContextMenu(app, FolderTreeView.images, app.getSelectedTree(), folder);
|
95 |
menu.setPopupPosition(event.getClientX(), event.getClientY()); |
96 |
menu.show(); |
97 |
} |
98 |
} |
99 |
}; |
100 |
|
101 |
protected ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>(); |
102 |
|
103 |
private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>(); |
104 |
|
105 |
protected SingleSelectionModel<Folder> selectionModel; |
106 |
|
107 |
public FolderTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) { |
108 |
app = _app; |
109 |
this.selectionModel = selectionModel;
|
110 |
} |
111 |
|
112 |
@Override
|
113 |
public <T> NodeInfo<?> getNodeInfo(T value) {
|
114 |
if (value == null) { |
115 |
Folder f = new Folder("Loading ..."); |
116 |
rootDataProvider.getList().add(f); |
117 |
return new DefaultNodeInfo<Folder>(rootDataProvider, folderCell, selectionModel, null); |
118 |
} |
119 |
final Folder f = (Folder) value;
|
120 |
if (dataProviderMap.get(f) == null) { |
121 |
dataProviderMap.put(f, new ListDataProvider<Folder>());
|
122 |
} |
123 |
final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
|
124 |
//This prevents the loading indicator
|
125 |
// dataProvider.getList().clear();
|
126 |
// dataProvider.getList().addAll(f.getSubfolders());
|
127 |
fetchFolder(f, dataProvider, false, null); |
128 |
return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null); |
129 |
} |
130 |
|
131 |
@Override
|
132 |
public boolean isLeaf(Object o) { |
133 |
if (o instanceof Folder) { |
134 |
Folder f = (Folder) o; |
135 |
return f.getSubfolders().isEmpty();
|
136 |
} |
137 |
return false; |
138 |
} |
139 |
|
140 |
private native void log(String msg) /*-{ |
141 |
$wnd.console.log(msg);
|
142 |
}-*/;
|
143 |
|
144 |
protected void fetchFolder(final Iterator<Folder> iter, final Command callback) { |
145 |
if (iter.hasNext()) {
|
146 |
final Folder f = iter.next();
|
147 |
|
148 |
String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix()); |
149 |
GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
|
150 |
@Override
|
151 |
public void onSuccess(Folder _result) { |
152 |
fetchFolder(iter, callback); |
153 |
} |
154 |
|
155 |
@Override
|
156 |
public void onError(Throwable t) { |
157 |
GWT.log("Error getting folder", t);
|
158 |
app.setError(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().listIterator();
|
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 = new ArrayList<Folder>(_result.getSubfolders()).listIterator(); |
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 |
app.setError(t); |
245 |
if (t instanceof RestException) |
246 |
app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
247 |
else
|
248 |
app.displayError("System error fetching folder: " + t.getMessage());
|
249 |
} |
250 |
|
251 |
@Override
|
252 |
protected void onUnauthorized(Response response) { |
253 |
app.sessionExpired(); |
254 |
} |
255 |
}; |
256 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
257 |
Scheduler.get().scheduleDeferred(getFolder); |
258 |
} |
259 |
} |