Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (6.7 kB)

1 a57faaf0 Christos Stathis
/*
2 cae2a8db Christos Stathis
 * Copyright 2011-2012 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 9326b841 Christos Stathis
import gr.grnet.pithos.web.client.FilePermissionsDialog.Images;
38 a57faaf0 Christos Stathis
39 d5c6298f Christos Stathis
import java.util.HashMap;
40 d5c6298f Christos Stathis
import java.util.Map;
41 a57faaf0 Christos Stathis
42 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
43 a57faaf0 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
44 9326b841 Christos Stathis
import com.google.gwt.event.logical.shared.ValueChangeEvent;
45 9326b841 Christos Stathis
import com.google.gwt.event.logical.shared.ValueChangeHandler;
46 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.AbstractImagePrototype;
47 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.CheckBox;
48 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.Composite;
49 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.FlexTable;
50 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
51 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.PushButton;
52 a57faaf0 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
53 a57faaf0 Christos Stathis
54 a57faaf0 Christos Stathis
55 a57faaf0 Christos Stathis
public class PermissionsList extends Composite {
56 a57faaf0 Christos Stathis
57 d5c6298f Christos Stathis
        Map<String, Boolean[]> permissions = null;
58 a57faaf0 Christos Stathis
        
59 a57faaf0 Christos Stathis
        final Images images;
60 a57faaf0 Christos Stathis
        
61 a57faaf0 Christos Stathis
        final VerticalPanel permPanel = new VerticalPanel();
62 a57faaf0 Christos Stathis
        
63 a57faaf0 Christos Stathis
        final FlexTable permTable = new FlexTable();
64 a57faaf0 Christos Stathis
        
65 a57faaf0 Christos Stathis
        final String owner;
66 a57faaf0 Christos Stathis
        
67 7811b9d1 Christos Stathis
        protected boolean hasChanges = false;
68 a7c43f26 Christos Stathis
69 ecf95c9e Christos Stathis
    private boolean readonly = false;
70 a57faaf0 Christos Stathis
        
71 ecf95c9e Christos Stathis
        public PermissionsList(final Images theImages, Map<String, Boolean[]> thePermissions, String theOwner, boolean inheritsPermissions){
72 a57faaf0 Christos Stathis
                images = theImages;
73 ecf95c9e Christos Stathis
                owner = theOwner;
74 d5c6298f Christos Stathis
                permissions =  new HashMap<String, Boolean[]>(thePermissions);
75 ecf95c9e Christos Stathis
        readonly = inheritsPermissions;
76 a57faaf0 Christos Stathis
                permTable.setText(0, 0, "Users/Groups");
77 a57faaf0 Christos Stathis
                permTable.setText(0, 1, "Read");
78 a57faaf0 Christos Stathis
                permTable.setText(0, 2, "Write");
79 d5c6298f Christos Stathis
                permTable.setText(0, 3, "");
80 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
81 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
82 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
83 a57faaf0 Christos Stathis
                permTable.getFlexCellFormatter().setStyleName(0, 3, "props-toplabels");
84 a57faaf0 Christos Stathis
                permPanel.add(permTable);
85 a57faaf0 Christos Stathis
                permPanel.addStyleName("pithos-TabPanelBottom");
86 a57faaf0 Christos Stathis
                initWidget(permPanel);
87 d5c6298f Christos Stathis
                updatePermissionTable();
88 a57faaf0 Christos Stathis
        }
89 a57faaf0 Christos Stathis
90 a57faaf0 Christos Stathis
        public boolean hasChanges(){
91 ecf95c9e Christos Stathis
                return hasChanges;
92 a57faaf0 Christos Stathis
        }
93 a57faaf0 Christos Stathis
94 a57faaf0 Christos Stathis
        /**
95 a57faaf0 Christos Stathis
         * Retrieve the permissions.
96 a57faaf0 Christos Stathis
         *
97 a57faaf0 Christos Stathis
         * @return the permissions
98 a57faaf0 Christos Stathis
         */
99 d5c6298f Christos Stathis
        public Map<String, Boolean[]> getPermissions() {
100 a57faaf0 Christos Stathis
                return permissions;
101 a57faaf0 Christos Stathis
        }
102 a57faaf0 Christos Stathis
103 d5c6298f Christos Stathis
        public void addPermission(String user, boolean read, boolean write){
104 d5c6298f Christos Stathis
                permissions.put(user, new Boolean[] {Boolean.valueOf(read), Boolean.valueOf(write)});
105 ecf95c9e Christos Stathis
                hasChanges = true;
106 d5c6298f Christos Stathis
        updatePermissionTable();
107 a57faaf0 Christos Stathis
        }
108 d5c6298f Christos Stathis
109 a57faaf0 Christos Stathis
        /**
110 a57faaf0 Christos Stathis
         * Shows the permission table 
111 a57faaf0 Christos Stathis
         * 
112 a57faaf0 Christos Stathis
         */
113 d5c6298f Christos Stathis
        void updatePermissionTable(){
114 a57faaf0 Christos Stathis
                int i = 1;
115 d5c6298f Christos Stathis
        for (int j=1; j<permTable.getRowCount(); j++)
116 d5c6298f Christos Stathis
            permTable.removeRow(j);
117 d5c6298f Christos Stathis
                for(final String user : permissions.keySet()) {
118 ecf95c9e Christos Stathis
            if (!user.contains(":")) //not a group
119 d5c6298f Christos Stathis
                permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permUser()).getHTML() + "&nbsp;" + user + "</span>");
120 d5c6298f Christos Stathis
            else
121 d5c6298f Christos Stathis
                permTable.setHTML(i, 0, "<span>" + AbstractImagePrototype.create(images.permGroup()).getHTML() + "&nbsp;" + user.split(":")[1].trim() + "</span>");
122 ecf95c9e Christos Stathis
            permTable.getFlexCellFormatter().setStyleName(i, 0, "props-labels");
123 d5c6298f Christos Stathis
124 d5c6298f Christos Stathis
            Boolean[] userPerms = permissions.get(user);
125 d5c6298f Christos Stathis
            Boolean readP = userPerms[0];
126 d5c6298f Christos Stathis
            Boolean writeP = userPerms[1];
127 d5c6298f Christos Stathis
128 a57faaf0 Christos Stathis
                        CheckBox read = new CheckBox();
129 d5c6298f Christos Stathis
                        read.setValue(readP != null ? readP : false);
130 ecf95c9e Christos Stathis
            permTable.setWidget(i, 1, read);
131 ecf95c9e Christos Stathis
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 1, HasHorizontalAlignment.ALIGN_CENTER);
132 ecf95c9e Christos Stathis
133 ecf95c9e Christos Stathis
            CheckBox write = new CheckBox();
134 ecf95c9e Christos Stathis
            write.setValue(writeP != null ? writeP : false);
135 ecf95c9e Christos Stathis
            permTable.setWidget(i, 2, write);
136 ecf95c9e Christos Stathis
            permTable.getFlexCellFormatter().setHorizontalAlignment(i, 2, HasHorizontalAlignment.ALIGN_CENTER);
137 ecf95c9e Christos Stathis
138 ecf95c9e Christos Stathis
            if (!readonly) {
139 ecf95c9e Christos Stathis
                read.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
140 ecf95c9e Christos Stathis
                    @Override
141 ecf95c9e Christos Stathis
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
142 ecf95c9e Christos Stathis
                        Boolean[] ps = permissions.get(user);
143 ecf95c9e Christos Stathis
                        ps[0] = booleanValueChangeEvent.getValue();
144 3ebb88f4 Christos Stathis
                        hasChanges = true;
145 ecf95c9e Christos Stathis
                    }
146 ecf95c9e Christos Stathis
                });
147 ecf95c9e Christos Stathis
                write.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
148 ecf95c9e Christos Stathis
                    @Override
149 ecf95c9e Christos Stathis
                    public void onValueChange(ValueChangeEvent<Boolean> booleanValueChangeEvent) {
150 ecf95c9e Christos Stathis
                        Boolean[] ps = permissions.get(user);
151 ecf95c9e Christos Stathis
                        ps[1] = booleanValueChangeEvent.getValue();
152 3ebb88f4 Christos Stathis
                        hasChanges = true;
153 ecf95c9e Christos Stathis
                    }
154 ecf95c9e Christos Stathis
                });
155 ecf95c9e Christos Stathis
                PushButton removeButton = new PushButton(AbstractImagePrototype.create(images.delete()).createImage(), new ClickHandler() {
156 ecf95c9e Christos Stathis
                    @Override
157 ebead1b5 Christos Stathis
                    public void onClick(ClickEvent event) {
158 ecf95c9e Christos Stathis
                        permissions.remove(user);
159 ecf95c9e Christos Stathis
                        updatePermissionTable();
160 ecf95c9e Christos Stathis
                        hasChanges = true;
161 ecf95c9e Christos Stathis
                    }
162 ecf95c9e Christos Stathis
                });
163 ecf95c9e Christos Stathis
                permTable.setWidget(i, 3, removeButton);
164 ecf95c9e Christos Stathis
                permTable.getFlexCellFormatter().setHorizontalAlignment(i, 3, HasHorizontalAlignment.ALIGN_CENTER);
165 ecf95c9e Christos Stathis
            }
166 ecf95c9e Christos Stathis
            else {
167 ecf95c9e Christos Stathis
                read.setEnabled(false);
168 ecf95c9e Christos Stathis
                write.setEnabled(false);
169 ecf95c9e Christos Stathis
            }
170 d5c6298f Christos Stathis
                        i++;
171 a57faaf0 Christos Stathis
                }
172 a57faaf0 Christos Stathis
        }
173 a57faaf0 Christos Stathis
}