You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by "TRIFILETTI, Mel" <me...@doir.wa.gov.au> on 2005/11/07 09:35:16 UTC

Compound components made up other jsf components

Hi

I've just completed my first simple component. Now I want to develop a
more complex component which will contain multiple instances of my first
component, which will be used as building blocks.

E.g. Component B will be made up of two Component A components.

Having a look at the UIComponentBase I don't see any addChildren()
methods only getChildren()? I would expect that in the constructor of
Component B I could create and add my child components.

Is what am I asking possible? Are they any articles on advanced
components?

Mel





"DISCLAIMER: This email, including any attachments, is intended only for use by the addressee(s) and may contain confidential and/or personal information and may also be the subject of legal privilege. If you are not the intended recipient, you must not disclose or use the information contained in it. In this case, please let me know by return email, delete the message permanently from your system and destroy any copies.

Before you take any action based upon advice and/or information contained in this email you should carefully consider the advice and information and consider obtaining relevant independent advice.

Re: Compound components made up other jsf components

Posted by Bruno Aranda <br...@gmail.com>.
Using getChildren you get the List of children, which you can modify.
You can add children like this:

component.getChildren().add(yourChildComponent);

Regards,

Bruno

2005/11/7, TRIFILETTI, Mel <me...@doir.wa.gov.au>:
>
> Hi
>
> I've just completed my first simple component. Now I want to develop a
> more complex component which will contain multiple instances of my first
> component, which will be used as building blocks.
>
> E.g. Component B will be made up of two Component A components.
>
> Having a look at the UIComponentBase I don't see any addChildren()
> methods only getChildren()? I would expect that in the constructor of
> Component B I could create and add my child components.
>
> Is what am I asking possible? Are they any articles on advanced
> components?
>
> Mel
>
>
>
>
>
> "DISCLAIMER: This email, including any attachments, is intended only for use by the addressee(s) and may contain confidential and/or personal information and may also be the subject of legal privilege. If you are not the intended recipient, you must not disclose or use the information contained in it. In this case, please let me know by return email, delete the message permanently from your system and destroy any copies.
>
> Before you take any action based upon advice and/or information contained in this email you should carefully consider the advice and information and consider obtaining relevant independent advice.
>

Re: Compound components made up other jsf components

Posted by Mike Kienenberger <mk...@gmail.com>.
I've started a new wiki page on this topic as it's come up a few times
in the past.   I've tried to organize it a little, but there's room
for improvement, and likely a few errors as well, some of which I may
have introduced in organizing it :)

http://wiki.apache.org/myfaces/Creating_Composite_Components

On 11/7/05, Nico Krijnen <ni...@graphit.nl> wrote:
> Hi Mel,
>
> You can simply use the getChildren() method and add components to the list
> you receive.
> Maybe interesting for other people as well: I created a utility base class
> that can simplify creation om composite components, see code below.
> Extend your component from this class and implement the
> readyToCreateChildComponents() and createChildComponents() methods. When you
> want to recreate the child components (for example because the underlying
> datamodel changed), you can call the invalidate() method on the component.
>
> Nico Krijnen
>
>
> public abstract class BaseChildCreatorComponent extends UINamingContainer {
>
>         /*
>          * Child component buildup
>          */
>
>         private boolean childrenValid = false;
>
>         public void invalidate() {
>                 childrenValid = false;
>         }
>
>         private void validateChildren() {
>                 if (!childrenValid) {
>                         childrenValid = true;
>                         List children = super.getChildren();
>                         children.clear();
>                         createChildComponents(children);
>                 }
>         }
>
>         protected abstract boolean readyToCreateChildComponents();
>
>         protected abstract void createChildComponents(List children);
>
>         /*
>          * Child component rendering
>          */
>
>         public boolean getRendersChildren() {
>                 return true;
>         }
>
>         public void encodeChildren(FacesContext context) throws IOException
> {
>                 validateChildren();
>                 if (this.getChildCount() > 0) {
>                         for (Iterator it = this.getChildren().iterator();
> it.hasNext();) {
>                                 UIComponent child = (UIComponent) it.next();
>                                 RendererUtils.renderChild(context, child);
>                         }
>                 }
>         }
>
>         /*
>          * State saving
>          */
>
>         public void restoreState(FacesContext context, Object state) {
>                 Object values[] = (Object[]) state;
>                 super.restoreState(context, values[0]);
>                 childrenValid = ((Boolean) values[1]).booleanValue();
>         }
>
>         public Object saveState(FacesContext context) {
>                 Object values[] = new Object[2];
>                 values[0] = super.saveState(context);
>                 values[1] = new Boolean(childrenValid);
>                 return ((Object) (values));
>         }
>
> }
>
> -----Oorspronkelijk bericht-----
> Van: TRIFILETTI, Mel [mailto:mel.TRIFILETTI@doir.wa.gov.au]
> Verzonden: maandag 7 november 2005 9:35
> Aan: MyFaces Discussion
> Onderwerp: Compound components made up other jsf components
>
>
> Hi
>
> I've just completed my first simple component. Now I want to develop a
> more complex component which will contain multiple instances of my first
> component, which will be used as building blocks.
>
>
> E.g. Component B will be made up of two Component A components.
>
> Having a look at the UIComponentBase I don't see any addChildren()
> methods only getChildren()? I would expect that in the constructor of
> Component B I could create and add my child components.
>
> Is what am I asking possible? Are they any articles on advanced
> components?
>
> Mel
>
>
>
>
>
>
> "DISCLAIMER: This email, including any attachments, is intended only for use
> by the addressee(s) and may contain confidential and/or personal information
> and may also be the subject of legal privilege. If you are not the intended
> recipient, you must not disclose or use the information contained in it. In
> this case, please let me know by return email, delete the message
> permanently from your system and destroy any copies.
>
>
> Before you take any action based upon advice and/or information contained in
> this email you should carefully consider the advice and information and
> consider obtaining relevant independent advice.
>
>
>

RE: Compound components made up other jsf components

Posted by Nico Krijnen <ni...@graphit.nl>.
Hi Mel,

You can simply use the getChildren() method and add components to the list
you receive.
Maybe interesting for other people as well: I created a utility base class
that can simplify creation om composite components, see code below.
Extend your component from this class and implement the
readyToCreateChildComponents() and createChildComponents() methods. When you
want to recreate the child components (for example because the underlying
datamodel changed), you can call the invalidate() method on the component.

Nico Krijnen


public abstract class BaseChildCreatorComponent extends UINamingContainer {

	/*
	 * Child component buildup
	 */

	private boolean childrenValid = false;

	public void invalidate() {
		childrenValid = false;
	}

	private void validateChildren() {
		if (!childrenValid) {
			childrenValid = true;
			List children = super.getChildren();
			children.clear();
			createChildComponents(children);
		}
	}

	protected abstract boolean readyToCreateChildComponents();

	protected abstract void createChildComponents(List children);

	/*
	 * Child component rendering
	 */

	public boolean getRendersChildren() {
		return true;
	}

	public void encodeChildren(FacesContext context) throws IOException
{
		validateChildren();
		if (this.getChildCount() > 0) {
			for (Iterator it = this.getChildren().iterator();
it.hasNext();) {
				UIComponent child = (UIComponent) it.next();
				RendererUtils.renderChild(context, child);
			}
		}
	}

	/*
	 * State saving
	 */

	public void restoreState(FacesContext context, Object state) {
		Object values[] = (Object[]) state;
		super.restoreState(context, values[0]);
		childrenValid = ((Boolean) values[1]).booleanValue();
	}

	public Object saveState(FacesContext context) {
		Object values[] = new Object[2];
		values[0] = super.saveState(context);
		values[1] = new Boolean(childrenValid);
		return ((Object) (values));
	}

}

-----Oorspronkelijk bericht-----
Van: TRIFILETTI, Mel [mailto:mel.TRIFILETTI@doir.wa.gov.au] 
Verzonden: maandag 7 november 2005 9:35
Aan: MyFaces Discussion
Onderwerp: Compound components made up other jsf components


Hi

I've just completed my first simple component. Now I want to develop a
more complex component which will contain multiple instances of my first
component, which will be used as building blocks.


E.g. Component B will be made up of two Component A components.

Having a look at the UIComponentBase I don't see any addChildren()
methods only getChildren()? I would expect that in the constructor of
Component B I could create and add my child components.

Is what am I asking possible? Are they any articles on advanced
components?

Mel






"DISCLAIMER: This email, including any attachments, is intended only for use
by the addressee(s) and may contain confidential and/or personal information
and may also be the subject of legal privilege. If you are not the intended
recipient, you must not disclose or use the information contained in it. In
this case, please let me know by return email, delete the message
permanently from your system and destroy any copies.


Before you take any action based upon advice and/or information contained in
this email you should carefully consider the advice and information and
consider obtaining relevant independent advice.