Example usage for org.apache.wicket.validation.validator UrlValidator ALLOW_2_SLASHES

List of usage examples for org.apache.wicket.validation.validator UrlValidator ALLOW_2_SLASHES

Introduction

In this page you can find the example usage for org.apache.wicket.validation.validator UrlValidator ALLOW_2_SLASHES.

Prototype

int ALLOW_2_SLASHES

To view the source code for org.apache.wicket.validation.validator UrlValidator ALLOW_2_SLASHES.

Click Source Link

Document

Allow two slashes in the path component of the URL.

Usage

From source file:org.sakaiproject.profile2.tool.pages.panels.ChangeProfilePictureUrl.java

License:Educational Community License

/**
 * Does the actual rendering of the panel
 * @param userUuid// w w w  .j  a  va2 s .c om
 */
private void renderChangeProfilePictureUrl(final String userUuid) {

    //setup SimpleText object to back the single form field 
    StringModel stringModel = new StringModel();

    ProfilePreferences prefs = preferencesLogic.getPreferencesRecordForUser(userUuid);
    ProfilePrivacy privacy = privacyLogic.getPrivacyRecordForUser(userUuid);

    //do they already have a URL that should be loaded in here?
    ProfileImage profileImage = imageLogic.getProfileImage(userUuid, prefs, privacy,
            ProfileConstants.PROFILE_IMAGE_MAIN);

    //if its not blank AND it's not equalt to the default image url, show it
    String externalUrl = profileImage.getExternalImageUrl();
    if (StringUtils.isNotBlank(externalUrl)
            && !StringUtils.equals(externalUrl, imageLogic.getUnavailableImageURL())) {
        stringModel.setString(profileImage.getExternalImageUrl());
    }

    //setup form   
    Form form = new Form("form", new Model(stringModel));
    form.setOutputMarkupId(true);

    //add warning message if superUser and not editing own image
    Label editWarning = new Label("editWarning");
    editWarning.setVisible(false);
    if (sakaiProxy.isSuperUserAndProxiedToUser(userUuid)) {
        editWarning.setDefaultModel(new StringResourceModel("text.edit.other.warning", null,
                new Object[] { sakaiProxy.getUserDisplayName(userUuid) }));
        editWarning.setEscapeModelStrings(false);
        editWarning.setVisible(true);
    }
    form.add(editWarning);

    //close button component
    CloseButton closeButton = new CloseButton("closeButton", this);
    closeButton.setOutputMarkupId(true);
    form.add(closeButton);

    //text
    Label textEnterUrl = new Label("textEnterUrl", new ResourceModel("text.image.url"));
    form.add(textEnterUrl);

    //upload
    TextField urlField = new TextField("urlField", new PropertyModel(stringModel, "string"));
    urlField.setMarkupId("pictureurl");
    urlField.setOutputMarkupId(true);
    urlField.setRequired(true);
    urlField.add(new UrlValidator(new String[] { "http", "https" }, UrlValidator.ALLOW_2_SLASHES));
    form.add(urlField);

    //feedback (styled to remove the list)
    final FeedbackPanel feedback = new FeedbackPanel("feedback");
    feedback.setOutputMarkupId(true);
    form.add(feedback);

    //submit button
    IndicatingAjaxButton submitButton = new IndicatingAjaxButton("submit", form) {

        protected void onSubmit(AjaxRequestTarget target, Form form) {

            //get the model (already validated)
            StringModel stringModel = (StringModel) form.getModelObject();

            //get the url
            String url = stringModel.getString();

            //save via ProfileImageService
            if (imageLogic.setExternalProfileImage(userUuid, url, null, null)) {
                //log it
                log.info("User " + userUuid + " successfully changed profile picture by url.");

                //post update event
                sakaiProxy.postEvent(ProfileConstants.EVENT_PROFILE_IMAGE_CHANGE_URL, "/profile/" + userUuid,
                        true);

                if (true == sakaiProxy.isWallEnabledGlobally()
                        && false == sakaiProxy.isSuperUserAndProxiedToUser(userUuid)) {
                    wallLogic.addNewEventToWall(ProfileConstants.EVENT_PROFILE_IMAGE_CHANGE_URL,
                            sakaiProxy.getCurrentUserId());
                }

                //refresh image data
                if (sakaiProxy.isSuperUserAndProxiedToUser(userUuid)) {
                    setResponsePage(new MyProfile(userUuid));
                } else {
                    setResponsePage(new MyProfile());
                }
            } else {
                error(new StringResourceModel("error.url.save.failed", this, null).getString());
                return;
            }

        };

        // update feedback panel if validation failed
        protected void onError(AjaxRequestTarget target, Form form) {
            log.debug("ChangeProfilePictureUrl.onSubmit validation failed.");
            target.add(feedback);
        }

    };
    submitButton.setModel(new ResourceModel("button.url.add"));
    form.add(submitButton);

    //add form to page
    add(form);
}