- Add "Save file as" menu that forces browser to download file instead of opening...
[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.rest.resource.PermissionHolder;
23
24 import java.util.HashSet;
25 import java.util.Set;
26
27 import com.google.gwt.user.client.ui.CheckBox;
28 import com.google.gwt.user.client.ui.ClickListener;
29 import com.google.gwt.user.client.ui.Composite;
30 import com.google.gwt.user.client.ui.FlexTable;
31 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
32 import com.google.gwt.user.client.ui.PushButton;
33 import com.google.gwt.user.client.ui.VerticalPanel;
34 import com.google.gwt.user.client.ui.Widget;
35
36
37 /**
38  * @author kman
39  *
40  */
41 public class PermissionsList extends Composite {
42
43         int selectedRow = -1;
44         int permissionCount=-1;
45         Set<PermissionHolder> permissions = null;
46         final Images images;
47         final VerticalPanel permPanel = new VerticalPanel();
48         final FlexTable permTable = new FlexTable();
49         final String owner;
50         PermissionHolder toRemove = null;
51         private boolean hasChanges = false;
52         private boolean hasAddition = false;
53
54         public PermissionsList(final Images images, Set<PermissionHolder> permissions, String owner){
55                 this.images = images;
56                 this.owner = owner;
57                 this.permissions =  new HashSet<PermissionHolder>();
58                 this.permissions.addAll(permissions);
59                 permTable.setText(0, 0, "Users/Groups");
60                 permTable.setText(0, 1, "Read");
61                 permTable.setText(0, 2, "Write");
62                 permTable.setText(0, 3, "Modify Access");
63                 permTable.setText(0, 4, "");
64                 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
65                 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
66                 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
67                 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
68                 permPanel.add(permTable);
69                 permPanel.addStyleName("gwt-TabPanelBottom");
70                 initWidget(permPanel);
71                 updateTable();
72         }
73
74         public boolean hasChanges(){
75                 return hasChanges || hasAddition;
76         }
77
78
79         public void updateTable(){
80                 int i=1;
81                 if(toRemove != null){
82                         permissions.remove(toRemove);
83                         toRemove = null;
84                 }
85                 for(final PermissionHolder dto : permissions){
86
87                         PushButton removeButton = new PushButton(images.delete().createImage(), new ClickListener() {
88
89                                 public void onClick(Widget sender) {
90                                         toRemove = dto;
91                                         updateTable();
92                                         hasChanges = true;
93                                 }
94                         });
95
96                         if(dto.getUser() !=null)
97                                 if(dto.getUser()!=null && dto.getUser().equals(owner)){
98                                         permTable.setHTML(i, 0, "<span>" + images.permUser().getHTML() + "&nbsp;Owner</span>");
99                                         removeButton.setVisible(false);
100                                 }
101                                 else
102                                         permTable.setHTML(i, 0, "<span>" + images.permUser().getHTML() + "&nbsp;"+dto.getUser()+"</span>");
103                         else if(dto.getGroup() != null)
104                                 permTable.setHTML(i, 0, "<span>" + images.permGroup().getHTML() + "&nbsp;"+dto.getGroup()+"</span>");
105                         CheckBox read = new CheckBox();
106                         read.setChecked(dto.isRead());
107                         CheckBox write = new CheckBox();
108                         write.setChecked(dto.isWrite());
109                         CheckBox modify = new CheckBox();
110                         modify.setChecked(dto.isModifyACL());
111                         if (dto.getUser()!=null && dto.getUser().equals(owner)) {
112                                 read.setEnabled(false);
113                                 write.setEnabled(false);
114                                 modify.setEnabled(false);
115                         }
116                         permTable.setWidget(i, 1, read);
117                         permTable.setWidget(i, 2, write);
118                         permTable.setWidget(i, 3, modify);
119                         permTable.setWidget(i, 4, removeButton);
120                         permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
121                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
122                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
123                         permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
124                         i++;
125                 }
126                 for(; i<permTable.getRowCount(); i++)
127                         permTable.removeRow(i);
128                 hasChanges = false;
129
130         }
131
132         public void updatePermissionsAccordingToInput(){
133                 int i=1;
134                 for(PermissionHolder dto : permissions){
135                         /*if(dto.getId() == null)
136                                 hasChanges =true;*/
137                         CheckBox r = (CheckBox) permTable.getWidget(i, 1);
138                         CheckBox w = (CheckBox) permTable.getWidget(i, 2);
139                         CheckBox m = (CheckBox) permTable.getWidget(i, 3);
140                         if(dto.isRead() != r.isChecked() || dto.isWrite() != w.isChecked() || dto.isModifyACL() != m.isChecked())
141                                 hasChanges = true;
142                         dto.setRead(r.isChecked());
143                         dto.setWrite(w.isChecked());
144                         dto.setModifyACL(m.isChecked());
145                         i++;
146                 }
147         }
148
149
150         /**
151          * Retrieve the permissions.
152          *
153          * @return the permissions
154          */
155         public Set<PermissionHolder> getPermissions() {
156                 return permissions;
157         }
158
159         public void addPermission(PermissionHolder permission){
160                 permissions.add(permission);
161                 hasAddition = true;
162         }
163
164
165 }