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.UserCatalogs;
63 public class PermissionsList extends Composite {
65 Map<String, Boolean[]> permissions = null;
69 final VerticalPanel permPanel = new VerticalPanel();
71 final FlexTable permTable = new FlexTable();
75 protected boolean hasChanges = false;
77 private boolean readonly = false;
79 Command changePermissionsCallback;
81 private final Pithos app;
83 public PermissionsList(Pithos app, final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
85 changePermissionsCallback = _changePermissionsCallback;
88 permissions = new HashMap<String, Boolean[]>(thePermissions);
89 permTable.setText(0, 0, "Users/Groups");
90 permTable.setText(0, 1, "Read Only");
91 permTable.setText(0, 2, "Read/Write");
92 permTable.setText(0, 3, "");
93 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
94 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
95 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
96 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
97 permPanel.add(permTable);
98 permPanel.addStyleName("pithos-TabPanelBottom");
99 initWidget(permPanel);
100 updatePermissionTable();
103 public boolean hasChanges(){
108 * Retrieve the permissions.
110 * @return the permissions
112 public Map<String, Boolean[]> getPermissions() {
116 public void addPermission(String userID, boolean read, boolean write){
117 permissions.put(userID, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
119 updatePermissionTable();
120 if (changePermissionsCallback != null)
121 changePermissionsCallback.execute();
125 * Shows the permission table
128 void updatePermissionTable(){
131 for (int j=1; j<permTable.getRowCount(); j++)
132 permTable.removeRow(j);
133 for(final String userID : permissions.keySet()) {
134 if (!userID.contains(":")) {
136 final String displayName = app.getUserDisplayNameForID(userID);
137 if(displayName != null) {
141 "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " " + displayName + "</span>"
145 new GetUserCatalogs(app, userID) {
147 public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) {
148 app.getUserCatalogs().updateFrom(userCatalogs);
149 final String displayName = app.getUserDisplayNameForID(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);