Finished tools menu
[pithos-web-client] / src / gr / grnet / pithos / web / client / grouptree / GroupTreeViewModel.java
1 /*
2  * Copyright 2011 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 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;
42
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.Map;
46 import java.util.Set;
47
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;
57
58 public class GroupTreeViewModel implements TreeViewModel {
59
60     protected Pithos app;
61
62     private ListDataProvider<String> rootDataProvider = new ListDataProvider<String>();
63     
64     private Cell<String> rootCell = new AbstractCell<String>(ContextMenuEvent.getType().getName()) {
65
66                 @Override
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));
71                 }
72                 
73         @Override
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());
79                 menu.show();
80             }
81         }
82         };
83
84         private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) {
85
86                 @Override
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()));
91                 }
92                 
93         @Override
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());
99                 menu.show();
100             }
101         }
102         };
103
104     private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) {
105
106                 @Override
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()));
111                 }
112
113         @Override
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());
119                 menu.show();
120             }
121         }
122     };
123
124     protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>();
125
126     protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>();
127     
128     protected Map<String, Set<File>> sharedFiles = new HashMap<String, Set<File>>();
129
130     SingleSelectionModel<String> rootSelectionModel;
131     SingleSelectionModel<Group> groupSelectionModel;
132     SingleSelectionModel<User> userSelectionModel;
133
134     public GroupTreeViewModel(Pithos _app) {
135         app = _app;
136         rootSelectionModel = new SingleSelectionModel<String>();
137         app.addSelectionModel(rootSelectionModel);
138         rootSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
139                         
140                         @Override
141                         public void onSelectionChange(SelectionChangeEvent event) {
142                                 if (rootSelectionModel.getSelectedObject() != null) {
143                                         app.deselectOthers(app.getGroupTreeView(), rootSelectionModel);
144                                         app.showFiles(new HashSet<File>());
145                                 }
146                         }
147                 });
148
149         groupSelectionModel = new SingleSelectionModel<Group>();
150         app.addSelectionModel(groupSelectionModel);
151         groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
152                         
153                         @Override
154                         public void onSelectionChange(SelectionChangeEvent event) {
155                                 if (groupSelectionModel.getSelectedObject() != null) {
156                                         app.deselectOthers(app.getGroupTreeView(), groupSelectionModel);
157                                         app.showFiles(new HashSet<File>());
158                                 }
159                         }
160                 });
161
162         userSelectionModel = new SingleSelectionModel<User>();
163         app.addSelectionModel(userSelectionModel);
164         userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
165                         
166                         @Override
167                         public void onSelectionChange(SelectionChangeEvent event) {
168                                 if (userSelectionModel.getSelectedObject() != null) {
169                                         app.deselectOthers(app.getGroupTreeView(), userSelectionModel);
170                                         app.showFiles(new HashSet<File>());
171                                 }
172                         }
173                 });
174 }
175
176     @Override
177     public <T> NodeInfo<?> getNodeInfo(T value) {
178         if (value == null) {
179                 rootDataProvider.getList().add("");
180             return new DefaultNodeInfo<String>(rootDataProvider, rootCell,  rootSelectionModel, null);
181         }
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);
186         }
187         else { //Group
188                 Group g = (Group) value;
189                         if (userDataProviderMap.get(g) == null) {
190                                 userDataProviderMap.put(g, new ListDataProvider<User>());
191                         }
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);
197         }
198     }
199
200         @Override
201     public boolean isLeaf(Object o) {
202         if (o instanceof String) {
203                 return ((String) o).length() == 0 || app.getAccount().getGroups().isEmpty();
204         }
205         else if (o instanceof Group)
206                 return ((Group) o).getMembers().isEmpty();
207         else if (o != null)
208                 return true;
209         return false;
210     }
211         
212         public void initialize() {
213         rootDataProvider.getList().clear();
214         rootDataProvider.getList().add("Groups");
215         }
216
217         public void updateGroupNode(Group group) {
218                 if (group == null) {
219                         groupsDataProvider.getList().clear();
220                         groupsDataProvider.getList().addAll(app.getAccount().getGroups());
221                 }
222                 else {
223                         if (userDataProviderMap.get(group) == null) {
224                                 userDataProviderMap.put(group, new ListDataProvider<User>());
225                         }
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));
230                 }
231         }
232
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();
240                 return null;
241         }
242 }