Revision cd2dd1fe src/gr/grnet/pithos/web/client/AddUserDialog.java

b/src/gr/grnet/pithos/web/client/AddUserDialog.java
34 34
 */
35 35
package gr.grnet.pithos.web.client;
36 36

  
37
import gr.grnet.pithos.web.client.grouptree.Group;
38
import gr.grnet.pithos.web.client.rest.PostRequest;
39
import gr.grnet.pithos.web.client.rest.RestException;
40

  
41 37
import com.google.gwt.core.client.GWT;
42 38
import com.google.gwt.core.client.Scheduler;
43 39
import com.google.gwt.dom.client.NativeEvent;
......
47 43
import com.google.gwt.event.dom.client.KeyDownEvent;
48 44
import com.google.gwt.http.client.Response;
49 45
import com.google.gwt.http.client.URL;
50
import com.google.gwt.regexp.shared.RegExp;
51 46
import com.google.gwt.user.client.Event.NativePreviewEvent;
52
import com.google.gwt.user.client.ui.Anchor;
53
import com.google.gwt.user.client.ui.Button;
54
import com.google.gwt.user.client.ui.DialogBox;
55
import com.google.gwt.user.client.ui.FlexTable;
56
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
57
import com.google.gwt.user.client.ui.TextBox;
58
import com.google.gwt.user.client.ui.VerticalPanel;
47
import com.google.gwt.user.client.ui.*;
48
import gr.grnet.pithos.web.client.grouptree.Group;
49
import gr.grnet.pithos.web.client.rest.PostRequest;
50
import gr.grnet.pithos.web.client.rest.RestException;
59 51

  
60 52
/**
61 53
 * The 'Folder properties' dialog box implementation.
......
65 57
    protected Pithos app;
66 58

  
67 59
    Group group;
68
	/**
69
	 * The widget that holds the folderName of the folder.
70
	 */
71
	TextBox userName = new TextBox();
60
    /**
61
     * The widget that holds the folderName of the folder.
62
     */
63
    TextBox userName = new TextBox();
72 64

  
73
	final VerticalPanel inner;
65
    final VerticalPanel inner;
74 66

  
75
	/**
76
	 * The widget's constructor.
77
	 */
78
	public AddUserDialog(final Pithos app, Group _group) {
67
    /**
68
     * The widget's constructor.
69
     */
70
    public AddUserDialog(final Pithos app, Group _group) {
79 71
        this.app = app;
80 72
        this.group = _group;
81
        
82
		Anchor close = new Anchor("close");
83
		close.addStyleName("close");
84
		close.addClickHandler(new ClickHandler() {
85
			
86
			@Override
87
			public void onClick(ClickEvent event) {
88
				hide();
89
			}
90
		});
91

  
92
		setGlassEnabled(true);
93
		setStyleName("pithos-DialogBox");
94

  
95
		// Enable IE selection for the dialog (must disable it upon closing it)
96
		Pithos.enableIESelection();
97

  
98
		// Use this opportunity to set the dialog's caption.
99
		setText("Add user");
100

  
101
		// Outer contains inner and buttons
102
		VerticalPanel outer = new VerticalPanel();
103
		outer.add(close);
104
		// Inner contains generalPanel and permPanel
105
		inner = new VerticalPanel();
106
		inner.addStyleName("inner");
107

  
108
		VerticalPanel generalPanel = new VerticalPanel();
73

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

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

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

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

  
90
        // Use this opportunity to set the dialog's caption.
91
        setText("Add user");
92

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

  
100
        VerticalPanel generalPanel = new VerticalPanel();
109 101
        FlexTable generalTable = new FlexTable();
110 102
        generalTable.setText(0, 0, "Username");
111 103

  
......
119 111

  
120 112
        outer.add(inner);
121 113

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

  
136 128
        setWidget(outer);
137
	}
138

  
139
	@Override
140
	public void center() {
141
		super.center();
142
		userName.setFocus(true);
143
	}
144

  
145
	@Override
146
	protected void onPreviewNativeEvent(NativePreviewEvent preview) {
147
		super.onPreviewNativeEvent(preview);
148

  
149
		NativeEvent evt = preview.getNativeEvent();
150
		if (evt.getType().equals(KeyDownEvent.getType().getName()))
151
			// Use the popup's key preview hooks to close the dialog when either
152
			// enter or escape is pressed.
153
			switch (evt.getKeyCode()) {
154
				case KeyCodes.KEY_ENTER:
155
					addUser();
129
    }
130

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

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

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

  
164

  
165
	/**
166
	 * Enables IE selection prevention and hides the dialog
167
	 * (we disable the prevention on creation of the dialog)
168
	 */
169
	public void closeDialog() {
170
		Pithos.preventIESelection();
171
		hide();
172
	}
173

  
174
	/**
175
	 * Generate an RPC request to create a new folder.
176
	 */
177
	void addUser() {
178
		String name = userName.getText().trim();
179
		if (name.length() == 0)
180
			return;
181
		RegExp emailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i");
182
		if (!emailValidator.test(name)) {
183
			app.displayWarning("Username must be a valid email address");
184
			return;
185
		}
186
			
187
    	group.addMember(name);
188
    	String path = "?update=";
189
    	PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUserID(), path) {
190
			
191
			@Override
192
			public void onSuccess(Resource result) {
193
				app.updateGroupNode(group);
194
			}
195
			
196
			@Override
197
			public void onError(Throwable t) {
198
				GWT.log("", t);
199
				app.setError(t);
200
				if (t instanceof RestException) {
201
					app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
202
				}
203
				else 
204
					app.displayError("System error updating group:" + t.getMessage());
205
			}
206

  
207
			@Override
208
			protected void onUnauthorized(Response response) {
209
				app.sessionExpired();
210
			}
211
		};
212
		updateGroup.setHeader("X-Auth-Token", app.getUserToken());
213
		String groupMembers = "";
214
		for (String u : group.getMembers())
215
			groupMembers += (URL.encodePathSegment(u) + ",");
216
		updateGroup.setHeader("X-Account-Group-" + URL.encodePathSegment(group.getName()), groupMembers);
217
		Scheduler.get().scheduleDeferred(updateGroup);
218
	}
153
                    break;
154
            }
155
        }
156
    }
157

  
158

  
159
    /**
160
     * Enables IE selection prevention and hides the dialog
161
     * (we disable the prevention on creation of the dialog)
162
     */
163
    public void closeDialog() {
164
        Pithos.preventIESelection();
165
        hide();
166
    }
167

  
168
    /**
169
     * Generate an RPC request to create a new folder.
170
     */
171
    void addUser() {
172
        String name = userName.getText().trim();
173
        if(name.length() == 0) {
174
            return;
175
        }
176
        if(!Const.EMAIL_REGEX.test(name)) {
177
            app.displayWarning("Username must be a valid email address");
178
            return;
179
        }
180

  
181
        group.addMember(name);
182
        String path = "?update=";
183
        PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUserID(), path) {
184

  
185
            @Override
186
            public void onSuccess(Resource result) {
187
                app.updateGroupNode(group);
188
            }
189

  
190
            @Override
191
            public void onError(Throwable t) {
192
                GWT.log("", t);
193
                app.setError(t);
194
                if(t instanceof RestException) {
195
                    app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText());
196
                }
197
                else {
198
                    app.displayError("System error updating group:" + t.getMessage());
199
                }
200
            }
201

  
202
            @Override
203
            protected void onUnauthorized(Response response) {
204
                app.sessionExpired();
205
            }
206
        };
207
        updateGroup.setHeader(Const.X_AUTH_TOKEN, app.getUserToken());
208
        String groupMembers = "";
209
        for(String u : group.getMembers()) {
210
            groupMembers += (URL.encodePathSegment(u) + ",");
211
        }
212
        updateGroup.setHeader(Const.X_ACCOUNT_GROUP_ + URL.encodePathSegment(group.getName()), groupMembers);
213
        Scheduler.get().scheduleDeferred(updateGroup);
214
    }
219 215
}

Also available in: Unified diff