Skip to content

Instantly share code, notes, and snippets.

@nerdEd
Created June 29, 2009 17:33
Show Gist options
  • Save nerdEd/137708 to your computer and use it in GitHub Desktop.
Save nerdEd/137708 to your computer and use it in GitHub Desktop.
public void encodeBegin( FacesContext context ) throws IOException {
super.encodeBegin( context );
// Without this 'if' statement everything gets rendered again on every partial submit
if( getChildCount() == 0 ) {
processTagAttributes();
if( fieldSetContainerList != null && !fieldSetContainerList.isEmpty() ) {
// Iterate over field containers to generate rows
for( List <DynamicFieldContainer> dynamicFieldContainerList : fieldSetContainerList ) {
// This is the panelGroup that wraps a single row.
HtmlPanelGroup rowPanelGroup = new HtmlPanelGroup();
rowPanelGroup.setStyleClass( "fieldRow" );
// Iterate over collection of fields to populate row with fields
for( DynamicFieldContainer dynamicFieldContainer : dynamicFieldContainerList ) {
ScreenFieldDcl screenFieldDcl = dynamicFieldContainer.getScreenFieldDcl();
IFieldDataDcl fieldDataDcl = dynamicFieldContainer.getFieldDataDcl();
if( !"Yes".equals( screenFieldDcl.getHidden() ) ) {
if( "Line".equals( screenFieldDcl.getDataType() ) ) {
HtmlPanelGroup linePanelGroup = new HtmlPanelGroup();
linePanelGroup.setStyleClass( "fieldWrapper lineFieldWrapper" );
rowPanelGroup.getChildren().add( linePanelGroup );
}
else if( "Blank".equals( screenFieldDcl.getDataType() ) ) {
HtmlPanelGroup blankPanelGroup = new HtmlPanelGroup();
blankPanelGroup.setStyleClass( "fieldWrapper" );
rowPanelGroup.getChildren().add( blankPanelGroup );
}
else if( "Text".equals( screenFieldDcl.getDataType() ) ) {
UIComponent textField = generateTextField( screenFieldDcl, fieldDataDcl );
rowPanelGroup.getChildren().add( textField );
}
// Add current row to this components children
this.getChildren().add( rowPanelGroup );
}
}
}
}
}
}
private UIComponent generateTextField( ScreenFieldDcl screenFieldDcl, IFieldDataDcl fieldDataDcl ) {
// Setup Field Wrapper
HtmlPanelGroup fieldWrapper = new HtmlPanelGroup();
fieldWrapper.setStyleClass( "fieldWrapper" );
// This is the field label.
HtmlOutputLabel fieldLabel = new HtmlOutputLabel();
fieldLabel.setValue( screenFieldDcl.getDisplay() + ":" );
fieldWrapper.getChildren().add( fieldLabel );
// Setup panel groups that wrap text input
HtmlPanelGroup entryWrapperPanelGroup = new HtmlPanelGroup();
entryWrapperPanelGroup.setStyleClass( "entryWrapper" );
HtmlPanelGroup textBoxWrapperPanelGroup = new HtmlPanelGroup();
textBoxWrapperPanelGroup.setStyleClass( "textBoxWrapper" );
entryWrapperPanelGroup.getChildren().add( textBoxWrapperPanelGroup );
// Retrieve context for retrieving values
FacesContext facesContext = FacesContext.getCurrentInstance();
ELContext elContext = facesContext.getELContext();
HtmlInputText inputText = new HtmlInputText();
inputText.setTitle( screenFieldDcl.getDisplay() );
// Setup value binding
ExpressionFactoryImpl expressionFactory = new ExpressionFactoryImpl();
ValueExpression valueExpression = expressionFactory.createValueExpression( fieldDataDcl, Object.class );
inputText.setValueExpression( "value", valueExpression );
// Set initial value of input
inputText.setValue( fieldDataDcl.getValue() );
// Setup max length
if( !StringHelperUtl.isEmpty( screenFieldDcl.getLength() ) ) {
inputText.setMaxlength( TypeHelperUtl.getInteger( screenFieldDcl.getLength() ) );
}
// Setup converter
Converter converter = ( Converter )facesContext.getApplication().getELResolver().getValue( elContext, null, "stringConverter" );
inputText.setConverter( converter );
// Setup value change listener
if( backingBean != null && !StringHelperUtl.isEmpty( beanListenerName ) ) {
Class[] actionListenerParameterTypes = new Class[]{ ValueChangeEvent.class };
MethodExpression methodExpression = expressionFactory.createMethodExpression( elContext, "#{" + backingBean + "." + beanListenerName + "}", null, actionListenerParameterTypes );
MethodExpressionValueChangeListener methodExpressionValueChangeListener = new MethodExpressionValueChangeListener( methodExpression );
inputText.addValueChangeListener( methodExpressionValueChangeListener );
}
inputText.setPartialSubmit( true );
// Insert input text into innermost wrapper
textBoxWrapperPanelGroup.getChildren().add( inputText );
// Insert input wrapper into field wrapper
fieldWrapper.getChildren().add( entryWrapperPanelGroup );
return fieldWrapper;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment