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