Statistics
| Branch: | Tag: | Revision:

root / src / gr / grnet / pithos / web / client / GroupCreateDialog.java @ ee77dbf1

History | View | Annotate | Download (6.1 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 com.google.gwt.dom.client.NativeEvent;
38
import com.google.gwt.event.dom.client.ClickEvent;
39
import com.google.gwt.event.dom.client.ClickHandler;
40
import com.google.gwt.event.dom.client.KeyCodes;
41
import com.google.gwt.event.dom.client.KeyDownEvent;
42
import com.google.gwt.user.client.Command;
43
import com.google.gwt.user.client.Event.NativePreviewEvent;
44
import com.google.gwt.user.client.ui.*;
45
import gr.grnet.pithos.web.client.commands.AddUserCommand;
46
import gr.grnet.pithos.web.client.grouptree.Group;
47

    
48
/**
49
 * The 'Folder properties' dialog box implementation.
50
 */
51
public class GroupCreateDialog extends DialogBox {
52

    
53
    protected Pithos app;
54

    
55
    private Command callback;
56

    
57
    /**
58
     * The widget that holds the folderName of the folder.
59
     */
60
    TextBox groupNameTextBox = new TextBox();
61

    
62
    final VerticalPanel inner;
63

    
64
    public GroupCreateDialog(final Pithos app) {
65
        this(app, null);
66
    }
67

    
68
    /**
69
     * The widget's constructor.
70
     */
71
    public GroupCreateDialog(final Pithos app, Command callback) {
72
        this.app = app;
73
        this.callback = callback;
74

    
75
        Anchor close = new Anchor("close");
76
        close.addStyleName("close");
77
        close.addClickHandler(new ClickHandler() {
78

    
79
            @Override
80
            public void onClick(ClickEvent event) {
81
                hide();
82
            }
83
        });
84

    
85
        setGlassEnabled(true);
86
        setStyleName("pithos-DialogBox");
87

    
88
        // Enable IE selection for the dialog (must disable it upon closing it)
89
        Pithos.enableIESelection();
90

    
91
        // Use this opportunity to set the dialog's caption.
92
        setText("Create group");
93

    
94
        // Outer contains inner and buttons
95
        VerticalPanel outer = new VerticalPanel();
96
        outer.add(close);
97
        // Inner contains generalPanel and permPanel
98
        inner = new VerticalPanel();
99
        inner.addStyleName("inner");
100

    
101
        VerticalPanel generalPanel = new VerticalPanel();
102
        FlexTable generalTable = new FlexTable();
103
        generalTable.setText(0, 0, "Name");
104

    
105
        generalTable.setWidget(0, 1, groupNameTextBox);
106

    
107
        generalTable.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
108
        generalTable.getFlexCellFormatter().setStyleName(0, 1, "props-values");
109
        generalTable.setCellSpacing(4);
110
        generalPanel.add(generalTable);
111
        inner.add(generalPanel);
112

    
113
        outer.add(inner);
114

    
115
        // Create the 'Create/Update' button, along with a listener that hides the dialog
116
        // when the button is clicked and quits the application.
117
        String okLabel = "Create";
118
        final Button ok = new Button(okLabel, new ClickHandler() {
119
            @Override
120
            public void onClick(ClickEvent event) {
121
                createGroup();
122
                closeDialog();
123
            }
124
        });
125
        ok.addStyleName("button");
126
        outer.add(ok);
127
        outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER);
128

    
129
        setWidget(outer);
130
    }
131

    
132
    @Override
133
    public void center() {
134
        super.center();
135
        groupNameTextBox.setFocus(true);
136
    }
137

    
138
    @Override
139
    protected void onPreviewNativeEvent(NativePreviewEvent preview) {
140
        super.onPreviewNativeEvent(preview);
141

    
142
        NativeEvent evt = preview.getNativeEvent();
143
        if(evt.getType().equals(KeyDownEvent.getType().getName()))
144
        // Use the popup's key preview hooks to close the dialog when either
145
        // enter or escape is pressed.
146
        {
147
            switch(evt.getKeyCode()) {
148
                case KeyCodes.KEY_ENTER:
149
                    createGroup();
150
                    closeDialog();
151
                    break;
152
                case KeyCodes.KEY_ESCAPE:
153
                    closeDialog();
154
                    break;
155
            }
156
        }
157
    }
158

    
159

    
160
    /**
161
     * Enables IE selection prevention and hides the dialog
162
     * (we disable the prevention on creation of the dialog)
163
     */
164
    public void closeDialog() {
165
        Pithos.preventIESelection();
166
        hide();
167
        if(callback != null) {
168
            callback.execute();
169
        }
170
    }
171

    
172
    /**
173
     * Generate an RPC request to create a new folder.
174
     */
175
    void createGroup() {
176
        final String groupName = groupNameTextBox.getText().trim();
177
        if(groupName.length() == 0) {
178
            return;
179
        }
180
        Group group = app.addGroup(groupName);
181

    
182
        new AddUserCommand(app, null, group).execute();
183
    }
184
}