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