Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / tagtree / TagTreeViewModel.java @ ea30dad9

History | View | Annotate | Download (7.1 kB)

1 a6592a15 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 GRNET S.A. All rights reserved.
3 a6592a15 Christos Stathis
 *
4 a6592a15 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 a6592a15 Christos Stathis
 * without modification, are permitted provided that the following
6 a6592a15 Christos Stathis
 * conditions are met:
7 a6592a15 Christos Stathis
 *
8 a6592a15 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 a6592a15 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 a6592a15 Christos Stathis
 *      disclaimer.
11 a6592a15 Christos Stathis
 *
12 a6592a15 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 a6592a15 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 a6592a15 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 a6592a15 Christos Stathis
 *      provided with the distribution.
16 a6592a15 Christos Stathis
 *
17 a6592a15 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 a6592a15 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 a6592a15 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 a6592a15 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 a6592a15 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 a6592a15 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 a6592a15 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 a6592a15 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 a6592a15 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 a6592a15 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 a6592a15 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 a6592a15 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 a6592a15 Christos Stathis
 *
30 a6592a15 Christos Stathis
 * The views and conclusions contained in the software and
31 a6592a15 Christos Stathis
 * documentation are those of the authors and should not be
32 a6592a15 Christos Stathis
 * interpreted as representing official policies, either expressed
33 a6592a15 Christos Stathis
 * or implied, of GRNET S.A.
34 a6592a15 Christos Stathis
 */
35 a6592a15 Christos Stathis
36 a6592a15 Christos Stathis
package gr.grnet.pithos.web.client.tagtree;
37 a6592a15 Christos Stathis
38 7811b9d1 Christos Stathis
import gr.grnet.pithos.web.client.Pithos;
39 7811b9d1 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.AccountResource;
40 7811b9d1 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.File;
41 7811b9d1 Christos Stathis
import gr.grnet.pithos.web.client.foldertree.Folder;
42 7811b9d1 Christos Stathis
import gr.grnet.pithos.web.client.rest.GetRequest;
43 7811b9d1 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
44 7811b9d1 Christos Stathis
import gr.grnet.pithos.web.client.tagtree.TagTreeView.Templates;
45 7811b9d1 Christos Stathis
46 7811b9d1 Christos Stathis
import java.util.Iterator;
47 7811b9d1 Christos Stathis
import java.util.LinkedHashSet;
48 7811b9d1 Christos Stathis
import java.util.List;
49 7811b9d1 Christos Stathis
import java.util.Set;
50 7811b9d1 Christos Stathis
51 a6592a15 Christos Stathis
import com.google.gwt.cell.client.AbstractCell;
52 a6592a15 Christos Stathis
import com.google.gwt.cell.client.Cell;
53 a6592a15 Christos Stathis
import com.google.gwt.cell.client.TextCell;
54 a845b044 Christos Stathis
import com.google.gwt.core.client.GWT;
55 a845b044 Christos Stathis
import com.google.gwt.core.client.Scheduler;
56 a6592a15 Christos Stathis
import com.google.gwt.event.dom.client.ContextMenuEvent;
57 9539e23d Christos Stathis
import com.google.gwt.http.client.Response;
58 a6592a15 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtml;
59 a6592a15 Christos Stathis
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
60 a6592a15 Christos Stathis
import com.google.gwt.text.shared.SafeHtmlRenderer;
61 a6592a15 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
62 a6592a15 Christos Stathis
import com.google.gwt.view.client.ListDataProvider;
63 a6592a15 Christos Stathis
import com.google.gwt.view.client.SingleSelectionModel;
64 a6592a15 Christos Stathis
import com.google.gwt.view.client.TreeViewModel;
65 a6592a15 Christos Stathis
66 a6592a15 Christos Stathis
public class TagTreeViewModel implements TreeViewModel {
67 a6592a15 Christos Stathis
68 a6592a15 Christos Stathis
    private Cell<Tag> tagCell = new AbstractCell<Tag>(ContextMenuEvent.getType().getName()) {
69 a6592a15 Christos Stathis
70 a6592a15 Christos Stathis
       @Override
71 ebead1b5 Christos Stathis
        public void render(Context context, Tag tag, SafeHtmlBuilder safeHtmlBuilder) {
72 0d1e8310 Christos Stathis
            String html = AbstractImagePrototype.create(TagTreeView.images.tag()).getHTML();
73 a6592a15 Christos Stathis
            safeHtmlBuilder.appendHtmlConstant(html);
74 a6592a15 Christos Stathis
            safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(tag.getName()));
75 a6592a15 Christos Stathis
        }
76 a6592a15 Christos Stathis
77 a6592a15 Christos Stathis
        @Override
78 ebead1b5 Christos Stathis
        public void onBrowserEvent(Context context, com.google.gwt.dom.client.Element parent, Tag tag, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<Tag> valueUpdater) {
79 a6592a15 Christos Stathis
            if (event.getType().equals(com.google.gwt.event.dom.client.ContextMenuEvent.getType().getName())) {
80 a6592a15 Christos Stathis
                TagTreeViewModel.this.selectionModel.setSelected(tag, true);
81 a6592a15 Christos Stathis
            }
82 a6592a15 Christos Stathis
        }
83 a6592a15 Christos Stathis
    };
84 a6592a15 Christos Stathis
85 a6592a15 Christos Stathis
    private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
86 a6592a15 Christos Stathis
    private ListDataProvider<Tag> tagDataProvider = new ListDataProvider<Tag>();
87 a6592a15 Christos Stathis
88 7811b9d1 Christos Stathis
    protected SingleSelectionModel<Tag> selectionModel;
89 a6592a15 Christos Stathis
90 7811b9d1 Christos Stathis
    protected Pithos app;
91 a7c43f26 Christos Stathis
92 a7c43f26 Christos Stathis
    public TagTreeViewModel(Pithos _app, SingleSelectionModel<Tag> selectionModel) {
93 a7c43f26 Christos Stathis
        app = _app;
94 a6592a15 Christos Stathis
        this.selectionModel = selectionModel;
95 a6592a15 Christos Stathis
    }
96 a6592a15 Christos Stathis
97 a6592a15 Christos Stathis
    @Override
98 a6592a15 Christos Stathis
    public <T> NodeInfo<?> getNodeInfo(T value) {
99 a6592a15 Christos Stathis
        if (value == null) {
100 a6592a15 Christos Stathis
            return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
101 a6592a15 Christos Stathis
                @Override
102 a6592a15 Christos Stathis
                public SafeHtml render(String object) {
103 a6592a15 Christos Stathis
                    SafeHtmlBuilder builder = new SafeHtmlBuilder();
104 0d1e8310 Christos Stathis
                    render(object, builder);
105 a6592a15 Christos Stathis
                    return builder.toSafeHtml();
106 a6592a15 Christos Stathis
                }
107 a6592a15 Christos Stathis
108 a6592a15 Christos Stathis
                @Override
109 a6592a15 Christos Stathis
                public void render(String object, SafeHtmlBuilder builder) {
110 a6592a15 Christos Stathis
                    String html = AbstractImagePrototype.create(TagTreeView.images.tag()).getHTML();
111 a6592a15 Christos Stathis
                    builder.appendHtmlConstant(html);
112 a6592a15 Christos Stathis
                    builder.append(Templates.INSTANCE.nameSpan(object));
113 a6592a15 Christos Stathis
                }
114 a845b044 Christos Stathis
            }), null, null);
115 a6592a15 Christos Stathis
        }
116 a6592a15 Christos Stathis
        else if (value instanceof String) {
117 a6592a15 Christos Stathis
            return new DefaultNodeInfo<Tag>(tagDataProvider, tagCell, selectionModel, null);
118 a6592a15 Christos Stathis
        }
119 a6592a15 Christos Stathis
        else
120 a6592a15 Christos Stathis
            return null;
121 a6592a15 Christos Stathis
    }
122 a6592a15 Christos Stathis
123 a6592a15 Christos Stathis
    @Override
124 a6592a15 Christos Stathis
    public boolean isLeaf(Object o) {
125 a6592a15 Christos Stathis
        if (o == null)
126 a6592a15 Christos Stathis
            return false;
127 a6592a15 Christos Stathis
        if (o instanceof String)
128 0d1e8310 Christos Stathis
            return tagDataProvider.getList().isEmpty();
129 7811b9d1 Christos Stathis
                return true;
130 a6592a15 Christos Stathis
    }
131 a6592a15 Christos Stathis
132 a6592a15 Christos Stathis
    public Tag getSelection() {
133 a6592a15 Christos Stathis
        return selectionModel.getSelectedObject();
134 a6592a15 Christos Stathis
    }
135 a6592a15 Christos Stathis
136 a6592a15 Christos Stathis
    public void updateTag(Tag tag) {
137 a845b044 Christos Stathis
        fetchTag(tag);
138 a6592a15 Christos Stathis
    }
139 a6592a15 Christos Stathis
140 a845b044 Christos Stathis
    public void fetchTag(Tag t) {
141 a845b044 Christos Stathis
        AccountResource account = app.getAccount();
142 a845b044 Christos Stathis
        Iterator<Folder> iter = account.getContainers().iterator();
143 a845b044 Christos Stathis
        fetchTag(iter, t, new LinkedHashSet<File>());
144 a845b044 Christos Stathis
    }
145 a845b044 Christos Stathis
146 7811b9d1 Christos Stathis
    protected void fetchTag(final Iterator<Folder> iter, final Tag t, final Set<File> files) {
147 a845b044 Christos Stathis
        if (iter.hasNext()) {
148 a845b044 Christos Stathis
            Folder f = iter.next();
149 b51c628b Christos Stathis
            String path = f.getUri() + "?format=json&meta=" + t.getName();
150 b51c628b Christos Stathis
            GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), app.getUsername(), path) {
151 a845b044 Christos Stathis
                @Override
152 7811b9d1 Christos Stathis
                public void onSuccess(Folder _result) {
153 7811b9d1 Christos Stathis
                    files.addAll(_result.getFiles());
154 a845b044 Christos Stathis
                    fetchTag(iter, t, files);
155 a845b044 Christos Stathis
                }
156 a845b044 Christos Stathis
157 a845b044 Christos Stathis
                @Override
158 7811b9d1 Christos Stathis
                public void onError(Throwable th) {
159 7811b9d1 Christos Stathis
                    GWT.log("Error getting folder", th);
160 3f8622d4 Christos Stathis
                                        app.setError(th);
161 7811b9d1 Christos Stathis
                    if (th instanceof RestException)
162 7811b9d1 Christos Stathis
                        app.displayError("Error getting folder: " + ((RestException) th).getHttpStatusText());
163 a845b044 Christos Stathis
                    else
164 7811b9d1 Christos Stathis
                        app.displayError("System error fetching folder: " + th.getMessage());
165 a845b044 Christos Stathis
                }
166 9539e23d Christos Stathis
167 9539e23d Christos Stathis
                                @Override
168 9539e23d Christos Stathis
                                protected void onUnauthorized(Response response) {
169 9539e23d Christos Stathis
                                        app.sessionExpired();
170 9539e23d Christos Stathis
                                }
171 a845b044 Christos Stathis
            };
172 a845b044 Christos Stathis
            getFolder.setHeader("X-Auth-Token", app.getToken());
173 a845b044 Christos Stathis
            Scheduler.get().scheduleDeferred(getFolder);
174 a845b044 Christos Stathis
        }
175 a845b044 Christos Stathis
        else {
176 a845b044 Christos Stathis
            app.showFiles(files);
177 a845b044 Christos Stathis
        }
178 a845b044 Christos Stathis
    }
179 0d1e8310 Christos Stathis
180 ef5b664d Christos Stathis
    public void initialize(List<Tag> allTags) {
181 ef5b664d Christos Stathis
        tagDataProvider.getList().addAll(allTags);
182 0d1e8310 Christos Stathis
        rootDataProvider.getList().add("Tags");
183 0d1e8310 Christos Stathis
    }
184 a6592a15 Christos Stathis
}