In File/Share do not show "Add group" if user has no groups
[pithos-web-client] / src / gr / grnet / pithos / web / client / PermissionsList.java
1 /*
2  * Copyright 2011-2013 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.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;
45
46 import java.util.HashMap;
47 import java.util.Map;
48
49
50 public class PermissionsList extends Composite {
51
52     Map<String, Boolean[]> permissions = null;
53
54     final FileShareDialog.PrivateSharingImages images;
55
56     final VerticalPanel permPanel = new VerticalPanel();
57
58     final FlexTable permTable = new FlexTable();
59
60     final String owner;
61
62     protected boolean hasChanges = false;
63
64     private boolean readonly = false;
65
66     Command changePermissionsCallback;
67
68     private final Pithos app;
69
70     public PermissionsList(
71         Pithos app,
72         final FileShareDialog.PrivateSharingImages theImages,
73         Map<String, Boolean[]> thePermissions,
74         String theOwner,
75         boolean inheritsPermissions,
76         Command _changePermissionsCallback
77     ) {
78         this.app = app;
79         changePermissionsCallback = _changePermissionsCallback;
80         images = theImages;
81         owner = theOwner;
82         permissions = new HashMap<String, Boolean[]>(thePermissions);
83         updatePermissionsHeader();
84         permPanel.add(permTable);
85         permPanel.addStyleName("pithos-TabPanelBottom");
86         initWidget(permPanel);
87         updatePermissionTable();
88     }
89
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");
100         }
101         else {
102             permTable.setText(0, 0, "");
103             permTable.setText(0, 1, "");
104             permTable.setText(0, 2, "");
105             permTable.setText(0, 3, "");
106         }
107     }
108
109     public boolean hasPermissions() {
110         return permissionCount() > 0;
111     }
112
113     public int permissionCount() {
114         return permissions == null
115             ? 0
116             : permissions.size();
117     }
118
119     public boolean hasChanges() {
120         return hasChanges;
121     }
122
123     /**
124      * Retrieve the permissions.
125      *
126      * @return the permissions
127      */
128     public Map<String, Boolean[]> getPermissions() {
129         return permissions;
130     }
131
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)});
135         hasChanges = true;
136         updatePermissionTable();
137         if(changePermissionsCallback != null) {
138             changePermissionsCallback.execute();
139         }
140     }
141
142     private boolean isGroup(String userID) {
143         return userID.contains(Const.COLON);
144     }
145
146     /**
147      * Shows the permission table
148      */
149     void updatePermissionTable() {
150         int i = 1;
151         final int ii = i;
152         for(int j = 1; j < permTable.getRowCount(); j++) {
153             permTable.removeRow(j);
154         }
155
156         updatePermissionsHeader();
157
158         for(final String userID : permissions.keySet()) {
159             if(!isGroup(userID)) {
160                 final String displayName = app.getDisplayNameForUserID(userID);
161                 if(displayName != null) {
162                     permTable.setHTML(
163                         i,
164                         0,
165                         Const.inSpan(
166                             AbstractImagePrototype.create(
167                                 images.permUser()).getHTML() +
168                                 Const.HTML_NBSP +
169                                 displayName
170                         )
171                     );
172                 }
173                 else {
174                     new UpdateUserCatalogs(app, userID) {
175                         @Override
176                         public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
177                             final String displayName = updatedUserCatalogs.getDisplayName(userID);
178                             permTable.setHTML(
179                                 ii,
180                                 0,
181                                 Const.inSpan(
182                                     AbstractImagePrototype.create(
183                                         images.permUser()).getHTML() +
184                                         Const.HTML_NBSP +
185                                         displayName
186                                 )
187                             );
188                         }
189                     }.scheduleDeferred();
190                 }
191             }
192             else {
193                 permTable.setHTML(
194                     i,
195                     0,
196                     Const.inSpan(
197                         AbstractImagePrototype.create(
198                             images.permGroup()).getHTML() +
199                             Const.HTML_NBSP +
200                             userID.split(Const.COLON)[1].trim()
201                     )
202                 );
203             }
204             permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
205
206             Boolean[] userPerms = permissions.get(userID);
207             Boolean readP = userPerms[0];
208             Boolean writeP = userPerms[1];
209
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);
214
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);
219
220             if(!readonly) {
221                 read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
222                     @Override
223                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
224                         Boolean[] ps = permissions.get(userID);
225                         ps[0] = booleanValueChangeEvent.getValue();
226                         ps[1] = !booleanValueChangeEvent.getValue();
227                         hasChanges = true;
228                         if(changePermissionsCallback != null) {
229                             changePermissionsCallback.execute();
230                         }
231                     }
232                 });
233                 write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
234                     @Override
235                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
236                         Boolean[] ps = permissions.get(userID);
237                         ps[0] = !booleanValueChangeEvent.getValue();
238                         ps[1] = booleanValueChangeEvent.getValue();
239                         hasChanges = true;
240                         if(changePermissionsCallback != null) {
241                             changePermissionsCallback.execute();
242                         }
243                     }
244                 });
245                 Anchor removeButton = new Anchor("remove");
246                 removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
247                 removeButton.addClickHandler(new ClickHandler() {
248                     @Override
249                     public void onClick(ClickEvent event) {
250                         permissions.remove(userID);
251                         updatePermissionTable();
252                         hasChanges = true;
253                         if(changePermissionsCallback != null) {
254                             changePermissionsCallback.execute();
255                         }
256                     }
257                 });
258                 permTable.setWidget(i, 3, removeButton);
259                 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
260             }
261             else {
262                 read.setEnabled(false);
263                 write.setEnabled(false);
264             }
265             i++;
266         }
267     }
268 }