Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / GroupPropertiesDialog.java @ ab1eb3f8

History | View | Annotate | Download (5.9 kB)

1 ab1eb3f8 Christos Stathis
/*
2 ab1eb3f8 Christos Stathis
 * Copyright 2008, 2009 Electronic Business Systems Ltd.
3 ab1eb3f8 Christos Stathis
 *
4 ab1eb3f8 Christos Stathis
 * This file is part of GSS.
5 ab1eb3f8 Christos Stathis
 *
6 ab1eb3f8 Christos Stathis
 * GSS is free software: you can redistribute it and/or modify
7 ab1eb3f8 Christos Stathis
 * it under the terms of the GNU General Public License as published by
8 ab1eb3f8 Christos Stathis
 * the Free Software Foundation, either version 3 of the License, or
9 ab1eb3f8 Christos Stathis
 * (at your option) any later version.
10 ab1eb3f8 Christos Stathis
 *
11 ab1eb3f8 Christos Stathis
 * GSS is distributed in the hope that it will be useful,
12 ab1eb3f8 Christos Stathis
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ab1eb3f8 Christos Stathis
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 ab1eb3f8 Christos Stathis
 * GNU General Public License for more details.
15 ab1eb3f8 Christos Stathis
 *
16 ab1eb3f8 Christos Stathis
 * You should have received a copy of the GNU General Public License
17 ab1eb3f8 Christos Stathis
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18 ab1eb3f8 Christos Stathis
 */
19 ab1eb3f8 Christos Stathis
package gr.grnet.pithos.web.client;
20 ab1eb3f8 Christos Stathis
21 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.PostCommand;
22 ab1eb3f8 Christos Stathis
import gr.grnet.pithos.web.client.rest.RestException;
23 ab1eb3f8 Christos Stathis
24 ab1eb3f8 Christos Stathis
import com.google.gwt.core.client.GWT;
25 ab1eb3f8 Christos Stathis
import com.google.gwt.dom.client.NativeEvent;
26 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickEvent;
27 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.ClickHandler;
28 ab1eb3f8 Christos Stathis
import com.google.gwt.event.dom.client.KeyCodes;
29 ab1eb3f8 Christos Stathis
import com.google.gwt.http.client.URL;
30 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.DeferredCommand;
31 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.Event.NativePreviewEvent;
32 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Button;
33 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.DialogBox;
34 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.Grid;
35 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
36 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.HorizontalPanel;
37 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.TextBox;
38 ab1eb3f8 Christos Stathis
import com.google.gwt.user.client.ui.VerticalPanel;
39 ab1eb3f8 Christos Stathis
40 ab1eb3f8 Christos Stathis
/**
41 ab1eb3f8 Christos Stathis
 * @author kman
42 ab1eb3f8 Christos Stathis
 */
43 ab1eb3f8 Christos Stathis
public class GroupPropertiesDialog extends DialogBox {
44 ab1eb3f8 Christos Stathis
45 ab1eb3f8 Christos Stathis
        /**
46 ab1eb3f8 Christos Stathis
         * The widget that holds the folderName of the folder.
47 ab1eb3f8 Christos Stathis
         */
48 ab1eb3f8 Christos Stathis
        private TextBox groupName = new TextBox();
49 ab1eb3f8 Christos Stathis
50 ab1eb3f8 Christos Stathis
        /**
51 ab1eb3f8 Christos Stathis
         * A flag that denotes whether the dialog will be used to create or modify a
52 ab1eb3f8 Christos Stathis
         * folder.
53 ab1eb3f8 Christos Stathis
         */
54 ab1eb3f8 Christos Stathis
        private final boolean create;
55 ab1eb3f8 Christos Stathis
56 ab1eb3f8 Christos Stathis
        /**
57 ab1eb3f8 Christos Stathis
         * The widget's constructor.
58 ab1eb3f8 Christos Stathis
         *
59 ab1eb3f8 Christos Stathis
         * @param _create true if the dialog is displayed for creating a new
60 ab1eb3f8 Christos Stathis
         *            sub-folder of the selected folder, false if it is displayed
61 ab1eb3f8 Christos Stathis
         *            for modifying the selected folder
62 ab1eb3f8 Christos Stathis
         */
63 ab1eb3f8 Christos Stathis
        public GroupPropertiesDialog(final boolean _create) {
64 ab1eb3f8 Christos Stathis
                setAnimationEnabled(true);
65 ab1eb3f8 Christos Stathis
                create = _create;
66 ab1eb3f8 Christos Stathis
                // Use this opportunity to set the dialog's caption.
67 ab1eb3f8 Christos Stathis
                if (create)
68 ab1eb3f8 Christos Stathis
                        setText("Create Group");
69 ab1eb3f8 Christos Stathis
                else
70 ab1eb3f8 Christos Stathis
                        setText("Group properties");
71 ab1eb3f8 Christos Stathis
                final VerticalPanel panel = new VerticalPanel();
72 ab1eb3f8 Christos Stathis
                setWidget(panel);
73 ab1eb3f8 Christos Stathis
                groupName.getElement().setId("groupDialog.textBox.name");
74 ab1eb3f8 Christos Stathis
                final Grid generalTable = new Grid(1, 2);
75 ab1eb3f8 Christos Stathis
                generalTable.setText(0, 0, "Group Name");
76 ab1eb3f8 Christos Stathis
                generalTable.setWidget(0, 1, groupName);
77 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(0, 0, "props-labels");
78 ab1eb3f8 Christos Stathis
                generalTable.getCellFormatter().setStyleName(0, 1, "props-values");
79 ab1eb3f8 Christos Stathis
                generalTable.setCellSpacing(4);
80 ab1eb3f8 Christos Stathis
81 ab1eb3f8 Christos Stathis
                panel.add(generalTable);
82 ab1eb3f8 Christos Stathis
                final HorizontalPanel buttons = new HorizontalPanel();
83 ab1eb3f8 Christos Stathis
                final Button ok = new Button("OK", new ClickHandler() {
84 ab1eb3f8 Christos Stathis
                        @Override
85 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
86 ab1eb3f8 Christos Stathis
                                createGroup(groupName.getText());
87 ab1eb3f8 Christos Stathis
                                hide();
88 ab1eb3f8 Christos Stathis
                        }
89 ab1eb3f8 Christos Stathis
                });
90 ab1eb3f8 Christos Stathis
                ok.getElement().setId("groupDialog.button.ok");
91 ab1eb3f8 Christos Stathis
                buttons.add(ok);
92 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
93 ab1eb3f8 Christos Stathis
                // Create the 'Cancel' button, along with a listener that hides the
94 ab1eb3f8 Christos Stathis
                // dialog
95 ab1eb3f8 Christos Stathis
                // when the button is clicked.
96 ab1eb3f8 Christos Stathis
                final Button cancel = new Button("Cancel", new ClickHandler() {
97 ab1eb3f8 Christos Stathis
                        @Override
98 ab1eb3f8 Christos Stathis
                        public void onClick(ClickEvent event) {
99 ab1eb3f8 Christos Stathis
                                hide();
100 ab1eb3f8 Christos Stathis
                        }
101 ab1eb3f8 Christos Stathis
                });
102 ab1eb3f8 Christos Stathis
                cancel.getElement().setId("groupDialog.button.cancel");
103 ab1eb3f8 Christos Stathis
                buttons.add(cancel);
104 ab1eb3f8 Christos Stathis
                buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
105 ab1eb3f8 Christos Stathis
                buttons.setSpacing(8);
106 ab1eb3f8 Christos Stathis
                buttons.addStyleName("pithos-TabPanelBottom");
107 ab1eb3f8 Christos Stathis
                panel.add(buttons);
108 ab1eb3f8 Christos Stathis
                panel.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
109 ab1eb3f8 Christos Stathis
                //panel.addStyleName("pithos-DialogBox");
110 ab1eb3f8 Christos Stathis
                panel.addStyleName("pithos-TabPanelBottom");
111 ab1eb3f8 Christos Stathis
        }
112 ab1eb3f8 Christos Stathis
        @Override
113 ab1eb3f8 Christos Stathis
        public void center() {
114 ab1eb3f8 Christos Stathis
                super.center();
115 ab1eb3f8 Christos Stathis
                groupName.setFocus(true);
116 ab1eb3f8 Christos Stathis
        }
117 ab1eb3f8 Christos Stathis
118 ab1eb3f8 Christos Stathis
        @Override
119 ab1eb3f8 Christos Stathis
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
120 ab1eb3f8 Christos Stathis
                super.onPreviewNativeEvent(preview);
121 ab1eb3f8 Christos Stathis
122 ab1eb3f8 Christos Stathis
                NativeEvent evt = preview.getNativeEvent();
123 ab1eb3f8 Christos Stathis
                if (evt.getType().equals("keydown"))
124 ab1eb3f8 Christos Stathis
                        // Use the popup's key preview hooks to close the dialog when either
125 ab1eb3f8 Christos Stathis
                        // enter or escape is pressed.
126 ab1eb3f8 Christos Stathis
                        switch (evt.getKeyCode()) {
127 ab1eb3f8 Christos Stathis
                                case KeyCodes.KEY_ENTER:
128 ab1eb3f8 Christos Stathis
                                        hide();
129 ab1eb3f8 Christos Stathis
                                        createGroup( groupName.getText());
130 ab1eb3f8 Christos Stathis
                                        break;
131 ab1eb3f8 Christos Stathis
                                case KeyCodes.KEY_ESCAPE:
132 ab1eb3f8 Christos Stathis
                                        hide();
133 ab1eb3f8 Christos Stathis
                                        break;
134 ab1eb3f8 Christos Stathis
                        }
135 ab1eb3f8 Christos Stathis
        }
136 ab1eb3f8 Christos Stathis
137 ab1eb3f8 Christos Stathis
138 ab1eb3f8 Christos Stathis
        /**
139 ab1eb3f8 Christos Stathis
         * Generate an RPC request to create a new group.
140 ab1eb3f8 Christos Stathis
         *
141 ab1eb3f8 Christos Stathis
         * @param userId the ID of the user whose namespace will be searched for
142 ab1eb3f8 Christos Stathis
         *            groups
143 ab1eb3f8 Christos Stathis
         * @param aGroupName the name of the group to create
144 ab1eb3f8 Christos Stathis
         */
145 ab1eb3f8 Christos Stathis
        private void createGroup(String aGroupName) {
146 ab1eb3f8 Christos Stathis
147 ab1eb3f8 Christos Stathis
                if (aGroupName == null || aGroupName.length() == 0) {
148 ab1eb3f8 Christos Stathis
                        GSS.get().displayError("Empty group name!");
149 ab1eb3f8 Christos Stathis
                        return;
150 ab1eb3f8 Christos Stathis
                }
151 ab1eb3f8 Christos Stathis
                GWT.log("createGroup(" + aGroupName + ")", null);
152 ab1eb3f8 Christos Stathis
                PostCommand cg = new PostCommand(GSS.get().getCurrentUserResource().getGroupsPath()+"?name="+URL.encodeComponent(aGroupName), "", 201){
153 ab1eb3f8 Christos Stathis
154 ab1eb3f8 Christos Stathis
                        @Override
155 ab1eb3f8 Christos Stathis
                        public void onComplete() {
156 ab1eb3f8 Christos Stathis
                                GSS.get().getGroups().updateGroups();
157 ab1eb3f8 Christos Stathis
                                GSS.get().showUserList();
158 ab1eb3f8 Christos Stathis
                        }
159 ab1eb3f8 Christos Stathis
160 ab1eb3f8 Christos Stathis
                        @Override
161 ab1eb3f8 Christos Stathis
                        public void onError(Throwable t) {
162 ab1eb3f8 Christos Stathis
                                GWT.log("", t);
163 ab1eb3f8 Christos Stathis
                                if(t instanceof RestException){
164 ab1eb3f8 Christos Stathis
                                        int statusCode = ((RestException)t).getHttpStatusCode();
165 ab1eb3f8 Christos Stathis
                                        if(statusCode == 405)
166 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("You don't have the necessary permissions");
167 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 404)
168 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Resource does not exist");
169 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 409)
170 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("A group with the same name already exists");
171 ab1eb3f8 Christos Stathis
                                        else if(statusCode == 413)
172 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Your quota has been exceeded");
173 ab1eb3f8 Christos Stathis
                                        else
174 ab1eb3f8 Christos Stathis
                                                GSS.get().displayError("Unable to create group:"+((RestException)t).getHttpStatusText());
175 ab1eb3f8 Christos Stathis
                                }
176 ab1eb3f8 Christos Stathis
                                else
177 ab1eb3f8 Christos Stathis
                                        GSS.get().displayError("System error creating group:"+t.getMessage());
178 ab1eb3f8 Christos Stathis
                        }
179 ab1eb3f8 Christos Stathis
                };
180 ab1eb3f8 Christos Stathis
                DeferredCommand.addCommand(cg);
181 ab1eb3f8 Christos Stathis
182 ab1eb3f8 Christos Stathis
        }
183 ab1eb3f8 Christos Stathis
}