Search in sources :

Example 1 with FormComponent

use of org.apache.wicket.markup.html.form.FormComponent in project wicket by apache.

the class FormGroup method onInitialize.

@Override
protected void onInitialize() {
    super.onInitialize();
    this.label = newLabel("label", labelModel);
    this.help = newHelpLabel("help", helpModel);
    this.feedback = newFeedbackMessageContainer("error");
    addToBorder(this.label, this.help, this.feedback);
    final List<FormComponent<?>> formComponents = findFormComponents();
    final int size = formComponents.size();
    if (size > 0) {
        addOutputMarkupId(formComponents);
        final FormComponent<?> formComponent = formComponents.get(size - 1);
        label.add(new AttributeModifier("for", formComponent.getMarkupId()));
        final boolean useFormComponentLabel = true;
        if (useFormComponentLabel) {
            label.setDefaultModel(new LoadableDetachableModel<String>() {

                @Override
                protected String load() {
                    if (formComponent.getLabel() != null && !Strings.isEmpty(formComponent.getLabel().getObject())) {
                        return formComponent.getLabel().getObject();
                    } else {
                        String text = formComponent.getDefaultLabel("wicket:unknown");
                        if (!"wicket:unknown".equals(text) && !Strings.isEmpty(text)) {
                            return text;
                        } else {
                            return labelModel.getObject();
                        }
                    }
                }
            });
        }
    }
}
Also used : FormComponent(org.apache.wicket.markup.html.form.FormComponent) AttributeModifier(org.apache.wicket.AttributeModifier)

Example 2 with FormComponent

use of org.apache.wicket.markup.html.form.FormComponent in project wicket by apache.

the class FormTester method select.

/**
 * Simulates selecting an option of a <code>FormComponent</code>. Supports
 * <code>RadioGroup</code>, <code>CheckGroup</code>, and <code>AbstractChoice</code> family
 * currently. The behavior is similar to interacting on the browser: For a single choice, such
 * as <code>Radio</code> or <code>DropDownList</code>, the selection will toggle each other. For
 * multiple choice, such as <code>Checkbox</code> or <code>ListMultipleChoice</code>, the
 * selection will accumulate.
 *
 * @param formComponentId
 *            relative path (from <code>Form</code>) to the selectable
 *            <code>FormComponent</code>
 * @param index
 *            index of the selectable option, starting from 0
 * @return This
 */
public FormTester select(final String formComponentId, int index) {
    checkClosed();
    FormComponent<?> component = (FormComponent<?>) workingForm.get(formComponentId);
    ChoiceSelector choiceSelector = choiceSelectorFactory.create(component);
    choiceSelector.doSelect(index);
    for (FormComponentUpdatingBehavior updater : component.getBehaviors(FormComponentUpdatingBehavior.class)) {
        tester.invokeListener(component, updater);
    }
    return this;
}
Also used : FormComponent(org.apache.wicket.markup.html.form.FormComponent) FormComponentUpdatingBehavior(org.apache.wicket.markup.html.form.FormComponentUpdatingBehavior)

Example 3 with FormComponent

use of org.apache.wicket.markup.html.form.FormComponent in project wicket by apache.

the class FormTester method setFile.

/**
 * Sets the <code>File</code> on a {@link FileUploadField}.
 *
 * @param formComponentId
 *            relative path (from <code>Form</code>) to the selectable
 *            <code>FormComponent</code>. The <code>FormComponent</code> must be of a type
 *            <code>FileUploadField</code>.
 * @param file
 *            the <code>File</code> to upload or {@code null} for an empty input
 * @param contentType
 *            the content type of the file. Must be a valid mime type.
 * @return This
 */
public FormTester setFile(final String formComponentId, final File file, final String contentType) {
    checkClosed();
    FormComponent<?> formComponent = (FormComponent<?>) workingForm.get(formComponentId);
    MockHttpServletRequest servletRequest = tester.getRequest();
    if (formComponent instanceof FileUploadField) {
        servletRequest.addFile(formComponent.getInputName(), file, contentType);
    } else if (formComponent instanceof MultiFileUploadField) {
        String inputName = formComponent.getInputName() + MultiFileUploadField.MAGIC_SEPARATOR + multiFileUploadIndex++;
        servletRequest.addFile(inputName, file, contentType);
    } else {
        fail("'" + formComponentId + "' is not " + "a FileUploadField. You can only attach a file to form " + "component of this type.");
    }
    return this;
}
Also used : FormComponent(org.apache.wicket.markup.html.form.FormComponent) MultiFileUploadField(org.apache.wicket.markup.html.form.upload.MultiFileUploadField) MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) MultiFileUploadField(org.apache.wicket.markup.html.form.upload.MultiFileUploadField) FileUploadField(org.apache.wicket.markup.html.form.upload.FileUploadField)

Example 4 with FormComponent

use of org.apache.wicket.markup.html.form.FormComponent in project wicket by apache.

the class Component method initModel.

/**
 * Called when a null model is about to be retrieved in order to allow a subclass to provide an
 * initial model.
 * <p>
 * By default this implementation looks components in the parent chain owning a
 * {@link IComponentInheritedModel} to provide a model for this component via
 * {@link IComponentInheritedModel#wrapOnInheritance(Component)}.
 * <p>
 * For example a {@link FormComponent} has the opportunity to instantiate a model on the fly
 * using its {@code id} and the containing {@link Form}'s model, if the form holds a
 * {@link CompoundPropertyModel}.
 *
 * @return The model
 */
protected IModel<?> initModel() {
    IModel<?> foundModel = null;
    // Search parents for IComponentInheritedModel (i.e. CompoundPropertyModel)
    for (Component current = getParent(); current != null; current = current.getParent()) {
        // Get model
        // Don't call the getModel() that could initialize many in between
        // completely useless models.
        // IModel model = current.getDefaultModel();
        IModel<?> model = current.getModelImpl();
        if (model instanceof IWrapModel && !(model instanceof IComponentInheritedModel)) {
            model = ((IWrapModel<?>) model).getWrappedModel();
        }
        if (model instanceof IComponentInheritedModel) {
            // return the shared inherited
            foundModel = ((IComponentInheritedModel<?>) model).wrapOnInheritance(this);
            setFlag(FLAG_INHERITABLE_MODEL, true);
            break;
        }
    }
    // No model for this component!
    return foundModel;
}
Also used : IWrapModel(org.apache.wicket.model.IWrapModel) IRequestableComponent(org.apache.wicket.request.component.IRequestableComponent) FormComponent(org.apache.wicket.markup.html.form.FormComponent) IComponentInheritedModel(org.apache.wicket.model.IComponentInheritedModel)

Example 5 with FormComponent

use of org.apache.wicket.markup.html.form.FormComponent in project ocvn by devgateway.

the class FileInputBootstrapFormComponentWrapper method addBootstrapFileInputComponent.

private void addBootstrapFileInputComponent() {
    // this is where the newly uploaded files are saved
    final IModel<List<FileUpload>> internalUploadModel = new ListModel<>();
    /*
         * some customization of the BootstrapFileInput Component
         */
    FileInputConfig fileInputConfig = new FileInputConfig();
    fileInputConfig.put(new Key<String>("browseLabel"), new StringResourceModel("browseLabel", FileInputBootstrapFormComponentWrapper.this, null).getString());
    fileInputConfig.put(new Key<String>("uploadClass"), "btn btn-blue");
    fileInputConfig.put(new Key<String>("browseClass"), "btn btn-blue");
    bootstrapFileInput = new BootstrapFileInput("bootstrapFileInput", internalUploadModel, fileInputConfig) {

        private static final long serialVersionUID = 1L;

        @SuppressWarnings("unchecked")
        @Override
        protected void onSubmit(final AjaxRequestTarget target) {
            super.onSubmit(target);
            List<FileUpload> fileUploads = internalUploadModel.getObject();
            if (fileUploads != null) {
                // check if we uploaded too many files
                if (maxFiles > 0 && filesModel.size() + fileUploads.size() > maxFiles) {
                    if (maxFiles == 1) {
                        FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("OneUpload", FileInputBootstrapFormComponentWrapper.this, null).getString());
                    } else {
                        FileInputBootstrapFormComponentWrapper.this.fatal(new StringResourceModel("tooManyFiles", FileInputBootstrapFormComponentWrapper.this, Model.of(maxFiles)).getString());
                    }
                    FileInputBootstrapFormComponentWrapper.this.invalid();
                } else {
                    // and update the model
                    for (FileUpload upload : fileUploads) {
                        FileMetadata fileMetadata = new FileMetadata();
                        fileMetadata.setName(upload.getClientFileName());
                        fileMetadata.setContentType(upload.getContentType());
                        fileMetadata.setSize(upload.getSize());
                        FileContent fileContent = new FileContent();
                        fileContent.setBytes(upload.getBytes());
                        fileMetadata.setContent(fileContent);
                        filesModel.add(fileMetadata);
                    // don't display the success notification
                    // FileInputBootstrapFormComponentWrapper.this.success(new
                    // StringResourceModel("successUpload",
                    // FileInputBootstrapFormComponentWrapper.this,
                    // null, new
                    // Model(upload.getClientFileName())).getString());
                    }
                }
            }
            FileInputBootstrapFormComponentWrapper.this.getModel().setObject((T) filesModel);
            target.add(fileUploadFeedback);
            target.add(pendingFiles);
        }
    };
    add(bootstrapFileInput);
    /**
     * due to an upgrade of FormGroup in wicket7/wicket-bootrap-0.10, the
     * visitor that finds inner FormComponentS, will now find two instead of
     * one: the FileInputBootstrapFormComponentWrapper and also the
     * BootstrapFileInputField. This is the RIGHT result, previously in
     * wicket 6.x it only got the first level of children, hence only one
     * FormComponent (the FileInputBootstrapFormComponentWrapper). It would
     * then read the label from FileInputBootstrapFormComponentWrapper and
     * use it for displaying the label of the FormGroup. In
     * wicket7/wicket-bootstrap-0.10 this will result in reading the label
     * of BootstrapFileInputField which is null. So you will notice no
     * labels for FormGroupS. We fix this by forcing the label of the
     * underlying fileInput element to the same model as the label used by
     * FileInputBootstrapFormComponentWrapper
     */
    FormComponent<?> fileInput = (FormComponent<?>) bootstrapFileInput.get("fileInputForm").get("fileInput");
    fileInput.setLabel(this.getLabel());
    // only to admins
    if (visibleOnlyToAdmin) {
        MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, SecurityConstants.Roles.ROLE_ADMIN);
    }
    // want to read only
    if (disableDeleteButton) {
        MetaDataRoleAuthorizationStrategy.authorize(bootstrapFileInput, Component.RENDER, MetaDataRoleAuthorizationStrategy.NO_ROLE);
    }
}
Also used : FormComponent(org.apache.wicket.markup.html.form.FormComponent) FileMetadata(org.devgateway.toolkit.persistence.dao.FileMetadata) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) FileContent(org.devgateway.toolkit.persistence.dao.FileContent) ListModel(org.apache.wicket.model.util.ListModel) BootstrapFileInput(de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.BootstrapFileInput) ArrayList(java.util.ArrayList) List(java.util.List) FileInputConfig(de.agilecoders.wicket.extensions.markup.html.bootstrap.form.fileinput.FileInputConfig) StringResourceModel(org.apache.wicket.model.StringResourceModel) FileUpload(org.apache.wicket.markup.html.form.upload.FileUpload)

Aggregations

FormComponent (org.apache.wicket.markup.html.form.FormComponent)19 InputPanel (com.evolveum.midpoint.web.component.prism.InputPanel)6 AjaxRequestTarget (org.apache.wicket.ajax.AjaxRequestTarget)6 Component (org.apache.wicket.Component)5 ArrayList (java.util.ArrayList)4 AttributeModifier (org.apache.wicket.AttributeModifier)4 PropertyModel (org.apache.wicket.model.PropertyModel)4 TextPanel (com.evolveum.midpoint.web.component.input.TextPanel)3 List (java.util.List)3 IModel (org.apache.wicket.model.IModel)3 ListModel (org.apache.wicket.model.util.ListModel)3 AutoCompleteTextPanel (com.evolveum.midpoint.gui.api.component.autocomplete.AutoCompleteTextPanel)2 CheckBoxHeaderColumn (com.evolveum.midpoint.web.component.data.column.CheckBoxHeaderColumn)2 NotNullValidator (com.evolveum.midpoint.web.component.input.validator.NotNullValidator)2 VisibleEnableBehaviour (com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour)2 EmptyOnBlurAjaxFormUpdatingBehaviour (com.evolveum.midpoint.web.page.admin.configuration.component.EmptyOnBlurAjaxFormUpdatingBehaviour)2 LookupTableType (com.evolveum.midpoint.xml.ns._public.common.common_3.LookupTableType)2 AjaxFormComponentUpdatingBehavior (org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior)2 AttributeAppender (org.apache.wicket.behavior.AttributeAppender)2 IColumn (org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn)2