2c8c40c5877408c27c4f7fff5d37db97c1c7ca13
[pithos-web-client] / src / gr / grnet / pithos / web / client / PermissionsList.java
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 package gr.grnet.pithos.web.client;
36
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;
41
42 import java.util.HashMap;
43 import java.util.Map;
44
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;
62
63
64 public class PermissionsList extends Composite {
65
66         Map<String, Boolean[]> permissions = null;
67         
68         final Images images;
69         
70         final VerticalPanel permPanel = new VerticalPanel();
71         
72         final FlexTable permTable = new FlexTable();
73         
74         final String owner;
75         
76         protected boolean hasChanges = false;
77
78     private boolean readonly = false;
79     
80     Command changePermissionsCallback;
81
82     private final Pithos app;
83         
84         public PermissionsList(Pithos app, final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
85         this.app = app;
86                 changePermissionsCallback = _changePermissionsCallback;
87                 images = theImages;
88                 owner = theOwner;
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();
102         }
103
104         public boolean hasChanges(){
105                 return hasChanges;
106         }
107
108         /**
109          * Retrieve the permissions.
110          *
111          * @return the permissions
112          */
113         public Map<String, Boolean[]> getPermissions() {
114                 return permissions;
115         }
116
117         public void addPermission(String userID, boolean read, boolean write){
118                 permissions.put(userID, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
119                 hasChanges = true;
120         updatePermissionTable();
121         if (changePermissionsCallback != null)
122                 changePermissionsCallback.execute();
123         }
124
125         /**
126          * Shows the permission table 
127          * 
128          */
129         void updatePermissionTable(){
130                 int i = 1;
131         final int ii = i;
132         for (int j=1; j<permTable.getRowCount(); j++)
133             permTable.removeRow(j);
134                 for(final String userID : permissions.keySet()) {
135             if (!userID.contains(":")) {
136                  //not a group
137                 final String displayName = app.getUserDisplayNameForID(userID);
138                 if(displayName != null) {
139                     permTable.setHTML(
140                         i,
141                         0,
142                         "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
143                     );
144                 }
145                 else {
146                     new UpdateUserCatalogs(app, userID) {
147                         @Override
148                         public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
149                             final String displayName = updatedUserCatalogs.getDisplayName(userID);
150                             permTable.setHTML(
151                                 ii,
152                                 0,
153                                 "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
154                             );
155                         }
156                     }.scheduleDeferred();
157                 }
158             }
159             else {
160                 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;" + userID.split(":")[1].trim() + "</span>");
161             }
162             permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
163
164             Boolean[] userPerms = permissions.get(userID);
165             Boolean readP = userPerms[0];
166             Boolean writeP = userPerms[1];
167
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);
172
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);
177
178             if (!readonly) {
179                 read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
180                     @Override
181                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
182                         Boolean[] ps = permissions.get(userID);
183                         ps[0] = booleanValueChangeEvent.getValue();
184                         ps[1] = !booleanValueChangeEvent.getValue();
185                         hasChanges = true;
186                         if (changePermissionsCallback != null)
187                                 changePermissionsCallback.execute();
188                     }
189                 });
190                 write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
191                     @Override
192                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
193                         Boolean[] ps = permissions.get(userID);
194                         ps[0] = !booleanValueChangeEvent.getValue();
195                         ps[1] = booleanValueChangeEvent.getValue();
196                         hasChanges = true;
197                         if (changePermissionsCallback != null)
198                                 changePermissionsCallback.execute();
199                     }
200                 });
201                 Anchor removeButton = new Anchor("remove");
202                 removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
203                 removeButton.addClickHandler(new ClickHandler() {
204                     @Override
205                     public void onClick(ClickEvent event) {
206                         permissions.remove(userID);
207                         updatePermissionTable();
208                         hasChanges = true;
209                         if (changePermissionsCallback != null)
210                                 changePermissionsCallback.execute();
211                     }
212                 });
213                 permTable.setWidget(i, 3, removeButton);
214                 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
215             }
216             else {
217                 read.setEnabled(false);
218                 write.setEnabled(false);
219             }
220                         i++;
221                 }
222         }
223 }