2 * Copyright 2011-2013 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.
35 package gr.grnet.pithos.web.client;
37 import com.google.gwt.event.dom.client.ClickEvent;
38 import com.google.gwt.event.dom.client.ClickHandler;
39 import com.google.gwt.event.logical.shared.ValueChangeEvent;
40 import com.google.gwt.event.logical.shared.ValueChangeHandler;
41 import com.google.gwt.user.client.Command;
42 import com.google.gwt.user.client.ui.*;
43 import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
44 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
46 import java.util.HashMap;
50 public class PermissionsList extends Composite {
52 Map<String, Boolean[]> permissions = null;
54 final FileShareDialog.PrivateSharingImages images;
56 final VerticalPanel permPanel = new VerticalPanel();
58 final FlexTable permTable = new FlexTable();
62 protected boolean hasChanges = false;
64 private boolean readonly = false;
66 Command changePermissionsCallback;
68 private final Pithos app;
70 public PermissionsList(
72 final FileShareDialog.PrivateSharingImages theImages,
73 Map<String, Boolean[]> thePermissions,
75 boolean inheritsPermissions,
76 Command _changePermissionsCallback
79 changePermissionsCallback = _changePermissionsCallback;
82 permissions = new HashMap<String, Boolean[]>(thePermissions);
83 updatePermissionsHeader();
84 permPanel.add(permTable);
85 permPanel.addStyleName("pithos-TabPanelBottom");
86 initWidget(permPanel);
87 updatePermissionTable();
90 private void updatePermissionsHeader() {
91 if(hasPermissions()) {
92 permTable.setText(0, 0, "Users/Groups");
93 permTable.setText(0, 1, "Read Only");
94 permTable.setText(0, 2, "Read/Write");
95 permTable.setText(0, 3, "");
96 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
97 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
98 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
99 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
102 permTable.setText(0, 0, "");
103 permTable.setText(0, 1, "");
104 permTable.setText(0, 2, "");
105 permTable.setText(0, 3, "");
109 public boolean hasPermissions() {
110 return permissionCount() > 0;
113 public int permissionCount() {
114 return permissions == null
116 : permissions.size();
119 public boolean hasChanges() {
124 * Retrieve the permissions.
126 * @return the permissions
128 public Map<String, Boolean[]> getPermissions() {
132 public void addPermission(String userID, boolean read, boolean write) {
133 Pithos.LOG("PermissionsList::addPermission(userID=", userID, ", read=", read, ", write=", write, ")");
134 permissions.put(userID, new Boolean[]{Boolean.valueOf(read), Boolean.valueOf(write)});
136 updatePermissionTable();
137 if(changePermissionsCallback != null) {
138 changePermissionsCallback.execute();
142 private boolean isGroup(String userID) {
143 return userID.contains(Const.COLON);
147 * Shows the permission table
149 void updatePermissionTable() {
152 for(int j = 1; j < permTable.getRowCount(); j++) {
153 permTable.removeRow(j);
156 updatePermissionsHeader();
158 for(final String userID : permissions.keySet()) {
159 if(!isGroup(userID)) {
160 final String displayName = app.getDisplayNameForUserID(userID);
161 if(displayName != null) {
166 AbstractImagePrototype.create(
167 images.permUser()).getHTML() +
174 new UpdateUserCatalogs(app, userID) {
176 public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
177 final String displayName = updatedUserCatalogs.getDisplayName(userID);
182 AbstractImagePrototype.create(
183 images.permUser()).getHTML() +
189 }.scheduleDeferred();
197 AbstractImagePrototype.create(
198 images.permGroup()).getHTML() +
200 userID.split(Const.COLON)[1].trim()
204 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
206 Boolean[] userPerms = permissions.get(userID);
207 Boolean readP = userPerms[0];
208 Boolean writeP = userPerms[1];
210 RadioButton read = new RadioButton("permissions" + i);
211 read.setValue(readP != null ? readP : false);
212 permTable.setWidget(i, 1, read);
213 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
215 RadioButton write = new RadioButton("permissions" + i);
216 write.setValue(writeP != null ? writeP : false);
217 permTable.setWidget(i, 2, write);
218 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
221 read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
223 public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
224 Boolean[] ps = permissions.get(userID);
225 ps[0] = booleanValueChangeEvent.getValue();
226 ps[1] = !booleanValueChangeEvent.getValue();
228 if(changePermissionsCallback != null) {
229 changePermissionsCallback.execute();
233 write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
235 public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
236 Boolean[] ps = permissions.get(userID);
237 ps[0] = !booleanValueChangeEvent.getValue();
238 ps[1] = booleanValueChangeEvent.getValue();
240 if(changePermissionsCallback != null) {
241 changePermissionsCallback.execute();
245 Anchor removeButton = new Anchor("remove");
246 removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
247 removeButton.addClickHandler(new ClickHandler() {
249 public void onClick(ClickEvent event) {
250 permissions.remove(userID);
251 updatePermissionTable();
253 if(changePermissionsCallback != null) {
254 changePermissionsCallback.execute();
258 permTable.setWidget(i, 3, removeButton);
259 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
262 read.setEnabled(false);
263 write.setEnabled(false);