root / web_client / src / gr / grnet / pithos / web / client / foldertree / FolderTreeViewModel.java @ 5eef5c8a
History | View | Annotate | Download (8.9 kB)
1 | 9e8e14e4 | Christos Stathis | /*
|
---|---|---|---|
2 | 58777026 | Christos Stathis | * Copyright 2011 GRNET S.A. All rights reserved.
|
3 | 58777026 | Christos Stathis | *
|
4 | 58777026 | Christos Stathis | * Redistribution and use in source and binary forms, with or
|
5 | 58777026 | Christos Stathis | * without modification, are permitted provided that the following
|
6 | 58777026 | Christos Stathis | * conditions are met:
|
7 | 58777026 | Christos Stathis | *
|
8 | 58777026 | Christos Stathis | * 1. Redistributions of source code must retain the above
|
9 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
10 | 58777026 | Christos Stathis | * disclaimer.
|
11 | 58777026 | Christos Stathis | *
|
12 | 58777026 | Christos Stathis | * 2. Redistributions in binary form must reproduce the above
|
13 | 58777026 | Christos Stathis | * copyright notice, this list of conditions and the following
|
14 | 58777026 | Christos Stathis | * disclaimer in the documentation and/or other materials
|
15 | 58777026 | Christos Stathis | * provided with the distribution.
|
16 | 58777026 | Christos Stathis | *
|
17 | 58777026 | Christos Stathis | * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 | 58777026 | Christos Stathis | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 | 58777026 | Christos Stathis | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 | 58777026 | Christos Stathis | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 | 58777026 | Christos Stathis | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 | 58777026 | Christos Stathis | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 | 58777026 | Christos Stathis | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 | 58777026 | Christos Stathis | * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 | 58777026 | Christos Stathis | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 | 58777026 | Christos Stathis | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 | 58777026 | Christos Stathis | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 | 58777026 | Christos Stathis | * POSSIBILITY OF SUCH DAMAGE.
|
29 | 58777026 | Christos Stathis | *
|
30 | 58777026 | Christos Stathis | * The views and conclusions contained in the software and
|
31 | 58777026 | Christos Stathis | * documentation are those of the authors and should not be
|
32 | 58777026 | Christos Stathis | * interpreted as representing official policies, either expressed
|
33 | 58777026 | Christos Stathis | * or implied, of GRNET S.A.
|
34 | 9e8e14e4 | Christos Stathis | */
|
35 | 9e8e14e4 | Christos Stathis | |
36 | 9e8e14e4 | Christos Stathis | package gr.grnet.pithos.web.client.foldertree; |
37 | 9e8e14e4 | Christos Stathis | |
38 | 287c3d8d | Christos Stathis | import com.google.gwt.cell.client.AbstractCell; |
39 | 287c3d8d | Christos Stathis | import com.google.gwt.cell.client.Cell; |
40 | 9e8e14e4 | Christos Stathis | import com.google.gwt.core.client.GWT; |
41 | 9e8e14e4 | Christos Stathis | import com.google.gwt.core.client.Scheduler; |
42 | 9e8e14e4 | Christos Stathis | import com.google.gwt.core.client.Scheduler.ScheduledCommand; |
43 | 287c3d8d | Christos Stathis | import com.google.gwt.event.dom.client.ContextMenuEvent; |
44 | 287c3d8d | Christos Stathis | import com.google.gwt.safehtml.shared.SafeHtmlBuilder; |
45 | 287c3d8d | Christos Stathis | import com.google.gwt.user.client.ui.AbstractImagePrototype; |
46 | 9e8e14e4 | Christos Stathis | import com.google.gwt.view.client.ListDataProvider; |
47 | 9e8e14e4 | Christos Stathis | import com.google.gwt.view.client.SingleSelectionModel; |
48 | 9e8e14e4 | Christos Stathis | import com.google.gwt.view.client.TreeViewModel; |
49 | 287c3d8d | Christos Stathis | import gr.grnet.pithos.web.client.FolderContextMenu; |
50 | 9e8e14e4 | Christos Stathis | import gr.grnet.pithos.web.client.GSS; |
51 | 287c3d8d | Christos Stathis | import gr.grnet.pithos.web.client.foldertree.FolderTreeView.Templates; |
52 | 9e8e14e4 | Christos Stathis | import gr.grnet.pithos.web.client.rest.GetRequest; |
53 | 9e8e14e4 | Christos Stathis | import gr.grnet.pithos.web.client.rest.RestException; |
54 | fa5600f7 | Christos Stathis | import java.util.HashMap; |
55 | 5cd18037 | Christos Stathis | import java.util.Iterator; |
56 | fa5600f7 | Christos Stathis | import java.util.Map; |
57 | 5cd18037 | Christos Stathis | import java.util.Set; |
58 | 9e8e14e4 | Christos Stathis | |
59 | 9e8e14e4 | Christos Stathis | public class FolderTreeViewModel implements TreeViewModel { |
60 | 9e8e14e4 | Christos Stathis | |
61 | 287c3d8d | Christos Stathis | private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) { |
62 | 287c3d8d | Christos Stathis | |
63 | 287c3d8d | Christos Stathis | @Override
|
64 | 287c3d8d | Christos Stathis | public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) { |
65 | 5eef5c8a | Christos Stathis | String html;
|
66 | 5eef5c8a | Christos Stathis | if (folder.isTrash())
|
67 | 5eef5c8a | Christos Stathis | html = AbstractImagePrototype.create(FolderTreeView.images.emptyTrash()).getHTML(); |
68 | 5eef5c8a | Christos Stathis | else
|
69 | 5eef5c8a | Christos Stathis | html = AbstractImagePrototype.create(FolderTreeView.images.folderYellow()).getHTML(); |
70 | 287c3d8d | Christos Stathis | safeHtmlBuilder.appendHtmlConstant(html); |
71 | 287c3d8d | Christos Stathis | safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
|
72 | 287c3d8d | Christos Stathis | } |
73 | 287c3d8d | Christos Stathis | |
74 | 287c3d8d | Christos Stathis | @Override
|
75 | 287c3d8d | Christos Stathis | 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) { |
76 | 287c3d8d | Christos Stathis | if (event.getType().equals(ContextMenuEvent.getType().getName())) {
|
77 | fa5600f7 | Christos Stathis | FolderTreeViewModel.this.selectionModel.setSelected(folder, true);
|
78 | fa5600f7 | Christos Stathis | FolderContextMenu menu = new FolderContextMenu(FolderTreeView.images, folder);
|
79 | 287c3d8d | Christos Stathis | menu.setPopupPosition(event.getClientX(), event.getClientY()); |
80 | 287c3d8d | Christos Stathis | menu.show(); |
81 | 287c3d8d | Christos Stathis | } |
82 | 287c3d8d | Christos Stathis | } |
83 | 287c3d8d | Christos Stathis | }; |
84 | 287c3d8d | Christos Stathis | |
85 | 1ac430a1 | Christos Stathis | private ListDataProvider<Folder> rootDataProvider = new ListDataProvider<Folder>(); |
86 | 1ac430a1 | Christos Stathis | |
87 | fa5600f7 | Christos Stathis | private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>(); |
88 | fa5600f7 | Christos Stathis | |
89 | fbff60ff | Christos Stathis | private SingleSelectionModel<Folder> selectionModel; |
90 | fbff60ff | Christos Stathis | |
91 | fbff60ff | Christos Stathis | public FolderTreeViewModel(SingleSelectionModel<Folder> selectionModel) { |
92 | fbff60ff | Christos Stathis | this.selectionModel = selectionModel;
|
93 | 9e8e14e4 | Christos Stathis | } |
94 | 1ac430a1 | Christos Stathis | |
95 | 9e8e14e4 | Christos Stathis | @Override
|
96 | 9e8e14e4 | Christos Stathis | public <T> NodeInfo<?> getNodeInfo(T value) {
|
97 | 9e8e14e4 | Christos Stathis | if (value == null) { |
98 | 9e8e14e4 | Christos Stathis | Folder f = new Folder("Loading ..."); |
99 | 1ac430a1 | Christos Stathis | rootDataProvider.getList().add(f); |
100 | 287c3d8d | Christos Stathis | return new DefaultNodeInfo<Folder>(rootDataProvider, folderCell, selectionModel, null); |
101 | 9e8e14e4 | Christos Stathis | } |
102 | 9e8e14e4 | Christos Stathis | else {
|
103 | 5cd18037 | Christos Stathis | final Folder f = (Folder) value;
|
104 | fa5600f7 | Christos Stathis | if (dataProviderMap.get(f) == null) { |
105 | fa5600f7 | Christos Stathis | dataProviderMap.put(f, new ListDataProvider<Folder>());
|
106 | fa5600f7 | Christos Stathis | } |
107 | fa5600f7 | Christos Stathis | final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
|
108 | fa5600f7 | Christos Stathis | fetchFolder(f, dataProvider); |
109 | 287c3d8d | Christos Stathis | return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null); |
110 | 9e8e14e4 | Christos Stathis | } |
111 | 9e8e14e4 | Christos Stathis | } |
112 | 9e8e14e4 | Christos Stathis | |
113 | 9e8e14e4 | Christos Stathis | @Override
|
114 | 9e8e14e4 | Christos Stathis | public boolean isLeaf(Object o) { |
115 | 9e8e14e4 | Christos Stathis | if (o instanceof Folder) { |
116 | 9e8e14e4 | Christos Stathis | Folder f = (Folder) o; |
117 | 9e8e14e4 | Christos Stathis | return f.getSubfolders().isEmpty();
|
118 | 9e8e14e4 | Christos Stathis | } |
119 | 9e8e14e4 | Christos Stathis | return false; |
120 | 9e8e14e4 | Christos Stathis | } |
121 | 9e8e14e4 | Christos Stathis | |
122 | 5cd18037 | Christos Stathis | private void fetchFolder(final Iterator<Folder> iter, final ListDataProvider<Folder> dataProvider, final Set<Folder> folders) { |
123 | 26cbae7a | Christos Stathis | final GSS app = GSS.get();
|
124 | 5cd18037 | Christos Stathis | if (iter.hasNext()) {
|
125 | 5cd18037 | Christos Stathis | final Folder f = iter.next();
|
126 | 5cd18037 | Christos Stathis | |
127 | 1ac430a1 | Christos Stathis | String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix(); |
128 | 5cd18037 | Christos Stathis | GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
|
129 | 5cd18037 | Christos Stathis | @Override
|
130 | 5cd18037 | Christos Stathis | public void onSuccess(Folder result) { |
131 | 5cd18037 | Christos Stathis | fetchFolder(iter, dataProvider, folders); |
132 | 5cd18037 | Christos Stathis | } |
133 | 5cd18037 | Christos Stathis | |
134 | 5cd18037 | Christos Stathis | @Override
|
135 | 5cd18037 | Christos Stathis | public void onError(Throwable t) { |
136 | 5cd18037 | Christos Stathis | GWT.log("Error getting folder", t);
|
137 | 5cd18037 | Christos Stathis | if (t instanceof RestException) |
138 | 5cd18037 | Christos Stathis | GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
139 | 5cd18037 | Christos Stathis | else
|
140 | 5cd18037 | Christos Stathis | GSS.get().displayError("System error fetching folder: " + t.getMessage());
|
141 | 5cd18037 | Christos Stathis | } |
142 | 5cd18037 | Christos Stathis | }; |
143 | fbff60ff | Christos Stathis | getFolder.setHeader("X-Auth-Token", app.getToken());
|
144 | 5cd18037 | Christos Stathis | Scheduler.get().scheduleDeferred(getFolder); |
145 | 5cd18037 | Christos Stathis | } |
146 | 5cd18037 | Christos Stathis | else {
|
147 | 5cd18037 | Christos Stathis | dataProvider.getList().clear(); |
148 | 5cd18037 | Christos Stathis | dataProvider.getList().addAll(folders); |
149 | 5eef5c8a | Christos Stathis | if (dataProvider.equals(rootDataProvider)) {
|
150 | fbff60ff | Christos Stathis | selectionModel.setSelected(dataProvider.getList().get(0), true); |
151 | 5eef5c8a | Christos Stathis | Folder f = new Folder("Trash"); |
152 | 5eef5c8a | Christos Stathis | f.setTrash(true);
|
153 | 5eef5c8a | Christos Stathis | f.setContainer("trash");
|
154 | 5eef5c8a | Christos Stathis | dataProvider.getList().add(f); |
155 | 5eef5c8a | Christos Stathis | } |
156 | 5cd18037 | Christos Stathis | } |
157 | 5cd18037 | Christos Stathis | } |
158 | 1ac430a1 | Christos Stathis | |
159 | 1ac430a1 | Christos Stathis | public void initialize(AccountResource account) { |
160 | 1ac430a1 | Christos Stathis | Iterator<Folder> iter = account.getContainers().iterator();
|
161 | 1ac430a1 | Christos Stathis | fetchFolder(iter, rootDataProvider, account.getContainers()); |
162 | 1ac430a1 | Christos Stathis | } |
163 | fbff60ff | Christos Stathis | |
164 | fbff60ff | Christos Stathis | public Folder getSelection() {
|
165 | fbff60ff | Christos Stathis | return selectionModel.getSelectedObject();
|
166 | fbff60ff | Christos Stathis | } |
167 | fa5600f7 | Christos Stathis | |
168 | fa5600f7 | Christos Stathis | public void updateFolder(Folder folder) { |
169 | fa5600f7 | Christos Stathis | if (dataProviderMap.get(folder) == null) { |
170 | fa5600f7 | Christos Stathis | dataProviderMap.put(folder, new ListDataProvider<Folder>());
|
171 | fa5600f7 | Christos Stathis | } |
172 | fa5600f7 | Christos Stathis | final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
|
173 | 5eef5c8a | Christos Stathis | if (!folder.isTrash())
|
174 | 5eef5c8a | Christos Stathis | fetchFolder(folder, dataProvider); |
175 | 5eef5c8a | Christos Stathis | else
|
176 | 5eef5c8a | Christos Stathis | GSS.get().showFiles(folder); |
177 | fa5600f7 | Christos Stathis | } |
178 | fa5600f7 | Christos Stathis | |
179 | fa5600f7 | Christos Stathis | public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider) { |
180 | fa5600f7 | Christos Stathis | dataProvider.flush(); |
181 | fa5600f7 | Christos Stathis | Scheduler.get().scheduleDeferred(new ScheduledCommand() {
|
182 | fa5600f7 | Christos Stathis | @Override
|
183 | fa5600f7 | Christos Stathis | public void execute() { |
184 | fa5600f7 | Christos Stathis | final GSS app = GSS.get();
|
185 | fa5600f7 | Christos Stathis | String path = app.getApiPath() + app.getUsername() + "/" + f.getContainer() + "?format=json&delimiter=/&prefix=" + f.getPrefix(); |
186 | fa5600f7 | Christos Stathis | GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path, f) {
|
187 | fa5600f7 | Christos Stathis | @Override
|
188 | fa5600f7 | Christos Stathis | public void onSuccess(Folder result) { |
189 | 816de19f | Christos Stathis | app.showFiles(result); |
190 | fa5600f7 | Christos Stathis | Iterator<Folder> iter = result.getSubfolders().iterator();
|
191 | fa5600f7 | Christos Stathis | fetchFolder(iter, dataProvider, result.getSubfolders()); |
192 | fa5600f7 | Christos Stathis | } |
193 | fa5600f7 | Christos Stathis | |
194 | fa5600f7 | Christos Stathis | @Override
|
195 | fa5600f7 | Christos Stathis | public void onError(Throwable t) { |
196 | fa5600f7 | Christos Stathis | GWT.log("Error getting folder", t);
|
197 | fa5600f7 | Christos Stathis | if (t instanceof RestException) |
198 | fa5600f7 | Christos Stathis | GSS.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
|
199 | fa5600f7 | Christos Stathis | else
|
200 | fa5600f7 | Christos Stathis | GSS.get().displayError("System error fetching folder: " + t.getMessage());
|
201 | fa5600f7 | Christos Stathis | } |
202 | fa5600f7 | Christos Stathis | }; |
203 | fa5600f7 | Christos Stathis | getFolder.setHeader("X-Auth-Token", app.getToken());
|
204 | fa5600f7 | Christos Stathis | Scheduler.get().scheduleDeferred(getFolder); |
205 | fa5600f7 | Christos Stathis | } |
206 | fa5600f7 | Christos Stathis | }); |
207 | fa5600f7 | Christos Stathis | } |
208 | 9e8e14e4 | Christos Stathis | } |