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.grouptree.GroupTreeView.Templates;
42 import java.util.HashMap;
43 import java.util.HashSet;
47 import com.google.gwt.cell.client.AbstractCell;
48 import com.google.gwt.cell.client.Cell;
49 import com.google.gwt.event.dom.client.ContextMenuEvent;
50 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
51 import com.google.gwt.user.client.ui.AbstractImagePrototype;
52 import com.google.gwt.view.client.ListDataProvider;
53 import com.google.gwt.view.client.SelectionChangeEvent;
54 import com.google.gwt.view.client.SingleSelectionModel;
55 import com.google.gwt.view.client.TreeViewModel;
57 public class GroupTreeViewModel implements TreeViewModel {
61 private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
63 private Cell<String> rootCell = new AbstractCell<String>(ContextMenuEvent.getType().getName()) {
66 public void render(Context context, String value, SafeHtmlBuilder sb) {
67 String html = AbstractImagePrototype.create(GroupTreeView.images.groups()).getHTML();
68 sb.appendHtmlConstant(html).appendHtmlConstant(" ");
69 sb.append(Templates.INSTANCE.nameSpan(value));
73 public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, String s, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<String> valueUpdater) {
74 GroupTreeViewModel.this.rootSelectionModel.setSelected(s, true);
75 if (event.getType().equals(ContextMenuEvent.getType().getName())) {
76 GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, null);
77 menu.setPopupPosition(event.getClientX(), event.getClientY());
83 private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
86 public void render(Context context, Group value, SafeHtmlBuilder sb) {
87 String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
88 sb.appendHtmlConstant(html).appendHtmlConstant(" ");
89 sb.append(Templates.INSTANCE.nameSpan(value.getName()));
93 public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, Group group, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<Group> valueUpdater) {
94 GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
95 if (event.getType().equals(ContextMenuEvent.getType().getName())) {
96 GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
97 menu.setPopupPosition(event.getClientX(), event.getClientY());
103 private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) {
106 public void render(Context context, User value, SafeHtmlBuilder sb) {
107 String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
108 sb.appendHtmlConstant(html).appendHtmlConstant(" ");
109 sb.append(Templates.INSTANCE.nameSpan(value.getName()));
113 public void onBrowserEvent(Cell.Context context, com.google.gwt.dom.client.Element parent, User user, com.google.gwt.dom.client.NativeEvent event, com.google.gwt.cell.client.ValueUpdater<User> valueUpdater) {
114 GroupTreeViewModel.this.userSelectionModel.setSelected(user, true);
115 if (event.getType().equals(ContextMenuEvent.getType().getName())) {
116 UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
117 menu.setPopupPosition(event.getClientX(), event.getClientY());
123 protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
125 protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
127 protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
129 SingleSelectionModel<String> rootSelectionModel;
130 SingleSelectionModel<Group> groupSelectionModel;
131 SingleSelectionModel<User> userSelectionModel;
133 public GroupTreeViewModel(Pithos _app) {
135 rootSelectionModel = new SingleSelectionModel<String>();
136 app.addSelectionModel(rootSelectionModel);
137 rootSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
140 public void onSelectionChange(SelectionChangeEvent event) {
141 if (rootSelectionModel.getSelectedObject() != null) {
142 app.deselectOthers(app.getGroupTreeView(), rootSelectionModel);
143 app.showFiles(new HashSet<File>());
148 groupSelectionModel = new SingleSelectionModel<Group>();
149 app.addSelectionModel(groupSelectionModel);
150 groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
153 public void onSelectionChange(SelectionChangeEvent event) {
154 if (groupSelectionModel.getSelectedObject() != null) {
155 app.deselectOthers(app.getGroupTreeView(), groupSelectionModel);
156 app.showFiles(new HashSet<File>());
161 userSelectionModel = new SingleSelectionModel<User>();
162 app.addSelectionModel(userSelectionModel);
163 userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
166 public void onSelectionChange(SelectionChangeEvent event) {
167 if (userSelectionModel.getSelectedObject() != null) {
168 app.deselectOthers(app.getGroupTreeView(), userSelectionModel);
169 app.showFiles(new HashSet<File>());
176 public <T> NodeInfo<?> getNodeInfo(T value) {
178 rootDataProvider.getList().add("");
179 return new DefaultNodeInfo<String>(rootDataProvider, rootCell, rootSelectionModel, null);
181 else if (value instanceof String) {
182 groupsDataProvider.getList().clear();
183 groupsDataProvider.getList().addAll(app.getAccount().getGroups());
184 return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null);
187 Group g = (Group) value;
188 if (userDataProviderMap.get(g) == null) {
189 userDataProviderMap.put(g, new ListDataProvider<User>());
191 final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
192 dataProvider.getList().clear();
193 for (String u : g.getMembers())
194 dataProvider.getList().add(new User(u, g));
195 return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null);
200 public boolean isLeaf(Object o) {
201 if (o instanceof String) {
202 return ((String) o).length() == 0 || app.getAccount().getGroups().isEmpty();
204 else if (o instanceof Group)
205 return ((Group) o).getMembers().isEmpty();
211 public void initialize() {
212 rootDataProvider.getList().clear();
213 rootDataProvider.getList().add("Groups");
216 public void updateGroupNode(Group group) {
218 groupsDataProvider.getList().clear();
219 groupsDataProvider.getList().addAll(app.getAccount().getGroups());
222 if (userDataProviderMap.get(group) == null) {
223 userDataProviderMap.put(group, new ListDataProvider<User>());
225 final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
226 dataProvider.getList().clear();
227 for (String u : group.getMembers())
228 dataProvider.getList().add(new User(u, group));
232 public Object getSelectedObject() {
233 if (rootSelectionModel.getSelectedObject() != null)
234 return rootSelectionModel.getSelectedObject();
235 if (groupSelectionModel.getSelectedObject() != null)
236 return groupSelectionModel.getSelectedObject();
237 if (userSelectionModel.getSelectedObject() != null)
238 return userSelectionModel.getSelectedObject();