Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (7.3 kB)

1
/*
2
 * Copyright 2011-2012 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 gr.grnet.pithos.web.client.FilePermissionsDialog.Images;
38

    
39
import java.util.HashMap;
40
import java.util.Map;
41

    
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.Command;
47
import com.google.gwt.user.client.ui.AbstractImagePrototype;
48
import com.google.gwt.user.client.ui.CheckBox;
49
import com.google.gwt.user.client.ui.Composite;
50
import com.google.gwt.user.client.ui.FlexTable;
51
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
52
import com.google.gwt.user.client.ui.PushButton;
53
import com.google.gwt.user.client.ui.VerticalPanel;
54

    
55

    
56
public class PermissionsList extends Composite {
57

    
58
        Map<String, Boolean[]> permissions = null;
59
        
60
        final Images 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
        public PermissionsList(final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions, Command _changePermissionsCallback){
75
                changePermissionsCallback = _changePermissionsCallback;
76
                images = theImages;
77
                owner = theOwner;
78
                permissions =  new HashMap<String, Boolean[]>(thePermissions);
79
                permTable.setText(0, 0, "Users/Groups");
80
                permTable.setText(0, 1, "Read");
81
                permTable.setText(0, 2, "Write");
82
                permTable.setText(0, 3, "");
83
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
84
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
85
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
86
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
87
                permPanel.add(permTable);
88
                permPanel.addStyleName("pithos-TabPanelBottom");
89
                initWidget(permPanel);
90
                updatePermissionTable();
91
        }
92

    
93
        public boolean hasChanges(){
94
                return hasChanges;
95
        }
96

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

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

    
114
        /**
115
         * Shows the permission table 
116
         * 
117
         */
118
        void updatePermissionTable(){
119
                int i = 1;
120
        for (int j=1; j<permTable.getRowCount(); j++)
121
            permTable.removeRow(j);
122
                for(final String user : permissions.keySet()) {
123
            if (!user.contains(":")) //not a group
124
                permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + user + "</span>");
125
            else
126
                permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;" + user.split(":")[1].trim() + "</span>");
127
            permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
128

    
129
            Boolean[] userPerms = permissions.get(user);
130
            Boolean readP = userPerms[0];
131
            Boolean writeP = userPerms[1];
132

    
133
                        CheckBox read = new CheckBox();
134
                        read.setValue(readP != null ? readP : false);
135
            permTable.setWidget(i, 1, read);
136
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
137

    
138
            CheckBox write = new CheckBox();
139
            write.setValue(writeP != null ? writeP : false);
140
            permTable.setWidget(i, 2, write);
141
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
142

    
143
            if (!readonly) {
144
                read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
145
                    @Override
146
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
147
                        Boolean[] ps = permissions.get(user);
148
                        ps[0] = booleanValueChangeEvent.getValue();
149
                        hasChanges = true;
150
                        if (changePermissionsCallback != null)
151
                                changePermissionsCallback.execute();
152
                    }
153
                });
154
                write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
155
                    @Override
156
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
157
                        Boolean[] ps = permissions.get(user);
158
                        ps[1] = booleanValueChangeEvent.getValue();
159
                        hasChanges = true;
160
                        if (changePermissionsCallback != null)
161
                                changePermissionsCallback.execute();
162
                    }
163
                });
164
                PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
165
                    @Override
166
                    public void onClick(ClickEvent event) {
167
                        permissions.remove(user);
168
                        updatePermissionTable();
169
                        hasChanges = true;
170
                        if (changePermissionsCallback != null)
171
                                changePermissionsCallback.execute();
172
                    }
173
                });
174
                permTable.setWidget(i, 3, removeButton);
175
                permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
176
            }
177
            else {
178
                read.setEnabled(false);
179
                write.setEnabled(false);
180
            }
181
                        i++;
182
                }
183
        }
184
}