Search in sources :

Example 1 with FieldUpdater

use of org.activityinfo.ui.client.component.form.field.FieldUpdater in project activityinfo by bedatadriven.

the class DropPanelDropController method previewDropNewWidget.

private void previewDropNewWidget(final DragContext context) throws VetoDragException {
    final Template template = ((DnDLabel) context.draggable).getTemplate();
    if (template instanceof FieldTemplate) {
        final FormField formField = ((FieldTemplate) template).create();
        vetoDropIfNeeded(context);
        FormClass formClass = formDesigner.getModel().getFormClassByElementId(resourceId);
        formDesigner.getFormFieldWidgetFactory().createWidget(formClass, formField, new FieldUpdater() {

            @Override
            public void onInvalid(String errorMessage) {
            }

            @Override
            public void update(Object value) {
                formDesigner.getSavedGuard().setSaved(false);
            }
        }).then(new Function<FormFieldWidget, Void>() {

            @Nullable
            @Override
            public Void apply(@Nullable FormFieldWidget formFieldWidget) {
                final FieldWidgetContainer fieldWidgetContainer = new FieldWidgetContainer(formDesigner, formFieldWidget, formField, resourceId);
                containerMap.put(formField.getId(), fieldWidgetContainer);
                drop(fieldWidgetContainer, context, formField);
                initFieldProperties(fieldWidgetContainer, formField);
                return null;
            }
        });
    } else if (template instanceof SectionTemplate) {
        final FormSection formSection = ((SectionTemplate) template).create();
        vetoDropIfNeeded(context);
        FieldsHolderWidgetContainer widgetContainer = FieldsHolderWidgetContainer.section(formDesigner, formSection, resourceId);
        // parent drop container
        containerMap.put(resourceId, widgetContainer);
        drop(widgetContainer, context, formSection);
    } else if (template instanceof LabelTemplate) {
        final FormLabel formLabel = ((LabelTemplate) template).create();
        final LabelWidgetContainer fieldWidgetContainer = new LabelWidgetContainer(formDesigner, formLabel, resourceId);
        containerMap.put(formLabel.getId(), fieldWidgetContainer);
        drop(fieldWidgetContainer, context, formLabel);
    } else if (template instanceof SubFormTemplate) {
        final FormField formField = ((SubFormTemplate) template).create();
        vetoDropIfNeeded(context);
        FormClass subForm = formDesigner.getModel().registerNewSubform(formField.getId(), ((SubFormTemplate) template).getKind());
        subForm.setDatabaseId(formDesigner.getRootFormClass().getDatabaseId());
        subForm.setParentFormId(formDesigner.getRootFormClass().getId());
        subForm.setLabel(formField.getLabel());
        SubFormReferenceType type = (SubFormReferenceType) formField.getType();
        type.setClassId(subForm.getId());
        final FieldsHolderWidgetContainer widgetContainer = FieldsHolderWidgetContainer.subform(formDesigner, formField, subForm, formField.getId());
        // parent drop container
        containerMap.put(formField.getId(), widgetContainer);
        drop(widgetContainer.asWidget(), context);
        int widgetIndex = dropTarget.getWidgetIndex(widgetContainer.asWidget());
        formDesigner.getModel().getElementContainer(resourceId).insertElement(widgetIndex, formField);
        formDesigner.getEventBus().fireEvent(new PanelUpdatedEvent(widgetContainer, PanelUpdatedEvent.EventType.ADDED));
        formDesigner.getDragController().makeDraggable(widgetContainer.asWidget(), widgetContainer.getDragHandle());
        removePositioner();
    }
    // forbid drop of source control widget
    throw new VetoDragException();
}
Also used : FieldUpdater(org.activityinfo.ui.client.component.form.field.FieldUpdater) VetoDragException(com.allen_sauer.gwt.dnd.client.VetoDragException) LabelWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.LabelWidgetContainer) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormFieldWidget(org.activityinfo.ui.client.component.form.field.FormFieldWidget) PanelUpdatedEvent(org.activityinfo.ui.client.component.formdesigner.event.PanelUpdatedEvent) FieldWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.FieldWidgetContainer) FieldsHolderWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.FieldsHolderWidgetContainer) Nullable(javax.annotation.Nullable)

Example 2 with FieldUpdater

use of org.activityinfo.ui.client.component.form.field.FieldUpdater in project activityinfo by bedatadriven.

the class FormDesignerPanel method buildWidgetContainers.

private void buildWidgetContainers(final FormDesigner formDesigner, final FormElementContainer container, final FormClass owner, final int depth, final List<Promise<Void>> promises) {
    for (FormElement element : container.getElements()) {
        if (element instanceof FormSection) {
            FormSection formSection = (FormSection) element;
            containerMap.put(formSection.getId(), FieldsHolderWidgetContainer.section(formDesigner, formSection, container.getId()));
            buildWidgetContainers(formDesigner, formSection, owner, depth + 1, promises);
        } else if (element instanceof FormLabel) {
            FormLabel label = (FormLabel) element;
            containerMap.put(label.getId(), new LabelWidgetContainer(formDesigner, label, container.getId()));
        } else if (element instanceof FormField && !isSubFormKey(owner, (FormField) element)) {
            final FormField formField = (FormField) element;
            if (formField.getType() instanceof SubFormReferenceType) {
                // subform
                SubFormReferenceType subform = (SubFormReferenceType) formField.getType();
                Promise<Void> promise = formDesigner.getResourceLocator().getFormClass(subform.getClassId()).then(new Function<FormClass, Void>() {

                    @Override
                    public Void apply(FormClass subform) {
                        formDesigner.getModel().registerSubform(formField.getId(), subform);
                        containerMap.put(formField.getId(), FieldsHolderWidgetContainer.subform(formDesigner, formField, subform, container.getId()));
                        buildWidgetContainers(formDesigner, subform, subform, depth + 1, promises);
                        return null;
                    }
                });
                promises.add(promise);
            } else {
                if (formField.getId().asString().startsWith("_")) {
                    // skip if form field is built-in
                    continue;
                }
                Promise<Void> promise = formDesigner.getFormFieldWidgetFactory().createWidget(owner, formField, new FieldUpdater() {

                    @Override
                    public void onInvalid(String errorMessage) {
                    }

                    @Override
                    public void update(Object value) {
                        formDesigner.getSavedGuard().setSaved(false);
                    }
                }).then(new Function<FormFieldWidget, Void>() {

                    @Nullable
                    @Override
                    public Void apply(@Nullable FormFieldWidget input) {
                        containerMap.put(formField.getId(), new FieldWidgetContainer(formDesigner, input, formField, container.getId()));
                        if (container instanceof FormClass && ((FormClass) container).isSubForm()) {
                            // Possibly returned after initial fill - refill for current subform
                            fillPanel((FormClass) container, formDesigner);
                        }
                        return null;
                    }
                });
                promises.add(promise);
            }
        }
    }
}
Also used : FieldUpdater(org.activityinfo.ui.client.component.form.field.FieldUpdater) LabelWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.LabelWidgetContainer) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) FormFieldWidget(org.activityinfo.ui.client.component.form.field.FormFieldWidget) FieldWidgetContainer(org.activityinfo.ui.client.component.formdesigner.container.FieldWidgetContainer) Nullable(javax.annotation.Nullable)

Example 3 with FieldUpdater

use of org.activityinfo.ui.client.component.form.field.FieldUpdater in project activityinfo by bedatadriven.

the class FormWidgetCreator method createWidgets.

public Promise<Void> createWidgets(final FormClass formClass, final FieldUpdated fieldUpdated) {
    List<Promise<Void>> promises = Lists.newArrayList();
    for (final FormField field : formClass.getFields()) {
        if (field.getType() instanceof SubFormReferenceType) {
            FormClass subForm = model.getSubFormByOwnerFieldId(field.getId());
            if (subForm.getSubFormKind() != SubFormKind.REPEATING) {
                // for repeating we create it internally in sub SimpleFormPanel
                Promise<Void> subFormWidgetsPromise = createWidgets(subForm, fieldUpdated);
                promises.add(subFormWidgetsPromise);
            }
        } else {
            Promise<Void> promise = widgetFactory.createWidget(formClass, field, new FieldUpdater<FieldValue>() {

                @Override
                public void onInvalid(String errorMessage) {
                    containers.get(field.getId()).setInvalid(errorMessage);
                }

                @Override
                public void update(FieldValue value) {
                    containers.get(field.getId()).setValid();
                    fieldUpdated.onFieldUpdated(field, value);
                }
            }).then(new Function<FormFieldWidget, Void>() {

                @Override
                public Void apply(FormFieldWidget widget) {
                    FieldContainer fieldContainer = containerFactory.createContainer(field, widget, 4);
                    containers.put(field.getId(), fieldContainer);
                    model.addContainerOfClass(formClass.getId(), fieldContainer);
                    return null;
                }
            });
            promises.add(promise);
        }
    }
    return Promise.waitAll(promises);
}
Also used : FieldUpdater(org.activityinfo.ui.client.component.form.field.FieldUpdater) SubFormReferenceType(org.activityinfo.model.type.subform.SubFormReferenceType) Promise(org.activityinfo.promise.Promise) FormFieldWidget(org.activityinfo.ui.client.component.form.field.FormFieldWidget) FormClass(org.activityinfo.model.form.FormClass) FieldValue(org.activityinfo.model.type.FieldValue) FormField(org.activityinfo.model.form.FormField)

Aggregations

SubFormReferenceType (org.activityinfo.model.type.subform.SubFormReferenceType)3 FieldUpdater (org.activityinfo.ui.client.component.form.field.FieldUpdater)3 FormFieldWidget (org.activityinfo.ui.client.component.form.field.FormFieldWidget)3 Nullable (javax.annotation.Nullable)2 FieldWidgetContainer (org.activityinfo.ui.client.component.formdesigner.container.FieldWidgetContainer)2 LabelWidgetContainer (org.activityinfo.ui.client.component.formdesigner.container.LabelWidgetContainer)2 VetoDragException (com.allen_sauer.gwt.dnd.client.VetoDragException)1 FormClass (org.activityinfo.model.form.FormClass)1 FormField (org.activityinfo.model.form.FormField)1 FieldValue (org.activityinfo.model.type.FieldValue)1 Promise (org.activityinfo.promise.Promise)1 FieldsHolderWidgetContainer (org.activityinfo.ui.client.component.formdesigner.container.FieldsHolderWidgetContainer)1 PanelUpdatedEvent (org.activityinfo.ui.client.component.formdesigner.event.PanelUpdatedEvent)1