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.safehtml.shared.SafeHtml;
58 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
59 import com.google.gwt.text.shared.SafeHtmlRenderer;
60 import com.google.gwt.user.client.ui.AbstractImagePrototype;
61 import com.google.gwt.view.client.ListDataProvider;
62 import com.google.gwt.view.client.SingleSelectionModel;
63 import com.google.gwt.view.client.TreeViewModel;
65 public class TagTreeViewModel implements TreeViewModel {
67 private Cell<Tag> tagCell = new AbstractCell<Tag>(ContextMenuEvent.getType().getName()) {
70 public void render(@SuppressWarnings("unused") Context context, Tag tag, SafeHtmlBuilder safeHtmlBuilder) {
71 String html = AbstractImagePrototype.create(TagTreeView.images.tag()).getHTML();
72 safeHtmlBuilder.appendHtmlConstant(html);
73 safeHtmlBuilder.append(Templates.INSTANCE.nameSpan(tag.getName()));
77 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) {
78 if (event.getType().equals(com.google.gwt.event.dom.client.ContextMenuEvent.getType().getName())) {
79 TagTreeViewModel.this.selectionModel.setSelected(tag, true);
84 private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
85 private ListDataProvider<Tag> tagDataProvider = new ListDataProvider<Tag>();
87 protected SingleSelectionModel<Tag> selectionModel;
91 public TagTreeViewModel(Pithos _app, SingleSelectionModel<Tag> selectionModel) {
93 this.selectionModel = selectionModel;
97 public <T> NodeInfo<?> getNodeInfo(T value) {
99 return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() {
101 public SafeHtml render(String object) {
102 SafeHtmlBuilder builder = new SafeHtmlBuilder();
103 render(object, builder);
104 return builder.toSafeHtml();
108 public void render(String object, SafeHtmlBuilder builder) {
109 String html = AbstractImagePrototype.create(TagTreeView.images.tag()).getHTML();
110 builder.appendHtmlConstant(html);
111 builder.append(Templates.INSTANCE.nameSpan(object));
115 else if (value instanceof String) {
117 return new DefaultNodeInfo<Tag>(tagDataProvider, tagCell, selectionModel, null);
124 public boolean isLeaf(Object o) {
127 if (o instanceof String)
128 return tagDataProvider.getList().isEmpty();
132 public Tag getSelection() {
133 return selectionModel.getSelectedObject();
136 public void updateTag(Tag tag) {
140 public void fetchTag(Tag t) {
141 AccountResource account = app.getAccount();
142 Iterator<Folder> iter = account.getContainers().iterator();
143 fetchTag(iter, t, new LinkedHashSet<File>());
146 protected void fetchTag(final Iterator<Folder> iter, final Tag t, final Set<File> files) {
147 if (iter.hasNext()) {
148 Folder f = iter.next();
149 String path = f.getUri() + "?format=json&meta=" + t.getName();
150 GetRequest<Folder> getFolder = new GetRequest<Folder>(Folder.class, app.getApiPath(), app.getUsername(), path) {
152 public void onSuccess(Folder _result) {
153 files.addAll(_result.getFiles());
154 fetchTag(iter, t, files);
158 public void onError(Throwable th) {
159 GWT.log("Error getting folder", th);
160 if (th instanceof RestException)
161 app.displayError("Error getting folder: " + ((RestException) th).getHttpStatusText());
163 app.displayError("System error fetching folder: " + th.getMessage());
166 getFolder.setHeader("X-Auth-Token", app.getToken());
167 Scheduler.get().scheduleDeferred(getFolder);
170 app.showFiles(files);
174 public void initialize(List<Tag> allTags) {
175 tagDataProvider.getList().addAll(allTags);
176 rootDataProvider.getList().add("Tags");