root / src / gr / grnet / pithos / web / client / tagtree / TagTreeViewModel.java @ a845b044
History | View | Annotate | Download (8.6 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011 GRNET S.A. All rights reserved.
|
3 |
*
|
4 |
* Redistribution and use in source and binary forms, with or
|
5 |
* without modification, are permitted provided that the following
|
6 |
* conditions are met:
|
7 |
*
|
8 |
* 1. Redistributions of source code must retain the above
|
9 |
* copyright notice, this list of conditions and the following
|
10 |
* disclaimer.
|
11 |
*
|
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.
|
16 |
*
|
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.
|
29 |
*
|
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.
|
34 |
*/
|
35 |
|
36 |
package gr.grnet.pithos.web.client.tagtree; |
37 |
|
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; |
64 |
import java.util.Set; |
65 |
|
66 |
public class TagTreeViewModel implements TreeViewModel { |
67 |
|
68 |
private Cell<Tag> tagCell = new AbstractCell<Tag>(ContextMenuEvent.getType().getName()) { |
69 |
|
70 |
@Override
|
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()));
|
75 |
} |
76 |
|
77 |
@Override
|
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);
|
81 |
} |
82 |
} |
83 |
}; |
84 |
|
85 |
private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>(); |
86 |
private ListDataProvider<Tag> tagDataProvider = new ListDataProvider<Tag>(); |
87 |
|
88 |
private SingleSelectionModel<Tag> selectionModel; |
89 |
|
90 |
public TagTreeViewModel(SingleSelectionModel<Tag> selectionModel) { |
91 |
this.selectionModel = selectionModel;
|
92 |
} |
93 |
|
94 |
@Override
|
95 |
public <T> NodeInfo<?> getNodeInfo(T value) {
|
96 |
if (value == null) { |
97 |
return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() { |
98 |
@Override
|
99 |
public SafeHtml render(String object) { |
100 |
SafeHtmlBuilder builder = new SafeHtmlBuilder();
|
101 |
render(object, builder); |
102 |
return builder.toSafeHtml();
|
103 |
} |
104 |
|
105 |
@Override
|
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));
|
110 |
} |
111 |
}), null, null); |
112 |
} |
113 |
else if (value instanceof String) { |
114 |
//fetchAllTags();
|
115 |
return new DefaultNodeInfo<Tag>(tagDataProvider, tagCell, selectionModel, null); |
116 |
} |
117 |
else
|
118 |
return null; |
119 |
} |
120 |
|
121 |
@Override
|
122 |
public boolean isLeaf(Object o) { |
123 |
if (o == null) |
124 |
return false; |
125 |
if (o instanceof String) |
126 |
return tagDataProvider.getList().isEmpty();
|
127 |
else
|
128 |
return true; |
129 |
} |
130 |
|
131 |
private void fetchTags(final Iterator<Folder> iter) { |
132 |
final Pithos app = Pithos.get();
|
133 |
if (iter.hasNext()) {
|
134 |
final Folder f = iter.next();
|
135 |
|
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) {
|
138 |
// @Override
|
139 |
// public void onSuccess(Tag result) {
|
140 |
// fetchFolder(iter, dataProvider, tags);
|
141 |
// }
|
142 |
//
|
143 |
// @Override
|
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());
|
148 |
// else
|
149 |
// Pithos.get().displayError("System error fetching folder: " + t.getMessage());
|
150 |
// }
|
151 |
// };
|
152 |
// getFolder.setHeader("X-Auth-Token", app.getToken());
|
153 |
// Scheduler.get().scheduleDeferred(getFolder);
|
154 |
} |
155 |
} |
156 |
//
|
157 |
// public void initialize(AccountResource account) {
|
158 |
// Iterator<Tag> iter = account.getContainers().iterator();
|
159 |
// fetchFolder(iter, rootDataProvider, account.getContainers());
|
160 |
// }
|
161 |
//
|
162 |
public Tag getSelection() {
|
163 |
return selectionModel.getSelectedObject();
|
164 |
} |
165 |
|
166 |
public void updateTag(Tag tag) { |
167 |
fetchTag(tag); |
168 |
} |
169 |
|
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>()); |
175 |
} |
176 |
|
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) {
|
183 |
@Override
|
184 |
public void onSuccess(Folder result) { |
185 |
files.addAll(result.getFiles()); |
186 |
fetchTag(iter, t, files); |
187 |
} |
188 |
|
189 |
@Override
|
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());
|
194 |
else
|
195 |
Pithos.get().displayError("System error fetching folder: " + t.getMessage());
|
196 |
} |
197 |
}; |
198 |
getFolder.setHeader("X-Auth-Token", app.getToken());
|
199 |
Scheduler.get().scheduleDeferred(getFolder); |
200 |
} |
201 |
else {
|
202 |
app.showFiles(files); |
203 |
} |
204 |
} |
205 |
|
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));
|
211 |
} |
212 |
rootDataProvider.getList().add("Tags");
|
213 |
} |
214 |
} |