Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / mysharedtree / MysharedTreeViewModel.java @ 3586ad5e

History | View | Annotate | Download (10.4 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 5d18aa82 Christos Stathis
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeView;
46 5d18aa82 Christos Stathis
import gr.grnet.pithos.web.client.othersharedtree.OtherSharedTreeViewModel;
47 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
48 105b4e49 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
49 105b4e49 Christos Stathis
50 0ec61115 Christos Stathis
import java.util.ArrayList;
51 105b4e49 Christos Stathis
import java.util.HashMap;
52 ff06bcd5 Christos Stathis
import java.util.HashSet;
53 105b4e49 Christos Stathis
import java.util.Iterator;
54 105b4e49 Christos Stathis
import java.util.List;
55 105b4e49 Christos Stathis
import java.util.Map;
56 ff06bcd5 Christos Stathis
import java.util.Set;
57 105b4e49 Christos Stathis
58 105b4e49 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
59 105b4e49 Christos Stathis
import com.google.gwt.cell.client.Cell;
60 105b4e49 Christos Stathis
import com.google.gwt.cell.client.TextCell;
61 105b4e49 Christos Stathis
import com.google.gwt.cell.client.ValueUpdater;
62 5d18aa82 Christos Stathis
import com.google.gwt.cell.client.Cell.Context;
63 105b4e49 Christos Stathis
import com.google.gwt.core.client.GWT;
64 105b4e49 Christos Stathis
import com.google.gwt.core.client.Scheduler;
65 105b4e49 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
66 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
67 f5023f13 Christos Stathis
import com.google.gwt.http.client.URL;
68 105b4e49 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtml;
69 105b4e49 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
70 105b4e49 Christos Stathis
import com.google.gwt.text.shared.SafeHtmlRenderer;
71 105b4e49 Christos Stathis
import com.google.gwt.user.client.Command;
72 105b4e49 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
73 105b4e49 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
74 105b4e49 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent;
75 105b4e49 Christos Stathis
import com.google.gwt.view.client.SelectionChangeEvent.Handler;
76 105b4e49 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
77 105b4e49 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
78 105b4e49 Christos Stathis
79 105b4e49 Christos Stathis
public class MysharedTreeViewModel implements TreeViewModel {
80 105b4e49 Christos Stathis
81 105b4e49 Christos Stathis
    protected Pithos app;
82 105b4e49 Christos Stathis
83 3f62b626 Christos Stathis
    Folder dummy = new Folder("No files shared by me");
84 3f62b626 Christos Stathis
    
85 5d18aa82 Christos Stathis
    private Cell<Folder> folderCell = new AbstractCell<Folder>(ContextMenuEvent.getType().getName()) {
86 105b4e49 Christos Stathis
87 105b4e49 Christos Stathis
       @Override
88 ebead1b5 Christos Stathis
        public void render(Context context, Folder folder, SafeHtmlBuilder safeHtmlBuilder) {
89 3f62b626 Christos Stathis
           if (!folder.equals(dummy)) {
90 3f62b626 Christos Stathis
                   String html = AbstractImagePrototype.create(MysharedTreeView.images.folderYellow()).getHTML();
91 3f62b626 Christos Stathis
                    safeHtmlBuilder.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
92 3f62b626 Christos Stathis
           }
93 105b4e49 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(folder.getName()));
94 105b4e49 Christos Stathis
        }
95 5d18aa82 Christos Stathis
96 5d18aa82 Christos Stathis
       @Override
97 5d18aa82 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) {
98 5d18aa82 Christos Stathis
           if (event.getType().equals(ContextMenuEvent.getType().getName())) {
99 5d18aa82 Christos Stathis
                   final int x = event.getClientX();
100 5d18aa82 Christos Stathis
                   final int y = event.getClientY();
101 5d18aa82 Christos Stathis
               MysharedTreeViewModel.this.selectionModel.setSelected(folder, true);
102 5d18aa82 Christos Stathis
               app.scheduleFolderHeadCommand(folder, new Command() {
103 5d18aa82 Christos Stathis
                                        
104 5d18aa82 Christos Stathis
                                        @Override
105 5d18aa82 Christos Stathis
                                        public void execute() {
106 5d18aa82 Christos Stathis
                                FolderContextMenu menu = new FolderContextMenu(app, MysharedTreeView.images, app.getSelectedTree(), folder);
107 5d18aa82 Christos Stathis
                                menu.setPopupPosition(x, y);
108 5d18aa82 Christos Stathis
                                menu.show();
109 5d18aa82 Christos Stathis
                                        }
110 5d18aa82 Christos Stathis
                                });
111 5d18aa82 Christos Stathis
           }
112 5d18aa82 Christos Stathis
       }
113 105b4e49 Christos Stathis
    };
114 105b4e49 Christos Stathis
115 cf49bbc9 Christos Stathis
    protected ListDataProvider<Folder> firstLevelDataProvider = new ListDataProvider<Folder>();
116 c46c9e31 Christos Stathis
 
117 220831bd Christos Stathis
    protected SingleSelectionModel<Folder> selectionModel;
118 105b4e49 Christos Stathis
119 105b4e49 Christos Stathis
    public MysharedTreeViewModel(Pithos _app, SingleSelectionModel<Folder> selectionModel) {
120 105b4e49 Christos Stathis
        app = _app;
121 105b4e49 Christos Stathis
        this.selectionModel = selectionModel;
122 105b4e49 Christos Stathis
    }
123 105b4e49 Christos Stathis
124 105b4e49 Christos Stathis
    @Override
125 105b4e49 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
126 105b4e49 Christos Stathis
        if (value == null) {
127 cf49bbc9 Christos Stathis
                fetchSharedContainers(null);
128 02b9d162 Christos Stathis
                if (firstLevelDataProvider.getList().get(0).equals(dummy))
129 02b9d162 Christos Stathis
                return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, null, null);
130 105b4e49 Christos Stathis
            return new DefaultNodeInfo<Folder>(firstLevelDataProvider, folderCell, selectionModel, null);
131 105b4e49 Christos Stathis
        }
132 c46c9e31 Christos Stathis
        return null;
133 105b4e49 Christos Stathis
    }
134 105b4e49 Christos Stathis
135 7c54d867 Christos Stathis
        private void fetchSharedContainers(final Command callback) {
136 56f9134b Christos Stathis
        String path = "?format=json&shared=";
137 56f9134b Christos Stathis
        GetRequest<AccountResource> getAccount = new GetRequest<AccountResource>(AccountResource.class, app.getApiPath(), app.getUsername(), path) {
138 56f9134b Christos Stathis
            @Override
139 56f9134b Christos Stathis
            public void onSuccess(final AccountResource _result) {
140 c46c9e31 Christos Stathis
                                firstLevelDataProvider.getList().clear();
141 c46c9e31 Christos Stathis
                for (Folder c : _result.getContainers()) {
142 c46c9e31 Christos Stathis
                        if (c.isHome())
143 c46c9e31 Christos Stathis
                                firstLevelDataProvider.getList().add(0, c); //Pithos is always first
144 c46c9e31 Christos Stathis
                        else if (!c.isTrash())
145 c46c9e31 Christos Stathis
                                firstLevelDataProvider.getList().add(c);
146 c46c9e31 Christos Stathis
                }
147 3f62b626 Christos Stathis
                if (firstLevelDataProvider.getList().isEmpty())
148 3f62b626 Christos Stathis
                        firstLevelDataProvider.getList().add(dummy);
149 c46c9e31 Christos Stathis
                                if (callback != null)
150 c46c9e31 Christos Stathis
                                        callback.execute();
151 56f9134b Christos Stathis
            }
152 56f9134b Christos Stathis
153 56f9134b Christos Stathis
            @Override
154 56f9134b Christos Stathis
            public void onError(Throwable t) {
155 56f9134b Christos Stathis
                GWT.log("Error getting account", t);
156 56f9134b Christos Stathis
                                app.setError(t);
157 56f9134b Christos Stathis
                if (t instanceof RestException)
158 56f9134b Christos Stathis
                    app.displayError("Error getting account: " + ((RestException) t).getHttpStatusText());
159 56f9134b Christos Stathis
                else
160 56f9134b Christos Stathis
                    app.displayError("System error fetching user data: " + t.getMessage());
161 56f9134b Christos Stathis
            }
162 56f9134b Christos Stathis
163 ff06bcd5 Christos Stathis
                        @Override
164 56f9134b Christos Stathis
                        protected void onUnauthorized(Response response) {
165 56f9134b Christos Stathis
                                app.sessionExpired();
166 ff06bcd5 Christos Stathis
                        }
167 56f9134b Christos Stathis
        };
168 56f9134b Christos Stathis
        getAccount.setHeader("X-Auth-Token", app.getToken());
169 56f9134b Christos Stathis
        Scheduler.get().scheduleDeferred(getAccount);
170 105b4e49 Christos Stathis
        }
171 105b4e49 Christos Stathis
172 105b4e49 Christos Stathis
        @Override
173 105b4e49 Christos Stathis
    public boolean isLeaf(Object o) {
174 7c54d867 Christos Stathis
                if (o == null)
175 cf027879 Christos Stathis
                        return firstLevelDataProvider.getList().isEmpty();
176 c46c9e31 Christos Stathis
                return true;
177 105b4e49 Christos Stathis
    }
178 0ec61115 Christos Stathis
        
179 0ec61115 Christos Stathis
        private native void log(String msg) /*-{
180 0ec61115 Christos Stathis
                $wnd.console.log(msg);
181 0ec61115 Christos Stathis
        }-*/;
182 105b4e49 Christos Stathis
183 56f9134b Christos Stathis
    protected void fetchFolder(final Iterator<Folder> iter, final Command callback) {
184 105b4e49 Christos Stathis
        if (iter.hasNext()) {
185 105b4e49 Christos Stathis
            final Folder f = iter.next();
186 105b4e49 Christos Stathis
187 56f9134b Christos Stathis
            String path = "/" + f.getContainer() + "?format=json&shared=&delimiter=/&prefix=" + URL.encodeQueryString(f.getPrefix());
188 63ee965c Christos Stathis
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
189 105b4e49 Christos Stathis
                @Override
190 7811b9d1 Christos Stathis
                public void onSuccess(Folder _result) {
191 56f9134b Christos Stathis
                    fetchFolder(iter, callback);
192 105b4e49 Christos Stathis
                }
193 105b4e49 Christos Stathis
194 105b4e49 Christos Stathis
                @Override
195 105b4e49 Christos Stathis
                public void onError(Throwable t) {
196 105b4e49 Christos Stathis
                    GWT.log("Error getting folder", t);
197 3f8622d4 Christos Stathis
                                        app.setError(t);
198 105b4e49 Christos Stathis
                    if (t instanceof RestException)
199 105b4e49 Christos Stathis
                        app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
200 105b4e49 Christos Stathis
                    else
201 105b4e49 Christos Stathis
                        app.displayError("System error fetching folder: " + t.getMessage());
202 105b4e49 Christos Stathis
                }
203 9539e23d Christos Stathis
204 9539e23d Christos Stathis
                                @Override
205 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
206 9539e23d Christos Stathis
                                        app.sessionExpired();
207 9539e23d Christos Stathis
                                }
208 105b4e49 Christos Stathis
            };
209 105b4e49 Christos Stathis
            getFolder.setHeader("X-Auth-Token", app.getToken());
210 105b4e49 Christos Stathis
            Scheduler.get().scheduleDeferred(getFolder);
211 105b4e49 Christos Stathis
        }
212 105b4e49 Christos Stathis
        else if (callback != null)
213 105b4e49 Christos Stathis
            callback.execute();
214 105b4e49 Christos Stathis
    }
215 105b4e49 Christos Stathis
216 105b4e49 Christos Stathis
    public Folder getSelection() {
217 105b4e49 Christos Stathis
        return selectionModel.getSelectedObject();
218 105b4e49 Christos Stathis
    }
219 105b4e49 Christos Stathis
220 3586ad5e Christos Stathis
    public void updateFolder(Folder folder, boolean showfiles, Command callback) {
221 3586ad5e Christos Stathis
        fetchFolder(folder, showfiles, callback);
222 105b4e49 Christos Stathis
    }
223 105b4e49 Christos Stathis
224 3586ad5e Christos Stathis
    public void fetchFolder(final Folder f, final boolean showfiles, final Command callback) {
225 c46c9e31 Christos Stathis
        String path = "/" + f.getContainer() + "?format=json&shared=" + URL.encodeQueryString(f.getPrefix());
226 5d18aa82 Christos Stathis
        GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), f.getOwner(), path, f) {
227 105b4e49 Christos Stathis
            @Override
228 7811b9d1 Christos Stathis
            public void onSuccess(final Folder _result) {
229 3586ad5e Christos Stathis
                    for (File file : _result.getFiles()) {
230 3586ad5e Christos Stathis
                            String name = file.getName();
231 3586ad5e Christos Stathis
                                        if (name.lastIndexOf("/") != -1) {
232 3586ad5e Christos Stathis
                                                file.setName(name.substring(name.lastIndexOf("/") + 1, name.length()));
233 3586ad5e Christos Stathis
                                        }
234 3586ad5e Christos Stathis
                    }
235 3586ad5e Christos Stathis
236 105b4e49 Christos Stathis
                if (showfiles)
237 7811b9d1 Christos Stathis
                    app.showFiles(_result);
238 3586ad5e Christos Stathis
                if (callback != null)
239 3586ad5e Christos Stathis
                        callback.execute();
240 105b4e49 Christos Stathis
            }
241 105b4e49 Christos Stathis
242 105b4e49 Christos Stathis
            @Override
243 105b4e49 Christos Stathis
            public void onError(Throwable t) {
244 105b4e49 Christos Stathis
                GWT.log("Error getting folder", t);
245 3f8622d4 Christos Stathis
                                app.setError(t);
246 105b4e49 Christos Stathis
                if (t instanceof RestException)
247 105b4e49 Christos Stathis
                    app.displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
248 105b4e49 Christos Stathis
                else
249 105b4e49 Christos Stathis
                    app.displayError("System error fetching folder: " + t.getMessage());
250 105b4e49 Christos Stathis
            }
251 9539e23d Christos Stathis
252 9539e23d Christos Stathis
                        @Override
253 9539e23d Christos Stathis
                        protected void onUnauthorized(Response response) {
254 9539e23d Christos Stathis
                                app.sessionExpired();
255 9539e23d Christos Stathis
                        }
256 105b4e49 Christos Stathis
        };
257 105b4e49 Christos Stathis
        getFolder.setHeader("X-Auth-Token", app.getToken());
258 105b4e49 Christos Stathis
        Scheduler.get().scheduleDeferred(getFolder);
259 105b4e49 Christos Stathis
    }
260 7c54d867 Christos Stathis
261 7c54d867 Christos Stathis
        public void initialize(Command callback) {
262 7c54d867 Christos Stathis
                fetchSharedContainers(callback);
263 7c54d867 Christos Stathis
        }
264 105b4e49 Christos Stathis
}