Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (8.8 kB)

1
/*
2
 * Copyright 2011-2013 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
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.
16
 *
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.
29
 *
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.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import java.util.HashMap;
38
import java.util.Map;
39

    
40
import com.google.gwt.event.dom.client.ClickEvent;
41
import com.google.gwt.event.dom.client.ClickHandler;
42
import com.google.gwt.event.logical.shared.ValueChangeEvent;
43
import com.google.gwt.event.logical.shared.ValueChangeHandler;
44
import com.google.gwt.user.client.Command;
45
import com.google.gwt.user.client.ui.AbstractImagePrototype;
46
import com.google.gwt.user.client.ui.Anchor;
47
import com.google.gwt.user.client.ui.Composite;
48
import com.google.gwt.user.client.ui.FlexTable;
49
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
50
import com.google.gwt.user.client.ui.RadioButton;
51
import com.google.gwt.user.client.ui.VerticalPanel;
52
import gr.grnet.pithos.web.client.catalog.UpdateUserCatalogs;
53
import gr.grnet.pithos.web.client.catalog.UserCatalogs;
54

    
55

    
56
public class PermissionsList extends Composite {
57

    
58
        Map<String, Boolean[]> permissions = null;
59
        
60
        final FileShareDialog.PrivateSharingImages images;
61
        
62
        final VerticalPanel permPanel = new VerticalPanel();
63
        
64
        final FlexTable permTable = new FlexTable();
65
        
66
        final String owner;
67
        
68
        protected boolean hasChanges = false;
69

    
70
    private boolean readonly = false;
71
    
72
    Command changePermissionsCallback;
73

    
74
    private final Pithos app;
75
        
76
        public PermissionsList(Pithos app, final FileShareDialog.PrivateSharingImages theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
77
        this.app = app;
78
                changePermissionsCallback = _changePermissionsCallback;
79
                images = theImages;
80
                owner = theOwner;
81
                permissions =  new HashMap<String, Boolean[]>(thePermissions);
82
                permTable.setText(0, 0, "Users/Groups");
83
                permTable.setText(0, 1, "Read Only");
84
                permTable.setText(0, 2, "Read/Write");
85
                permTable.setText(0, 3, "");
86
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
87
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
88
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
89
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
90
                permPanel.add(permTable);
91
                permPanel.addStyleName("pithos-TabPanelBottom");
92
                initWidget(permPanel);
93
                updatePermissionTable();
94
        }
95

    
96
        public boolean hasChanges(){
97
                return hasChanges;
98
        }
99

    
100
        /**
101
         * Retrieve the permissions.
102
         *
103
         * @return the permissions
104
         */
105
        public Map<String, Boolean[]> getPermissions() {
106
                return permissions;
107
        }
108

    
109
        public void addPermission(String userID, boolean read, boolean write){
110
                permissions.put(userID, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
111
                hasChanges = true;
112
        updatePermissionTable();
113
        if (changePermissionsCallback != null)
114
                changePermissionsCallback.execute();
115
        }
116

    
117
        /**
118
         * Shows the permission table 
119
         * 
120
         */
121
        void updatePermissionTable(){
122
                int i = 1;
123
        final int ii = i;
124
        for (int j=1; j<permTable.getRowCount(); j++)
125
            permTable.removeRow(j);
126
                for(final String userID : permissions.keySet()) {
127
            if (!userID.contains(":")) {
128
                 //not a group
129
                final String displayName = app.getDisplayNameForUserID(userID);
130
                if(displayName != null) {
131
                    permTable.setHTML(
132
                        i,
133
                        0,
134
                        "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
135
                    );
136
                }
137
                else {
138
                    new UpdateUserCatalogs(app, userID) {
139
                        @Override
140
                        public void onSuccess(UserCatalogs requestedUserCatalogs, UserCatalogs updatedUserCatalogs) {
141
                            final String displayName = updatedUserCatalogs.getDisplayName(userID);
142
                            permTable.setHTML(
143
                                ii,
144
                                0,
145
                                "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + displayName + "</span>"
146
                            );
147
                        }
148
                    }.scheduleDeferred();
149
                }
150
            }
151
            else {
152
                permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;" + userID.split(":")[1].trim() + "</span>");
153
            }
154
            permTable.getFlexCellFormatter().setStyleName(i, 0, "props-values");
155

    
156
            Boolean[] userPerms = permissions.get(userID);
157
            Boolean readP = userPerms[0];
158
            Boolean writeP = userPerms[1];
159

    
160
                        RadioButton read = new RadioButton("permissions" + i);
161
                        read.setValue(readP != null ? readP : false);
162
            permTable.setWidget(i, 1, read);
163
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
164

    
165
            RadioButton write = new RadioButton("permissions" + i);
166
            write.setValue(writeP != null ? writeP : false);
167
            permTable.setWidget(i, 2, write);
168
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
169

    
170
            if (!readonly) {
171
                read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
172
                    @Override
173
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
174
                        Boolean[] ps = permissions.get(userID);
175
                        ps[0] = booleanValueChangeEvent.getValue();
176
                        ps[1] = !booleanValueChangeEvent.getValue();
177
                        hasChanges = true;
178
                        if (changePermissionsCallback != null)
179
                                changePermissionsCallback.execute();
180
                    }
181
                });
182
                write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
183
                    @Override
184
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
185
                        Boolean[] ps = permissions.get(userID);
186
                        ps[0] = !booleanValueChangeEvent.getValue();
187
                        ps[1] = booleanValueChangeEvent.getValue();
188
                        hasChanges = true;
189
                        if (changePermissionsCallback != null)
190
                                changePermissionsCallback.execute();
191
                    }
192
                });
193
                Anchor removeButton = new Anchor("remove");
194
                removeButton.addStyleName(Pithos.resources.pithosCss().commandAnchor());
195
                removeButton.addClickHandler(new ClickHandler() {
196
                    @Override
197
                    public void onClick(ClickEvent event) {
198
                        permissions.remove(userID);
199
                        updatePermissionTable();
200
                        hasChanges = true;
201
                        if (changePermissionsCallback != null)
202
                                changePermissionsCallback.execute();
203
                    }
204
                });
205
                permTable.setWidget(i, 3, removeButton);
206
                permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
207
            }
208
            else {
209
                read.setEnabled(false);
210
                write.setEnabled(false);
211
            }
212
                        i++;
213
                }
214
        }
215
}