Ability to add WebGalileo Faces component to JSF components tree at run-time

Top  Previous  Next

You can add any component to JSF component tree at runtime.

 

To accomplish that the below steps should be accomplished:

 

1.Create helper class like :

 

public abstract class AddComponent implements ActionListener { protected abstract UIComponent getInstance();

 

       protected abstract String getComponentID();

 

       protected static String formID = "form1";

       protected static String panelID = "panelgrid1";

 

       protected FacesContext context = null;

 

       public void processAction(ActionEvent event)

               throws AbortProcessingException {

 

               context = FacesContext.getCurrentInstance();

 

               UIForm form = (UIForm) context.getViewRoot().findComponent(formID);

               UIPanel panel = (UIPanel) form.findComponent(panelID);

               if (panel.findComponent(getComponentID()) == null) {

                       UIComponent component = getInstance();

                       component.setId(getComponentID());

                       panel.getChildren().add(component);

               }

       }

}

 

Some methods in the corresponding successors should be implemented: getInstance() to create instance of component and getComponentID() to generate new ID.

 

example

 

public class AddCalendar extends AddComponent {

       protected UIComponent getInstance() {

               Calendar calendar = new Calendar();

               ..

               return calendar;

       }

       

       protected String getComponentID() {

               return "calendar1";

       }

}

 

2.Define the following construction in JSP page to add the component at runtime to JSF components tree:

 

<h:form id="form1">

       <h:commandButton id="addComponent" value="Add Calendar">

               <f:actionListener type="testingapplication.calendar.AddCalendar"/>

       </h:commandButton>

       <h:panelGrid id="panelgrid1" columns="1">

       </h:panelGrid>

</h:form>