2 * Copyright 2011-2012 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.http.client.Request;
38 import com.google.gwt.http.client.Response;
39 import com.google.gwt.json.client.JSONObject;
40 import gr.grnet.pithos.web.client.FilePermissionsDialog.Images;
42 import java.util.HashMap;
45 import com.google.gwt.event.dom.client.ClickEvent;
46 import com.google.gwt.event.dom.client.ClickHandler;
47 import com.google.gwt.event.logical.shared.ValueChangeEvent;
48 import com.google.gwt.event.logical.shared.ValueChangeHandler;
49 import com.google.gwt.user.client.Command;
50 import com.google.gwt.user.client.ui.AbstractImagePrototype;
51 import com.google.gwt.user.client.ui.Anchor;
52 import com.google.gwt.user.client.ui.CheckBox;
53 import com.google.gwt.user.client.ui.Composite;
54 import com.google.gwt.user.client.ui.FlexTable;
55 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
56 import com.google.gwt.user.client.ui.PushButton;
57 import com.google.gwt.user.client.ui.RadioButton;
58 import com.google.gwt.user.client.ui.VerticalPanel;
59 import gr.grnet.pithos.web.client.catalog.GetUserCatalogs;
60 import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
61 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
64 public class PermissionsList extends Composite {
66 Map<String, Boolean[]> permissions = null;
70 final VerticalPanel permPanel = new VerticalPanel();
72 final FlexTable permTable = new FlexTable();
76 protected boolean hasChanges = false;
78 private boolean readonly = false;
80 Command changePermissionsCallback;
82 private final Pithos app;
84 public PermissionsList(Pithos app, final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
86 changePermissionsCallback = _changePermissionsCallback;
89 permissions = new HashMap<String, Boolean[]>(thePermissions);
90 permTable.setText(0, 0, "Users/Groups");
91 permTable.setText(0, 1, "Read Only");
92 permTable.setText(0, 2, "Read/Write");
93 permTable.setText(0, 3, "");
94 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
95 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
96 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
97 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
98 permPanel.add(permTable);
99 permPanel.addStyleName("pithos-TabPanelBottom");
100 initWidget(permPanel);
101 updatePermissionTable();
104 public boolean hasChanges(){
109 * Retrieve the permissions.
111 * @return the permissions
113 public Map<String, Boolean[]> getPermissions() {
117 public void addPermission(String userID, boolean read, boolean write){
118 permissions.put(userID, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
120 updatePermissionTable();
121 if (changePermissionsCallback != null)
122 changePermissionsCallback.execute();
126 * Shows the permission table
129 void updatePermissionTable(){
132 for (int j=1; j<permTable.getRowCount(); j++)
133 permTable.removeRow(j);
134 for(final String userID : permissions.keySet()) {
135 if (!userID.contains(":")) {
137 final String displayName = app.getUserDisplayNameForID(userID);
138 if(displayName != null) {
142 "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " " + displayName + "</span>"
146 new UpdateUserCatalogs(app, userID) {
148 public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
149 final String displayName = updatedUserCatalogs.getDisplayName(userID);
153 "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " " + displayName + "</span>"
156 }.scheduleDeferred();
160 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " " + userID.split(":")[1].trim() + "</span>");
162 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
164 Boolean[] userPerms = permissions.get(userID);
165 Boolean readP = userPerms[0];
166 Boolean writeP = userPerms[1];
168 RadioButton read = new RadioButton("permissions" + i);
169 read.setValue(readP != null ? readP : false);
170 permTable.setWidget(i, 1, read);
171 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
173 RadioButton write = new RadioButton("permissions" + i);
174 write.setValue(writeP != null ? writeP : false);
175 permTable.setWidget(i, 2, write);
176 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
179 read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
181 public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
182 Boolean[] ps = permissions.get(userID);
183 ps[0] = booleanValueChangeEvent.getValue();
184 ps[1] = !booleanValueChangeEvent.getValue();
186 if (changePermissionsCallback != null)
187 changePermissionsCallback.execute();
190 write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
192 public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
193 Boolean[] ps = permissions.get(userID);
194 ps[0] = !booleanValueChangeEvent.getValue();
195 ps[1] = booleanValueChangeEvent.getValue();
197 if (changePermissionsCallback != null)
198 changePermissionsCallback.execute();
201 Anchor removeButton = new Anchor("remove");
202 removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
203 removeButton.addClickHandler(new ClickHandler() {
205 public void onClick(ClickEvent event) {
206 permissions.remove(userID);
207 updatePermissionTable();
209 if (changePermissionsCallback != null)
210 changePermissionsCallback.execute();
213 permTable.setWidget(i, 3, removeButton);
214 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
217 read.setEnabled(false);
218 write.setEnabled(false);