Updated Dialog Add URL: added check to prefix starting with a valid protocol

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@135028 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2016-11-29 11:02:44 +00:00
parent 9b1750ff8b
commit 7c1bfb252c
1 changed files with 88 additions and 84 deletions

View File

@ -14,11 +14,15 @@ import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Label;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* The Class DialogAddUrl.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Nov 29, 2016
*/
public class DialogAddUrl extends Dialog {
@ -27,8 +31,15 @@ public class DialogAddUrl extends Dialog {
private TextField<String> txtName;
private TextField<String> txtUrl = new TextField<String>();
private TextArea textAreaDescription = new TextArea();
private final String [] URL_PROTOCOL = {"http://","https://","ftp://"};
private Label labelError = new Label();
/**
* Instantiates a new dialog add url.
*
* @param headerTitle the header title
*/
public DialogAddUrl(String headerTitle) {
FormLayout layout = new FormLayout();
@ -36,10 +47,7 @@ public class DialogAddUrl extends Dialog {
layout.setDefaultWidth(300);
setLayout(layout);
this.setIcon(Resources.getIconAddUrl());
setButtonAlign(HorizontalAlignment.CENTER);
// setHideOnButtonClick(true);
// setIcon(IconHelper.createStyle("user"));
setHeading(ConstantsExplorer.MESSAGE_ADD_URL_IN + " "+ headerTitle);
setModal(true);
// setBodyBorder(true);
@ -47,40 +55,21 @@ public class DialogAddUrl extends Dialog {
setWidth(widthDialog);
setResizable(false);
setButtons(Dialog.OKCANCEL);
// this.getButtonById(Dialog.CANCEL).setText("Reset");
Label labetInfo = new Label();
// labetInfo.setText("If you want create an http/ftp uri, you must include one of this prefix: \"http://\" or \"ftp://\"");
// labetInfo.setText("An correct URL for document directly displayable through browser must have the following form - http://host.name/path");
// labetInfo.setText("An correct URL must have the following form http://host.name/path");
labetInfo.setText("A correct url (for a document displayable directly through the browser) has the following form - http://host.name/path");
labetInfo.setText("A valid URL has the following form - http|https|ftp://www.domain.com/path");
labetInfo.getElement().getStyle().setMarginBottom(10, Unit.PX);
labelError.getElement().getStyle().setColor("red");
showError("", false);
txtName = new TextField<String>();
txtName.setAllowBlank(false);
// txt.setMinLength(2);
txtName.setFieldLabel(ConstantsExplorer.DIALOG_NAME);
// txtName.setValue(msgTxt);
textAreaDescription.setFieldLabel(ConstantsExplorer.DIALOG_DESCRIPTION);
textAreaDescription.setHeight(heightTextArea);
// formData = new FormData("-20");
// vp = new VerticalPanel();
// vp.setSpacing(10);
// textAreaDescription.addKeyListener(new KeyListener() { // KEY ENTER
//
// public void componentKeyPress(ComponentEvent event) {
// if (event.getKeyCode() == KeyboardEvents.Enter.getEventCode())
// getButtonById(Dialog.OK).fireEvent(Events.OnClick);
//
// }
// });
txtName.addKeyListener(new KeyListener() { // KEY ENTER
public void componentKeyPress(ComponentEvent event) {
@ -90,17 +79,11 @@ public class DialogAddUrl extends Dialog {
}
});
txtUrl.setFieldLabel(ConstantsExplorer.DIALOG_URL);
txtUrl.setAllowBlank(false);
txtUrl.setEmptyText("http://host.name/path");
txtUrl.setEmptyText("E.g. http://www.domain.com/");
//txtUrl.setRegex("^http\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$");
// txtUrl.setRegex("^(ht|f)tp(s?)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");
//
//txtUrl.setRegex("^(ht|f)tp(s?)://");
// txtUrl.getMessages().setRegexText("The field should be a valid URL! Ex. http://www.domain.com");
// txtUrl.setAutoValidate(true);
@ -108,9 +91,7 @@ public class DialogAddUrl extends Dialog {
@Override
public void componentSelected(ButtonEvent ce) {
// txtUrl.reset();
// txtName.reset();
// textAreaDescription.reset();
showError("", false);
hide();
}
});
@ -120,33 +101,66 @@ public class DialogAddUrl extends Dialog {
@Override
public void componentSelected(ButtonEvent ce) {
if(isValidForm())
showError("", false);
if(isValidForm()){
hide();
}else {
String error = "URL must start with: ";
for (int i=0; i<URL_PROTOCOL.length-1; i++) {
error+=URL_PROTOCOL[i]+" or ";
}
error+=URL_PROTOCOL[URL_PROTOCOL.length-1];
showError(error, true);
txtUrl.forceInvalid(error);
}
}
});
add(labetInfo);
add(txtUrl);
add(labelError);
add(txtName);
add(textAreaDescription);
add(labetInfo);
setFocusWidget(txtName);
setFocusWidget(txtUrl);
this.show();
}
/**
* Show error.
*
* @param txt the txt
* @param bool the bool
*/
public void showError(String txt, boolean bool){
labelError.setVisible(bool);
labelError.setText(txt);
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return txtName.getValue();
}
/**
* Gets the url.
*
* @return the url
*/
public String getUrl() {
return txtUrl.getValue();
}
/**
* Gets the description.
*
* @return the description
*/
public String getDescription() {
if(textAreaDescription.getValue()!=null)
@ -156,29 +170,19 @@ public class DialogAddUrl extends Dialog {
return "";
}
/**
* Checks if is valid form.
*
* @return true, if is valid form
*/
public boolean isValidForm(){
if(txtName.getValue() != null)
if(txtName.getValue() != null && !txtName.getValue().isEmpty() && txtUrl.getValue()!=null && !txtUrl.getValue().isEmpty()){
for (String prefix : URL_PROTOCOL) {
if(txtUrl.getValue().startsWith(prefix))
return true;
}
}
return false;
}
// private boolean isValidUrl(){
//
// try {
// URL url = new URL(txtUrl.getValue());
// URLConnection conn = url.openConnection();
// conn.connect();
// } catch (MalformedURLException e) {
// System.out.println(ConstantsExplorer.ERRORURLNOTREACHABLE);
// return false;
// } catch (IOException e) {
// System.out.println(ConstantsExplorer.ERRORURLNOTREACHABLE);
// return false;
// }
//
// return true;
// }
}