Example usage for org.apache.wicket.markup.html.basic Label replaceWith

List of usage examples for org.apache.wicket.markup.html.basic Label replaceWith

Introduction

In this page you can find the example usage for org.apache.wicket.markup.html.basic Label replaceWith.

Prototype

public Component replaceWith(Component replacement) 

Source Link

Document

Replaces this component with another.

Usage

From source file:com.tysanclan.site.projectewok.components.ProfilePanel.java

License:Open Source License

/**
 * /*from   w  ww . ja v a 2s .c o  m*/
 */
public ProfilePanel(String id, User user) {
    super(id);

    PageParameters params = new PageParameters();
    params.add("userid", user.getId().toString());

    add(new BookmarkablePageLink<User>("profilelink", MemberPage.class, params));

    Form<User> profileForm = new Form<User>("profile", ModelMaker.wrap(user)) {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;

        @SpringBean
        private ProfileService profileService;

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        @SuppressWarnings("unchecked")
        @Override
        protected void onSubmit() {
            User u = getModelObject();
            Profile profile = u.getProfile();

            TextField<String> realnameField = (TextField<String>) get("realname");
            TextField<String> photoURLField = (TextField<String>) get("photoURL");
            TextField<String> skypeField = (TextField<String>) get("skypename");
            TextField<String> twitterField = (TextField<String>) get("twitter");

            CheckBox photoPublicCheckbox = (CheckBox) get("public");
            CheckBox skypePublicBox = (CheckBox) get("skypepublic");
            TextArea<String> publicdescField = (TextArea<String>) get("publicdesc");
            TextArea<String> privatedescField = (TextArea<String>) get("privatedesc");

            String realname = realnameField.getModelObject();
            Date birthDate = getSelectedDate();
            String photoURL = photoURLField.getModelObject();
            Boolean photoPublic = photoPublicCheckbox.getModelObject();
            Boolean skypePublic = skypePublicBox.getModelObject();
            String publicdesc = publicdescField.getModelObject();
            String privatedesc = privatedescField.getModelObject();
            String aimName = skypeField.getModelObject();
            String twitter = twitterField.getModelObject();

            if (profile == null) {
                profile = profileService.createProfile(u);
            }

            if (!isBothNullOrEquals(realname, profile.getRealName())) {
                profileService.setRealname(profile, realname);
            }
            if (!isBothNullOrEquals(birthDate, profile.getBirthDate())) {
                profileService.setBirthDate(profile, birthDate);
            }
            if (!isBothNullOrEquals(twitter, profile.getTwitterUID())) {
                profileService.setTwitterUID(profile, twitter);
            }

            if (!isBothNullOrEquals(aimName, profile.getInstantMessengerAddress())
                    || !isBothNullOrEquals(skypePublic, profile.isInstantMessengerPublic())) {
                profileService.setAIMAddress(profile, aimName, skypePublic);
            }

            if (!isBothNullOrEquals(photoURL, profile.getPhotoURL())
                    || !isBothNullOrEquals(photoPublic, profile.isPhotoPublic())) {
                profileService.setPhotoURL(profile, photoURL, photoPublic);
            }
            if (!isBothNullOrEquals(publicdesc, profile.getPublicDescription())) {
                profileService.setPublicDescription(profile, publicdesc);
            }
            if (!isBothNullOrEquals(privatedesc, profile.getPrivateDescription())) {
                profileService.setPrivateDescription(profile, privatedesc);
            }

            ProfilePanel.this.onUpdated();
        }

        public <T> boolean isBothNullOrEquals(T value1, T value2) {
            if (value1 == null && value2 == null) {
                return true;
            }

            if (value1 == null)
                return false;
            if (value2 == null)
                return false;

            return value1.equals(value2);
        }
    };

    Profile profile = user.getProfile();

    profileForm.add(
            new TextField<String>("realname", new Model<String>(profile != null ? profile.getRealName() : "")));

    Calendar cal = DateUtil.getCalendarInstance();
    cal.add(Calendar.YEAR, -13);
    int year = cal.get(Calendar.YEAR);

    if (profile != null) {
        setSelectedDate(profile.getBirthDate());
    }

    profileForm.add(new InlineDatePicker("birthdate", profile != null ? profile.getBirthDate() : null) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void onDateSelected(Date date, AjaxRequestTarget target) {
            Label oldAge = getAge();
            Label newAge = new Label("age", getAgeModel(date));
            newAge.setOutputMarkupId(true);
            newAge.setOutputMarkupPlaceholderTag(true);
            oldAge.replaceWith(newAge);
            setAge(newAge);

            setSelectedDate(date);

            if (target != null) {
                target.add(newAge);
            }

        }
    }.setChangeMonth(true).setChangeYear(true).setYearRange("'1900:" + year + "'"));

    profileForm.add(new TextField<String>("twitter",
            new Model<String>(profile != null ? profile.getTwitterUID() : "")));

    TextField<String> photoURLField = new TextField<String>("photoURL",
            new Model<String>(profile != null ? profile.getPhotoURL() : ""));
    photoURLField.setOutputMarkupId(true);
    photoURLField.setOutputMarkupPlaceholderTag(true);

    photoURLField.add(new OnChangeAjaxBehavior() {
        private static final long serialVersionUID = 1L;

        /**
         * @see org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior#onUpdate(org.apache.wicket.ajax.AjaxRequestTarget)
         */
        @SuppressWarnings("unchecked")
        @Override
        protected void onUpdate(AjaxRequestTarget target) {
            TextField<String> urlComponent = (TextField<String>) getComponent();

            String newURL = urlComponent.getModelObject();

            WebMarkupContainer container = getImage();

            if (newURL == null || newURL.isEmpty()) {
                container.setVisible(false);

            } else {
                container.add(AttributeModifier.replace("src", newURL));
                container.setVisible(true);
            }
            if (target != null) {
                target.add(getImage());
            }

        }
    });

    String currentPhotoURL = profile != null ? profile.getPhotoURL() : null;

    image = new WebMarkupContainer("preview");
    if (currentPhotoURL == null || currentPhotoURL.isEmpty()) {
        image.setVisible(false);
    } else {
        image.add(AttributeModifier.replace("src", currentPhotoURL));
    }

    image.setOutputMarkupId(true);
    image.setOutputMarkupPlaceholderTag(true);

    age = new Label("age", getAgeModel(profile));
    age.setOutputMarkupId(true);
    age.setOutputMarkupPlaceholderTag(true);

    profileForm.add(age);

    profileForm.add(image);
    profileForm
            .add(new CheckBox("public", new Model<Boolean>(profile != null ? profile.isPhotoPublic() : false)));
    profileForm.add(new ContextImage("skypeicon", "images/skype-icon.gif"));
    profileForm.add(new CheckBox("skypepublic",
            new Model<Boolean>(profile != null ? profile.isInstantMessengerPublic() : false)));
    profileForm.add(new TextField<String>("skypename",
            new Model<String>(profile != null ? profile.getInstantMessengerAddress() : "")));

    profileForm.add(new BBCodeTextArea("publicdesc", profile != null ? profile.getPublicDescription() : ""));
    profileForm.add(new BBCodeTextArea("privatedesc", profile != null ? profile.getPrivateDescription() : ""));

    profileForm.add(photoURLField);

    add(profileForm);

}

From source file:com.tysanclan.site.projectewok.pages.member.AbstractManualElectionPage.java

License:Open Source License

/**
 * /*  w w  w . j a v  a 2s .co m*/
 */
public AbstractManualElectionPage(String title, Election election) {
    super(title);

    add(new ContextImage("chrome", "images/browser/chrome.jpg"));
    add(new ContextImage("firefox", "images/browser/firefox.png"));
    add(new ContextImage("opera", "images/browser/opera.jpg"));

    final boolean isNominationOpen = election.isNominationOpen();
    final int totalSize = election.getCandidates().size();

    add(new Label("label", isNominationOpen ? "Candidates" : "Cast your vote!"));

    preferences = new LinkedList<Long>();

    Form<Election> voteForm = new Form<Election>("vote", ModelMaker.wrap(election)) {

        private static final long serialVersionUID = 1L;

        @SpringBean
        private UserDAO userDAO;

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        @SuppressWarnings("unchecked")
        @Override
        protected void onSubmit() {
            DropDownChoice<User> userChoice = (DropDownChoice<User>) get("candidate");
            Label positionLabel = (Label) get("position");

            preferences.add(userChoice.getModelObject().getId());

            if (preferences.size() == totalSize) {
                List<User> users = new LinkedList<User>();

                for (Long id : preferences) {
                    users.add(userDAO.load(id));
                }

                onVoteSubmit(users);
            } else {
                List<User> remaining = new LinkedList<User>();
                remaining.addAll(userChoice.getChoices());
                remaining.remove(userChoice.getModelObject());

                positionLabel.replaceWith(new Label("position", new Model<Integer>(preferences.size() + 1)));

                userChoice.setChoices(ModelMaker.wrapChoices(remaining));
                userChoice.setModel(ModelMaker.wrap((User) null, true));
            }

        }

    };

    List<User> candidates = new LinkedList<User>();
    candidates.addAll(election.getCandidates());
    Collections.sort(candidates, new Comparator<User>() {
        /**
         * @see java.util.Comparator#compare(java.lang.Object,
         *      java.lang.Object)
         */
        @Override
        public int compare(User o1, User o2) {
            return o1.getUsername().compareToIgnoreCase(o2.getUsername());
        }
    });

    voteForm.add(new Label("position", new Model<Integer>(1)));

    voteForm.add(new DropDownChoice<User>("candidate", ModelMaker.wrap((User) null, true),
            ModelMaker.wrapChoices(candidates)));

    add(voteForm);
}

From source file:com.tysanclan.site.projectewok.pages.member.senate.ModifyRegulationPage.java

License:Open Source License

/**
*///from w w  w  .ja v a 2 s  .  c  o  m
private Form<RegulationChange> createModifyForm() {
    Form<RegulationChange> form = new Form<RegulationChange>("editForm") {
        private static final long serialVersionUID = 1L;

        @SpringBean
        private DemocracyService democracyService;

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        @SuppressWarnings("unchecked")
        @Override
        protected void onSubmit() {
            TextArea<String> descriptionArea = (TextArea<String>) get("description");
            TextField<String> newTitleField = (TextField<String>) get("newTitle");
            DropDownChoice<Regulation> regulationChoice = (DropDownChoice<Regulation>) get("regulation");

            String newDescription = descriptionArea.getModelObject();
            String newTitle = newTitleField.getModelObject();
            Regulation regulation = regulationChoice.getModelObject();

            RegulationChange vote = democracyService.createModifyRegulationVote(getUser(), regulation, newTitle,
                    newDescription);
            if (vote != null) {
                if (getUser().getRank() == Rank.SENATOR) {
                    setResponsePage(new RegulationModificationPage());
                } else {
                    setResponsePage(new VetoPage());
                }
            }

        }

    };

    form.add(new TextField<String>("newTitle", new Model<String>("")));

    form.add(new BBCodeTextArea("description", "").setRequired(true));

    form.add(new Label("example", new Model<String>("")).setEscapeModelStrings(false).setVisible(false)
            .setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true));

    form.add(new DropDownChoice<Regulation>("regulation", ModelMaker.wrap((Regulation) null, true),
            ModelMaker.wrapChoices(regulationDAO.findAll()), new IChoiceRenderer<Regulation>() {
                private static final long serialVersionUID = 1L;

                /**
                 * @see org.apache.wicket.markup.html.form.IChoiceRenderer#getDisplayValue(java.lang.Object)
                 */
                @Override
                public Object getDisplayValue(Regulation object) {
                    return object.getName();
                }

                /**
                 * @see org.apache.wicket.markup.html.form.IChoiceRenderer#getIdValue(java.lang.Object,
                 *      int)
                 */
                @Override
                public String getIdValue(Regulation object, int index) {
                    return object.getId().toString();
                }
            }).setNullValid(false).add(new AjaxFormComponentUpdatingBehavior("onchange") {
                private static final long serialVersionUID = 1L;

                @SuppressWarnings("unchecked")
                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    System.out.println("FOO!!");
                    Form<Regulation> regForm = (Form<Regulation>) getComponent().getParent();
                    Label example = (Label) regForm.get("example");

                    DropDownChoice<Regulation> regulationChoice = (DropDownChoice<Regulation>) regForm
                            .get("regulation");
                    Regulation regulation = regulationChoice.getModelObject();

                    if (regulation != null) {

                        Component example2 = new Label("example", new Model<String>(regulation.getContents()))
                                .setEscapeModelStrings(false).setVisible(true).setOutputMarkupId(true)
                                .setOutputMarkupPlaceholderTag(true);

                        example.replaceWith(example2);

                        if (target != null) {
                            target.add(example2);
                        }
                    } else {
                        example.setVisible(false);
                        if (target != null) {
                            target.add(example);
                        }
                    }
                }

            }));

    return form;
}

From source file:com.tysanclan.site.projectewok.pages.member.senate.RepealRegulationPage.java

License:Open Source License

/**
*///from w  w w .  java 2 s.c o m
private Form<RegulationChange> createRepealForm() {
    Form<RegulationChange> form = new Form<RegulationChange>("repealForm") {
        private static final long serialVersionUID = 1L;

        @SpringBean
        private DemocracyService democracyService;

        /**
         * @see org.apache.wicket.markup.html.form.Form#onSubmit()
         */
        @SuppressWarnings("unchecked")
        @Override
        protected void onSubmit() {
            DropDownChoice<Regulation> regulationChoice = (DropDownChoice<Regulation>) get("regulation");

            Regulation regulation = regulationChoice.getModelObject();

            RegulationChange vote = democracyService.createRepealRegulationVote(getUser(), regulation);
            if (vote != null) {
                if (getUser().getRank() == Rank.SENATOR) {
                    setResponsePage(new RegulationModificationPage());
                } else {
                    setResponsePage(new VetoPage());
                }
            }

        }

    };

    RegulationChangeFilter filter = new RegulationChangeFilter();
    filter.setUser(getUser());

    if (regulationChangeDAO.countByFilter(filter) > 0) {
        error("You have already submitted a regulation change proposal. You can not submit more than 1 simultaneously.");
        form.setEnabled(false);
    }

    form.add(new Label("example", new Model<String>("")).setEscapeModelStrings(false).setVisible(false)
            .setOutputMarkupId(true).setOutputMarkupPlaceholderTag(true));

    form.add(new DropDownChoice<Regulation>("regulation", ModelMaker.wrap((Regulation) null, true),
            ModelMaker.wrapChoices(regulationDAO.findAll()), new IChoiceRenderer<Regulation>() {
                private static final long serialVersionUID = 1L;

                /**
                 * @see org.apache.wicket.markup.html.form.IChoiceRenderer#getDisplayValue(java.lang.Object)
                 */
                @Override
                public Object getDisplayValue(Regulation object) {
                    return object.getName();
                }

                /**
                 * @see org.apache.wicket.markup.html.form.IChoiceRenderer#getIdValue(java.lang.Object,
                 *      int)
                 */
                @Override
                public String getIdValue(Regulation object, int index) {
                    return object.getId().toString();
                }
            }).setNullValid(false).add(new AjaxFormComponentUpdatingBehavior("onchange") {
                private static final long serialVersionUID = 1L;

                @SuppressWarnings("unchecked")
                @Override
                protected void onUpdate(AjaxRequestTarget target) {
                    Form<Regulation> regForm = (Form<Regulation>) getComponent().getParent();
                    Label example = (Label) regForm.get("example");

                    DropDownChoice<Regulation> regulationChoice = (DropDownChoice<Regulation>) regForm
                            .get("regulation");
                    Regulation regulation = regulationChoice.getModelObject();

                    if (regulation != null) {

                        Component example2 = new Label("example", new Model<String>(regulation.getContents()))
                                .setEscapeModelStrings(false).setVisible(true).setOutputMarkupId(true)
                                .setOutputMarkupPlaceholderTag(true);

                        example.replaceWith(example2);

                        if (target != null) {
                            target.add(example2);
                        }
                    } else {
                        example.setVisible(false);
                        if (target != null) {
                            target.add(example);
                        }
                    }
                }

            }));

    return form;
}

From source file:org.artifactory.webapp.wicket.page.security.user.UsersTable.java

License:Open Source License

private void createExternalUserComponent(final Item<ICellPopulator<UserModel>> item, final String componentId,
        final UserModel user) {
    // Create "action label" which allows to check the user status in remote server, by clicking the "action label".
    log.debug("User '{}' is from realm '{}'", user.getUsername(), user.getRealm());
    if (user.getStatus() == null) {
        final Model<String> m = Model.of("Check external status");
        final Label actionLabel = new Label(componentId, m);
        actionLabel.add(new CssClass("item-link"));
        item.add(actionLabel);/*from  www.j a v a 2 s.  c  o m*/
        item.add(new AjaxEventBehavior("onClick") {
            @Override
            protected void onEvent(final AjaxRequestTarget target) {

                log.debug("User '{}' is from realm '{}'", user.getUsername(), user.getRealm());
                Label statusLabel = createStatusComponent(user, componentId);
                actionLabel.replaceWith(statusLabel);
                target.add(item);
                Set<UserGroupInfo> userGroups = user.getGroups();
                provider.addExternalGroups(user.getUsername(), user.getRealm(), userGroups);
                user.addGroups(userGroups);
                target.add(UsersTable.this);

            }
        });
        // TODO find better way to implement te following code.
        // The following code (LinksColumn.current.hide()) hides the row's link panel (edit delete permissions panel).
        // Note: refreshing the table without hiding the link will cause the link panel to stay stuck on the screen
        item.add(new JavascriptEvent("onmousedown", "LinksColumn.current.hide();"));
    } else {
        Label statusLabel = createStatusComponent(user, componentId);
        item.add(statusLabel);
    }
}