root / src / gr / grnet / pithos / web / client / grouptree / GroupTreeViewModel.java @ c46c9e31
History | View | Annotate | Download (8 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.foldertree.File; |
40 |
import gr.grnet.pithos.web.client.grouptree.GroupTreeView.Templates; |
41 |
|
42 |
import java.util.HashMap; |
43 |
import java.util.HashSet; |
44 |
import java.util.Map; |
45 |
import java.util.Set; |
46 |
|
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; |
56 |
|
57 |
public class GroupTreeViewModel implements TreeViewModel { |
58 |
|
59 |
protected Pithos app;
|
60 |
|
61 |
private Cell<Group> groupCell = new AbstractCell<Group>(ContextMenuEvent.getType().getName()) { |
62 |
|
63 |
@Override
|
64 |
public void render(Context context, Group value, SafeHtmlBuilder sb) { |
65 |
String html = AbstractImagePrototype.create(GroupTreeView.images.group()).getHTML();
|
66 |
sb.appendHtmlConstant(html).appendHtmlConstant(" ");
|
67 |
sb.append(Templates.INSTANCE.nameSpan(value.getName()));
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
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) { |
72 |
GroupTreeViewModel.this.groupSelectionModel.setSelected(group, true);
|
73 |
if (event.getType().equals(ContextMenuEvent.getType().getName())) {
|
74 |
GroupContextMenu menu = new GroupContextMenu(app, GroupTreeView.images, group);
|
75 |
menu.setPopupPosition(event.getClientX(), event.getClientY()); |
76 |
menu.show(); |
77 |
} |
78 |
} |
79 |
}; |
80 |
|
81 |
private Cell<User> userCell = new AbstractCell<User>(ContextMenuEvent.getType().getName()) { |
82 |
|
83 |
@Override
|
84 |
public void render(Context context, User value, SafeHtmlBuilder sb) { |
85 |
String html = AbstractImagePrototype.create(GroupTreeView.images.user()).getHTML();
|
86 |
sb.appendHtmlConstant(html).appendHtmlConstant(" ");
|
87 |
sb.append(Templates.INSTANCE.nameSpan(value.getName()));
|
88 |
} |
89 |
|
90 |
@Override
|
91 |
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) { |
92 |
GroupTreeViewModel.this.userSelectionModel.setSelected(user, true);
|
93 |
if (event.getType().equals(ContextMenuEvent.getType().getName())) {
|
94 |
UserContextMenu menu = new UserContextMenu(app, GroupTreeView.images, user);
|
95 |
menu.setPopupPosition(event.getClientX(), event.getClientY()); |
96 |
menu.show(); |
97 |
} |
98 |
} |
99 |
}; |
100 |
|
101 |
protected ListDataProvider<Group> groupsDataProvider = new ListDataProvider<Group>(); |
102 |
|
103 |
protected Map<Group, ListDataProvider<User>> userDataProviderMap = new HashMap<Group, ListDataProvider<User>>(); |
104 |
|
105 |
SingleSelectionModel<Group> groupSelectionModel; |
106 |
SingleSelectionModel<User> userSelectionModel;
|
107 |
|
108 |
public GroupTreeViewModel(Pithos _app) {
|
109 |
app = _app; |
110 |
|
111 |
groupSelectionModel = new SingleSelectionModel<Group>(); |
112 |
app.addSelectionModel(groupSelectionModel); |
113 |
groupSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
|
114 |
|
115 |
@Override
|
116 |
public void onSelectionChange(SelectionChangeEvent event) { |
117 |
if (groupSelectionModel.getSelectedObject() != null) { |
118 |
app.deselectOthers(app.getGroupTreeView(), groupSelectionModel); |
119 |
app.showFiles(new HashSet<File>()); |
120 |
app.showRelevantToolbarButtons(); |
121 |
} |
122 |
else {
|
123 |
if (app.getSelectedTree().equals(app.getGroupTreeView()))
|
124 |
app.setSelectedTree(null);
|
125 |
if (app.getSelectedTree() == null) |
126 |
app.showRelevantToolbarButtons(); |
127 |
} |
128 |
} |
129 |
}); |
130 |
|
131 |
userSelectionModel = new SingleSelectionModel<User>(); |
132 |
app.addSelectionModel(userSelectionModel); |
133 |
userSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
|
134 |
|
135 |
@Override
|
136 |
public void onSelectionChange(SelectionChangeEvent event) { |
137 |
if (userSelectionModel.getSelectedObject() != null) { |
138 |
app.deselectOthers(app.getGroupTreeView(), userSelectionModel); |
139 |
app.showFiles(new HashSet<File>()); |
140 |
app.showRelevantToolbarButtons(); |
141 |
} |
142 |
else {
|
143 |
if (app.getSelectedTree().equals(app.getGroupTreeView()))
|
144 |
app.setSelectedTree(null);
|
145 |
if (app.getSelectedTree() == null) |
146 |
app.showRelevantToolbarButtons(); |
147 |
} |
148 |
} |
149 |
}); |
150 |
} |
151 |
|
152 |
@Override
|
153 |
public <T> NodeInfo<?> getNodeInfo(T value) {
|
154 |
if (value == null) { |
155 |
groupsDataProvider.getList().clear(); |
156 |
groupsDataProvider.getList().addAll(app.getAccount().getGroups()); |
157 |
return new DefaultNodeInfo<Group>(groupsDataProvider, groupCell, groupSelectionModel, null); |
158 |
} |
159 |
Group g = (Group) value; |
160 |
if (userDataProviderMap.get(g) == null) { |
161 |
userDataProviderMap.put(g, new ListDataProvider<User>());
|
162 |
} |
163 |
final ListDataProvider<User> dataProvider = userDataProviderMap.get(g);
|
164 |
dataProvider.getList().clear(); |
165 |
for (String u : g.getMembers()) |
166 |
dataProvider.getList().add(new User(u, g));
|
167 |
return new DefaultNodeInfo<User>(dataProvider, userCell, userSelectionModel, null); |
168 |
} |
169 |
|
170 |
@Override
|
171 |
public boolean isLeaf(Object o) { |
172 |
if (o instanceof String) { |
173 |
return ((String) o).length() == 0 || app.getAccount().getGroups().isEmpty(); |
174 |
} |
175 |
else if (o instanceof Group) |
176 |
return ((Group) o).getMembers().isEmpty(); |
177 |
else if (o != null) |
178 |
return true; |
179 |
return false; |
180 |
} |
181 |
|
182 |
public void updateGroupNode(Group group) { |
183 |
if (group == null) { |
184 |
groupsDataProvider.getList().clear(); |
185 |
groupsDataProvider.getList().addAll(app.getAccount().getGroups()); |
186 |
} |
187 |
else {
|
188 |
if (userDataProviderMap.get(group) == null) { |
189 |
userDataProviderMap.put(group, new ListDataProvider<User>());
|
190 |
} |
191 |
final ListDataProvider<User> dataProvider = userDataProviderMap.get(group);
|
192 |
dataProvider.getList().clear(); |
193 |
for (String u : group.getMembers()) |
194 |
dataProvider.getList().add(new User(u, group));
|
195 |
} |
196 |
} |
197 |
|
198 |
public Object getSelectedObject() { |
199 |
if (groupSelectionModel.getSelectedObject() != null) |
200 |
return groupSelectionModel.getSelectedObject();
|
201 |
if (userSelectionModel.getSelectedObject() != null) |
202 |
return userSelectionModel.getSelectedObject();
|
203 |
return null; |
204 |
} |
205 |
} |