f97c9580baeeae0957d338a53631a211d2fab01e
[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.UserCatalogs;
61
62
63 public class PermissionsList extends Composite {
64
65         Map<String, Boolean[]> permissions = null;
66         
67         final Images images;
68         
69         final VerticalPanel permPanel = new VerticalPanel();
70         
71         final FlexTable permTable = new FlexTable();
72         
73         final String owner;
74         
75         protected boolean hasChanges = false;
76
77     private boolean readonly = false;
78     
79     Command changePermissionsCallback;
80
81     private final Pithos app;
82         
83         public PermissionsList(Pithos app, final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
84         this.app = app;
85                 changePermissionsCallback = _changePermissionsCallback;
86                 images = theImages;
87                 owner = theOwner;
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();
101         }
102
103         public boolean hasChanges(){
104                 return hasChanges;
105         }
106
107         /**
108          * Retrieve the permissions.
109          *
110          * @return the permissions
111          */
112         public Map<String, Boolean[]> getPermissions() {
113                 return permissions;
114         }
115
116         public void addPermission(String userID, boolean read, boolean write){
117                 permissions.put(userID, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
118                 hasChanges = true;
119         updatePermissionTable();
120         if (changePermissionsCallback != null)
121                 changePermissionsCallback.execute();
122         }
123
124         /**
125          * Shows the permission table 
126          * 
127          */
128         void updatePermissionTable(){
129                 int i = 1;
130         final int ii = i;
131         for (int j=1; j<permTable.getRowCount(); j++)
132             permTable.removeRow(j);
133                 for(final String userID : permissions.keySet()) {
134             if (!userID.contains(":")) {
135                  //not a group
136                 final String displayName = app.getUserDisplayNameForID(userID);
137                 if(displayName != null) {
138                     permTable.setHTML(
139                         i,
140                         0,
141                         "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
142                     );
143                 }
144                 else {
145                     new GetUserCatalogs(app, userID) {
146                         @Override
147                         public void onSuccess(Request request, Response response, JSONObject result, UserCatalogs userCatalogs) {
148                             app.getUserCatalogs().updateFrom(userCatalogs);
149                             final String displayName = app.getUserDisplayNameForID(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 }