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