3

In any Wicket repeater component, what is a common pattern to use different component classes based on the model objects class? My current approach is something like this, but i guess there is a way better solution:

BaseClass
|- AClass
|- BClass
`- CClass


protected void populateItem(Item<BaseClass> item) {
    BaseClass obj = item.getModelObject();
    if (obj instanceof AClass) {
        item.add(new APanel("content", Model.of((AClass) obj)));
    } else if (obj instanceof BClass) {
        item.add(new BPanel("content", Model.of((BClass) obj)));
    } else if (obj instanceof CClass) {
        item.add(new CPanel("content", Model.of((CClass) obj)));
    }
}
Imperative
  • 3,138
  • 2
  • 25
  • 40

2 Answers2

2

You can use a factory pattern that is external to your main page. The code in the factory would look similar to what you already have.

protected void populateItem(Item<BaseClass> item) {
  item.add(PanelFactory.getPanel("content", item.getModelObject());
}
JeredM
  • 897
  • 1
  • 14
  • 25
0

In small solution maybe I will use Your method (maybe with Map<dataClass,guiClass> but isn't too much important). If different paths require different markup use wicket fragments.

<html xmlns:wicket="http://wicket.sourceforge.net/">
<wicket:panel>
    <table>
        <tr wicket:id="lista">

            <td><span wicket:id="label"></span></td>
            <td><span wicket:id="dynamicValue"></span></td>
        </tr>

    </table>
    <wicket:fragment wicket:id="memoValue">
        <textarea wicket:id="value" cols="100" rows="15"></textarea>
    </wicket:fragment>

    <wicket:fragment wicket:id="smallValue">
        <input wicket:id="value"></input>
    </wicket:fragment>

    <wicket:fragment wicket:id="comboValue">
        <select wicket:id="value"></select>
    </wicket:fragment>

</wicket:panel>
</html>

and

@Override
            protected void populateItem(ListItem<IFieldDescription> item) {
                final IFieldDescription poz = item.getModelObject();
                System.out.println("Start:" + poz.getLabel());

                item.add(new Label("label", poz.getLabel()));
                if (poz instanceof IVirtualField) {
                    Fragment fragS = new Fragment("dynamicValue", "smallValue", ValuesPanel.this);
                    TextField<String> tf;
                    fragS.add(tf = new TextField<String>("value", new Model<String>("pole wirtualne")));
                    tf.setEnabled(false);
                    item.add(fragS);
                    return;
                }

                if (poz instanceof IRelationFieldDescription) {
                    IRelationFieldDescription rel = (IRelationFieldDescription)poz;
                    Fragment fragS = new Fragment("dynamicValue", "comboValue", ValuesPanel.this);
                    SingleFieldModel<Integer> model = new SingleFieldModel<Integer>(detachableRVModel, poz);
                    RelationDropDownChoice tf;
                    tf = new RelationDropDownChoice("value", model,new DetachableRTypeModel(rel.getSlave()));
                    fragS.add(tf);
                    //tf.setEnabled(false);
                    item.add(fragS);
                    return;
                }

                if (poz instanceof MemoField) {
                    Fragment fragM = new Fragment("dynamicValue", "memoValue", ValuesPanel.this);
                    SingleFieldModel<String> model = new SingleFieldModel<String>(detachableRVModel, poz);
                    fragM.add(new TextArea<String>("value", model));
                    // IModel<Integer> imo = new
                    // Model<Integer>(poz.getLength());
                    // tf.add(new AttributeAppender("maxlength", imo, " "));
                    // IModel<Integer> siz = new Model<Integer>(50);
                    // tf.add(new AttributeAppender("size", imo, " "));
                    item.add(fragM);
                    System.out.println("Memo:" + poz.getLabel());
                    return;

                }
                {
                    Fragment fragS = new Fragment("dynamicValue", "smallValue", ValuesPanel.this);
                    SingleFieldModel<String> model = new SingleFieldModel<String>(detachableRVModel, poz);
                    TextField<String> tf;
                    fragS.add(tf = new TextField<String>("value", model));
                    IModel<Integer> imo = new Model<Integer>(poz.getLength());
                    tf.add(new AttributeAppender("maxlength", imo, " "));
                    IModel<Integer> siz = new Model<Integer>(50);
                    tf.add(new AttributeAppender("size", siz, " "));
                    System.out.println("Single:" + poz.getLabel());
                    item.add(fragS);
                }
            }
Jacek Cz
  • 1,872
  • 1
  • 15
  • 22