Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / foldertree / FolderTreeViewModel.java @ 0c665202

History | View | Annotate | Download (10.5 kB)

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