root / web_client / src / gr / grnet / pithos / web / client / foldertree / FolderTreeViewModel.java @ 045234b6
History | View | Annotate | Download (9.9 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.core.client.Scheduler.ScheduledCommand; |
53 |
import com.google.gwt.event.dom.client.ContextMenuEvent; |
54 |
import com.google.gwt.http.client.Response; |
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(@SuppressWarnings("unused") 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("Home"); |
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(FolderTreeView.images.folderYellow()).getHTML(); |
83 |
name = Templates.INSTANCE.nameSpan(folder.getName());
|
84 |
} |
85 |
safeHtmlBuilder.appendHtmlConstant(html); |
86 |
safeHtmlBuilder.append(name); |
87 |
} |
88 |
|
89 |
@Override
|
90 |
public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Folder folder, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") 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 |
fetchFolder(f, dataProvider, false, null); |
124 |
return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null); |
125 |
} |
126 |
|
127 |
@Override
|
128 |
public boolean isLeaf(Object o) { |
129 |
if (o instanceof Folder) { |
130 |
Folder f = (Folder) o; |
131 |
return f.getSubfolders().isEmpty();
|
132 |
} |
133 |
return false; |
134 |
} |
135 |
|
136 |
protected void fetchFolder(final Iterator<Folder> iter, final Command callback) { |
137 |
if (iter.hasNext()) {
|
138 |
final Folder f = iter.next();
|
139 |
|
140 |
String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix(); |
141 |
GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
|
142 |
@Override
|
143 |
public void onSuccess(@SuppressWarnings("unused") Folder _result) { |
144 |
fetchFolder(iter, callback); |
145 |
} |
146 |
|
147 |
@Override
|
148 |
public void onError(Throwable t) { |
149 |
GWT.log("Error getting folder", t);
|
150 |
if (t instanceof RestException) |
151 |
app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
152 |
else
|
153 |
app.displayError("System error fetching folder: " + t.getMessage());
|
154 |
} |
155 |
|
156 |
@Override
|
157 |
protected void onUnauthorized(Response response) { |
158 |
app.sessionExpired(); |
159 |
} |
160 |
}; |
161 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
162 |
Scheduler.get().scheduleDeferred(getFolder); |
163 |
} |
164 |
else if (callback != null) |
165 |
callback.execute(); |
166 |
} |
167 |
|
168 |
public void initialize(final AccountResource account) { |
169 |
Iterator<Folder> iter = account.getContainers().iterator();
|
170 |
fetchFolder(iter, new Command() {
|
171 |
@Override
|
172 |
public void execute() { |
173 |
rootDataProvider.getList().clear(); |
174 |
rootDataProvider.getList().addAll(account.getContainers()); |
175 |
selectionModel.setSelected(rootDataProvider.getList().get(0), true); |
176 |
} |
177 |
}); |
178 |
} |
179 |
|
180 |
public Folder getSelection() {
|
181 |
return selectionModel.getSelectedObject();
|
182 |
} |
183 |
|
184 |
public void updateFolder(Folder folder, boolean showfiles, Command callback) { |
185 |
if (dataProviderMap.get(folder) == null) { |
186 |
dataProviderMap.put(folder, new ListDataProvider<Folder>());
|
187 |
} |
188 |
final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
|
189 |
fetchFolder(folder, dataProvider, showfiles, callback); |
190 |
} |
191 |
|
192 |
public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles, final Command callback) { |
193 |
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
|
194 |
@Override
|
195 |
public void execute() { |
196 |
String path = "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix(); |
197 |
GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
|
198 |
@Override
|
199 |
public void onSuccess(final Folder _result) { |
200 |
if (showfiles)
|
201 |
app.showFiles(_result); |
202 |
Iterator<Folder> iter = _result.getSubfolders().iterator();
|
203 |
fetchFolder(iter, new Command() {
|
204 |
@Override
|
205 |
public void execute() { |
206 |
dataProvider.getList().clear(); |
207 |
dataProvider.getList().addAll(_result.getSubfolders()); |
208 |
app.getFolderTreeView().updateChildren(f); |
209 |
if (callback != null) |
210 |
callback.execute(); |
211 |
} |
212 |
}); |
213 |
} |
214 |
|
215 |
@Override
|
216 |
public void onError(Throwable t) { |
217 |
GWT.log("Error getting folder", t);
|
218 |
if (t instanceof RestException) |
219 |
app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
220 |
else
|
221 |
app.displayError("System error fetching folder: " + t.getMessage());
|
222 |
} |
223 |
|
224 |
@Override
|
225 |
protected void onUnauthorized(Response response) { |
226 |
app.sessionExpired(); |
227 |
} |
228 |
}; |
229 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
230 |
Scheduler.get().scheduleDeferred(getFolder); |
231 |
} |
232 |
}); |
233 |
} |
234 |
} |