fix dnd bug, and use filelist.getSelectedFiles() on file drop
[pithos] / src / gr / ebs / gss / admin / client / ui / 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.admin.client.ui;
20
21 import gr.ebs.gss.admin.client.TwoAdmin;
22 import gr.ebs.gss.server.domain.dto.PermissionDTO;
23
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import com.google.gwt.core.client.GWT;
28 import com.google.gwt.event.dom.client.ClickEvent;
29 import com.google.gwt.event.dom.client.ClickHandler;
30 import com.google.gwt.http.client.URL;
31 import com.google.gwt.user.client.rpc.AsyncCallback;
32 import com.google.gwt.user.client.ui.Button;
33 import com.google.gwt.user.client.ui.CheckBox;
34 import com.google.gwt.user.client.ui.Composite;
35 import com.google.gwt.user.client.ui.FlexTable;
36 import com.google.gwt.user.client.ui.HTML;
37 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
38 import com.google.gwt.user.client.ui.VerticalPanel;
39
40
41 /**
42  * @author kman
43  *
44  */
45 public class PermissionsList extends Composite {
46
47         int selectedRow = -1;
48         int permissionCount=-1;
49         Set<PermissionDTO> permissions = null;
50
51         final VerticalPanel permPanel = new VerticalPanel();
52         final FlexTable permTable = new FlexTable();
53         final String owner;
54         PermissionDTO toRemove = null;
55         private boolean hasChanges = false;
56         private boolean hasAddition = false;
57         private boolean allowEditPermissions = false;
58         private String uri;
59         public PermissionsList(Set<PermissionDTO> thePermissions, String anOwner, boolean _allowEditPermissions){
60                 owner = anOwner;
61                 permissions =  new HashSet<PermissionDTO>();
62                 permissions.addAll(thePermissions);
63                 permTable.setText(0, 0, "Users/Groups");
64                 permTable.setText(0, 1, "Read");
65                 permTable.setText(0, 2, "Write");
66                 permTable.setText(0, 3, "Modify Access");
67                 permTable.setText(0, 4, "");
68                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
69                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
70                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
71                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
72                 permPanel.add(permTable);
73                 Button savePermissions = new Button("Save");
74                 permPanel.add(savePermissions);
75
76                 savePermissions.addClickHandler(new ClickHandler() {
77
78                         @Override
79                         public void onClick(ClickEvent event) {
80                                 updatePermissionsAccordingToInput();
81                                 if(hasChanges())
82                                         TwoAdmin.get().getAdminService().setFilePermissions(uri,permissions, new AsyncCallback<Void>() {
83
84                                                 @Override
85                                                 public void onSuccess(Void result) {
86
87
88                                                 }
89
90                                                 @Override
91                                                 public void onFailure(Throwable caught) {
92                                                         GWT.log("Error fetching file", caught);
93                                                         TwoAdmin.get().showErrorBox("Unable to Find File");
94
95                                                 }
96                                         });
97
98                         }
99                 });
100                 permPanel.addStyleName("gss-TabPanelBottom");
101                 allowEditPermissions = _allowEditPermissions;
102                 initWidget(permPanel);
103                 updateTable();
104         }
105
106         public boolean hasChanges(){
107                 return hasChanges || hasAddition;
108         }
109
110         public void clear(){
111                 permissions= new HashSet<PermissionDTO>();
112                 uri=null;
113                 updateTable();
114         }
115         public void update(Set<PermissionDTO> per, String aUri){
116                 permissions=per;
117                 uri=aUri;
118                 updateTable();
119         }
120         public void updateTable(){
121                 int i=1;
122                 if(toRemove != null){
123                         permissions.remove(toRemove);
124                         toRemove = null;
125                 }
126                 for(final PermissionDTO dto : permissions){
127
128                         Button removeButton = new Button("remove", new ClickHandler() {
129                                 @Override
130                                 public void onClick(ClickEvent event) {
131                                         toRemove = dto;
132                                         updateTable();
133                                         hasChanges = true;
134                                 }
135                         });
136
137                         if(dto.getUser() !=null)
138                                 if(dto.getUser()!=null && dto.getUser().getUsername().equals(owner)){
139                                         permTable.setHTML(i, 0, "<span>"  + "&nbsp;Owner</span>");
140                                         removeButton.setVisible(false);
141                                 }
142                                 else{
143                                         HTML userLabel = new HTML("<a href='#'>"+dto.getUser().getUsername()+"</a></span>");
144                                         permTable.setWidget(i, 0, userLabel);
145                                         userLabel.addClickHandler(new ClickHandler() {
146
147                                                 @Override
148                                                 public void onClick(ClickEvent event) {
149                                                         TwoAdmin.get().searchUsers("username:"+dto.getUser().getUsername());
150
151                                                 }
152                                         });
153                                 }
154                         else if(dto.getGroup() != null) {
155                                 //String user = GSS.get().getCurrentUserResource().getUsername();
156                                 String[] names = dto.getGroup().getName().split("/");
157                                 String name = URL.decodeComponent(names[names.length - 1]);
158                                 //String ownr = names.length>1 ? URL.decodeComponent(names[names.length - 3]) : user;
159                                 String groupName =  name;
160                                 permTable.setHTML(i, 0, "<span>" +  "&nbsp;"+groupName+"</span>");
161                         }
162                         CheckBox read = new CheckBox();
163                         read.setValue(dto.getRead());
164                         CheckBox write = new CheckBox();
165                         write.setValue(dto.getWrite());
166                         CheckBox modify = new CheckBox();
167                         modify.setValue(dto.getModifyACL());
168                         permTable.setWidget(i, 1, read);
169                         permTable.setWidget(i, 2, write);
170                         permTable.setWidget(i, 3, modify);
171                         if (dto.getUser()!=null && dto.getUser().getUsername().equals(owner) || !allowEditPermissions) {
172                                 read.setEnabled(false);
173                                 write.setEnabled(false);
174                                 modify.setEnabled(false);
175                         }
176                         else
177                                 permTable.setWidget(i, 4, removeButton);
178                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
179                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
180                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
181                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
182                         i++;
183                 }
184                 for(; i<permTable.getRowCount(); i++)
185                         permTable.removeRow(i);
186                 hasChanges = false;
187
188         }
189
190         public void updatePermissionsAccordingToInput(){
191                 int i=1;
192                 for(PermissionDTO dto : permissions){
193                         /*if(dto.getId() == null)
194                                 hasChanges =true;*/
195                         CheckBox r = (CheckBox) permTable.getWidget(i, 1);
196                         CheckBox w = (CheckBox) permTable.getWidget(i, 2);
197                         CheckBox m = (CheckBox) permTable.getWidget(i, 3);
198                         if(dto.getRead() != r.getValue() || dto.getWrite() != w.getValue() || dto.getModifyACL() != m.getValue())
199                                 hasChanges = true;
200                         dto.setRead(r.getValue());
201                         dto.setWrite(w.getValue());
202                         dto.setModifyACL(m.getValue());
203                         i++;
204                 }
205         }
206
207
208         /**
209          * Retrieve the permissions.
210          *
211          * @return the permissions
212          */
213         public Set<PermissionDTO> getPermissions() {
214                 return permissions;
215         }
216
217         public void addPermission(PermissionDTO permission){
218                 permissions.add(permission);
219                 hasAddition = true;
220         }
221
222
223 }