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 com.google.gwt.cell.client.AbstractCell;
39 import com.google.gwt.cell.client.Cell;
40 import com.google.gwt.cell.client.TextCell;
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.core.client.Scheduler;
43 import com.google.gwt.core.client.Scheduler.ScheduledCommand;
44 import com.google.gwt.event.dom.client.ContextMenuEvent;
45 import com.google.gwt.safehtml.shared.SafeHtml;
46 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
47 import com.google.gwt.text.shared.SafeHtmlRenderer;
48 import com.google.gwt.user.client.ui.AbstractImagePrototype;
49 import com.google.gwt.view.client.ListDataProvider;
50 import com.google.gwt.view.client.SingleSelectionModel;
51 import com.google.gwt.view.client.TreeViewModel;
52 import gr.grnet.pithos.web.client.Pithos;
53 import gr.grnet.pithos.web.client.foldertree.AccountResource;
54 import gr.grnet.pithos.web.client.foldertree.File;
55 import gr.grnet.pithos.web.client.foldertree.Folder;
56 import gr.grnet.pithos.web.client.rest.GetRequest;
57 import gr.grnet.pithos.web.client.rest.RestException;
58 import gr.grnet.pithos.web.client.tagtree.TagTreeView.Templates;
59 import java.util.ArrayList;
60 import java.util.Arrays;
61 import java.util.Iterator;
62 import java.util.LinkedHashSet;
63 import java.util.List;
66 public class TagTreeViewModel implements TreeViewModel {
68 private Cell<Tag> tagCell = new AbstractCell<Tag>(ContextMenuEvent.getType().getName()) {
71 public void render(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(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 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 private SingleSelectionModel<Tag> selectionModel;
90 public TagTreeViewModel(SingleSelectionModel<Tag> selectionModel) {
91 this.selectionModel = selectionModel;
95 public <T> NodeInfo<?> getNodeInfo(T value) {
97 return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
99 public SafeHtml render(String object) {
100 SafeHtmlBuilder builder = new SafeHtmlBuilder();
101 render(object, builder);
102 return builder.toSafeHtml();
106 public void render(String object, SafeHtmlBuilder builder) {
107 String html = AbstractImagePrototype.create(TagTreeView.images.tag()).getHTML();
108 builder.appendHtmlConstant(html);
109 builder.append(Templates.INSTANCE.nameSpan(object));
113 else if (value instanceof String) {
115 return new DefaultNodeInfo<Tag>(tagDataProvider, tagCell, selectionModel, null);
122 public boolean isLeaf(Object o) {
125 if (o instanceof String)
126 return tagDataProvider.getList().isEmpty();
131 private void fetchTags(final Iterator<Folder> iter) {
132 final Pithos app = Pithos.get();
133 if (iter.hasNext()) {
134 final Folder f = iter.next();
136 String path = app.getApiPath() + app.getUsername() + "/" + f.getUri() + "?format=json&delimiter=/&prefix=" + f.getPrefix();
137 // GetRequest<Tag> getFolder = new GetRequest<Tag>(Tag.class, path, f) {
139 // public void onSuccess(Tag result) {
140 // fetchFolder(iter, dataProvider, tags);
144 // public void onError(Throwable t) {
145 // GWT.log("Error getting folder", t);
146 // if (t instanceof RestException)
147 // Pithos.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
149 // Pithos.get().displayError("System error fetching folder: " + t.getMessage());
152 // getFolder.setHeader("X-Auth-Token", app.getToken());
153 // Scheduler.get().scheduleDeferred(getFolder);
157 // public void initialize(AccountResource account) {
158 // Iterator<Tag> iter = account.getContainers().iterator();
159 // fetchFolder(iter, rootDataProvider, account.getContainers());
162 public Tag getSelection() {
163 return selectionModel.getSelectedObject();
166 public void updateTag(Tag tag) {
170 public void fetchTag(Tag t) {
171 Pithos app = Pithos.get();
172 AccountResource account = app.getAccount();
173 Iterator<Folder> iter = account.getContainers().iterator();
174 fetchTag(iter, t, new LinkedHashSet<File>());
177 private void fetchTag(final Iterator<Folder> iter, final Tag t, final Set<File> files) {
178 final Pithos app = Pithos.get();
179 if (iter.hasNext()) {
180 Folder f = iter.next();
181 String path = app.getApiPath() + app.getUsername() + f.getUri() + "?format=json&meta=" + t.getName();
182 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, path) {
184 public void onSuccess(Folder result) {
185 files.addAll(result.getFiles());
186 fetchTag(iter, t, files);
190 public void onError(Throwable t) {
191 GWT.log("Error getting folder", t);
192 if (t instanceof RestException)
193 Pithos.get().displayError("Error getting folder: " + ((RestException) t).getHttpStatusText());
195 Pithos.get().displayError("System error fetching folder: " + t.getMessage());
198 getFolder.setHeader("X-Auth-Token", app.getToken());
199 Scheduler.get().scheduleDeferred(getFolder);
202 app.showFiles(files);
206 public void initialize(AccountResource account) {
207 List<Tag> tagList = tagDataProvider.getList();
208 for (Folder f : account.getContainers()) {
209 for (String t : f.getTags())
210 tagList.add(new Tag(t));
212 rootDataProvider.getList().add("Tags");