From 7c1bfb252c38f7134f2048851091b42415fc4de0 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Tue, 29 Nov 2016 11:02:44 +0000 Subject: [PATCH] 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 --- .../client/view/windows/DialogAddUrl.java | 172 +++++++++--------- 1 file changed, 88 insertions(+), 84 deletions(-) diff --git a/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogAddUrl.java b/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogAddUrl.java index e203588..2074c49 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogAddUrl.java +++ b/src/main/java/org/gcube/portlets/user/workspace/client/view/windows/DialogAddUrl.java @@ -14,32 +14,40 @@ 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 { - + private int widthDialog = 500; private int heightTextArea = 150; private TextField txtName; private TextField txtUrl = new TextField(); 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(); layout.setLabelWidth(90); 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,60 +55,35 @@ 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(); 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) { - if (event.getKeyCode() == KeyboardEvents.Enter.getEventCode()) + if (event.getKeyCode() == KeyboardEvents.Enter.getEventCode()) getButtonById(Dialog.OK).fireEvent(Events.Select); - + } }); - - - - + txtUrl.setFieldLabel(ConstantsExplorer.DIALOG_URL); txtUrl.setAllowBlank(false); - txtUrl.setEmptyText("http://host.name/path"); -// txtUrl.setRegex("^http\\://[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$"); - - -// txtUrl.setRegex("^(ht|f)tp(s?)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$"); -// + 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?)://"); // txtUrl.getMessages().setRegexText("The field should be a valid URL! Ex. http://www.domain.com"); // txtUrl.setAutoValidate(true); @@ -108,77 +91,98 @@ public class DialogAddUrl extends Dialog { @Override public void componentSelected(ButtonEvent ce) { -// txtUrl.reset(); -// txtName.reset(); -// textAreaDescription.reset(); + showError("", false); hide(); } }); - - + + this.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener() { @Override public void componentSelected(ButtonEvent ce) { - - if(isValidForm()) + showError("", false); + if(isValidForm()){ hide(); + }else { + String error = "URL must start with: "; + for (int i=0; i0) return textAreaDescription.getValue(); - + return ""; } - + + /** + * Checks if is valid form. + * + * @return true, if is valid form + */ public boolean isValidForm(){ - - if(txtName.getValue() != null) - return true; - + + 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; -// } }