Change copyright notice
[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 gr.grnet.pithos.web.client.FilePermissionsDialog.Images;
38
39 import java.util.HashMap;
40 import java.util.Map;
41
42 import com.google.gwt.event.dom.client.ClickEvent;
43 import com.google.gwt.event.dom.client.ClickHandler;
44 import com.google.gwt.event.logical.shared.ValueChangeEvent;
45 import com.google.gwt.event.logical.shared.ValueChangeHandler;
46 import com.google.gwt.user.client.Command;
47 import com.google.gwt.user.client.ui.AbstractImagePrototype;
48 import com.google.gwt.user.client.ui.Anchor;
49 import com.google.gwt.user.client.ui.Composite;
50 import com.google.gwt.user.client.ui.FlexTable;
51 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
52 import com.google.gwt.user.client.ui.RadioButton;
53 import com.google.gwt.user.client.ui.VerticalPanel;
54 import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
55 import gr.grnet.pithos.web.client.catalog.UserCatalogs;
56
57
58 public class PermissionsList extends Composite {
59
60         Map<String, Boolean[]> permissions = null;
61         
62         final Images images;
63         
64         final VerticalPanel permPanel = new VerticalPanel();
65         
66         final FlexTable permTable = new FlexTable();
67         
68         final String owner;
69         
70         protected boolean hasChanges = false;
71
72     private boolean readonly = false;
73     
74     Command changePermissionsCallback;
75
76     private final Pithos app;
77         
78         public PermissionsList(Pithos app, final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
79         this.app = app;
80                 changePermissionsCallback = _changePermissionsCallback;
81                 images = theImages;
82                 owner = theOwner;
83                 permissions =  new HashMap<String, Boolean[]>(thePermissions);
84                 permTable.setText(0, 0, "Users/Groups");
85                 permTable.setText(0, 1, "Read Only");
86                 permTable.setText(0, 2, "Read/Write");
87                 permTable.setText(0, 3, "");
88                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
89                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
90                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
91                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
92                 permPanel.add(permTable);
93                 permPanel.addStyleName("pithos-TabPanelBottom");
94                 initWidget(permPanel);
95                 updatePermissionTable();
96         }
97
98         public boolean hasChanges(){
99                 return hasChanges;
100         }
101
102         /**
103          * Retrieve the permissions.
104          *
105          * @return the permissions
106          */
107         public Map<String, Boolean[]> getPermissions() {
108                 return permissions;
109         }
110
111         public void addPermission(String userID, boolean read, boolean write){
112                 permissions.put(userID, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
113                 hasChanges = true;
114         updatePermissionTable();
115         if (changePermissionsCallback != null)
116                 changePermissionsCallback.execute();
117         }
118
119         /**
120          * Shows the permission table 
121          * 
122          */
123         void updatePermissionTable(){
124                 int i = 1;
125         final int ii = i;
126         for (int j=1; j<permTable.getRowCount(); j++)
127             permTable.removeRow(j);
128                 for(final String userID : permissions.keySet()) {
129             if (!userID.contains(":")) {
130                  //not a group
131                 final String displayName = app.getDisplayNameForUserID(userID);
132                 if(displayName != null) {
133                     permTable.setHTML(
134                         i,
135                         0,
136                         "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
137                     );
138                 }
139                 else {
140                     new UpdateUserCatalogs(app, userID) {
141                         @Override
142                         public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
143                             final String displayName = updatedUserCatalogs.getDisplayName(userID);
144                             permTable.setHTML(
145                                 ii,
146                                 0,
147                                 "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
148                             );
149                         }
150                     }.scheduleDeferred();
151                 }
152             }
153             else {
154                 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;" + userID.split(":")[1].trim() + "</span>");
155             }
156             permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
157
158             Boolean[] userPerms = permissions.get(userID);
159             Boolean readP = userPerms[0];
160             Boolean writeP = userPerms[1];
161
162                         RadioButton read = new RadioButton("permissions" + i);
163                         read.setValue(readP != null ? readP : false);
164             permTable.setWidget(i, 1, read);
165             permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
166
167             RadioButton write = new RadioButton("permissions" + i);
168             write.setValue(writeP != null ? writeP : false);
169             permTable.setWidget(i, 2, write);
170             permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
171
172             if (!readonly) {
173                 read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
174                     @Override
175                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
176                         Boolean[] ps = permissions.get(userID);
177                         ps[0] = booleanValueChangeEvent.getValue();
178                         ps[1] = !booleanValueChangeEvent.getValue();
179                         hasChanges = true;
180                         if (changePermissionsCallback != null)
181                                 changePermissionsCallback.execute();
182                     }
183                 });
184                 write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
185                     @Override
186                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
187                         Boolean[] ps = permissions.get(userID);
188                         ps[0] = !booleanValueChangeEvent.getValue();
189                         ps[1] = booleanValueChangeEvent.getValue();
190                         hasChanges = true;
191                         if (changePermissionsCallback != null)
192                                 changePermissionsCallback.execute();
193                     }
194                 });
195                 Anchor removeButton = new Anchor("remove");
196                 removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
197                 removeButton.addClickHandler(new ClickHandler() {
198                     @Override
199                     public void onClick(ClickEvent event) {
200                         permissions.remove(userID);
201                         updatePermissionTable();
202                         hasChanges = true;
203                         if (changePermissionsCallback != null)
204                                 changePermissionsCallback.execute();
205                     }
206                 });
207                 permTable.setWidget(i, 3, removeButton);
208                 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
209             }
210             else {
211                 read.setEnabled(false);
212                 write.setEnabled(false);
213             }
214                         i++;
215                 }
216         }
217 }