From cd2dd1fe15d8b70e89909597232616c0db6367ea Mon Sep 17 00:00:00 2001 From: Christos KK Loverdos Date: Thu, 7 Feb 2013 13:54:09 +0200 Subject: [PATCH] Reformat AddUserDialog and extract constants --- src/gr/grnet/pithos/web/client/AddUserDialog.java | 280 ++++++++++----------- src/gr/grnet/pithos/web/client/Const.java | 4 + 2 files changed, 142 insertions(+), 142 deletions(-) diff --git a/src/gr/grnet/pithos/web/client/AddUserDialog.java b/src/gr/grnet/pithos/web/client/AddUserDialog.java index 00542c7..407dfe7 100644 --- a/src/gr/grnet/pithos/web/client/AddUserDialog.java +++ b/src/gr/grnet/pithos/web/client/AddUserDialog.java @@ -34,10 +34,6 @@ */ package gr.grnet.pithos.web.client; -import gr.grnet.pithos.web.client.grouptree.Group; -import gr.grnet.pithos.web.client.rest.PostRequest; -import gr.grnet.pithos.web.client.rest.RestException; - import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.Scheduler; import com.google.gwt.dom.client.NativeEvent; @@ -47,15 +43,11 @@ import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyDownEvent; import com.google.gwt.http.client.Response; import com.google.gwt.http.client.URL; -import com.google.gwt.regexp.shared.RegExp; import com.google.gwt.user.client.Event.NativePreviewEvent; -import com.google.gwt.user.client.ui.Anchor; -import com.google.gwt.user.client.ui.Button; -import com.google.gwt.user.client.ui.DialogBox; -import com.google.gwt.user.client.ui.FlexTable; -import com.google.gwt.user.client.ui.HasHorizontalAlignment; -import com.google.gwt.user.client.ui.TextBox; -import com.google.gwt.user.client.ui.VerticalPanel; +import com.google.gwt.user.client.ui.*; +import gr.grnet.pithos.web.client.grouptree.Group; +import gr.grnet.pithos.web.client.rest.PostRequest; +import gr.grnet.pithos.web.client.rest.RestException; /** * The 'Folder properties' dialog box implementation. @@ -65,47 +57,47 @@ public class AddUserDialog extends DialogBox { protected Pithos app; Group group; - /** - * The widget that holds the folderName of the folder. - */ - TextBox userName = new TextBox(); + /** + * The widget that holds the folderName of the folder. + */ + TextBox userName = new TextBox(); - final VerticalPanel inner; + final VerticalPanel inner; - /** - * The widget's constructor. - */ - public AddUserDialog(final Pithos app, Group _group) { + /** + * The widget's constructor. + */ + public AddUserDialog(final Pithos app, Group _group) { this.app = app; this.group = _group; - - Anchor close = new Anchor("close"); - close.addStyleName("close"); - close.addClickHandler(new ClickHandler() { - - @Override - public void onClick(ClickEvent event) { - hide(); - } - }); - - setGlassEnabled(true); - setStyleName("pithos-DialogBox"); - - // Enable IE selection for the dialog (must disable it upon closing it) - Pithos.enableIESelection(); - - // Use this opportunity to set the dialog's caption. - setText("Add user"); - - // Outer contains inner and buttons - VerticalPanel outer = new VerticalPanel(); - outer.add(close); - // Inner contains generalPanel and permPanel - inner = new VerticalPanel(); - inner.addStyleName("inner"); - - VerticalPanel generalPanel = new VerticalPanel(); + + Anchor close = new Anchor("close"); + close.addStyleName("close"); + close.addClickHandler(new ClickHandler() { + + @Override + public void onClick(ClickEvent event) { + hide(); + } + }); + + setGlassEnabled(true); + setStyleName("pithos-DialogBox"); + + // Enable IE selection for the dialog (must disable it upon closing it) + Pithos.enableIESelection(); + + // Use this opportunity to set the dialog's caption. + setText("Add user"); + + // Outer contains inner and buttons + VerticalPanel outer = new VerticalPanel(); + outer.add(close); + // Inner contains generalPanel and permPanel + inner = new VerticalPanel(); + inner.addStyleName("inner"); + + VerticalPanel generalPanel = new VerticalPanel(); FlexTable generalTable = new FlexTable(); generalTable.setText(0, 0, "Username"); @@ -119,101 +111,105 @@ public class AddUserDialog extends DialogBox { outer.add(inner); - // Create the 'Create/Update' button, along with a listener that hides the dialog - // when the button is clicked and quits the application. - String okLabel = "Create"; - final Button ok = new Button(okLabel, new ClickHandler() { - @Override - public void onClick(ClickEvent event) { - addUser(); - closeDialog(); - } - }); - ok.addStyleName("button"); - outer.add(ok); + // Create the 'Create/Update' button, along with a listener that hides the dialog + // when the button is clicked and quits the application. + String okLabel = "Create"; + final Button ok = new Button(okLabel, new ClickHandler() { + @Override + public void onClick(ClickEvent event) { + addUser(); + closeDialog(); + } + }); + ok.addStyleName("button"); + outer.add(ok); outer.setCellHorizontalAlignment(inner, HasHorizontalAlignment.ALIGN_CENTER); setWidget(outer); - } - - @Override - public void center() { - super.center(); - userName.setFocus(true); - } - - @Override - protected void onPreviewNativeEvent(NativePreviewEvent preview) { - super.onPreviewNativeEvent(preview); - - NativeEvent evt = preview.getNativeEvent(); - if (evt.getType().equals(KeyDownEvent.getType().getName())) - // Use the popup's key preview hooks to close the dialog when either - // enter or escape is pressed. - switch (evt.getKeyCode()) { - case KeyCodes.KEY_ENTER: - addUser(); + } + + @Override + public void center() { + super.center(); + userName.setFocus(true); + } + + @Override + protected void onPreviewNativeEvent(NativePreviewEvent preview) { + super.onPreviewNativeEvent(preview); + + NativeEvent evt = preview.getNativeEvent(); + if(evt.getType().equals(KeyDownEvent.getType().getName())) + // Use the popup's key preview hooks to close the dialog when either + // enter or escape is pressed. + { + switch(evt.getKeyCode()) { + case KeyCodes.KEY_ENTER: + addUser(); + closeDialog(); + break; + case KeyCodes.KEY_ESCAPE: closeDialog(); - break; - case KeyCodes.KEY_ESCAPE: - closeDialog(); - break; - } - } - - - /** - * Enables IE selection prevention and hides the dialog - * (we disable the prevention on creation of the dialog) - */ - public void closeDialog() { - Pithos.preventIESelection(); - hide(); - } - - /** - * Generate an RPC request to create a new folder. - */ - void addUser() { - String name = userName.getText().trim(); - if (name.length() == 0) - return; - RegExp emailValidator = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i"); - if (!emailValidator.test(name)) { - app.displayWarning("Username must be a valid email address"); - return; - } - - group.addMember(name); - String path = "?update="; - PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUserID(), path) { - - @Override - public void onSuccess(Resource result) { - app.updateGroupNode(group); - } - - @Override - public void onError(Throwable t) { - GWT.log("", t); - app.setError(t); - if (t instanceof RestException) { - app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText()); - } - else - app.displayError("System error updating group:" + t.getMessage()); - } - - @Override - protected void onUnauthorized(Response response) { - app.sessionExpired(); - } - }; - updateGroup.setHeader("X-Auth-Token", app.getUserToken()); - String groupMembers = ""; - for (String u : group.getMembers()) - groupMembers += (URL.encodePathSegment(u) + ","); - updateGroup.setHeader("X-Account-Group-" + URL.encodePathSegment(group.getName()), groupMembers); - Scheduler.get().scheduleDeferred(updateGroup); - } + break; + } + } + } + + + /** + * Enables IE selection prevention and hides the dialog + * (we disable the prevention on creation of the dialog) + */ + public void closeDialog() { + Pithos.preventIESelection(); + hide(); + } + + /** + * Generate an RPC request to create a new folder. + */ + void addUser() { + String name = userName.getText().trim(); + if(name.length() == 0) { + return; + } + if(!Const.EMAIL_REGEX.test(name)) { + app.displayWarning("Username must be a valid email address"); + return; + } + + group.addMember(name); + String path = "?update="; + PostRequest updateGroup = new PostRequest(app.getApiPath(), app.getUserID(), path) { + + @Override + public void onSuccess(Resource result) { + app.updateGroupNode(group); + } + + @Override + public void onError(Throwable t) { + GWT.log("", t); + app.setError(t); + if(t instanceof RestException) { + app.displayError("Unable to update group:" + ((RestException) t).getHttpStatusText()); + } + else { + app.displayError("System error updating group:" + t.getMessage()); + } + } + + @Override + protected void onUnauthorized(Response response) { + app.sessionExpired(); + } + }; + updateGroup.setHeader(Const.X_AUTH_TOKEN, app.getUserToken()); + String groupMembers = ""; + for(String u : group.getMembers()) { + groupMembers += (URL.encodePathSegment(u) + ","); + } + updateGroup.setHeader(Const.X_ACCOUNT_GROUP_ + URL.encodePathSegment(group.getName()), groupMembers); + Scheduler.get().scheduleDeferred(updateGroup); + } } diff --git a/src/gr/grnet/pithos/web/client/Const.java b/src/gr/grnet/pithos/web/client/Const.java index c50bced..f87dd3c 100644 --- a/src/gr/grnet/pithos/web/client/Const.java +++ b/src/gr/grnet/pithos/web/client/Const.java @@ -1,5 +1,7 @@ package gr.grnet.pithos.web.client; +import com.google.gwt.regexp.shared.RegExp; + /** * Gathers application-wide constants, like X-Auth-Token */ @@ -19,6 +21,8 @@ public class Const { public static final String X_MOVE_FROM = "X-Move-From"; public static final String HOME_CONTAINER = "pithos"; public static final String TRASH_CONTAINER = "trash"; + public static final RegExp EMAIL_REGEX = RegExp.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+[.][A-Z]{2,4}$", "i"); + public static final String X_ACCOUNT_GROUP_ = "X-Account-Group-"; private Const() {} -- 1.7.10.4