Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / PermissionsAddDialog.java @ 1e1441bf

History | View | Annotate | Download (6.4 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.grouptree.Group;
38

    
39
import java.util.List;
40

    
41
import com.google.gwt.dom.client.NativeEvent;
42
import com.google.gwt.event.dom.client.ClickEvent;
43
import com.google.gwt.event.dom.client.ClickHandler;
44
import com.google.gwt.event.dom.client.KeyCodes;
45
import com.google.gwt.regexp.shared.RegExp;
46
import com.google.gwt.user.client.Event.NativePreviewEvent;
47
import com.google.gwt.user.client.ui.Anchor;
48
import com.google.gwt.user.client.ui.Button;
49
import com.google.gwt.user.client.ui.CheckBox;
50
import com.google.gwt.user.client.ui.DialogBox;
51
import com.google.gwt.user.client.ui.FlexTable;
52
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
53
import com.google.gwt.user.client.ui.ListBox;
54
import com.google.gwt.user.client.ui.RadioButton;
55
import com.google.gwt.user.client.ui.TextBox;
56
import com.google.gwt.user.client.ui.VerticalPanel;
57

    
58
public class PermissionsAddDialog extends DialogBox {
59

    
60
        private TextBox userBox = new TextBox();
61

    
62
        private ListBox groupBox = new ListBox();
63

    
64
        private RadioButton read = new RadioButton("permissions");
65

    
66
        private RadioButton write = new RadioButton("permissions");
67

    
68
        private PermissionsList permList;
69

    
70
        boolean userAdd;
71

    
72
    private Pithos app;
73

    
74
        public PermissionsAddDialog(Pithos _app, List<Group> _groups, PermissionsList _permList, boolean _userAdd) {
75
        app = _app;
76
                userAdd = _userAdd;
77
                permList = _permList;
78

    
79
                Anchor close = new Anchor("close");
80
                close.addStyleName("close");
81
                close.addClickHandler(new ClickHandler() {
82
                        
83
                        @Override
84
                        public void onClick(ClickEvent event) {
85
                                hide();
86
                        }
87
                });
88
                setText("Add permission");
89
                setStyleName("pithos-DialogBox");
90

    
91
        final VerticalPanel panel = new VerticalPanel();
92
        panel.add(close);
93

    
94
        VerticalPanel inner = new VerticalPanel();
95
                inner.addStyleName("inner");
96

    
97
        final FlexTable permTable = new FlexTable();
98
        permTable.setText(0, 0, "Users/Groups");
99
        permTable.setText(0, 1, "Read Only");
100
        permTable.setText(0, 2, "Read/Write");
101
        permTable.getFlexCellFormatter().setStyleName(0, 0, "props-toplabels");
102
        permTable.getFlexCellFormatter().setStyleName(0, 1, "props-toplabels");
103
        permTable.getFlexCellFormatter().setStyleName(0, 2, "props-toplabels");
104

    
105
        if (userAdd) {
106
            permTable.setWidget(1, 0, userBox);
107
        }
108
        else {
109
            for (Group group : _groups)
110
                groupBox.addItem(group.getName(), group.getName());
111
            permTable.setWidget(1, 0, groupBox);
112
        }
113
                
114
        read.setValue(true);
115
        permTable.setWidget(1, 1, read);
116
        permTable.setWidget(1, 2, write);
117

    
118
        permTable.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
119
        permTable.getFlexCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_CENTER);
120
        permTable.getFlexCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER);
121
        inner.add(permTable);
122

    
123
        final Button ok = new Button("OK", new ClickHandler() {
124
            @Override
125
            public void onClick(ClickEvent event) {
126
                addPermission();
127
                hide();
128
            }
129
        });
130

    
131
        ok.addStyleName("button");
132
        inner.add(ok);
133

    
134
        panel.add(inner);
135
        panel.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
136
        
137
        setWidget(panel);
138
        }
139

    
140
        protected void addPermission() {
141
        String selected = null;
142
                if (userAdd) {
143
                        selected = userBox.getText().trim();
144
                        RegExp emailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+.[A-Z]{2,4}$", "i");
145
                        if (!emailValidator.test(selected)) {
146
                                app.displayWarning("Username must be a valid email address");
147
                                return;
148
                        }
149
                } else if (groupBox.getSelectedIndex() > -1) {
150
                        String groupName = groupBox.getValue(groupBox.getSelectedIndex());
151
                        selected = app.getUsername() + ":" + groupName;
152
                }
153
        if (permList.getPermissions().get(selected) != null) {
154
            return;
155
        }
156
        if (selected == null || selected.length() == 0 || selected.equals(app.getUsername() + ":")) {
157
                app.displayWarning("You have to select a username or group");
158
                return;
159
        }
160

    
161
                boolean readValue = read.getValue();
162
                boolean writeValue = write.getValue();
163

    
164
                permList.addPermission(selected, readValue, writeValue);
165
        }
166

    
167
        @Override
168
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
169
                super.onPreviewNativeEvent(preview);
170

    
171
                NativeEvent evt = preview.getNativeEvent();
172
                if (evt.getType().equals("keydown"))
173
                        // Use the popup's key preview hooks to close the dialog when either
174
                        // enter or escape is pressed.
175
                        switch (evt.getKeyCode()) {
176
                                case KeyCodes.KEY_ENTER:
177
                                        addPermission();
178
                                        hide();
179
                                        break;
180
                                case KeyCodes.KEY_ESCAPE:
181
                                        hide();
182
                                        break;
183
                        }
184
        }
185

    
186

    
187
        @Override
188
        public void center() {
189
                super.center();
190
                if (userAdd)
191
                        userBox.setFocus(true);
192
        }
193
}