2 * Copyright 2011-2012 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 com.google.gwt.cell.client.AbstractCell;
39 import com.google.gwt.cell.client.Cell;
40 import com.google.gwt.event.dom.client.ContextMenuEvent;
41 import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
42 import com.google.gwt.user.client.ui.AbstractImagePrototype;
43 import com.google.gwt.view.client.ListDataProvider;
44 import com.google.gwt.view.client.SelectionChangeEvent;
45 import com.google.gwt.view.client.SingleSelectionModel;
46 import com.google.gwt.view.client.TreeViewModel;
47 import gr.grnet.pithos.web.client.Pithos;
48 import gr.grnet.pithos.web.client.commands.CreateGroupCommand;
49 import gr.grnet.pithos.web.client.foldertree.File;
50 import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates;
52 import java.util.HashMap;
53 import java.util.HashSet;
54 import java.util.List;
57 public class GroupTreeViewModel implements TreeViewModel {
58 final Group CreateGroupPlaceholder = new Group("Create new group...");
62 private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
65 public void render(Context context, Group group, SafeHtmlBuilder sb) {
66 String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
67 sb.appendHtmlConstant(html).appendHtmlConstant(" ");
68 sb.append(Templates.INSTANCE.nameSpan(group.getName()));
72 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) {
73 if(!group.equals(CreateGroupPlaceholder)) {
74 GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
75 if(event.getType().equals(ContextMenuEvent.getType().getName())) {
76 GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
77 menu.setPopupPosition(event.getClientX(), event.getClientY());
84 private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) {
87 public void render(Context context, User user, final SafeHtmlBuilder sb) {
88 String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
89 sb.appendHtmlConstant(html).appendHtmlConstant(" ");
90 final String userDisplayName = user.getUserDisplayName();
91 sb.append(Templates.INSTANCE.nameSpan(userDisplayName));
95 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) {
96 GroupTreeViewModel.this.userSelectionModel.setSelected(user, true);
97 if(event.getType().equals(ContextMenuEvent.getType().getName())) {
98 UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
99 menu.setPopupPosition(event.getClientX(), event.getClientY());
105 protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
107 protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
109 SingleSelectionModel<Group> groupSelectionModel;
110 SingleSelectionModel<User> userSelectionModel;
112 public GroupTreeViewModel(Pithos _app) {
115 groupSelectionModel = new SingleSelectionModel<Group>();
116 app.addSelectionModel(groupSelectionModel);
117 groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
120 public void onSelectionChange(SelectionChangeEvent event) {
121 Group selected = groupSelectionModel.getSelectedObject();
122 if(selected != null) {
123 app.deselectOthers(app.getGroupTreeView(), groupSelectionModel);
124 app.showFiles(new HashSet<File>());
125 app.disableUploadArea();
126 app.upload.setEnabled(false);
127 app.showRelevantToolbarButtons();
128 if(selected.equals(CreateGroupPlaceholder)) {
129 new CreateGroupCommand(app, null).execute();
130 groupSelectionModel.setSelected(CreateGroupPlaceholder, false);
134 if(app.getSelectedTree().equals(app.getGroupTreeView())) {
135 app.setSelectedTree(null);
137 if(app.getSelectedTree() == null) {
138 app.showRelevantToolbarButtons();
144 userSelectionModel = new SingleSelectionModel<User>();
145 app.addSelectionModel(userSelectionModel);
146 userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
149 public void onSelectionChange(SelectionChangeEvent event) {
150 if(userSelectionModel.getSelectedObject() != null) {
151 app.deselectOthers(app.getGroupTreeView(), userSelectionModel);
152 app.showFiles(new HashSet<File>());
153 app.showRelevantToolbarButtons();
156 if(app.getSelectedTree().equals(app.getGroupTreeView())) {
157 app.setSelectedTree(null);
159 if(app.getSelectedTree() == null) {
160 app.showRelevantToolbarButtons();
168 public <T> NodeInfo<?> getNodeInfo(T value) {
169 app.LOG("GroupTreeViewModel::getNodeInfo(), value = ", value);
171 groupsDataProvider.getList().clear();
172 groupsDataProvider.getList().addAll(app.getAccount().getGroups());
173 groupsDataProvider.getList().add(CreateGroupPlaceholder);
174 return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null);
176 final Group group = (Group) value;
177 if(userDataProviderMap.get(group) == null) {
178 userDataProviderMap.put(group, new ListDataProvider<User>());
180 final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
181 dataProvider.getList().clear();
182 final List<User> users = group.getUsers();
183 for(User user : users) {
184 app.LOG("GroupTreeViewModel::getNodeInfo(), Add ", user);
185 dataProvider.getList().add(user);
189 return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null);
193 public boolean isLeaf(Object o) {
194 if(o instanceof User) {
197 else if(o instanceof Group) {
198 return ((Group) o).getUsers().isEmpty();
203 public void updateGroupNode(Group group) {
205 groupsDataProvider.getList().clear();
206 groupsDataProvider.getList().addAll(app.getAccount().getGroups());
207 groupsDataProvider.getList().add(CreateGroupPlaceholder);
210 if(userDataProviderMap.get(group) == null) {
211 userDataProviderMap.put(group, new ListDataProvider<User>());
213 final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
214 dataProvider.getList().clear();
215 for(User user : group.getUsers()) {
216 app.LOG("GroupTreeViewModel::updateGroupNode(), group = ", group, ". Add ", user);
217 dataProvider.getList().add(user);
222 public Object getSelectedObject() {
223 if(groupSelectionModel.getSelectedObject() != null) {
224 return groupSelectionModel.getSelectedObject();
226 if(userSelectionModel.getSelectedObject() != null) {
227 return userSelectionModel.getSelectedObject();