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:
parent
9b1750ff8b
commit
7c1bfb252c
|
@ -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.TextArea;
|
||||||
import com.extjs.gxt.ui.client.widget.form.TextField;
|
import com.extjs.gxt.ui.client.widget.form.TextField;
|
||||||
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
|
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;
|
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 {
|
public class DialogAddUrl extends Dialog {
|
||||||
|
|
||||||
|
@ -27,8 +31,15 @@ public class DialogAddUrl extends Dialog {
|
||||||
private TextField<String> txtName;
|
private TextField<String> txtName;
|
||||||
private TextField<String> txtUrl = new TextField<String>();
|
private TextField<String> txtUrl = new TextField<String>();
|
||||||
private TextArea textAreaDescription = new TextArea();
|
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) {
|
public DialogAddUrl(String headerTitle) {
|
||||||
|
|
||||||
FormLayout layout = new FormLayout();
|
FormLayout layout = new FormLayout();
|
||||||
|
@ -36,10 +47,7 @@ public class DialogAddUrl extends Dialog {
|
||||||
layout.setDefaultWidth(300);
|
layout.setDefaultWidth(300);
|
||||||
setLayout(layout);
|
setLayout(layout);
|
||||||
this.setIcon(Resources.getIconAddUrl());
|
this.setIcon(Resources.getIconAddUrl());
|
||||||
|
|
||||||
setButtonAlign(HorizontalAlignment.CENTER);
|
setButtonAlign(HorizontalAlignment.CENTER);
|
||||||
// setHideOnButtonClick(true);
|
|
||||||
// setIcon(IconHelper.createStyle("user"));
|
|
||||||
setHeading(ConstantsExplorer.MESSAGE_ADD_URL_IN + " "+ headerTitle);
|
setHeading(ConstantsExplorer.MESSAGE_ADD_URL_IN + " "+ headerTitle);
|
||||||
setModal(true);
|
setModal(true);
|
||||||
// setBodyBorder(true);
|
// setBodyBorder(true);
|
||||||
|
@ -47,40 +55,21 @@ public class DialogAddUrl extends Dialog {
|
||||||
setWidth(widthDialog);
|
setWidth(widthDialog);
|
||||||
setResizable(false);
|
setResizable(false);
|
||||||
setButtons(Dialog.OKCANCEL);
|
setButtons(Dialog.OKCANCEL);
|
||||||
// this.getButtonById(Dialog.CANCEL).setText("Reset");
|
|
||||||
|
|
||||||
|
|
||||||
Label labetInfo = new Label();
|
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("A valid URL has the following form - http|https|ftp://www.domain.com/path");
|
||||||
// labetInfo.setText("An correct URL for document directly displayable through browser must have the following form - http://host.name/path");
|
labetInfo.getElement().getStyle().setMarginBottom(10, Unit.PX);
|
||||||
// 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");
|
|
||||||
|
|
||||||
|
|
||||||
|
labelError.getElement().getStyle().setColor("red");
|
||||||
|
showError("", false);
|
||||||
|
|
||||||
txtName = new TextField<String>();
|
txtName = new TextField<String>();
|
||||||
txtName.setAllowBlank(false);
|
txtName.setAllowBlank(false);
|
||||||
// txt.setMinLength(2);
|
// txt.setMinLength(2);
|
||||||
txtName.setFieldLabel(ConstantsExplorer.DIALOG_NAME);
|
txtName.setFieldLabel(ConstantsExplorer.DIALOG_NAME);
|
||||||
// txtName.setValue(msgTxt);
|
|
||||||
|
|
||||||
textAreaDescription.setFieldLabel(ConstantsExplorer.DIALOG_DESCRIPTION);
|
textAreaDescription.setFieldLabel(ConstantsExplorer.DIALOG_DESCRIPTION);
|
||||||
textAreaDescription.setHeight(heightTextArea);
|
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
|
txtName.addKeyListener(new KeyListener() { // KEY ENTER
|
||||||
|
|
||||||
public void componentKeyPress(ComponentEvent event) {
|
public void componentKeyPress(ComponentEvent event) {
|
||||||
|
@ -90,17 +79,11 @@ public class DialogAddUrl extends Dialog {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
txtUrl.setFieldLabel(ConstantsExplorer.DIALOG_URL);
|
txtUrl.setFieldLabel(ConstantsExplorer.DIALOG_URL);
|
||||||
txtUrl.setAllowBlank(false);
|
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("^http\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$");
|
||||||
|
//txtUrl.setRegex("^(ht|f)tp(s?)://");
|
||||||
|
|
||||||
// txtUrl.setRegex("^(ht|f)tp(s?)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$");
|
|
||||||
//
|
|
||||||
// txtUrl.getMessages().setRegexText("The field should be a valid URL! Ex. http://www.domain.com");
|
// txtUrl.getMessages().setRegexText("The field should be a valid URL! Ex. http://www.domain.com");
|
||||||
// txtUrl.setAutoValidate(true);
|
// txtUrl.setAutoValidate(true);
|
||||||
|
|
||||||
|
@ -108,9 +91,7 @@ public class DialogAddUrl extends Dialog {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void componentSelected(ButtonEvent ce) {
|
public void componentSelected(ButtonEvent ce) {
|
||||||
// txtUrl.reset();
|
showError("", false);
|
||||||
// txtName.reset();
|
|
||||||
// textAreaDescription.reset();
|
|
||||||
hide();
|
hide();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -120,33 +101,66 @@ public class DialogAddUrl extends Dialog {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void componentSelected(ButtonEvent ce) {
|
public void componentSelected(ButtonEvent ce) {
|
||||||
|
showError("", false);
|
||||||
if(isValidForm())
|
if(isValidForm()){
|
||||||
hide();
|
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(txtUrl);
|
||||||
|
add(labelError);
|
||||||
add(txtName);
|
add(txtName);
|
||||||
add(textAreaDescription);
|
add(textAreaDescription);
|
||||||
add(labetInfo);
|
setFocusWidget(txtUrl);
|
||||||
|
|
||||||
setFocusWidget(txtName);
|
|
||||||
this.show();
|
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() {
|
public String getName() {
|
||||||
return txtName.getValue();
|
return txtName.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the url.
|
||||||
|
*
|
||||||
|
* @return the url
|
||||||
|
*/
|
||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
return txtUrl.getValue();
|
return txtUrl.getValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the description.
|
||||||
|
*
|
||||||
|
* @return the description
|
||||||
|
*/
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
|
|
||||||
if(textAreaDescription.getValue()!=null)
|
if(textAreaDescription.getValue()!=null)
|
||||||
|
@ -156,29 +170,19 @@ public class DialogAddUrl extends Dialog {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if is valid form.
|
||||||
|
*
|
||||||
|
* @return true, if is valid form
|
||||||
|
*/
|
||||||
public boolean isValidForm(){
|
public boolean isValidForm(){
|
||||||
|
|
||||||
if(txtName.getValue() != null)
|
if(txtName.getValue() != null && !txtName.getValue().isEmpty() && txtUrl.getValue()!=null && !txtUrl.getValue().isEmpty()){
|
||||||
return true;
|
for (String prefix : URL_PROTOCOL) {
|
||||||
|
if(txtUrl.getValue().startsWith(prefix))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
return false;
|
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;
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue