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.grouptree;
38 import gr.grnet.pithos.web.client.Pithos;
39 import gr.grnet.pithos.web.client.foldertree.File;
40 import gr.grnet.pithos.web.client.foldertree.FolderTreeViewModel;
41 import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates;
43 import java.util.HashMap;
44 import java.util.HashSet;
48 import com.google.gwt.cell.client.AbstractCell;
49 import com.google.gwt.cell.client.Cell;
50 import com.google.gwt.event.dom.client.ContextMenuEvent;
51 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
52 import com.google.gwt.user.client.ui.AbstractImagePrototype;
53 import com.google.gwt.view.client.ListDataProvider;
54 import com.google.gwt.view.client.SelectionChangeEvent;
55 import com.google.gwt.view.client.SingleSelectionModel;
56 import com.google.gwt.view.client.TreeViewModel;
58 public class GroupTreeViewModel implements TreeViewModel {
62 private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
64 private Cell<String> rootCell = new AbstractCell<String>(ContextMenuEvent.getType().getName()) {
67 public void render(@SuppressWarnings("unused") Context context, String value, SafeHtmlBuilder sb) {
68 String html = AbstractImagePrototype.create(GroupTreeView.images.groups()).getHTML();
69 sb.appendHtmlConstant(html);
70 sb.append(Templates.INSTANCE.nameSpan(value));
74 public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, @SuppressWarnings("unused") String s, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<String> valueUpdater) {
75 GroupTreeViewModel.this.rootSelectionModel.setSelected(s, true);
76 if (event.getType().equals(ContextMenuEvent.getType().getName())) {
77 GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, null);
78 menu.setPopupPosition(event.getClientX(), event.getClientY());
84 private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
87 public void render(@SuppressWarnings("unused") Context context, Group value, SafeHtmlBuilder sb) {
88 String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
89 sb.appendHtmlConstant(html);
90 sb.append(Templates.INSTANCE.nameSpan(value.getName()));
94 public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, Group group, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<Group> valueUpdater) {
95 GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
96 if (event.getType().equals(ContextMenuEvent.getType().getName())) {
97 GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
98 menu.setPopupPosition(event.getClientX(), event.getClientY());
104 private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) {
107 public void render(@SuppressWarnings("unused") Context context, User value, SafeHtmlBuilder sb) {
108 String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
109 sb.appendHtmlConstant(html);
110 sb.append(Templates.INSTANCE.nameSpan(value.getName()));
114 public void onBrowserEvent(@SuppressWarnings("unused") Cell.Context context, @SuppressWarnings("unused") com.google.gwt.dom.client.Element parent, User user, com.google.gwt.dom.client.NativeEvent event, @SuppressWarnings("unused") com.google.gwt.cell.client.ValueUpdater<User> valueUpdater) {
115 GroupTreeViewModel.this.userSelectionModel.setSelected(user, true);
116 if (event.getType().equals(ContextMenuEvent.getType().getName())) {
117 UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
118 menu.setPopupPosition(event.getClientX(), event.getClientY());
124 protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
126 protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
128 protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
130 SingleSelectionModel<String> rootSelectionModel;
131 SingleSelectionModel<Group> groupSelectionModel;
132 SingleSelectionModel<User> userSelectionModel;
134 public GroupTreeViewModel(Pithos _app) {
136 rootSelectionModel = new SingleSelectionModel<String>();
137 app.addSelectionModel(rootSelectionModel);
138 rootSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
141 public void onSelectionChange(SelectionChangeEvent event) {
142 if (rootSelectionModel.getSelectedObject() != null) {
143 app.deselectOthers(app.getGroupTreeView(), rootSelectionModel);
144 app.showFiles(new HashSet<File>());
149 groupSelectionModel = new SingleSelectionModel<Group>();
150 app.addSelectionModel(groupSelectionModel);
151 groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
154 public void onSelectionChange(SelectionChangeEvent event) {
155 if (groupSelectionModel.getSelectedObject() != null) {
156 app.deselectOthers(app.getGroupTreeView(), groupSelectionModel);
157 app.showFiles(new HashSet<File>());
162 userSelectionModel = new SingleSelectionModel<User>();
163 app.addSelectionModel(userSelectionModel);
164 userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
167 public void onSelectionChange(SelectionChangeEvent event) {
168 if (userSelectionModel.getSelectedObject() != null) {
169 app.deselectOthers(app.getGroupTreeView(), userSelectionModel);
170 app.showFiles(new HashSet<File>());
177 public <T> NodeInfo<?> getNodeInfo(T value) {
179 rootDataProvider.getList().add("");
180 return new DefaultNodeInfo<String>(rootDataProvider, rootCell, rootSelectionModel, null);
182 else if (value instanceof String) {
183 groupsDataProvider.getList().clear();
184 groupsDataProvider.getList().addAll(app.getAccount().getGroups());
185 return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null);
188 Group g = (Group) value;
189 if (userDataProviderMap.get(g) == null) {
190 userDataProviderMap.put(g, new ListDataProvider<User>());
192 final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
193 dataProvider.getList().clear();
194 for (String u : g.getMembers())
195 dataProvider.getList().add(new User(u, g));
196 return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null);
201 public boolean isLeaf(Object o) {
202 if (o instanceof String) {
203 return ((String) o).length() == 0 || app.getAccount().getGroups().isEmpty();
205 else if (o instanceof Group)
206 return ((Group) o).getMembers().isEmpty();
212 public void initialize() {
213 rootDataProvider.getList().clear();
214 rootDataProvider.getList().add("Groups");
217 public void updateGroupNode(Group group) {
219 groupsDataProvider.getList().clear();
220 groupsDataProvider.getList().addAll(app.getAccount().getGroups());
223 if (userDataProviderMap.get(group) == null) {
224 userDataProviderMap.put(group, new ListDataProvider<User>());
226 final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
227 dataProvider.getList().clear();
228 for (String u : group.getMembers())
229 dataProvider.getList().add(new User(u, group));
233 public Object getSelectedObject() {
234 if (rootSelectionModel.getSelectedObject() != null)
235 return rootSelectionModel.getSelectedObject();
236 if (groupSelectionModel.getSelectedObject() != null)
237 return groupSelectionModel.getSelectedObject();
238 if (userSelectionModel.getSelectedObject() != null)
239 return userSelectionModel.getSelectedObject();