379c21fbea8f8e2ae6bec33d442bd7641437d864
[pithos-web-client] / src / gr / grnet / pithos / web / client / grouptree / GroupTreeViewModel.java
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.grouptree;
37
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;
51
52 import java.util.HashMap;
53 import java.util.HashSet;
54 import java.util.List;
55 import java.util.Map;
56
57 public class GroupTreeViewModel implements TreeViewModel {
58     final Group CreateGroupPlaceholder = new Group("Create new group...");
59
60     protected Pithos app;
61
62     private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
63
64         @Override
65         public void render(Context context, Group group, SafeHtmlBuilder sb) {
66             String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
67             sb.appendHtmlConstant(html).appendHtmlConstant("&nbsp;");
68             sb.append(Templates.INSTANCE.nameSpan(group.getName()));
69         }
70
71         @Override
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());
78                     menu.show();
79                 }
80             }
81         }
82     };
83
84     private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) {
85
86         @Override
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("&nbsp;");
90             final String userDisplayName = user.getUserDisplayName();
91             sb.append(Templates.INSTANCE.nameSpan(userDisplayName));
92         }
93
94         @Override
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());
100                 menu.show();
101             }
102         }
103     };
104
105     protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
106
107     protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
108
109     SingleSelectionModel<Group> groupSelectionModel;
110     SingleSelectionModel<User> userSelectionModel;
111
112     public GroupTreeViewModel(Pithos _app) {
113         app = _app;
114
115         groupSelectionModel = new SingleSelectionModel<Group>();
116         app.addSelectionModel(groupSelectionModel);
117         groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
118
119             @Override
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);
131                     }
132                 }
133                 else {
134                     if(app.getSelectedTree().equals(app.getGroupTreeView())) {
135                         app.setSelectedTree(null);
136                     }
137                     if(app.getSelectedTree() == null) {
138                         app.showRelevantToolbarButtons();
139                     }
140                 }
141             }
142         });
143
144         userSelectionModel = new SingleSelectionModel<User>();
145         app.addSelectionModel(userSelectionModel);
146         userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
147
148             @Override
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();
154                 }
155                 else {
156                     if(app.getSelectedTree().equals(app.getGroupTreeView())) {
157                         app.setSelectedTree(null);
158                     }
159                     if(app.getSelectedTree() == null) {
160                         app.showRelevantToolbarButtons();
161                     }
162                 }
163             }
164         });
165     }
166
167     @Override
168     public <T> NodeInfo<?> getNodeInfo(T value) {
169         app.LOG("GroupTreeViewModel::getNodeInfo(), value = ", value);
170         if(value == null) {
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);
175         }
176         final Group group = (Group) value;
177         if(userDataProviderMap.get(group) == null) {
178             userDataProviderMap.put(group, new ListDataProvider<User>());
179         }
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);
186         }
187
188
189         return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null);
190     }
191
192     @Override
193     public boolean isLeaf(Object o) {
194         if(o instanceof User) {
195             return true;
196         }
197         else if(o instanceof Group) {
198             return ((Group) o).getUsers().isEmpty();
199         }
200         return false;
201     }
202
203     public void updateGroupNode(Group group) {
204         if(group == null) {
205             groupsDataProvider.getList().clear();
206             groupsDataProvider.getList().addAll(app.getAccount().getGroups());
207             groupsDataProvider.getList().add(CreateGroupPlaceholder);
208         }
209         else {
210             if(userDataProviderMap.get(group) == null) {
211                 userDataProviderMap.put(group, new ListDataProvider<User>());
212             }
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);
218             }
219         }
220     }
221
222     public Object getSelectedObject() {
223         if(groupSelectionModel.getSelectedObject() != null) {
224             return groupSelectionModel.getSelectedObject();
225         }
226         if(userSelectionModel.getSelectedObject() != null) {
227             return userSelectionModel.getSelectedObject();
228         }
229         return null;
230     }
231 }