Display the full name in the ACL. The full name is displayed once right click and...
[pithos] / src / gr / ebs / gss / client / PermissionsList.java
1 /*
2  * Copyright 2008, 2009 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.client;
20
21 import gr.ebs.gss.client.FilePropertiesDialog.Images;
22 import gr.ebs.gss.client.commands.GetUserCommand;
23 import gr.ebs.gss.client.rest.resource.PermissionHolder;
24
25 import java.util.HashSet;
26 import java.util.Set;
27
28 import com.google.gwt.event.dom.client.ClickEvent;
29 import com.google.gwt.event.dom.client.ClickHandler;
30 import com.google.gwt.user.client.ui.AbstractImagePrototype;
31 import com.google.gwt.user.client.ui.CheckBox;
32 import com.google.gwt.user.client.ui.Composite;
33 import com.google.gwt.user.client.ui.FlexTable;
34 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
35 import com.google.gwt.user.client.ui.PushButton;
36 import com.google.gwt.user.client.ui.VerticalPanel;
37
38
39 /**
40  * @author kman
41  *
42  */
43 public class PermissionsList extends Composite {
44
45         int selectedRow = -1;
46         int permissionCount=-1;
47         Set<PermissionHolder> permissions = null;
48         final Images images;
49         final VerticalPanel permPanel = new VerticalPanel();
50         final FlexTable permTable = new FlexTable();
51         final String owner;
52         PermissionHolder toRemove = null;
53         private boolean hasChanges = false;
54         private boolean hasAddition = false;
55
56         public PermissionsList(final Images theImages, Set<PermissionHolder> thePermissions, String anOwner){
57                 images = theImages;
58                 owner = anOwner;
59                 permissions =  new HashSet<PermissionHolder>();
60                 permissions.addAll(thePermissions);
61                 permTable.setText(0, 0, "Users/Groups");
62                 permTable.setText(0, 1, "Read");
63                 permTable.setText(0, 2, "Write");
64                 permTable.setText(0, 3, "Modify Access");
65                 permTable.setText(0, 4, "");
66                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
67                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
68                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
69                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
70                 permPanel.add(permTable);
71                 permPanel.addStyleName("gss-TabPanelBottom");
72                 initWidget(permPanel);
73                 updateTable();
74         }
75
76         public boolean hasChanges(){
77                 return hasChanges || hasAddition;
78         }
79
80
81         public void updateTable(){
82                 int i=1;
83                 if(toRemove != null){
84                         permissions.remove(toRemove);
85                         toRemove = null;
86                 }
87                 for(final PermissionHolder dto : permissions){
88
89                         PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
90                                 @Override
91                                 public void onClick(ClickEvent event) {
92                                         toRemove = dto;
93                                         updateTable();
94                                         hasChanges = true;
95                                 }
96                         });
97                         
98                         if(dto.getUser() !=null)
99                                 if(dto.getUser()!=null && dto.getUser().equals(owner)){
100                                         permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;Owner</span>");
101                                         removeButton.setVisible(false);
102                                 }
103                                 else{
104                                         if(GSS.get().findUserFullName(dto.getUser()) == null){
105                                                 GetUserCommand guc = new GetUserCommand(dto.getUser());
106                                                 guc.execute();
107                                                 GSS.get().putUserToMap(dto.getUser(), dto.getUser());
108                                         }
109                                         permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;"+ GSS.get().findUserFullName(dto.getUser()) +"</span>");
110                                 }
111                         else if(dto.getGroup() != null)
112                                 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;"+dto.getGroup()+"</span>");
113                         CheckBox read = new CheckBox();
114                         read.setValue(dto.isRead());
115                         CheckBox write = new CheckBox();
116                         write.setValue(dto.isWrite());
117                         CheckBox modify = new CheckBox();
118                         modify.setValue(dto.isModifyACL());
119                         if (dto.getUser()!=null && dto.getUser().equals(owner)) {
120                                 read.setEnabled(false);
121                                 write.setEnabled(false);
122                                 modify.setEnabled(false);
123                         }
124                         permTable.setWidget(i, 1, read);
125                         permTable.setWidget(i, 2, write);
126                         permTable.setWidget(i, 3, modify);
127                         permTable.setWidget(i, 4, removeButton);
128                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
129                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
130                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
131                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
132                         i++;
133                 }
134                 for(; i<permTable.getRowCount(); i++)
135                         permTable.removeRow(i);
136                 hasChanges = false;
137
138         }
139
140         public void updatePermissionsAccordingToInput(){
141                 int i=1;
142                 for(PermissionHolder dto : permissions){
143                         /*if(dto.getId() == null)
144                                 hasChanges =true;*/
145                         CheckBox r = (CheckBox) permTable.getWidget(i, 1);
146                         CheckBox w = (CheckBox) permTable.getWidget(i, 2);
147                         CheckBox m = (CheckBox) permTable.getWidget(i, 3);
148                         if(dto.isRead() != r.getValue() || dto.isWrite() != w.getValue() || dto.isModifyACL() != m.getValue())
149                                 hasChanges = true;
150                         dto.setRead(r.getValue());
151                         dto.setWrite(w.getValue());
152                         dto.setModifyACL(m.getValue());
153                         i++;
154                 }
155         }
156
157
158         /**
159          * Retrieve the permissions.
160          *
161          * @return the permissions
162          */
163         public Set<PermissionHolder> getPermissions() {
164                 return permissions;
165         }
166
167         public void addPermission(PermissionHolder permission){
168                 permissions.add(permission);
169                 hasAddition = true;
170         }
171
172
173 }