Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jboss.errai.ui.shared.api.annotations;
  
  
  import  javax.enterprise.inject.Instance;
 import  javax.inject.Inject;
 
 import  com.google.gwt.user.client.ui.Composite;

Must be used only on classes extending from Composite.

Indicates that the annotated class will participate in the Errai UI templating framework. Instances of the annotated Composite widget must be retrieved via Inject or Instance references in bean classes.

Unless otherwise specified in the value() attribute, a corresponding ComponentName.html file must be placed in the same directory on the class-path as the custom ComponentName type.

Example:

 package org.example;
 
 @Templated
 public class CustomComponent extends Composite
 {
 }
 
And the corresponding HTML template:
 <form>
   <legend>Log in to your account</legend>
  
   <label for="username">Username</label>
   <input data-field="username" id="username" type="text" placeholder="Username">
  
   <label for="password">Password</label>
   <input data-field="password" id="password" type="password" placeholder="Password">
  
   <button data-field="login" >Log in</button>
   <button data-field="cancel" >Cancel</button>
 </form>
 

Each element with a data-field attribute may be bound to a field, method, or constructor parameter in the annotated class, using the DataField annotation. Events triggered by elements or widgets in the template may be handled using the EventHandler annotation to specify handler methods.

 package org.example;
 
 @Templated
 public class CustomComponent extends Composite
 {
    @Inject
    @DataField
    private TextBox username;
 
    @Inject
    @DataField
    private TextBox password;
 
    @Inject
    @DataField
    private Button login;
 
    @Inject
    @DataField
    private Button cancel;
 
    @EventHandler("login")
    private void doLogin(ClickEvent event)
    {
       // log in
    }
 }
 

Obtaining a widget reference via Inject:

 @ApplicationScoped
 public class ExampleBean
 {
    @Inject
    private CustomComponent comp;
 }
 

Obtaining widget references on demand, via Instance. One may also create multiple instances of a Templated widget using this approach:

 @ApplicationScoped
 public class ExampleBean
 {
    @Inject
    Instance<CustomComponent> instance;
 
    public CustomComponent getNewComponent()
    {
       return instance.get();
    }
 }
 

See also: DataField, Bound, AutoBound, EventHandler, SinkNative

Author(s):
Lincoln Baxter, III
public @interface Templated
{
   
Specifies the resource path (com/example/foo/CompositeComponent.html) and fragment ( #name) of the HTML template with which the annotated widget should be composited.

The resource path is the location of the HTML file on the classpath. If omitted, this defaults to the fully qualified class name of the annotated type, plus `.html`. If the fragment is omitted, composition will be performed using the first single element found (and all inner HTML) in the specified template.

The fragment corresponds to an element with matching data-field value. If specified, this single element (and all inner HTML) will be used as the root of the widget.

   String value() default "";
New to GrepCode? Check out our FAQ X