This post describes how you can programmatically add a jenia4faces PopupFrame component to a Netbeans 6.0 Visual Web project.
There are various reasons why you would need to do that. In my case the requirement was that we needed a link in each row of a dynamically-created table that brings up a popup frame that is used for editing the database entry for this table row. Since the table is created dynamically, the same must be done with the PopupFrame component.
First you need to add the Jenia servlet in your web.xml file. This is a required step for using any Jenia component, no matter whether you add it to your page through JSP or from within Java code.
<servlet> <servlet-name>Jenia servlet</servlet-name> <servlet-class>org.jenia.faces.util.Servlet</servlet-class> <load-on-startup> </load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jenia servlet</servlet-name> <url-pattern>/jenia4faces/*</url-pattern> </servlet-mapping>
Next you need to import the appropriate Java class in your Page bean. In the case of the PopupFrame this is
Now you can initialize the component and set its basic properties.
// Create the popup component HtmlPopupFrame popup = new HtmlPopupFrame(); // Set its properties popup.setId("editPopup"); popup.setCenter("true"); popup.setWidth("550px"); popup.setHeight("400px"); popup.setTitle("Edit Task"); popup.setResizable("false"); popup.setImmediate(true);
Now it's the most tricky part: setting the method bindings for the actionOpen and actionClose properties. First we get a reference to the EL Expression Factory and create a MethodExpression.
FacesContext fc = FacesContext.getCurrentInstance(); ELContext elctx = fc.getELContext(); ExpressionFactory elFactory = fc.getApplication().getExpressionFactory(); MethodExpression openExpression = elFactory.createMethodExpression(elctx, "#{Staff.fireEditPopup}", String.class, new Class[]{});
MethodBindingMethodExpressionAdapter bind1 = new MethodBindingMethodExpressionAdapter(openExpression); popup.setActionOpen(bind1);
The PopupFrame component renders as a <a>. It can accept either plain text or an image as a child, so that it displays as a simple (text) hyperlink or an image hyperlink. This leaves us with an option to use either a StaticText or am ImageComponent in Netbeans. If you are not using Netbeans, you can add the respective standard JSF components (eg. <h:outputText>).
// Create an image component ImageComponent edit = new ImageComponent(); edit.setId("editLink"); edit.setUrl("resources/edit.png"); edit.setHeight(16); edit.setWidth(16); // Add the image to the popup (as a child) popup.getChildren().add(edit);
The Popup component is now ready. We can add it to the component tree. (In my case, I am adding it to the tablecolumn.)