root / src / gr / grnet / pithos / web / client / grouptree / GroupTreeViewModel.java @ e8288e3f
History | View | Annotate | Download (8.6 kB)
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 gr.grnet.pithos.web.client.Pithos; |
39 |
import gr.grnet.pithos.web.client.commands.CreateGroupCommand; |
40 |
import gr.grnet.pithos.web.client.foldertree.File; |
41 |
import gr.grnet.pithos.web.client.foldertree.FileFolderResource; |
42 |
import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates; |
43 |
|
44 |
import java.util.ArrayList; |
45 |
import java.util.HashMap; |
46 |
import java.util.HashSet; |
47 |
import java.util.Map; |
48 |
|
49 |
import com.google.gwt.cell.client.AbstractCell; |
50 |
import com.google.gwt.cell.client.Cell; |
51 |
import com.google.gwt.event.dom.client.ContextMenuEvent; |
52 |
import com.google.gwt.safehtml.shared.SafeHtmlBuilder; |
53 |
import com.google.gwt.user.client.ui.AbstractImagePrototype; |
54 |
import com.google.gwt.view.client.ListDataProvider; |
55 |
import com.google.gwt.view.client.SelectionChangeEvent; |
56 |
import com.google.gwt.view.client.SingleSelectionModel; |
57 |
import com.google.gwt.view.client.TreeViewModel; |
58 |
|
59 |
public class GroupTreeViewModel implements TreeViewModel { |
60 |
|
61 |
protected Pithos app;
|
62 |
|
63 |
private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) { |
64 |
|
65 |
@Override
|
66 |
public void render(Context context, Group value, SafeHtmlBuilder sb) { |
67 |
String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
|
68 |
sb.appendHtmlConstant(html).appendHtmlConstant(" ");
|
69 |
sb.append(Templates.INSTANCE.nameSpan(value.getName()));
|
70 |
} |
71 |
|
72 |
@Override
|
73 |
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) { |
74 |
if (!group.equals(createGroup)) {
|
75 |
GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
|
76 |
if (event.getType().equals(ContextMenuEvent.getType().getName())) {
|
77 |
GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
|
78 |
menu.setPopupPosition(event.getClientX(), event.getClientY()); |
79 |
menu.show(); |
80 |
} |
81 |
} |
82 |
} |
83 |
}; |
84 |
|
85 |
private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) { |
86 |
|
87 |
@Override
|
88 |
public void render(Context context, User value, SafeHtmlBuilder sb) { |
89 |
String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
|
90 |
sb.appendHtmlConstant(html).appendHtmlConstant(" ");
|
91 |
sb.append(Templates.INSTANCE.nameSpan(value.getName()));
|
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 |
final Group createGroup = new Group("Create new group..."); |
113 |
|
114 |
public GroupTreeViewModel(Pithos _app) {
|
115 |
app = _app; |
116 |
|
117 |
groupSelectionModel = new SingleSelectionModel<Group>(); |
118 |
app.addSelectionModel(groupSelectionModel); |
119 |
groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
|
120 |
|
121 |
@Override
|
122 |
public void onSelectionChange(SelectionChangeEvent event) { |
123 |
Group selected = groupSelectionModel.getSelectedObject();
|
124 |
if (selected != null) { |
125 |
app.deselectOthers(app.getGroupTreeView(), groupSelectionModel); |
126 |
app.showFiles(new ArrayList<FileFolderResource>()); |
127 |
app.disableUploadArea(); |
128 |
app.upload.setEnabled(false);
|
129 |
app.showRelevantToolbarButtons(); |
130 |
if (selected.equals(createGroup)) {
|
131 |
new CreateGroupCommand(app, null).execute(); |
132 |
groupSelectionModel.setSelected(createGroup, false);
|
133 |
} |
134 |
} |
135 |
else {
|
136 |
if (app.getSelectedTree().equals(app.getGroupTreeView()))
|
137 |
app.setSelectedTree(null);
|
138 |
if (app.getSelectedTree() == null) |
139 |
app.showRelevantToolbarButtons(); |
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 ArrayList<FileFolderResource>()); |
153 |
app.showRelevantToolbarButtons(); |
154 |
} |
155 |
else {
|
156 |
if (app.getSelectedTree().equals(app.getGroupTreeView()))
|
157 |
app.setSelectedTree(null);
|
158 |
if (app.getSelectedTree() == null) |
159 |
app.showRelevantToolbarButtons(); |
160 |
} |
161 |
} |
162 |
}); |
163 |
} |
164 |
|
165 |
@Override
|
166 |
public <T> NodeInfo<?> getNodeInfo(T value) {
|
167 |
if (value == null) { |
168 |
groupsDataProvider.getList().clear(); |
169 |
groupsDataProvider.getList().addAll(app.getAccount().getGroups()); |
170 |
groupsDataProvider.getList().add(createGroup); |
171 |
return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null); |
172 |
} |
173 |
Group g = (Group) value; |
174 |
if (userDataProviderMap.get(g) == null) { |
175 |
userDataProviderMap.put(g, new ListDataProvider<User>());
|
176 |
} |
177 |
final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
|
178 |
dataProvider.getList().clear(); |
179 |
for (String u : g.getMembers()) |
180 |
dataProvider.getList().add(new User(u, g.getName()));
|
181 |
return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null); |
182 |
} |
183 |
|
184 |
@Override
|
185 |
public boolean isLeaf(Object o) { |
186 |
if (o instanceof User) { |
187 |
return true; |
188 |
} |
189 |
else if (o instanceof Group) |
190 |
return ((Group) o).getMembers().isEmpty(); |
191 |
return false; |
192 |
} |
193 |
|
194 |
public void updateGroupNode(Group group) { |
195 |
if (group == null) { |
196 |
groupsDataProvider.getList().clear(); |
197 |
groupsDataProvider.getList().addAll(app.getAccount().getGroups()); |
198 |
groupsDataProvider.getList().add(createGroup); |
199 |
} |
200 |
else {
|
201 |
if (userDataProviderMap.get(group) == null) { |
202 |
userDataProviderMap.put(group, new ListDataProvider<User>());
|
203 |
} |
204 |
final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
|
205 |
dataProvider.getList().clear(); |
206 |
for (String u : group.getMembers()) |
207 |
dataProvider.getList().add(new User(u, group.getName()));
|
208 |
} |
209 |
} |
210 |
|
211 |
public Object getSelectedObject() { |
212 |
if (groupSelectionModel.getSelectedObject() != null) |
213 |
return groupSelectionModel.getSelectedObject();
|
214 |
if (userSelectionModel.getSelectedObject() != null) |
215 |
return userSelectionModel.getSelectedObject();
|
216 |
return null; |
217 |
} |
218 |
} |