root / src / gr / grnet / pithos / web / client / tagtree / TagTreeViewModel.java @ cc0120ab
History | View | Annotate | Download (7.1 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011-2012 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 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; |
45 |
|
46 |
import java.util.Iterator; |
47 |
import java.util.LinkedHashSet; |
48 |
import java.util.List; |
49 |
import java.util.Set; |
50 |
|
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; |
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 |
protected SingleSelectionModel<Tag> selectionModel; |
89 |
|
90 |
protected Pithos app;
|
91 |
|
92 |
public TagTreeViewModel(Pithos _app, SingleSelectionModel<Tag> selectionModel) { |
93 |
app = _app; |
94 |
this.selectionModel = selectionModel;
|
95 |
} |
96 |
|
97 |
@Override
|
98 |
public <T> NodeInfo<?> getNodeInfo(T value) {
|
99 |
if (value == null) { |
100 |
return new DefaultNodeInfo<String>(rootDataProvider, new TextCell(new SafeHtmlRenderer<String>() { |
101 |
@Override
|
102 |
public SafeHtml render(String object) { |
103 |
SafeHtmlBuilder builder = new SafeHtmlBuilder();
|
104 |
render(object, builder); |
105 |
return builder.toSafeHtml();
|
106 |
} |
107 |
|
108 |
@Override
|
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));
|
113 |
} |
114 |
}), null, null); |
115 |
} |
116 |
else if (value instanceof String) { |
117 |
return new DefaultNodeInfo<Tag>(tagDataProvider, tagCell, selectionModel, null); |
118 |
} |
119 |
else
|
120 |
return null; |
121 |
} |
122 |
|
123 |
@Override
|
124 |
public boolean isLeaf(Object o) { |
125 |
if (o == null) |
126 |
return false; |
127 |
if (o instanceof String) |
128 |
return tagDataProvider.getList().isEmpty();
|
129 |
return true; |
130 |
} |
131 |
|
132 |
public Tag getSelection() {
|
133 |
return selectionModel.getSelectedObject();
|
134 |
} |
135 |
|
136 |
public void updateTag(Tag tag) { |
137 |
fetchTag(tag); |
138 |
} |
139 |
|
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>()); |
144 |
} |
145 |
|
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.getUserID(), path) {
|
151 |
@Override
|
152 |
public void onSuccess(Folder _result) { |
153 |
files.addAll(_result.getFiles()); |
154 |
fetchTag(iter, t, files); |
155 |
} |
156 |
|
157 |
@Override
|
158 |
public void onError(Throwable th) { |
159 |
GWT.log("Error getting folder", th);
|
160 |
app.setError(th); |
161 |
if (th instanceof RestException) |
162 |
app.displayError("Error getting folder: " + ((RestException) th).getHttpStatusText());
|
163 |
else
|
164 |
app.displayError("System error fetching folder: " + th.getMessage());
|
165 |
} |
166 |
|
167 |
@Override
|
168 |
protected void onUnauthorized(Response response) { |
169 |
app.sessionExpired(); |
170 |
} |
171 |
}; |
172 |
getFolder.setHeader("X-Auth-Token", app.getUserToken());
|
173 |
Scheduler.get().scheduleDeferred(getFolder); |
174 |
} |
175 |
else {
|
176 |
app.showFiles(files); |
177 |
} |
178 |
} |
179 |
|
180 |
public void initialize(List<Tag> allTags) { |
181 |
tagDataProvider.getList().addAll(allTags); |
182 |
rootDataProvider.getList().add("Tags");
|
183 |
} |
184 |
} |