Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / mysharedtree / MysharedTreeViewModel.java @ 39f6d7b0

History | View | Annotate | Download (12 kB)

1 105b4e49 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 105b4e49 Christos Stathis
 *
4 105b4e49 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 105b4e49 Christos Stathis
 * without modification, are permitted provided that the following
6 105b4e49 Christos Stathis
 * conditions are met:
7 105b4e49 Christos Stathis
 *
8 105b4e49 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 105b4e49 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 105b4e49 Christos Stathis
 *      disclaimer.
11 105b4e49 Christos Stathis
 *
12 105b4e49 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 105b4e49 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 105b4e49 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 105b4e49 Christos Stathis
 *      provided with the distribution.
16 105b4e49 Christos Stathis
 *
17 105b4e49 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 105b4e49 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 105b4e49 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 105b4e49 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 105b4e49 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 105b4e49 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 105b4e49 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 105b4e49 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 105b4e49 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 105b4e49 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 105b4e49 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 105b4e49 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 105b4e49 Christos Stathis
 *
30 105b4e49 Christos Stathis
 * The views and conclusions contained in the software and
31 105b4e49 Christos Stathis
 * documentation are those of the authors and should not be
32 105b4e49 Christos Stathis
 * interpreted as representing official policies, either expressed
33 105b4e49 Christos Stathis
 * or implied, of GRNET S.A.
34 105b4e49 Christos Stathis
 */
35 105b4e49 Christos Stathis
36 105b4e49 Christos Stathis
package gr.grnet.pithos.web.client.mysharedtree;
37 105b4e49 Christos Stathis
38 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.FolderContextMenu;
39 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
40 56f9134b Christos Stathis
import gr.grnet.pithos.web.client.foldertree.AccountResource;
41 ff06bcd5 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
42 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
43 a2f617f8 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.FolderTreeView;
44 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.mysharedtree.MysharedTreeView.Templates;
45 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
46 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
47 105b4e49 Christos Stathis
48 0ec61115 Christos Stathis
import java.util.ArrayList;
49 105b4e49 Christos Stathis
import java.util.HashMap;
50 ff06bcd5 Christos Stathis
import java.util.HashSet;
51 105b4e49 Christos Stathis
import java.util.Iterator;
52 105b4e49 Christos Stathis
import java.util.List;
53 105b4e49 Christos Stathis
import java.util.Map;
54 ff06bcd5 Christos Stathis
import java.util.Set;
55 105b4e49 Christos Stathis
56 105b4e49 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
57 105b4e49 Christos Stathis
import com.google.gwt.cell.client.Cell;
58 105b4e49 Christos Stathis
import com.google.gwt.cell.client.TextCell;
59 105b4e49 Christos Stathis
import com.google.gwt.cell.client.ValueUpdater;
60 105b4e49 Christos Stathis
import com.google.gwt.core.client.GWT;
61 105b4e49 Christos Stathis
import com.google.gwt.core.client.Scheduler;
62 105b4e49 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
63 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
64 f5023f13 Christos Stathis
import com.google.gwt.http.client.URL;
65 105b4e49 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtml;
66 105b4e49 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
67 105b4e49 Christos Stathis
import com.google.gwt.text.shared.SafeHtmlRenderer;
68 105b4e49 Christos Stathis
import com.google.gwt.user.client.Command;
69 105b4e49 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
70 105b4e49 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
71 105b4e49 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
72 105b4e49 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
73 105b4e49 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
74 105b4e49 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
75 105b4e49 Christos Stathis
76 105b4e49 Christos Stathis
public class MysharedTreeViewModel implements TreeViewModel {
77 105b4e49 Christos Stathis
78 105b4e49 Christos Stathis
    protected Pithos app;
79 105b4e49 Christos Stathis
80 f1b24b82 Christos Stathis
    private Cell<Folder> folderCell = new AbstractCell<Folder>() {
81 105b4e49 Christos Stathis
82 105b4e49 Christos Stathis
       @Override
83 ebead1b5 Christos Stathis
        public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
84 105b4e49 Christos Stathis
            String html = AbstractImagePrototype.create(MysharedTreeView.images.folderYellow()).getHTML();
85 a7b6e464 Christos Stathis
            safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
86 105b4e49 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
87 105b4e49 Christos Stathis
        }
88 105b4e49 Christos Stathis
89 105b4e49 Christos Stathis
        @Override
90 a2f617f8 Christos Stathis
        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) {
91 105b4e49 Christos Stathis
            if (event.getType().equals(ContextMenuEvent.getType().getName())) {
92 a2f617f8 Christos Stathis
                    final int x = event.getClientX();
93 a2f617f8 Christos Stathis
                    final int y = event.getClientY();
94 105b4e49 Christos Stathis
                MysharedTreeViewModel.this.selectionModel.setSelected(folder, true);
95 a2f617f8 Christos Stathis
                app.scheduleFolderHeadCommand(folder, new Command() {
96 a2f617f8 Christos Stathis
                                        
97 a2f617f8 Christos Stathis
                                        @Override
98 a2f617f8 Christos Stathis
                                        public void execute() {
99 a2f617f8 Christos Stathis
                                FolderContextMenu menu = new FolderContextMenu(app, MysharedTreeView.images, app.getSelectedTree(), folder);
100 a2f617f8 Christos Stathis
                                menu.setPopupPosition(x, y);
101 a2f617f8 Christos Stathis
                                menu.show();
102 a2f617f8 Christos Stathis
                                        }
103 a2f617f8 Christos Stathis
                                });
104 105b4e49 Christos Stathis
            }
105 105b4e49 Christos Stathis
        }
106 105b4e49 Christos Stathis
    };
107 105b4e49 Christos Stathis
108 cf49bbc9 Christos Stathis
    protected ListDataProvider<Folder> firstLevelDataProvider = new ListDataProvider<Folder>();
109 105b4e49 Christos Stathis
110 cf49bbc9 Christos Stathis
    private Map<Folder, ListDataProvider<Folder>> dataProviderMap = new HashMap<Folder, ListDataProvider<Folder>>();
111 ff06bcd5 Christos Stathis
    
112 220831bd Christos Stathis
    protected SingleSelectionModel<Folder> selectionModel;
113 105b4e49 Christos Stathis
114 105b4e49 Christos Stathis
    public MysharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
115 105b4e49 Christos Stathis
        app = _app;
116 105b4e49 Christos Stathis
        this.selectionModel = selectionModel;
117 105b4e49 Christos Stathis
    }
118 105b4e49 Christos Stathis
119 105b4e49 Christos Stathis
    @Override
120 105b4e49 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
121 105b4e49 Christos Stathis
        if (value == null) {
122 ff06bcd5 Christos Stathis
            ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
123 c9ccc229 Christos Stathis
            rootDataProvider.getList().add("Shared by me");
124 105b4e49 Christos Stathis
            return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
125 105b4e49 Christos Stathis
                @Override
126 105b4e49 Christos Stathis
                public SafeHtml render(String object) {
127 105b4e49 Christos Stathis
                    SafeHtmlBuilder builder = new SafeHtmlBuilder();
128 105b4e49 Christos Stathis
                    render(object, builder);
129 105b4e49 Christos Stathis
                    return builder.toSafeHtml();
130 105b4e49 Christos Stathis
                }
131 105b4e49 Christos Stathis
132 105b4e49 Christos Stathis
                @Override
133 105b4e49 Christos Stathis
                public void render(String object, SafeHtmlBuilder builder) {
134 105b4e49 Christos Stathis
                    String html = AbstractImagePrototype.create(MysharedTreeView.images.myShared()).getHTML();
135 a7b6e464 Christos Stathis
                    builder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
136 105b4e49 Christos Stathis
                    builder.append(MysharedTreeView.Templates.INSTANCE.nameSpan(object));
137 105b4e49 Christos Stathis
                }
138 56f9134b Christos Stathis
            }),  null, null);
139 105b4e49 Christos Stathis
        }
140 105b4e49 Christos Stathis
        else if (value instanceof String) {
141 cf49bbc9 Christos Stathis
                fetchSharedContainers(null);
142 105b4e49 Christos Stathis
            return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, selectionModel, null);
143 105b4e49 Christos Stathis
        }
144 105b4e49 Christos Stathis
        else {
145 105b4e49 Christos Stathis
            final Folder f = (Folder) value;
146 105b4e49 Christos Stathis
            if (dataProviderMap.get(f) == null) {
147 cf49bbc9 Christos Stathis
                dataProviderMap.put(f, new ListDataProvider<Folder>());
148 105b4e49 Christos Stathis
            }
149 cf49bbc9 Christos Stathis
            final ListDataProvider<Folder> dataProvider = dataProviderMap.get(f);
150 cf49bbc9 Christos Stathis
            fetchFolder(f, dataProvider, false);
151 105b4e49 Christos Stathis
            return new DefaultNodeInfo<Folder>(dataProvider, folderCell, selectionModel, null);
152 105b4e49 Christos Stathis
        }
153 105b4e49 Christos Stathis
    }
154 105b4e49 Christos Stathis
155 7c54d867 Christos Stathis
        private void fetchSharedContainers(final Command callback) {
156 56f9134b Christos Stathis
        String path = "?format=json&shared=";
157 56f9134b Christos Stathis
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), app.getUsername(), path) {
158 56f9134b Christos Stathis
            @Override
159 56f9134b Christos Stathis
            public void onSuccess(final AccountResource _result) {
160 56f9134b Christos Stathis
                    Iterator<Folder> iter = _result.getContainers().listIterator();
161 56f9134b Christos Stathis
                    fetchFolder(iter, new Command() {
162 56f9134b Christos Stathis
                                        
163 56f9134b Christos Stathis
                                        @Override
164 56f9134b Christos Stathis
                                        public void execute() {
165 56f9134b Christos Stathis
                                                firstLevelDataProvider.getList().clear();
166 56f9134b Christos Stathis
                                Folder t = null;
167 56f9134b Christos Stathis
                                for (Folder c : _result.getContainers()) {
168 56f9134b Christos Stathis
                                        if (c.isHome())
169 56f9134b Christos Stathis
                                                firstLevelDataProvider.getList().add(0, c); //Pithos is always first
170 56f9134b Christos Stathis
                                        else if (!c.isTrash())
171 56f9134b Christos Stathis
                                                firstLevelDataProvider.getList().add(c);
172 56f9134b Christos Stathis
                                }
173 56f9134b Christos Stathis
                                                if (callback != null)
174 56f9134b Christos Stathis
                                                        callback.execute();
175 56f9134b Christos Stathis
                                        }
176 56f9134b Christos Stathis
                                });
177 56f9134b Christos Stathis
            }
178 56f9134b Christos Stathis
179 56f9134b Christos Stathis
            @Override
180 56f9134b Christos Stathis
            public void onError(Throwable t) {
181 56f9134b Christos Stathis
                GWT.log("Error getting account", t);
182 56f9134b Christos Stathis
                                app.setError(t);
183 56f9134b Christos Stathis
                if (t instanceof RestException)
184 56f9134b Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
185 56f9134b Christos Stathis
                else
186 56f9134b Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
187 56f9134b Christos Stathis
            }
188 56f9134b Christos Stathis
189 ff06bcd5 Christos Stathis
                        @Override
190 56f9134b Christos Stathis
                        protected void onUnauthorized(Response response) {
191 56f9134b Christos Stathis
                                app.sessionExpired();
192 ff06bcd5 Christos Stathis
                        }
193 56f9134b Christos Stathis
        };
194 56f9134b Christos Stathis
        getAccount.setHeader("X-Auth-Token", app.getToken());
195 56f9134b Christos Stathis
        Scheduler.get().scheduleDeferred(getAccount);
196 105b4e49 Christos Stathis
        }
197 105b4e49 Christos Stathis
198 105b4e49 Christos Stathis
        @Override
199 105b4e49 Christos Stathis
    public boolean isLeaf(Object o) {
200 7c54d867 Christos Stathis
                if (o == null)
201 7c54d867 Christos Stathis
                        return false;
202 7c54d867 Christos Stathis
                else if (o instanceof Folder) {
203 105b4e49 Christos Stathis
            Folder f = (Folder) o;
204 105b4e49 Christos Stathis
            return f.getSubfolders().isEmpty();
205 105b4e49 Christos Stathis
        }
206 7c54d867 Christos Stathis
                else {
207 cf49bbc9 Christos Stathis
                        return firstLevelDataProvider.getList().isEmpty();
208 7c54d867 Christos Stathis
                }
209 105b4e49 Christos Stathis
    }
210 0ec61115 Christos Stathis
        
211 0ec61115 Christos Stathis
        private native void log(String msg) /*-{
212 0ec61115 Christos Stathis
                $wnd.console.log(msg);
213 0ec61115 Christos Stathis
        }-*/;
214 105b4e49 Christos Stathis
215 56f9134b Christos Stathis
    protected void fetchFolder(final Iterator<Folder> iter, final Command callback) {
216 105b4e49 Christos Stathis
        if (iter.hasNext()) {
217 105b4e49 Christos Stathis
            final Folder f = iter.next();
218 105b4e49 Christos Stathis
219 56f9134b Christos Stathis
            String path = "/" + f.getContainer() + "?format=json&shared=&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
220 63ee965c Christos Stathis
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
221 105b4e49 Christos Stathis
                @Override
222 7811b9d1 Christos Stathis
                public void onSuccess(Folder _result) {
223 56f9134b Christos Stathis
                    fetchFolder(iter, callback);
224 105b4e49 Christos Stathis
                }
225 105b4e49 Christos Stathis
226 105b4e49 Christos Stathis
                @Override
227 105b4e49 Christos Stathis
                public void onError(Throwable t) {
228 105b4e49 Christos Stathis
                    GWT.log("Error getting folder", t);
229 3f8622d4 Christos Stathis
                                        app.setError(t);
230 105b4e49 Christos Stathis
                    if (t instanceof RestException)
231 105b4e49 Christos Stathis
                        app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
232 105b4e49 Christos Stathis
                    else
233 105b4e49 Christos Stathis
                        app.displayError("System error fetching folder: " + t.getMessage());
234 105b4e49 Christos Stathis
                }
235 9539e23d Christos Stathis
236 9539e23d Christos Stathis
                                @Override
237 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
238 9539e23d Christos Stathis
                                        app.sessionExpired();
239 9539e23d Christos Stathis
                                }
240 105b4e49 Christos Stathis
            };
241 105b4e49 Christos Stathis
            getFolder.setHeader("X-Auth-Token", app.getToken());
242 105b4e49 Christos Stathis
            Scheduler.get().scheduleDeferred(getFolder);
243 105b4e49 Christos Stathis
        }
244 105b4e49 Christos Stathis
        else if (callback != null)
245 105b4e49 Christos Stathis
            callback.execute();
246 105b4e49 Christos Stathis
    }
247 105b4e49 Christos Stathis
248 105b4e49 Christos Stathis
    public Folder getSelection() {
249 105b4e49 Christos Stathis
        return selectionModel.getSelectedObject();
250 105b4e49 Christos Stathis
    }
251 105b4e49 Christos Stathis
252 cf49bbc9 Christos Stathis
    public void updateFolder(Folder folder, boolean showfiles) {
253 105b4e49 Christos Stathis
        if (dataProviderMap.get(folder) == null) {
254 cf49bbc9 Christos Stathis
            dataProviderMap.put(folder, new ListDataProvider<Folder>());
255 105b4e49 Christos Stathis
        }
256 cf49bbc9 Christos Stathis
        final ListDataProvider<Folder> dataProvider = dataProviderMap.get(folder);
257 105b4e49 Christos Stathis
        fetchFolder(folder, dataProvider, showfiles);
258 105b4e49 Christos Stathis
    }
259 105b4e49 Christos Stathis
260 cf49bbc9 Christos Stathis
    public void fetchFolder(final Folder f, final ListDataProvider<Folder> dataProvider, final boolean showfiles) {
261 56f9134b Christos Stathis
        String path = "/" + f.getContainer() + "?format=json&shared=&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
262 63ee965c Christos Stathis
        GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
263 105b4e49 Christos Stathis
            @Override
264 7811b9d1 Christos Stathis
            public void onSuccess(final Folder _result) {
265 105b4e49 Christos Stathis
                if (showfiles)
266 7811b9d1 Christos Stathis
                    app.showFiles(_result);
267 56f9134b Christos Stathis
                Iterator<Folder> iter = new ArrayList<Folder>(_result.getSubfolders()).listIterator();
268 56f9134b Christos Stathis
                fetchFolder(iter, new Command() {
269 105b4e49 Christos Stathis
                    @Override
270 105b4e49 Christos Stathis
                    public void execute() {
271 cf49bbc9 Christos Stathis
                        dataProvider.getList().clear();
272 cf49bbc9 Christos Stathis
                                   dataProvider.getList().addAll(_result.getSubfolders());
273 105b4e49 Christos Stathis
                        app.getMySharedTreeView().updateChildren(f);
274 105b4e49 Christos Stathis
                    }
275 105b4e49 Christos Stathis
                });
276 105b4e49 Christos Stathis
            }
277 105b4e49 Christos Stathis
278 105b4e49 Christos Stathis
            @Override
279 105b4e49 Christos Stathis
            public void onError(Throwable t) {
280 105b4e49 Christos Stathis
                GWT.log("Error getting folder", t);
281 3f8622d4 Christos Stathis
                                app.setError(t);
282 105b4e49 Christos Stathis
                if (t instanceof RestException)
283 105b4e49 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
284 105b4e49 Christos Stathis
                else
285 105b4e49 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
286 105b4e49 Christos Stathis
            }
287 9539e23d Christos Stathis
288 9539e23d Christos Stathis
                        @Override
289 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
290 9539e23d Christos Stathis
                                app.sessionExpired();
291 9539e23d Christos Stathis
                        }
292 105b4e49 Christos Stathis
        };
293 105b4e49 Christos Stathis
        getFolder.setHeader("X-Auth-Token", app.getToken());
294 105b4e49 Christos Stathis
        Scheduler.get().scheduleDeferred(getFolder);
295 105b4e49 Christos Stathis
    }
296 7c54d867 Christos Stathis
297 7c54d867 Christos Stathis
        public void initialize(Command callback) {
298 7c54d867 Christos Stathis
                fetchSharedContainers(callback);
299 7c54d867 Christos Stathis
        }
300 105b4e49 Christos Stathis
}