2 * Copyright 2011-2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
35 package gr.grnet.pithos.web.client;
37 import gr.grnet.pithos.web.client.FilePermissionsDialog.Images;
39 import java.util.HashMap;
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.ui.AbstractImagePrototype;
47 import com.google.gwt.user.client.ui.CheckBox;
48 import com.google.gwt.user.client.ui.Composite;
49 import com.google.gwt.user.client.ui.FlexTable;
50 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
51 import com.google.gwt.user.client.ui.PushButton;
52 import com.google.gwt.user.client.ui.VerticalPanel;
55 public class PermissionsList extends Composite {
57 Map<String, Boolean[]> permissions = null;
61 final VerticalPanel permPanel = new VerticalPanel();
63 final FlexTable permTable = new FlexTable();
67 protected boolean hasChanges = false;
69 private boolean readonly = false;
71 public PermissionsList(final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions){
74 permissions = new HashMap<String, Boolean[]>(thePermissions);
75 permTable.setText(0, 0, "Users/Groups");
76 permTable.setText(0, 1, "Read");
77 permTable.setText(0, 2, "Write");
78 permTable.setText(0, 3, "");
79 permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
80 permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
81 permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
82 permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
83 permPanel.add(permTable);
84 permPanel.addStyleName("pithos-TabPanelBottom");
85 initWidget(permPanel);
86 updatePermissionTable();
89 public boolean hasChanges(){
94 * Retrieve the permissions.
96 * @return the permissions
98 public Map<String, Boolean[]> getPermissions() {
102 public void addPermission(String user, boolean read, boolean write){
103 permissions.put(user, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
105 updatePermissionTable();
109 * Shows the permission table
112 void updatePermissionTable(){
114 for (int j=1; j<permTable.getRowCount(); j++)
115 permTable.removeRow(j);
116 for(final String user : permissions.keySet()) {
117 if (!user.contains(":")) //not a group
118 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + " " + user + "</span>");
120 permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + " " + user.split(":")[1].trim() + "</span>");
121 permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
123 Boolean[] userPerms = permissions.get(user);
124 Boolean readP = userPerms[0];
125 Boolean writeP = userPerms[1];
127 CheckBox read = new CheckBox();
128 read.setValue(readP != null ? readP : false);
129 permTable.setWidget(i, 1, read);
130 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
132 CheckBox write = new CheckBox();
133 write.setValue(writeP != null ? writeP : false);
134 permTable.setWidget(i, 2, write);
135 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
138 read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
140 public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
141 Boolean[] ps = permissions.get(user);
142 ps[0] = booleanValueChangeEvent.getValue();
146 write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
148 public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
149 Boolean[] ps = permissions.get(user);
150 ps[1] = booleanValueChangeEvent.getValue();
154 PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
156 public void onClick(ClickEvent event) {
157 permissions.remove(user);
158 updatePermissionTable();
162 permTable.setWidget(i, 3, removeButton);
163 permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
166 read.setEnabled(false);
167 write.setEnabled(false);