i have a similar use case. below is the code for a component i wrote. it is a 
markup container, so you can add components to it, which can be referenced from 
the html:
public abstract class CompositeComponent extends FormComponent{
        

        public CompositeComponent(String id) {
                this(id, null);
        }
        
        public CompositeComponent(String id, IModel model) {
                // XXX: can make it in omModelChanged, but then i have to do 
some ugly checks
                super(id);
                setModel(new PartialModel(model));
        }

        abstract protected String[] getCompositeInput(List<String[]> list);

        abstract protected Object getPartialObject(Component component, Object 
modelObject);

        /* (non-Javadoc)
         * @see wicket.markup.html.form.FormComponent#getInput()
         */
        @Override
        public String[] getInputAsArray() {
                // go over all children, and create a list.
final List<String[]> list = new ArrayList<String[]>(); visitChildren(FormComponent.class, new Component.IVisitor(){

                        public Object component(Component component) {
                                
list.add(((FormComponent)component).getInputAsArray());
                                return Component.IVisitor.CONTINUE_TRAVERSAL;
                        }
                        
                });
return getCompositeInput(list); }


        private class PartialModel implements ICompoundModel {

                private IModel selfModel;

                public PartialModel(IModel model) {
                        selfModel = model;
                }

                public IModel getNestedModel() {
                        return null;
                }

                public Object getObject(Component component) {
                        resolveSelfModel();
                        if (component == null) {
                                return 
selfModel.getObject(CompositeComponent.this);
                        }
                        return getPartialObject(component, 
selfModel.getObject(CompositeComponent.this));
                }

                public void setObject(Component component, Object object) {
                        if (component == null) {
                                resolveSelfModel();
                                selfModel.setObject(CompositeComponent.this, 
object);
                        }
                }

                private void resolveSelfModel() {
                        if (selfModel == null) {
                                selfModel = getParent().getModel();
                        }
                }

                public void detach() {
                        // TODO Auto-generated method stub

                }

        }

        
}


example:

public class DateTime extends CompositeComponent {
        // ValueMap map = new ValueMap();
        private String dateFormat = "MM/dd/yyyy";

        private String timeFormat = "H:mm";

        private SimpleDateFormat dateFormatter = new 
SimpleDateFormat(dateFormat);

        private SimpleDateFormat timeFormatter = new 
SimpleDateFormat(timeFormat);

        public DateTime(String id) {
                super(id);
                add(new TextField(getDateId(), Date.class) {
                        public IConverter getConverter() {
                                return new DateConverter(dateFormat, true);
                        }
                });
                add(new TextField(getTimeId(), Date.class) {
                        public IConverter getConverter() {
                                return new DateConverter(timeFormat, true);
                        }
                });
                setType(Date.class);

        }

        public DateTime(String id, IModel model, boolean isEmbedded) {
                this(id);
                setModel(model);
        }

        private String getTimeId() {
                return getId() + "-time";
        }

        private String getDateId() {
                return getId() + "-date";
        }

        @Override
        protected String[] getCompositeInput(List<String[]> list) {

String ret = ArraysUtils.get(list.get(0), 0, "") + " "
                                + ArraysUtils.get(list.get(1), 0, "");
                if (ret.length() == 1) {
                        ret = "";
                }
                return new String[] { ret };
        }

        @Override
        protected Object getPartialObject(Component component, Object 
modelObject) {
                if (modelObject == null) {
                        return null;
                }
                try {
                        if (component.getId().equals(getDateId())) {
                                return 
dateFormatter.parse(dateFormatter.format(modelObject));
                        } else if (component.getId().equals(getTimeId())) {
                                return 
timeFormatter.parse(timeFormatter.format(modelObject));
                        }
                } catch (ParseException e) {
                        // cannot happen
                        throw new RuntimeException("Parsing partial of " + 
modelObject
                                        + " failed", e);
                }
                return null;

        }

        /*
         * (non-Javadoc)
* * @see wicket.Component#getConverter()
         */
        @Override
        public IConverter getConverter() {
                return new DateConverter(dateFormat + " " + timeFormat, false);
        }

}

Pekka Enberg wrote:
Hi Johan,

(Please cc me as I am not subscribed to the list.)

At some point in time, Johan Compagner wrote:
you could use a FormValidator to validate those 2 fields (as it was
one)

Yeah, this I already have.

At some point in time, Johan Compagner wrote:
to combine the 2 as one value (because the can't et the min and max
inside the IntegerRange object?) You could wrap the 2 text fields in a
formcomponent and use only a MarkupContainer for those textfields (so
not really textfields)
So that from the form point of view there is only one formcomponent
touching the IntegerRange object and that one can do convertValue() to
convert it to a IntegerRange object and test it then and if that all
is ok the converted input will be pushed to the model.

Sounds good. So now that I have use FormComponent, I suppose I can't use
a HTML template but have to do the rendering in
Component.onComponentTag() myself?

                                Pekka



-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user



--
===================================
Ittay Dror, Chief architect, openQRM TL, R&D, Qlusters Inc.
[EMAIL PROTECTED]
+972-3-6081994 Fax: +972-3-6081841

http://www.openQRM.org
- Keeps your Data-Center Up and Running


-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Wicket-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/wicket-user

Reply via email to