Author: alexsmirnov
Date: 2010-05-28 17:35:24 -0400 (Fri, 28 May 2010)
New Revision: 17388
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/EventProcessor.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/EventModel.java
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/listenerTagHandler.ftl
root/cdk/trunk/plugins/maven-cdk-plugin/src/it/annotated-component/src/main/java/org/richfaces/cdk/test/event/TestEvent.java
Log:
RESOLVED - issue RF-7789: Process JSF events.
https://jira.jboss.org/browse/RF-7789
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/EventProcessor.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/EventProcessor.java 2010-05-28
19:43:18 UTC (rev 17387)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/apt/processors/EventProcessor.java 2010-05-28
21:35:24 UTC (rev 17388)
@@ -37,6 +37,7 @@
import org.richfaces.cdk.model.ClassName;
import org.richfaces.cdk.model.ComponentLibrary;
import org.richfaces.cdk.model.EventModel;
+import org.richfaces.cdk.util.Strings;
/**
* <p class="changed_added_4_0">This class processes annotations for
FacesEvents.</p>
@@ -68,7 +69,10 @@
} catch (MirroredTypeException mirror) {
model.setListenerInterface(ClassName.parseName(mirror.getTypeMirror().toString()));
}
- model.setListenerMethodName(event.listenerMethod());
+ String listenerMethod = event.listenerMethod();
+ if(!Strings.isEmpty(listenerMethod)){
+ model.setListenerMethod(listenerMethod);
+ }
model.setSourceInterface(ClassName.parseName(event.source()));
for (Tag tag : event.tag()) {
if(!TagType.None.equals(tag.type())){
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/EventModel.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/EventModel.java 2010-05-28
19:43:18 UTC (rev 17387)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/EventModel.java 2010-05-28
21:35:24 UTC (rev 17388)
@@ -44,7 +44,7 @@
public class EventModel implements ModelElement<EventModel> {
private String description;
private ClassName listenerInterface;
- private String listenerMethodName;
+ private String listenerMethod;
private boolean generateListener;
private ClassName sourceInterface;
private boolean generateSource;
@@ -108,19 +108,19 @@
/**
* <p class="changed_added_4_0"></p>
- * @return the listenerMethodName
+ * @return the listenerMethod
*/
@Merge
- public String getListenerMethodName() {
- return this.listenerMethodName;
+ public String getListenerMethod() {
+ return this.listenerMethod;
}
/**
* <p class="changed_added_4_0"></p>
- * @param listenerMethodName the listenerMethodName to set
+ * @param listenerMethod the listenerMethod to set
*/
- public void setListenerMethodName(String listenerMethodName) {
- this.listenerMethodName = listenerMethodName;
+ public void setListenerMethod(String listenerMethodName) {
+ this.listenerMethod = listenerMethodName;
}
/**
Modified:
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java 2010-05-28
19:43:18 UTC (rev 17387)
+++
root/cdk/trunk/plugins/generator/src/main/java/org/richfaces/cdk/model/validator/ValidatorImpl.java 2010-05-28
21:35:24 UTC (rev 17388)
@@ -122,9 +122,11 @@
// TODO - infer listener interface name.
}
event.setGenerateListener(null ==
sourceUtils.asTypeElement(listenerInterface));
- String methodName = event.getListenerMethodName();
+ String methodName = event.getListenerMethod();
if (null == methodName) {
// TODO infer listener method name.
+ methodName="process";
+ event.setListenerMethod(methodName);
}
ClassName sourceInterface = event.getSourceInterface();
if (null == sourceInterface) {
Modified:
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/listenerTagHandler.ftl
===================================================================
---
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/listenerTagHandler.ftl 2010-05-28
19:43:18 UTC (rev 17387)
+++
root/cdk/trunk/plugins/generator/src/main/resources/META-INF/templates/listenerTagHandler.ftl 2010-05-28
21:35:24 UTC (rev 17388)
@@ -0,0 +1,160 @@
+package ${tag.targetClass.package};
+
+import java.io.IOException;
+import java.io.Serializable;
+
+import javax.el.ELContext;
+import javax.el.MethodExpression;
+import javax.el.ValueExpression;
+import javax.faces.component.UIComponent;
+import javax.faces.context.FacesContext;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.view.facelets.ComponentHandler;
+import javax.faces.view.facelets.FaceletContext;
+import javax.faces.view.facelets.TagAttribute;
+import javax.faces.view.facelets.TagAttributeException;
+import javax.faces.view.facelets.TagConfig;
+import javax.faces.view.facelets.TagException;
+import javax.faces.view.facelets.TagHandler;
+
+import ${model.type};
+import ${model.listenerInterface};
+import ${model.sourceInterface};
+
+public class ${tag.targetClass.simpleName} extends TagHandler {
+
+ @SuppressWarnings("serial")
+ public static final class LazyListener implements
${model.listenerInterface.simpleName}, Serializable {
+
+ private final String type;
+
+ private final ValueExpression binding;
+
+ public LazyListener(String type, ValueExpression binding) {
+ this.type = type;
+ this.binding = binding;
+ }
+
+ public void ${model.listenerMethod}(${model.type} event) throws
AbortProcessingException {
+ ${model.listenerInterface.simpleName} instance = null;
+ FacesContext faces = FacesContext.getCurrentInstance();
+ if (faces == null) {
+ return;
+ }
+
+ if (this.binding != null) {
+ instance = (${model.listenerInterface.simpleName})
binding.getValue(faces.getELContext());
+ }
+
+ if (instance == null && this.type != null) {
+ try {
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ if (null == classLoader) {
+ classLoader =
${model.listenerInterface.simpleName}.class.getClassLoader();
+ }
+ instance =
classLoader.loadClass(this.type).asSubclass(${model.listenerInterface.simpleName}.class).newInstance();
+ } catch (Exception e) {
+ throw new AbortProcessingException("Couldn't lazily
instantiate ${model.listenerInterface.simpleName}", e);
+ }
+
+ if (this.binding != null) {
+ binding.setValue(faces.getELContext(), instance);
+ }
+ }
+
+ if (instance != null) {
+ instance.${model.listenerMethod}(event);
+ }
+ }
+ }
+
+ @SuppressWarnings("serial")
+ public static final class MethodExpressionListener implements
${model.listenerInterface.simpleName}, Serializable {
+
+ private MethodExpression methodExpression;
+
+ public MethodExpressionListener(MethodExpression methodExpression) {
+ super();
+ this.methodExpression = methodExpression;
+ }
+
+ public void ${model.listenerMethod}(${model.type} actionEvent) throws
AbortProcessingException {
+ if (actionEvent == null) {
+ throw new NullPointerException();
+ }
+ FacesContext context = FacesContext.getCurrentInstance();
+ ELContext elContext = context.getELContext();
+ try {
+ methodExpression.invoke(elContext, new Object[] { actionEvent });
+ } catch (Exception e) {
+ new AbortProcessingException(e);
+ }
+ }
+ }
+
+ private TagAttribute binding;
+
+ private String listenerType;
+
+ private TagAttribute listenerMethod;
+
+ public TestListenerHandler(TagConfig config) {
+ super(config);
+
+ this.binding = this.getAttribute("binding");
+
+ TagAttribute type = this.getAttribute("type");
+ if (type != null) {
+ if (!type.isLiteral()) {
+ throw new TagAttributeException(type, "Must be a literal class name
of type ${model.listenerInterface.simpleName}");
+ } else {
+ // test it out
+ }
+
+ this.listenerType = type.getValue();
+ } else {
+ this.listenerType = null;
+ }
+
+ this.listenerMethod = this.getAttribute("listener");
+
+ if (this.listenerMethod != null && this.binding != null) {
+ throw new TagException(this.tag, "Attributes 'listener' and
'binding' cannot be used simultaneously");
+ }
+
+ if (this.listenerMethod != null && this.listenerType != null) {
+ throw new TagException(this.tag, "Attributes 'listener' and
'type' cannot be used simultaneously");
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
javax.faces.view.facelets.FaceletHandler#apply(javax.faces.view.facelets.FaceletContext,
+ * javax.faces.component.UIComponent)
+ */
+ public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
+ if (null != parent && ComponentHandler.isNew(parent)) {
+ if (!(parent instanceof ${model.sourceInterface.simpleName})) {
+ throw new TagException(this.tag, "Parent is not of type
${model.sourceInterface.simpleName}, type is: " + parent);
+ }
+
+ ${model.sourceInterface.simpleName} as =
(${model.sourceInterface.simpleName}) parent;
+
+ if (this.listenerMethod != null) {
+ MethodExpression listenerMethodExpression =
+ this.listenerMethod.getMethodExpression(ctx, Void.TYPE, new
Class<?>[] { ${model.type}.class });
+
+ as.add${model.listenerInterface.simpleName}(new
MethodExpressionListener(listenerMethodExpression));
+ } else {
+ ValueExpression b = null;
+ if (this.binding != null) {
+ b = this.binding.getValueExpression(ctx,
${model.listenerInterface.simpleName}.class);
+ }
+ ${model.listenerInterface.simpleName} listener = new
LazyListener(this.listenerType, b);
+ as.add${model.listenerInterface.simpleName}(listener);
+ }
+ }
+ }
+
+}
Modified:
root/cdk/trunk/plugins/maven-cdk-plugin/src/it/annotated-component/src/main/java/org/richfaces/cdk/test/event/TestEvent.java
===================================================================
---
root/cdk/trunk/plugins/maven-cdk-plugin/src/it/annotated-component/src/main/java/org/richfaces/cdk/test/event/TestEvent.java 2010-05-28
19:43:18 UTC (rev 17387)
+++
root/cdk/trunk/plugins/maven-cdk-plugin/src/it/annotated-component/src/main/java/org/richfaces/cdk/test/event/TestEvent.java 2010-05-28
21:35:24 UTC (rev 17388)
@@ -37,7 +37,10 @@
* @author asmirnov(a)exadel.com
*
*/
-@Event(listener = TestListener.class, source =
"org.richfaces.cdk.test.event.TestSource", tag = @Tag(name =
"testListener"))
+@Event(listener = TestListener.class,
+ source = "org.richfaces.cdk.test.event.TestSource",
+ listenerMethod="process",
+ tag = @Tag(name = "testListener", generate = true, handler =
"org.richfaces.cdk.test.view.facelets.TestListenerHandler"))
public class TestEvent extends FacesEvent {
/**
@@ -74,7 +77,7 @@
@Override
public void processListener(FacesListener listener) {
if (listener instanceof TestListener) {
- TestListener testListener= (TestListener) listener;
+ TestListener testListener = (TestListener) listener;
testListener.process(this);
}
}