Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / PermissionsList.java @ ed2b76a4

History | View | Annotate | Download (8.8 kB)

1 a57faaf0 Christos Stathis
/*
2 e6e9f6e6 Christos KK Loverdos
 * Copyright 2011-2013 GRNET S.A. All rights reserved.
3 63366925 Christos Stathis
 *
4 63366925 Christos Stathis
 * Redistribution and use in source and binary forms, with or
5 63366925 Christos Stathis
 * without modification, are permitted provided that the following
6 63366925 Christos Stathis
 * conditions are met:
7 63366925 Christos Stathis
 *
8 63366925 Christos Stathis
 *   1. Redistributions of source code must retain the above
9 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
10 63366925 Christos Stathis
 *      disclaimer.
11 63366925 Christos Stathis
 *
12 63366925 Christos Stathis
 *   2. Redistributions in binary form must reproduce the above
13 63366925 Christos Stathis
 *      copyright notice, this list of conditions and the following
14 63366925 Christos Stathis
 *      disclaimer in the documentation and/or other materials
15 63366925 Christos Stathis
 *      provided with the distribution.
16 63366925 Christos Stathis
 *
17 63366925 Christos Stathis
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18 63366925 Christos Stathis
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 63366925 Christos Stathis
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 63366925 Christos Stathis
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21 63366925 Christos Stathis
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 63366925 Christos Stathis
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 63366925 Christos Stathis
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24 63366925 Christos Stathis
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 63366925 Christos Stathis
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 63366925 Christos Stathis
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 63366925 Christos Stathis
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 63366925 Christos Stathis
 * POSSIBILITY OF SUCH DAMAGE.
29 63366925 Christos Stathis
 *
30 63366925 Christos Stathis
 * The views and conclusions contained in the software and
31 63366925 Christos Stathis
 * documentation are those of the authors and should not be
32 63366925 Christos Stathis
 * interpreted as representing official policies, either expressed
33 63366925 Christos Stathis
 * or implied, of GRNET S.A.
34 a57faaf0 Christos Stathis
 */
35 a57faaf0 Christos Stathis
package gr.grnet.pithos.web.client;
36 a57faaf0 Christos Stathis
37 d5c6298f Christos Stathis
import java.util.HashMap;
38 d5c6298f Christos Stathis
import java.util.Map;
39 a57faaf0 Christos Stathis
40 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
41 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
42 9326b841 Christos Stathis
import com.google.gwt.event.logical.shared.ValueChangeEvent;
43 9326b841 Christos Stathis
import com.google.gwt.event.logical.shared.ValueChangeHandler;
44 4d869bf1 Christos Stathis
import com.google.gwt.user.client.Command;
45 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
46 01bd6099 Christos Stathis
import com.google.gwt.user.client.ui.Anchor;
47 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
48 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
49 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
50 4ded5310 Christos Stathis
import com.google.gwt.user.client.ui.RadioButton;
51 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
52 eee72ed5 Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
53 8d09767f Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UserCatalogs;
54 a57faaf0 Christos Stathis
55 a57faaf0 Christos Stathis
56 a57faaf0 Christos Stathis
public class PermissionsList extends Composite {
57 a57faaf0 Christos Stathis
58 d5c6298f Christos Stathis
        Map<String, Boolean[]> permissions = null;
59 a57faaf0 Christos Stathis
        
60 64d3b76b Christos KK Loverdos
        final FileShareDialog.PrivateSharingImages images;
61 a57faaf0 Christos Stathis
        
62 a57faaf0 Christos Stathis
        final VerticalPanel permPanel = new VerticalPanel();
63 a57faaf0 Christos Stathis
        
64 a57faaf0 Christos Stathis
        final FlexTable permTable = new FlexTable();
65 a57faaf0 Christos Stathis
        
66 a57faaf0 Christos Stathis
        final String owner;
67 a57faaf0 Christos Stathis
        
68 7811b9d1 Christos Stathis
        protected boolean hasChanges = false;
69 a7c43f26 Christos Stathis
70 ecf95c9e Christos Stathis
    private boolean readonly = false;
71 4d869bf1 Christos Stathis
    
72 4d869bf1 Christos Stathis
    Command changePermissionsCallback;
73 8d09767f Christos KK Loverdos
74 8d09767f Christos KK Loverdos
    private final Pithos app;
75 a57faaf0 Christos Stathis
        
76 64d3b76b Christos KK Loverdos
        public PermissionsList(Pithos app, final FileShareDialog.PrivateSharingImages theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
77 8d09767f Christos KK Loverdos
        this.app = app;
78 4d869bf1 Christos Stathis
                changePermissionsCallback = _changePermissionsCallback;
79 a57faaf0 Christos Stathis
                images = theImages;
80 ecf95c9e Christos Stathis
                owner = theOwner;
81 d5c6298f Christos Stathis
                permissions =  new HashMap<String, Boolean[]>(thePermissions);
82 a57faaf0 Christos Stathis
                permTable.setText(0, 0, "Users/Groups");
83 4ded5310 Christos Stathis
                permTable.setText(0, 1, "Read Only");
84 4ded5310 Christos Stathis
                permTable.setText(0, 2, "Read/Write");
85 d5c6298f Christos Stathis
                permTable.setText(0, 3, "");
86 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
87 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
88 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
89 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
90 a57faaf0 Christos Stathis
                permPanel.add(permTable);
91 a57faaf0 Christos Stathis
                permPanel.addStyleName("pithos-TabPanelBottom");
92 a57faaf0 Christos Stathis
                initWidget(permPanel);
93 d5c6298f Christos Stathis
                updatePermissionTable();
94 a57faaf0 Christos Stathis
        }
95 a57faaf0 Christos Stathis
96 a57faaf0 Christos Stathis
        public boolean hasChanges(){
97 ecf95c9e Christos Stathis
                return hasChanges;
98 a57faaf0 Christos Stathis
        }
99 a57faaf0 Christos Stathis
100 a57faaf0 Christos Stathis
        /**
101 a57faaf0 Christos Stathis
         * Retrieve the permissions.
102 a57faaf0 Christos Stathis
         *
103 a57faaf0 Christos Stathis
         * @return the permissions
104 a57faaf0 Christos Stathis
         */
105 d5c6298f Christos Stathis
        public Map<String, Boolean[]> getPermissions() {
106 a57faaf0 Christos Stathis
                return permissions;
107 a57faaf0 Christos Stathis
        }
108 a57faaf0 Christos Stathis
109 8d09767f Christos KK Loverdos
        public void addPermission(String userID, boolean read, boolean write){
110 8d09767f Christos KK Loverdos
                permissions.put(userID, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
111 ecf95c9e Christos Stathis
                hasChanges = true;
112 d5c6298f Christos Stathis
        updatePermissionTable();
113 4d869bf1 Christos Stathis
        if (changePermissionsCallback != null)
114 4d869bf1 Christos Stathis
                changePermissionsCallback.execute();
115 a57faaf0 Christos Stathis
        }
116 d5c6298f Christos Stathis
117 a57faaf0 Christos Stathis
        /**
118 a57faaf0 Christos Stathis
         * Shows the permission table 
119 a57faaf0 Christos Stathis
         * 
120 a57faaf0 Christos Stathis
         */
121 d5c6298f Christos Stathis
        void updatePermissionTable(){
122 a57faaf0 Christos Stathis
                int i = 1;
123 8d09767f Christos KK Loverdos
        final int ii = i;
124 d5c6298f Christos Stathis
        for (int j=1; j<permTable.getRowCount(); j++)
125 d5c6298f Christos Stathis
            permTable.removeRow(j);
126 8d09767f Christos KK Loverdos
                for(final String userID : permissions.keySet()) {
127 8d09767f Christos KK Loverdos
            if (!userID.contains(":")) {
128 8d09767f Christos KK Loverdos
                 //not a group
129 c8f8690d Christos KK Loverdos
                final String displayName = app.getDisplayNameForUserID(userID);
130 8d09767f Christos KK Loverdos
                if(displayName != null) {
131 8d09767f Christos KK Loverdos
                    permTable.setHTML(
132 8d09767f Christos KK Loverdos
                        i,
133 8d09767f Christos KK Loverdos
                        0,
134 8d09767f Christos KK Loverdos
                        "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
135 8d09767f Christos KK Loverdos
                    );
136 8d09767f Christos KK Loverdos
                }
137 8d09767f Christos KK Loverdos
                else {
138 eee72ed5 Christos KK Loverdos
                    new UpdateUserCatalogs(app, userID) {
139 8d09767f Christos KK Loverdos
                        @Override
140 eee72ed5 Christos KK Loverdos
                        public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
141 dc8cc18a Christos KK Loverdos
                            final String displayName = updatedUserCatalogs.getDisplayName(userID);
142 8d09767f Christos KK Loverdos
                            permTable.setHTML(
143 8d09767f Christos KK Loverdos
                                ii,
144 8d09767f Christos KK Loverdos
                                0,
145 8d09767f Christos KK Loverdos
                                "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
146 8d09767f Christos KK Loverdos
                            );
147 8d09767f Christos KK Loverdos
                        }
148 8d09767f Christos KK Loverdos
                    }.scheduleDeferred();
149 8d09767f Christos KK Loverdos
                }
150 8d09767f Christos KK Loverdos
            }
151 8d09767f Christos KK Loverdos
            else {
152 8d09767f Christos KK Loverdos
                permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;" + userID.split(":")[1].trim() + "</span>");
153 8d09767f Christos KK Loverdos
            }
154 26387dd8 Christos Stathis
            permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
155 d5c6298f Christos Stathis
156 8d09767f Christos KK Loverdos
            Boolean[] userPerms = permissions.get(userID);
157 d5c6298f Christos Stathis
            Boolean readP = userPerms[0];
158 d5c6298f Christos Stathis
            Boolean writeP = userPerms[1];
159 d5c6298f Christos Stathis
160 4ded5310 Christos Stathis
                        RadioButton read = new RadioButton("permissions" + i);
161 d5c6298f Christos Stathis
                        read.setValue(readP != null ? readP : false);
162 ecf95c9e Christos Stathis
            permTable.setWidget(i, 1, read);
163 ecf95c9e Christos Stathis
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
164 ecf95c9e Christos Stathis
165 4ded5310 Christos Stathis
            RadioButton write = new RadioButton("permissions" + i);
166 ecf95c9e Christos Stathis
            write.setValue(writeP != null ? writeP : false);
167 ecf95c9e Christos Stathis
            permTable.setWidget(i, 2, write);
168 ecf95c9e Christos Stathis
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
169 ecf95c9e Christos Stathis
170 ecf95c9e Christos Stathis
            if (!readonly) {
171 ecf95c9e Christos Stathis
                read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
172 ecf95c9e Christos Stathis
                    @Override
173 ecf95c9e Christos Stathis
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
174 8d09767f Christos KK Loverdos
                        Boolean[] ps = permissions.get(userID);
175 ecf95c9e Christos Stathis
                        ps[0] = booleanValueChangeEvent.getValue();
176 4ded5310 Christos Stathis
                        ps[1] = !booleanValueChangeEvent.getValue();
177 3ebb88f4 Christos Stathis
                        hasChanges = true;
178 4d869bf1 Christos Stathis
                        if (changePermissionsCallback != null)
179 4d869bf1 Christos Stathis
                                changePermissionsCallback.execute();
180 ecf95c9e Christos Stathis
                    }
181 ecf95c9e Christos Stathis
                });
182 ecf95c9e Christos Stathis
                write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
183 ecf95c9e Christos Stathis
                    @Override
184 ecf95c9e Christos Stathis
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
185 8d09767f Christos KK Loverdos
                        Boolean[] ps = permissions.get(userID);
186 4ded5310 Christos Stathis
                        ps[0] = !booleanValueChangeEvent.getValue();
187 ecf95c9e Christos Stathis
                        ps[1] = booleanValueChangeEvent.getValue();
188 3ebb88f4 Christos Stathis
                        hasChanges = true;
189 4d869bf1 Christos Stathis
                        if (changePermissionsCallback != null)
190 4d869bf1 Christos Stathis
                                changePermissionsCallback.execute();
191 ecf95c9e Christos Stathis
                    }
192 ecf95c9e Christos Stathis
                });
193 01bd6099 Christos Stathis
                Anchor removeButton = new Anchor("remove");
194 01bd6099 Christos Stathis
                removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
195 01bd6099 Christos Stathis
                removeButton.addClickHandler(new ClickHandler() {
196 ecf95c9e Christos Stathis
                    @Override
197 ebead1b5 Christos Stathis
                    public void onClick(ClickEvent event) {
198 8d09767f Christos KK Loverdos
                        permissions.remove(userID);
199 ecf95c9e Christos Stathis
                        updatePermissionTable();
200 ecf95c9e Christos Stathis
                        hasChanges = true;
201 4d869bf1 Christos Stathis
                        if (changePermissionsCallback != null)
202 4d869bf1 Christos Stathis
                                changePermissionsCallback.execute();
203 ecf95c9e Christos Stathis
                    }
204 ecf95c9e Christos Stathis
                });
205 ecf95c9e Christos Stathis
                permTable.setWidget(i, 3, removeButton);
206 ecf95c9e Christos Stathis
                permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
207 ecf95c9e Christos Stathis
            }
208 ecf95c9e Christos Stathis
            else {
209 ecf95c9e Christos Stathis
                read.setEnabled(false);
210 ecf95c9e Christos Stathis
                write.setEnabled(false);
211 ecf95c9e Christos Stathis
            }
212 d5c6298f Christos Stathis
                        i++;
213 a57faaf0 Christos Stathis
                }
214 a57faaf0 Christos Stathis
        }
215 a57faaf0 Christos Stathis
}