User Tools

Site Tools


java:addingcomponentsprogramatically

Adding JSF components programmatically

I've had to search a lot through internet in order to discover how to create JSF components programmaticaly. At the end, I didn't have to use it in the application I was creating, but I thought it worth to keep the research in some way.

 
import javax.faces.application.Application;
import javax.faces.component.html.*;
import javax.faces.context.FacesContext;
 
import com.rapidoaudit.model.AuditSkel;
 
 
public class SectionsCollection
{
	// rendered panel
	private HtmlPanelGrid grid = null; 
	//private List<UIComponent> sections;
	private int sectionsCounter; 
 
	public borrado_20110603_SectionsCollection()
	{
		sectionsCounter = 0; 
		//sections = new ArrayList<UIComponent>();
	}
 
	public void setComponent( HtmlPanelGrid newGrid )
	{
		grid = newGrid;
	}
 
	public HtmlPanelGrid getComponent()
	{
		Application app =  FacesContext.getCurrentInstance().getApplication();
 
		grid = (HtmlPanelGrid) app.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
		grid.setColumns(1);
		grid.setId("sections");
 
		return grid;
	}
 
	public void addSection(AuditSkel skel)
	{
 
		Section s1;
 
		skel.addSection();
 
		s1 = new Section(skel, sectionsCounter);
 
		grid.getChildren().add( s1.getGrid() );
 
		sectionsCounter++; 
 
	} // addSection
 
}
package com.rapidoaudit.view;
 
import javax.el.ELContext;
import javax.el.ValueExpression;
import javax.faces.application.Application;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UISelectItem;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputLabel;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.component.html.HtmlPanelGrid;
import javax.faces.component.html.HtmlPanelGroup;
import javax.faces.component.html.HtmlSelectOneMenu;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import javax.faces.context.FacesContext;
import javax.faces.convert.IntegerConverter;
 
import com.rapidoaudit.model.AuditSkel;
 
import java.util.Random; 
import org.primefaces.component.spinner.Spinner;
 
public class Section 
{
 
	int sectNumber; 
 
	// rendered panel
	private HtmlPanelGrid grid = null; 
 
	public Section( AuditSkel newSkel, int newSectNumber ) 
	{
		sectNumber = newSectNumber; 
	}
 
	public Section()
	{
		Random rnd = new Random(); 
 
		sectNumber = rnd.nextInt( 200000 ); 
	}
 
	public Section( int intSectNumber )
	{
		sectNumber = intSectNumber;
	}
 
 
	// for setting unique id's in the view
	private void compSetId( UIComponent comp, String id )
	{
		comp.setId( id + new Integer(sectNumber).toString() ); 
	}
 
	// for setting unique id's in the view
	@SuppressWarnings("unused")
	private void compSetId( UIComponent comp )
	{
		// create a random stream of numbers (just for the sake of keep faces happy)
		Random rnd = new Random();
 
		comp.setId( Integer.toString(rnd.nextInt(200000)));
	}
 
	/**
	 * To set a value from an Expression Language expression. 
	 * Example:
	 * 
	 * compSetValue( name_input, "#{auditSkel.name}" );
	 * 
	 * where "auditSkel" should be an existent bean
	 * 
	 * @param comp
	 * @param elExpression
	 */
	private void compSetValue( UIInput comp, String elExpression )
	{
		ELContext elContext = FacesContext.getCurrentInstance().getELContext();
 
		ValueExpression val1 = FacesContext.getCurrentInstance().
						    getApplication().
						    getExpressionFactory().
						    createValueExpression( elContext,
						    						elExpression,
						    						Object.class);
		comp.setValueExpression( "value", val1 );
 
	} // compSetValue
 
 
	public HtmlPanelGrid getGrid( )
	{
		if( grid == null )
		{
			Application app = FacesContext.getCurrentInstance().getApplication();
 
			grid = (HtmlPanelGrid) app.createComponent(HtmlPanelGrid.COMPONENT_TYPE);
 
			grid.setColumns(2);
			compSetId( grid, "Section" ); 
 
			// <h:outputLabel for="name" value="Name" styleClass="right"/>
			HtmlOutputLabel name_label = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
			name_label.setStyleClass("right");
			name_label.setValue("Name");
			name_label.setFor("type");
			compSetId( name_label, "name_label" );
			grid.getChildren().add(name_label);
 
			// <h:inputText id="name" value="Put a name here"/>
			HtmlInputText name_input = (HtmlInputText) app.createComponent(HtmlInputText.COMPONENT_TYPE);
			compSetId( name_input, "name_input" );
			// aqui me quedo, no sé cómo se puede hacer esto
			//compSetValue( name_input, "#{auditSkel.getSection("+ sectNumber +").name}" );
			compSetValue( name_input, "#{testProp.testObject(1).name}" ); 
			grid.getChildren().add(name_input);
 
			//<h:outputLabel for="type" value="Type" styleClass="right"/>
			HtmlOutputLabel type_label = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); 
			type_label.setStyleClass("right");
			type_label.setValue("Type");
			type_label.setFor("type");
			compSetId( type_label, "type_label" );
			grid.getChildren().add(type_label); 
 
			//<h:selectOneMenu id="type" value="text_many">
			//	<f:selectItem itemLabel="Text" itemValue="text_many"/>
			//	<f:selectItem itemLabel="Date" itemValue="date"/>
			//	<f:selectItem itemLabel="Text (one line)" itemValue="text_one"/>
			//	<f:selectItem itemLabel="Document" itemValue="file"/>
			//	<f:selectItem itemLabel="Image" itemvalue="image"/>
			//</h:selectOneMenu>
			HtmlSelectOneMenu type_menu = (HtmlSelectOneMenu) app.createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
			compSetId( type_menu, "type_menu" ); 
			type_menu.setValue("text_many");
 
			UISelectItem type_item_1 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE);
			type_item_1.setItemLabel("Text (many lines)");
			type_item_1.setItemValue("text_many");
			compSetId( type_item_1, "type_item_1" );
			type_menu.getChildren().add(type_item_1);
 
			UISelectItem type_item_2 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); 
			type_item_2.setItemLabel("Date");
			type_item_2.setItemValue("date");
			type_item_2.setId("type_item_2"); 
			compSetId( type_item_2, "type_item_2" );
			type_menu.getChildren().add(type_item_2);
 
			UISelectItem type_item_3 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); 
			type_item_3.setItemLabel("Text (one line)");
			type_item_3.setItemValue("text_one");
			compSetId( type_item_3, "type_item_3"); 
			type_menu.getChildren().add(type_item_3);
 
			UISelectItem type_item_4 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); 
			type_item_4.setItemLabel("Document");
			type_item_4.setItemValue("file");
			compSetId( type_item_4, "type_item_4"); 
			type_menu.getChildren().add(type_item_4);
 
			UISelectItem type_item_5 = (UISelectItem) app.createComponent(UISelectItem.COMPONENT_TYPE); 
			type_item_5.setItemLabel("Image");
			type_item_5.setItemValue("image");
			compSetId( type_item_5, "type_item_5"); 
			type_menu.getChildren().add(type_item_5);
 
			grid.getChildren().add(type_menu);
 
			//<h:outputLabel for="goesReport" value="Does this section appear on the report?" styleClass="right"/>
			HtmlOutputLabel goesReportLabel = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); 
			goesReportLabel.setStyleClass("right");
			goesReportLabel.setValue("Does this section appear on the report?");
			goesReportLabel.setFor("type");
			compSetId( goesReportLabel, "goesReportLabel" );
			grid.getChildren().add(goesReportLabel); 
 
			//<h:selectBooleanCheckbox id="goesReport"/>
			HtmlSelectBooleanCheckbox goesReport = (HtmlSelectBooleanCheckbox) app.createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE);
			compSetId( goesReport, "goesReport" );
			grid.getChildren().add(goesReport); 
 
			//<h:outputLabel for="isFinding" value="Is this section part of a finding?" styleClass="right"/>
			HtmlOutputLabel isFindingLabel = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE); 
			isFindingLabel.setStyleClass("right");
			isFindingLabel.setValue("Is this section part of a finding?"); 
			isFindingLabel.setFor("isFinding" );
			compSetId( isFindingLabel, "isFindingLabel" ); 
			grid.getChildren().add(isFindingLabel); 
 
			//<h:selectBooleanCheckbox id="isFinding"/>
			HtmlSelectBooleanCheckbox isFinding = (HtmlSelectBooleanCheckbox) app.createComponent(HtmlSelectBooleanCheckbox.COMPONENT_TYPE);
			compSetId( isFinding, "isFinding" );
			grid.getChildren().add(isFinding); 
 
			//<h:outputText value="In each audit work this will appear..."/>
			HtmlOutputText minimumMaximum = (HtmlOutputText) app.createComponent( HtmlOutputText.COMPONENT_TYPE );
			compSetId( minimumMaximum, "minimumMaximum" );
			minimumMaximum.setValue("In each audit work this will appear...");
			minimumMaximum.setStyleClass("right");
			grid.getChildren().add(minimumMaximum);
 
			HtmlOutputLabel empty1 = (HtmlOutputLabel) app.createComponent( HtmlOutputLabel.COMPONENT_TYPE );
			compSetId( empty1, "empty1" );
			empty1.setValue("");
			grid.getChildren().add(empty1);
 
			//<h:panelGrid/>
 
			//<h:outputLabel for="minima" value="...a minimum of" styleClass="right"/>
			HtmlOutputLabel aMinimumOf = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
			compSetId(aMinimumOf, "aMinimumOf"); 
			aMinimumOf.setValue("A minimum of" ); 
			aMinimumOf.setStyleClass("right"); 
			aMinimumOf.setFor("panelMin");
			grid.getChildren().add(aMinimumOf);
 
			//<h:panelGroup>
			HtmlPanelGroup panelMin = (HtmlPanelGroup) app.createComponent(HtmlPanelGroup.COMPONENT_TYPE);
			compSetId(panelMin, "panelMin");
 
			//	<p:spinner id="minima" value="1" min="0" max="1000"/>
			Spinner spMin = (Spinner) app.createComponent( Spinner.COMPONENT_TYPE ); 
			compSetId( spMin, "spMin" ); 
			spMin.setValue(1); 
			spMin.setMin(0); 
			spMin.setSize(5);
			spMin.setMax(1000);
			spMin.setConverter(new IntegerConverter());
			panelMin.getChildren().add(spMin);
 
			//	<h:outputText value="times"/>
			HtmlOutputText times = (HtmlOutputText) app.createComponent(HtmlOutputText.COMPONENT_TYPE);
			compSetId( times, "times" ); 
			times.setValue("times");
			panelMin.getChildren().add(times); 
 
			//</h:panelGroup>
			grid.getChildren().add(panelMin); 
 
 
			//<h:outputLabel for="maximum" value="...a maximum of" styleClass="right"/>
			HtmlOutputLabel aMaximumOf = (HtmlOutputLabel) app.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
			compSetId(aMaximumOf, "aMaximumOf"); 
			aMaximumOf.setValue("A maximum of" ); 
			aMaximumOf.setStyleClass("right"); 
			aMaximumOf.setFor("panelMin");
			grid.getChildren().add(aMaximumOf);
 
			//<h:panelGroup>
			HtmlPanelGroup panelMax = (HtmlPanelGroup) app.createComponent(HtmlPanelGroup.COMPONENT_TYPE);
			compSetId(panelMax, "panelMax");
 
			//	<p:spinner id="maximum" value="1"/>
			Spinner spMax = (Spinner) app.createComponent( Spinner.COMPONENT_TYPE ); 
			compSetId( spMax, "spMax" ); 
			spMax.setValue(1); 
			spMax.setMin(0); 
			spMax.setSize(5);
			spMax.setMax(1000);
			spMax.setValue(0);
			spMax.setConverter(new IntegerConverter());
			panelMax.getChildren().add(spMax);
 
			//	<h:outputText value="times"/>
			HtmlOutputText times2 = (HtmlOutputText) app.createComponent(HtmlOutputText.COMPONENT_TYPE);
			compSetId( times2, "times2" ); 
			times2.setValue("times");
			panelMax.getChildren().add(times2); 
 
			//	</h:panelGroup>
			grid.getChildren().add(panelMax); 
 
			//</h:panelGrid>			
 
		}
 
		return grid;
	}
 
}

~~DISQUS~~

java/addingcomponentsprogramatically.txt · Last modified: 2022/12/02 22:02 by 127.0.0.1