Remove permission button replaced by a link (issue #2373)
[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 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.CheckBox;
50 import com.google.gwt.user.client.ui.Composite;
51 import com.google.gwt.user.client.ui.FlexTable;
52 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
53 import com.google.gwt.user.client.ui.PushButton;
54 import com.google.gwt.user.client.ui.RadioButton;
55 import com.google.gwt.user.client.ui.VerticalPanel;
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         public PermissionsList(final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
77                 changePermissionsCallback = _changePermissionsCallback;
78                 images = theImages;
79                 owner = theOwner;
80                 permissions =  new HashMap<String, Boolean[]>(thePermissions);
81                 permTable.setText(0, 0, "Users/Groups");
82                 permTable.setText(0, 1, "Read Only");
83                 permTable.setText(0, 2, "Read/Write");
84                 permTable.setText(0, 3, "");
85                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
86                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
87                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
88                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
89                 permPanel.add(permTable);
90                 permPanel.addStyleName("pithos-TabPanelBottom");
91                 initWidget(permPanel);
92                 updatePermissionTable();
93         }
94
95         public boolean hasChanges(){
96                 return hasChanges;
97         }
98
99         /**
100          * Retrieve the permissions.
101          *
102          * @return the permissions
103          */
104         public Map<String, Boolean[]> getPermissions() {
105                 return permissions;
106         }
107
108         public void addPermission(String user, boolean read, boolean write){
109                 permissions.put(user, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
110                 hasChanges = true;
111         updatePermissionTable();
112         if (changePermissionsCallback != null)
113                 changePermissionsCallback.execute();
114         }
115
116         /**
117          * Shows the permission table 
118          * 
119          */
120         void updatePermissionTable(){
121                 int i = 1;
122         for (int j=1; j<permTable.getRowCount(); j++)
123             permTable.removeRow(j);
124                 for(final String user : permissions.keySet()) {
125             if (!user.contains(":")) //not a group
126                 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + user + "</span>");
127             else
128                 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;" + user.split(":")[1].trim() + "</span>");
129             permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
130
131             Boolean[] userPerms = permissions.get(user);
132             Boolean readP = userPerms[0];
133             Boolean writeP = userPerms[1];
134
135                         RadioButton read = new RadioButton("permissions" + i);
136                         read.setValue(readP != null ? readP : false);
137             permTable.setWidget(i, 1, read);
138             permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
139
140             RadioButton write = new RadioButton("permissions" + i);
141             write.setValue(writeP != null ? writeP : false);
142             permTable.setWidget(i, 2, write);
143             permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
144
145             if (!readonly) {
146                 read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
147                     @Override
148                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
149                         Boolean[] ps = permissions.get(user);
150                         ps[0] = booleanValueChangeEvent.getValue();
151                         ps[1] = !booleanValueChangeEvent.getValue();
152                         hasChanges = true;
153                         if (changePermissionsCallback != null)
154                                 changePermissionsCallback.execute();
155                     }
156                 });
157                 write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
158                     @Override
159                     public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
160                         Boolean[] ps = permissions.get(user);
161                         ps[0] = !booleanValueChangeEvent.getValue();
162                         ps[1] = booleanValueChangeEvent.getValue();
163                         hasChanges = true;
164                         if (changePermissionsCallback != null)
165                                 changePermissionsCallback.execute();
166                     }
167                 });
168                 Anchor removeButton = new Anchor("remove");
169                 removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
170                 removeButton.addClickHandler(new ClickHandler() {
171                     @Override
172                     public void onClick(ClickEvent event) {
173                         permissions.remove(user);
174                         updatePermissionTable();
175                         hasChanges = true;
176                         if (changePermissionsCallback != null)
177                                 changePermissionsCallback.execute();
178                     }
179                 });
180                 permTable.setWidget(i, 3, removeButton);
181                 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
182             }
183             else {
184                 read.setEnabled(false);
185                 write.setEnabled(false);
186             }
187                         i++;
188                 }
189         }
190 }