aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/components/customfield/EmbeddedForm.java
blob: d305afde1d55abd6ce88dfd862b210cf2a00fd38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.vaadin.tests.components.customfield;

import java.util.HashMap;
import java.util.Map;

import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.Layout;

/**
 * Form that displays its fields in the layout of another form.
 * 
 * The fields are still logically part of this form even though they are in the
 * layout of the parent form. The embedded form itself is automatically hidden.
 * 
 * TODO Known issue: any field factory creating an {@link EmbeddedForm}
 * (directly or indirectly) should re-use the field once it has been created to
 * avoid the creation of duplicate fields when e.g. setting the visible item
 * properties.
 */
public class EmbeddedForm extends Form {
    private Form parentForm;
    private Map<Object, Field> fields = new HashMap<Object, Field>();

    /**
     * Create a form that places its fields in another {@link Form}.
     * 
     * @param parentForm
     *            form to which to embed the fields, not null
     */
    public EmbeddedForm(Form parentForm) {
        this.parentForm = parentForm;
        setVisible(false);
    }

    @Override
    protected void attachField(Object propertyId, Field field) {
        if (propertyId == null || field == null) {
            return;
        }

        Layout layout = parentForm.getLayout();

        Field oldField = fields.get(propertyId);
        if (oldField != null) {
            layout.removeComponent(oldField);
        }

        fields.put(propertyId, field);

        if (layout instanceof CustomLayout) {
            ((CustomLayout) layout).addComponent(field, propertyId.toString());
        } else {
            layout.addComponent(field);
        }
    }

    @Override
    public boolean removeItemProperty(Object id) {
        // remove the field from the parent layout if already added there
        parentForm.getLayout().removeComponent(fields.get(id));
        fields.remove(id);

        return super.removeItemProperty(id);
    }
}