Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JSF and the fly likes How to add programmatically RichFaces a4j:attachQueue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "How to add programmatically RichFaces a4j:attachQueue " Watch "How to add programmatically RichFaces a4j:attachQueue " New topic
Author

How to add programmatically RichFaces a4j:attachQueue

Lecorniaud
Greenhorn

Joined: Apr 01, 2007
Posts: 1
hey,

I tried so hard but I cannot get a simple Richfaces a4j:attachQueue programmatically inserted in my page. I generate the RichFaces components from Java
code, not xhtml.

I have some problems with the output of the data. Every request should be queued and fired after 2 seconds.If there are more than one request, only the
last request should be fired, the other request should be in the queued and destroyed
<a4j:queue requestDelay="2000" ignoreDupResponses="true"></a4j:queue>


I want that, if the User selects an item of the DropDownMenu 1 ( see method getSelectOneMenu1() ) and than select an item of the DropDownMenu 2
( see method getSelectOneMenu2() ), the first request should be queued and destroyed, RichFace should show only the data of the last request.
For example , the user select Quinie of the DropDownMenu 1 and then selects Pierrette of the DropDownMenu 2, only Pierrette should be showed ,
not Quinie and then after 2 seconds Pierrette. At the moment i receive two names from the same queue Quinie and after 2 seconds Pierrette.
That is not what i,ve expected, because i,ve implemented UIAttachQueue and uiAttachQueue.setRequestGroupingId("mygroupingId");
My big problem ,i don,t know how to attach programmatically a4j:attachQueue to a4j:ajax as child .I want to generate programmatically something
like this, with java not with xhml.

<h:selectOneMenu value="#{queueBean.username}">
<f:selectItem itemValue="Quinie" itemLabel="Quinie" />
<f:selectItem itemValue="William" itemLabel="William" />
<f:selectItem itemValue="Aron" itemLabel="Aron" />
<f:selectItem itemValue="Faith" itemLabel="Faith" />
<f:selectItem itemValue="Divine" itemLabel="Divine" />
<a4j:ajax event="change" execute="@this"
listener="#{queueBean.selectComponents}" render="rep">
<a4j:attachQueue requestGroupingId="group" name="myQueue"/>
</a4j:ajax>
</h:selectOneMenu>



Please thake a look of my Java code .

Any help will be appreciated!



cheers,



Marco


That's what I do:

queuedemo.xhtml


<h2>JSF and RichFaces Queue</h2>

<a4j:queue id="myQueue" name="myQueue" requestDelay="2000" ignoreDupResponses="true"></a4j:queue>

<h:form id="form_id">

<rich:panel>

<f:facet name="header">

Dynamic RichFaces components

</f:facet>

<h:panelGrid binding="#{queueBean.panelGrid}" ></h:panelGrid>

<rich:panel>

<a4j:outputPanel id="outputPanel_id" ajaxRendered="false" layout="block">

<h:outputText binding="#{queueBean.outputText}" />

</a4j:outputPanel>

</rich:panel>

</rich:panel>

</h:form>



Here is my ManagedBean



@ManagedBean(name = "queueBean")
@RequestScoped
public class MyQueueBean {
private String username;
private String firstname;
private String lastname;
private List<String> renderComponents;
private HtmlPanelGrid panelGrid = null;
private HtmlOutputText outputText;

@PostConstruct
public void init() {
this.renderComponents = new ArrayList<String>();
}

private Application application = null;

private Application getApplication() {
if (null == this.application) {
this.application = FacesContext.getCurrentInstance().getApplication();
}
return this.application;
}

@SuppressWarnings("unchecked")
private <T> T createComponent(String componentType) {
T component = (T) getApplication().createComponent(componentType);
return component;
}

private ELContext getELContext() {
return FacesContext.getCurrentInstance().getELContext();
}

private ExpressionFactory getExpressionFactory() {
return getApplication().getExpressionFactory();
}

private ValueExpression createValueExpression(String binding, Class<?> expectedType) {
return getExpressionFactory().createValueExpression(getELContext(), binding, expectedType);
}

public AjaxBehavior createAjaxBehavior(String expression, List<String> rederList, List<String> executeList) {
AjaxBehavior ajaxBehavior = (AjaxBehavior) getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
if (StringUtils.isNotEmpty(expression)) {
ajaxBehavior.addAjaxBehaviorListener(createAjaxBehaviorListener(expression));
}
if (CollectionUtils.isNotEmpty(executeList)) {
ajaxBehavior.setExecute(executeList);
}
if (CollectionUtils.isNotEmpty(rederList)) {
ajaxBehavior.setRender(rederList);
}
return ajaxBehavior;
}

private MethodExpression createMethodExpression(String expression, Class<?> expectedReturnType,
Class<?>[] expectedParamTypes) {
return getExpressionFactory().createMethodExpression(getELContext(), expression, expectedReturnType,
expectedParamTypes);
}

private MethodExpressionAjaxBehaviorListener createAjaxBehaviorListener(String expression) {
return new MethodExpressionAjaxBehaviorListener(createMethodExpression(expression, Void.class,
new Class[] { AjaxBehaviorEvent.class }));
}

public HtmlPanelGrid getPanelGrid() {
if (null == this.panelGrid) {
this.panelGrid = createComponent(HtmlPanelGrid.COMPONENT_TYPE);
this.panelGrid.setId("panel_id");
this.panelGrid.setColumns(3);
this.panelGrid.getChildren().add(getSelectOneMenu1());
this.panelGrid.getChildren().add(getSelectOneMenu2());
this.panelGrid.getChildren().add(getSelectOneMenu3());
}
return this.panelGrid;
}

public void setPanelGrid(HtmlPanelGrid panelGrid) {
this.panelGrid = panelGrid;
}

private UIAttachQueue createUIAttachQueue() {
UIAttachQueue uiAttachQueue = createComponent(UIAttachQueue.COMPONENT_TYPE);
uiAttachQueue.setName("myQueue");
uiAttachQueue.setRequestGroupingId("mygroupingId");
return uiAttachQueue;
}

private HtmlSelectOneMenu getSelectOneMenu1() {
HtmlSelectOneMenu selectOneMenu1 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
selectOneMenu1.setId("select_1");
UISelectItems selectItems = createUISelectItemsComponent(createFirstItems());
selectOneMenu1.getChildren().add(selectItems);
selectOneMenu1.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));
UIAttachQueue uiAttachQueue = createUIAttachQueue();
uiAttachQueue.setId("uiqueueId1");
List<String> idsToBeRendered = new ArrayList<String>();
idsToBeRendered.add("outputPanel_id");
AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,
Arrays.asList("@this"));
ajaxBehavior.setQueueId(uiAttachQueue.getName());
uiAttachQueue.associateWith(ajaxBehavior);
selectOneMenu1.addClientBehavior("change", ajaxBehavior);
uiAttachQueue.associateWith(selectOneMenu1);
return selectOneMenu1;
}

private HtmlSelectOneMenu getSelectOneMenu2() {
HtmlSelectOneMenu selectOneMenu2 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
selectOneMenu2.setId("select_2");
UISelectItems selectItems = createUISelectItemsComponent(createSecondItems());
selectOneMenu2.getChildren().add(selectItems);
selectOneMenu2.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));
UIAttachQueue uiAttachQueue = createUIAttachQueue();
uiAttachQueue.setId("uiqueueId2");
List<String> idsToBeRendered = new ArrayList<String>();
idsToBeRendered.add("outputPanel_id");
AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,
Arrays.asList("@this"));
ajaxBehavior.setQueueId(uiAttachQueue.getName());
uiAttachQueue.associateWith(ajaxBehavior);
selectOneMenu2.addClientBehavior("change", ajaxBehavior);
uiAttachQueue.associateWith(selectOneMenu2);
return selectOneMenu2;
}

private HtmlSelectOneMenu getSelectOneMenu3() {
HtmlSelectOneMenu selectOneMenu3 = createComponent(HtmlSelectOneMenu.COMPONENT_TYPE);
selectOneMenu3.setId("select_3");
UISelectItems selectItems = createUISelectItemsComponent(createThirdItems());
selectOneMenu3.getChildren().add(selectItems);
selectOneMenu3.setValueExpression("value", createValueExpression("#{queueBean.username}", String.class));
UIAttachQueue uiAttachQueue = createUIAttachQueue();
uiAttachQueue.setId("uiqueueId3");
List<String> idsToBeRendered = new ArrayList<String>();
idsToBeRendered.add("outputPanel_id");
AjaxBehavior ajaxBehavior = createAjaxBehavior("#{queueBean.selectComponents}", idsToBeRendered,
Arrays.asList("@this"));
ajaxBehavior.setQueueId(uiAttachQueue.getName());
selectOneMenu3.addClientBehavior("change", ajaxBehavior);
uiAttachQueue.associateWith(ajaxBehavior);
uiAttachQueue.associateWith(selectOneMenu3);
return selectOneMenu3;
}

public HtmlOutputText getOutputText() {
if (this.outputText == null) {
this.outputText = createComponent(HtmlOutputText.COMPONENT_TYPE);
this.outputText.setId("output_id");
this.outputText.setValueExpression("value",
createValueExpression("Selected Name : #{queueBean.username}", String.class));
}
return this.outputText;
}

public void setOutputText(HtmlOutputText outputText) {
this.outputText = outputText;
}

private List<SelectItem> createFirstItems() {
FacesContext.getCurrentInstance();
List<SelectItem> asList = Arrays.asList(new SelectItem("Quinie", "Quinie"),
new SelectItem("William", "William"), new SelectItem("Aron", "Aron"));
return asList;
}

private List<SelectItem> createSecondItems() {
List<SelectItem> asList = Arrays.asList(new SelectItem("Pierrette", "Pierrette"), new SelectItem("Biblias",
"Biblias"), new SelectItem("Pauline", "Pauline"));
return asList;
}

private List<SelectItem> createThirdItems() {
List<SelectItem> asList = Arrays.asList(new SelectItem("Durand", "Durand"), new SelectItem("Igor", "Igor"),
new SelectItem("Nathalie", "Nathalie"));
return asList;
}

private <U> UISelectItems createUISelectItemsComponent(List<U> list) {
UISelectItems items = new UISelectItems();
items.setId("id_" + String.valueOf(new Random().nextInt(1000)));
items.setValue(list);
return items;
}

public void selectComponents(AjaxBehaviorEvent event) {
this.renderComponents.add(this.username);
}

public String getUsername() {
return this.username;
}

public void setUsername(String username) {
this.username = username;
}

public List<String> getRenderComponents() {
return this.renderComponents;
}

public void setRenderComponents(List<String> renderComponents) {
this.renderComponents = renderComponents;
}

public String getFirstname() {
return this.firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public String getLastname() {
return this.lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}
}
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15661
    
  15

Welcome to the JavaRanch, "Lecorniaud".

There are 2 important things you need to know:

1. What to use for a Display Name:
http://www.javaranch.com/name.jsp

2. How to make your code and XML samples more readable.
http://www.coderanch.com/how-to/java/UseCodeTags


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add programmatically RichFaces a4j:attachQueue
 
Similar Threads
Listener not invoked with a built HtmlSelectOneMenu component
How to create BINDING of command link with backing bean
populate selectOneMenu with list containing various type of object
How to get Values out of dynamic created panel
Setting select items programatically