My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

miercuri, 8 iunie 2016

Generic client behavior example

Before checking this application, please get familiar with client behaviors. A simple example is available here.

Now, let's write a generic client behavior example. We simply want to display an image, and react via a JS snippet of code to the following events that occur on this image: onclick, onmouseout, onmouseover, onmousedown and onmouseup.

1. The index.xhtml looks like this:

<h:body>
 <h:panelGrid columns="2">
  <h:graphicImage library="default" name="pfof.png">
   <b:genericBehavior/>
  </h:graphicImage>
  <div id="behaviorsId" style="width:300px;"/>
 </h:panelGrid>
</h:body>

2. We use a tag handler to programmatically register behaviors:

public class GenericBehaviorTagHandler extends TagHandler {

 private final GenericBehaviorHandler onmouseout = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onmouseover = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onclick = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onmousedown = new GenericBehaviorHandler();
 private final GenericBehaviorHandler onmouseup = new GenericBehaviorHandler();

 public GenericBehaviorTagHandler(TagConfig tagConfig) {
  super(tagConfig);
 }

 @Override
 public void apply(FaceletContext ctx, UIComponent parent) throws IOException {

  if (parent instanceof ClientBehaviorHolder) {
      ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) parent;

      Map<String, List<ClientBehavior>> behaviors = clientBehaviorHolder.getClientBehaviors();

      if (!(behaviors.containsKey("mouseout"))
          && !(behaviors.containsKey("click"))
          && !(behaviors.containsKey("mouseover"))
          && !(behaviors.containsKey("mousedown"))
          && !(behaviors.containsKey("mouseup"))) {

           clientBehaviorHolder.addClientBehavior("mouseout", onmouseout);
           clientBehaviorHolder.addClientBehavior("mouseover", onmouseover);
           clientBehaviorHolder.addClientBehavior("click", onclick);
           clientBehaviorHolder.addClientBehavior("mousedown", onmousedown);
           clientBehaviorHolder.addClientBehavior("mouseup", onmouseup);
      }
   }
 }
}

3. The behaviors are instances of GenericBehaviorHandler:

public class GenericBehaviorHandler extends ClientBehaviorBase {

 @Override
 public String getRendererType() {          
  return "genericbehaviorrenderer";
 }
}

4. And the behavior renderer is:

@FacesBehaviorRenderer(rendererType = "genericbehaviorrenderer")
public class GenericBehaviorRenderer extends ClientBehaviorRenderer {

 @Override
 public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior) {
  return "document.getElementById('behaviorsId').innerHTML += '" + behaviorContext.getEventName() + " |';";
 }
}


The complete example is available here.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

Visitors Starting 4 September 2015

Locations of Site Visitors