Tuesday, January 15, 2008

Programmatically adding a jenia4faces Popup component to a Netbeans 6.0 VWP project

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>
You also need the component jars in your project classpath.

Next you need to import the appropriate Java class in your Page bean. In the case of the PopupFrame this is

import org.jenia.faces.popup.component.html.HtmlPopupFrame;
If you want to dynamically add a different Jenia component, you can easily find the fully qualified name of the appropriate class by either browsing the Jenia jar files, or simply by using the Netbeans auto-completion function.

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);
Again, you can use the auto-completion function in order to browse the supported properties and set any of those as required for your project.

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[]{});
Then we create a method binding using this expression and bind the actionOpen property.
   MethodBindingMethodExpressionAdapter bind1 = new MethodBindingMethodExpressionAdapter(openExpression);
   popup.setActionOpen(bind1);
Repeat the steps above for the actionClose attribute.

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.)

column8.getChildren().add(popup);

5 comments:

Anonymous said...

I have a problem with using it in Netbeans 6.0. I can display popup window but inside it I get HTTP error. During compilation glassfish displays "javax.el.PropertyNotFoundException: Target Unreachable, identifier 'Staff' resolved to null
". Thansk for your help

Anonymous said...

Hello! It's working fine but how can I add any components/text inside the frame ?

Zzzzz said...

A popup is actually a frame, positioned in a specific location on the screen. The page that is displayed inside it, is defined by the actionOpen attribute. See the original documentation on the jenia4faces website for more info...

Migo said...

when I added jenia4faces_1.2.1.jar to my project library, netbeans 6.5 fails to bring up visual designer for all visual JSF pages and page segments, anyone encounter such problem?

Anonymous said...

The World Leading wow power leveling and wow gold wow power leveling