Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.1 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 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
38 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
39 9326b841 Christos Stathis
import com.google.gwt.event.logical.shared.ValueChangeEvent;
40 9326b841 Christos Stathis
import com.google.gwt.event.logical.shared.ValueChangeHandler;
41 4d869bf1 Christos Stathis
import com.google.gwt.user.client.Command;
42 87487bbf Christos KK Loverdos
import com.google.gwt.user.client.ui.*;
43 eee72ed5 Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
44 8d09767f Christos KK Loverdos
import gr.grnet.pithos.web.client.catalog.UserCatalogs;
45 a57faaf0 Christos Stathis
46 87487bbf Christos KK Loverdos
import java.util.HashMap;
47 87487bbf Christos KK Loverdos
import java.util.Map;
48 87487bbf Christos KK Loverdos
49 a57faaf0 Christos Stathis
50 a57faaf0 Christos Stathis
public class PermissionsList extends Composite {
51 a57faaf0 Christos Stathis
52 87487bbf Christos KK Loverdos
    Map<String, Boolean[]> permissions = null;
53 87487bbf Christos KK Loverdos
54 87487bbf Christos KK Loverdos
    final FileShareDialog.PrivateSharingImages images;
55 87487bbf Christos KK Loverdos
56 87487bbf Christos KK Loverdos
    final VerticalPanel permPanel = new VerticalPanel();
57 87487bbf Christos KK Loverdos
58 87487bbf Christos KK Loverdos
    final FlexTable permTable = new FlexTable();
59 87487bbf Christos KK Loverdos
60 87487bbf Christos KK Loverdos
    final String owner;
61 87487bbf Christos KK Loverdos
62 87487bbf Christos KK Loverdos
    protected boolean hasChanges = false;
63 a7c43f26 Christos Stathis
64 ecf95c9e Christos Stathis
    private boolean readonly = false;
65 87487bbf Christos KK Loverdos
66 4d869bf1 Christos Stathis
    Command changePermissionsCallback;
67 8d09767f Christos KK Loverdos
68 8d09767f Christos KK Loverdos
    private final Pithos app;
69 87487bbf Christos KK Loverdos
70 87487bbf Christos KK Loverdos
    public PermissionsList(
71 87487bbf Christos KK Loverdos
        Pithos app,
72 87487bbf Christos KK Loverdos
        final FileShareDialog.PrivateSharingImages theImages,
73 87487bbf Christos KK Loverdos
        Map<String, Boolean[]> thePermissions,
74 87487bbf Christos KK Loverdos
        String theOwner,
75 87487bbf Christos KK Loverdos
        boolean inheritsPermissions,
76 87487bbf Christos KK Loverdos
        Command _changePermissionsCallback
77 87487bbf Christos KK Loverdos
    ) {
78 8d09767f Christos KK Loverdos
        this.app = app;
79 87487bbf Christos KK Loverdos
        changePermissionsCallback = _changePermissionsCallback;
80 87487bbf Christos KK Loverdos
        images = theImages;
81 87487bbf Christos KK Loverdos
        owner = theOwner;
82 87487bbf Christos KK Loverdos
        permissions = new HashMap<String, Boolean[]>(thePermissions);
83 87487bbf Christos KK Loverdos
        updatePermissionsHeader();
84 87487bbf Christos KK Loverdos
        permPanel.add(permTable);
85 87487bbf Christos KK Loverdos
        permPanel.addStyleName("pithos-TabPanelBottom");
86 87487bbf Christos KK Loverdos
        initWidget(permPanel);
87 d5c6298f Christos Stathis
        updatePermissionTable();
88 87487bbf Christos KK Loverdos
    }
89 87487bbf Christos KK Loverdos
90 87487bbf Christos KK Loverdos
    private void updatePermissionsHeader() {
91 87487bbf Christos KK Loverdos
        if(hasPermissions()) {
92 87487bbf Christos KK Loverdos
            permTable.setText(0, 0, "Users/Groups");
93 87487bbf Christos KK Loverdos
            permTable.setText(0, 1, "Read Only");
94 87487bbf Christos KK Loverdos
            permTable.setText(0, 2, "Read/Write");
95 87487bbf Christos KK Loverdos
            permTable.setText(0, 3, "");
96 87487bbf Christos KK Loverdos
            permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
97 87487bbf Christos KK Loverdos
            permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
98 87487bbf Christos KK Loverdos
            permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
99 87487bbf Christos KK Loverdos
            permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
100 87487bbf Christos KK Loverdos
        }
101 87487bbf Christos KK Loverdos
        else {
102 87487bbf Christos KK Loverdos
            permTable.setText(0, 0, "");
103 87487bbf Christos KK Loverdos
            permTable.setText(0, 1, "");
104 87487bbf Christos KK Loverdos
            permTable.setText(0, 2, "");
105 87487bbf Christos KK Loverdos
            permTable.setText(0, 3, "");
106 87487bbf Christos KK Loverdos
        }
107 87487bbf Christos KK Loverdos
    }
108 87487bbf Christos KK Loverdos
109 87487bbf Christos KK Loverdos
    public boolean hasPermissions() {
110 87487bbf Christos KK Loverdos
        return permissionCount() > 0;
111 87487bbf Christos KK Loverdos
    }
112 87487bbf Christos KK Loverdos
113 87487bbf Christos KK Loverdos
    public int permissionCount() {
114 87487bbf Christos KK Loverdos
        return permissions == null
115 87487bbf Christos KK Loverdos
            ? 0
116 87487bbf Christos KK Loverdos
            : permissions.size();
117 87487bbf Christos KK Loverdos
    }
118 87487bbf Christos KK Loverdos
119 87487bbf Christos KK Loverdos
    public boolean hasChanges() {
120 87487bbf Christos KK Loverdos
        return hasChanges;
121 87487bbf Christos KK Loverdos
    }
122 87487bbf Christos KK Loverdos
123 87487bbf Christos KK Loverdos
    /**
124 87487bbf Christos KK Loverdos
     * Retrieve the permissions.
125 87487bbf Christos KK Loverdos
     *
126 87487bbf Christos KK Loverdos
     * @return the permissions
127 87487bbf Christos KK Loverdos
     */
128 87487bbf Christos KK Loverdos
    public Map<String, Boolean[]> getPermissions() {
129 87487bbf Christos KK Loverdos
        return permissions;
130 87487bbf Christos KK Loverdos
    }
131 87487bbf Christos KK Loverdos
132 87487bbf Christos KK Loverdos
    public void addPermission(String userID, boolean read, boolean write) {
133 34cd9aff Christos KK Loverdos
        Pithos.LOG("PermissionsList::addPermission(userID=", userID, ", read=", read, ", write=", write, ")");
134 87487bbf Christos KK Loverdos
        permissions.put(userID, new Boolean[]{Boolean.valueOf(read), Boolean.valueOf(write)});
135 87487bbf Christos KK Loverdos
        hasChanges = true;
136 87487bbf Christos KK Loverdos
        updatePermissionTable();
137 87487bbf Christos KK Loverdos
        if(changePermissionsCallback != null) {
138 87487bbf Christos KK Loverdos
            changePermissionsCallback.execute();
139 87487bbf Christos KK Loverdos
        }
140 87487bbf Christos KK Loverdos
    }
141 87487bbf Christos KK Loverdos
142 87487bbf Christos KK Loverdos
    private boolean isGroup(String userID) {
143 87487bbf Christos KK Loverdos
        return userID.contains(Const.COLON);
144 87487bbf Christos KK Loverdos
    }
145 87487bbf Christos KK Loverdos
146 87487bbf Christos KK Loverdos
    /**
147 87487bbf Christos KK Loverdos
     * Shows the permission table
148 87487bbf Christos KK Loverdos
     */
149 87487bbf Christos KK Loverdos
    void updatePermissionTable() {
150 87487bbf Christos KK Loverdos
        int i = 1;
151 8d09767f Christos KK Loverdos
        final int ii = i;
152 87487bbf Christos KK Loverdos
        for(int j = 1; j < permTable.getRowCount(); j++) {
153 d5c6298f Christos Stathis
            permTable.removeRow(j);
154 87487bbf Christos KK Loverdos
        }
155 87487bbf Christos KK Loverdos
156 87487bbf Christos KK Loverdos
        updatePermissionsHeader();
157 87487bbf Christos KK Loverdos
158 87487bbf Christos KK Loverdos
        for(final String userID : permissions.keySet()) {
159 87487bbf Christos KK Loverdos
            if(!isGroup(userID)) {
160 c8f8690d Christos KK Loverdos
                final String displayName = app.getDisplayNameForUserID(userID);
161 8d09767f Christos KK Loverdos
                if(displayName != null) {
162 8d09767f Christos KK Loverdos
                    permTable.setHTML(
163 8d09767f Christos KK Loverdos
                        i,
164 8d09767f Christos KK Loverdos
                        0,
165 87487bbf Christos KK Loverdos
                        Const.inSpan(
166 87487bbf Christos KK Loverdos
                            AbstractImagePrototype.create(
167 87487bbf Christos KK Loverdos
                                images.permUser()).getHTML() +
168 87487bbf Christos KK Loverdos
                                Const.HTML_NBSP +
169 87487bbf Christos KK Loverdos
                                displayName
170 87487bbf Christos KK Loverdos
                        )
171 8d09767f Christos KK Loverdos
                    );
172 8d09767f Christos KK Loverdos
                }
173 8d09767f Christos KK Loverdos
                else {
174 eee72ed5 Christos KK Loverdos
                    new UpdateUserCatalogs(app, userID) {
175 8d09767f Christos KK Loverdos
                        @Override
176 eee72ed5 Christos KK Loverdos
                        public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
177 dc8cc18a Christos KK Loverdos
                            final String displayName = updatedUserCatalogs.getDisplayName(userID);
178 8d09767f Christos KK Loverdos
                            permTable.setHTML(
179 8d09767f Christos KK Loverdos
                                ii,
180 8d09767f Christos KK Loverdos
                                0,
181 87487bbf Christos KK Loverdos
                                Const.inSpan(
182 87487bbf Christos KK Loverdos
                                    AbstractImagePrototype.create(
183 87487bbf Christos KK Loverdos
                                        images.permUser()).getHTML() +
184 87487bbf Christos KK Loverdos
                                        Const.HTML_NBSP +
185 87487bbf Christos KK Loverdos
                                        displayName
186 87487bbf Christos KK Loverdos
                                )
187 8d09767f Christos KK Loverdos
                            );
188 8d09767f Christos KK Loverdos
                        }
189 8d09767f Christos KK Loverdos
                    }.scheduleDeferred();
190 8d09767f Christos KK Loverdos
                }
191 8d09767f Christos KK Loverdos
            }
192 8d09767f Christos KK Loverdos
            else {
193 87487bbf Christos KK Loverdos
                permTable.setHTML(
194 87487bbf Christos KK Loverdos
                    i,
195 87487bbf Christos KK Loverdos
                    0,
196 87487bbf Christos KK Loverdos
                    Const.inSpan(
197 87487bbf Christos KK Loverdos
                        AbstractImagePrototype.create(
198 87487bbf Christos KK Loverdos
                            images.permGroup()).getHTML() +
199 87487bbf Christos KK Loverdos
                            Const.HTML_NBSP +
200 87487bbf Christos KK Loverdos
                            userID.split(Const.COLON)[1].trim()
201 87487bbf Christos KK Loverdos
                    )
202 87487bbf Christos KK Loverdos
                );
203 8d09767f Christos KK Loverdos
            }
204 26387dd8 Christos Stathis
            permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
205 d5c6298f Christos Stathis
206 8d09767f Christos KK Loverdos
            Boolean[] userPerms = permissions.get(userID);
207 d5c6298f Christos Stathis
            Boolean readP = userPerms[0];
208 d5c6298f Christos Stathis
            Boolean writeP = userPerms[1];
209 d5c6298f Christos Stathis
210 87487bbf Christos KK Loverdos
            RadioButton read = new RadioButton("permissions" + i);
211 87487bbf Christos KK Loverdos
            read.setValue(readP != null ? readP : false);
212 ecf95c9e Christos Stathis
            permTable.setWidget(i, 1, read);
213 ecf95c9e Christos Stathis
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
214 ecf95c9e Christos Stathis
215 4ded5310 Christos Stathis
            RadioButton write = new RadioButton("permissions" + i);
216 ecf95c9e Christos Stathis
            write.setValue(writeP != null ? writeP : false);
217 ecf95c9e Christos Stathis
            permTable.setWidget(i, 2, write);
218 ecf95c9e Christos Stathis
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
219 ecf95c9e Christos Stathis
220 87487bbf Christos KK Loverdos
            if(!readonly) {
221 ecf95c9e Christos Stathis
                read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
222 ecf95c9e Christos Stathis
                    @Override
223 ecf95c9e Christos Stathis
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
224 8d09767f Christos KK Loverdos
                        Boolean[] ps = permissions.get(userID);
225 ecf95c9e Christos Stathis
                        ps[0] = booleanValueChangeEvent.getValue();
226 4ded5310 Christos Stathis
                        ps[1] = !booleanValueChangeEvent.getValue();
227 3ebb88f4 Christos Stathis
                        hasChanges = true;
228 87487bbf Christos KK Loverdos
                        if(changePermissionsCallback != null) {
229 87487bbf Christos KK Loverdos
                            changePermissionsCallback.execute();
230 87487bbf Christos KK Loverdos
                        }
231 ecf95c9e Christos Stathis
                    }
232 ecf95c9e Christos Stathis
                });
233 ecf95c9e Christos Stathis
                write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
234 ecf95c9e Christos Stathis
                    @Override
235 ecf95c9e Christos Stathis
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
236 8d09767f Christos KK Loverdos
                        Boolean[] ps = permissions.get(userID);
237 4ded5310 Christos Stathis
                        ps[0] = !booleanValueChangeEvent.getValue();
238 ecf95c9e Christos Stathis
                        ps[1] = booleanValueChangeEvent.getValue();
239 3ebb88f4 Christos Stathis
                        hasChanges = true;
240 87487bbf Christos KK Loverdos
                        if(changePermissionsCallback != null) {
241 87487bbf Christos KK Loverdos
                            changePermissionsCallback.execute();
242 87487bbf Christos KK Loverdos
                        }
243 ecf95c9e Christos Stathis
                    }
244 ecf95c9e Christos Stathis
                });
245 01bd6099 Christos Stathis
                Anchor removeButton = new Anchor("remove");
246 01bd6099 Christos Stathis
                removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
247 01bd6099 Christos Stathis
                removeButton.addClickHandler(new ClickHandler() {
248 ecf95c9e Christos Stathis
                    @Override
249 ebead1b5 Christos Stathis
                    public void onClick(ClickEvent event) {
250 8d09767f Christos KK Loverdos
                        permissions.remove(userID);
251 ecf95c9e Christos Stathis
                        updatePermissionTable();
252 ecf95c9e Christos Stathis
                        hasChanges = true;
253 87487bbf Christos KK Loverdos
                        if(changePermissionsCallback != null) {
254 87487bbf Christos KK Loverdos
                            changePermissionsCallback.execute();
255 87487bbf Christos KK Loverdos
                        }
256 ecf95c9e Christos Stathis
                    }
257 ecf95c9e Christos Stathis
                });
258 ecf95c9e Christos Stathis
                permTable.setWidget(i, 3, removeButton);
259 ecf95c9e Christos Stathis
                permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
260 ecf95c9e Christos Stathis
            }
261 ecf95c9e Christos Stathis
            else {
262 ecf95c9e Christos Stathis
                read.setEnabled(false);
263 ecf95c9e Christos Stathis
                write.setEnabled(false);
264 ecf95c9e Christos Stathis
            }
265 87487bbf Christos KK Loverdos
            i++;
266 87487bbf Christos KK Loverdos
        }
267 87487bbf Christos KK Loverdos
    }
268 a57faaf0 Christos Stathis
}