You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2006/02/17 16:22:32 UTC

svn commit: r378542 - in /myfaces/tomahawk/trunk/sandbox: core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ core/src/main/tld/entities/ examples/src/main/java/org/apache/myfaces/examples/debug/ examples/src/main/java/org/apache/myfaces/...

Author: matzew
Date: Fri Feb 17 07:22:27 2006
New Revision: 378542

URL: http://svn.apache.org/viewcvs?rev=378542&view=rev
Log:
MYFACES-864 and example added.

Added:
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeCollector.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeManager.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeNotifierTag.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangePhaseListener.java
    myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/value_change_notifier_attributes.xml
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/debug/
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/debug/DebugPhaseListener.java
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/valueChangeNotifier/
    myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/valueChangeNotifier/NotifierBean.java
    myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/valueChangeNotifier.jsp

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeCollector.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeCollector.java?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeCollector.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeCollector.java Fri Feb 17 07:22:27 2006
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.valueChangeNotifier;
+
+import javax.faces.component.StateHolder;
+import javax.faces.context.FacesContext;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.ValueChangeEvent;
+import javax.faces.event.ValueChangeListener;
+
+/**
+ * receives valueChange events and pass them to the manager
+ * 
+ * @author Mario Ivankovits <imario - at - apache.org> (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ValueChangeCollector implements ValueChangeListener,
+		StateHolder
+{
+	private String method = null;
+	private boolean isTransient = false;
+	
+	public ValueChangeCollector()
+	{
+	}
+
+	protected ValueChangeCollector(String method)
+	{
+		this.method = method;
+	}
+
+	/**
+	 * This it the valueChange sink<br />
+	 * The received event will be cloned and collected by the manager.  
+	 */
+	public void processValueChange(ValueChangeEvent event)
+			throws AbortProcessingException
+	{
+		ValueChangeEvent clonedEvent = new ValueChangeEvent(event
+				.getComponent(), event.getOldValue(), event.getNewValue());
+
+		ValueChangeManager manager = ValueChangeManager.getManager(FacesContext
+				.getCurrentInstance());
+		manager.addEvent(method, clonedEvent);
+	}
+
+	public Object saveState(FacesContext context)
+	{
+		return new Object[]
+		{ this.method};
+	}
+
+	public void restoreState(FacesContext context, Object state)
+	{
+		Object[] o = (Object[]) state;
+		
+		this.method = (String) o[0];
+	}
+
+	public boolean isTransient()
+	{
+		return isTransient;
+	}
+
+	public void setTransient(boolean isTransient)
+	{
+		this.isTransient = isTransient;
+	}
+}
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeManager.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeManager.java?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeManager.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeManager.java Fri Feb 17 07:22:27 2006
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.valueChangeNotifier;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.faces.FacesException;
+import javax.faces.context.FacesContext;
+import javax.faces.el.MethodBinding;
+import javax.faces.el.MethodNotFoundException;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.ValueChangeEvent;
+
+/**
+ * Manage valueChange events to be fired after the UPDATE_MODEL phase.
+ * 
+ * @author Mario Ivankovits <imario - at - apache.org>  (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public final class ValueChangeManager
+{
+	public final static Class[] SIGNATURE = new Class[]
+                                                    	{ ValueChangeEvent.class };
+	private final static String VCL_MANAGER = "_VCL_MANAGER_"
+			+ ValueChangeNotifierTag.class.getName();
+
+	private List events = new ArrayList(10);
+
+	private static class Entry
+	{
+		private final String method;
+		private final ValueChangeEvent event;
+
+		public Entry(String method,
+				ValueChangeEvent event)
+		{
+			this.method = method;
+			this.event = event;
+		}
+	}
+
+	private ValueChangeManager()
+	{
+	}
+
+	/**
+	 * add a new event 
+	 */
+	public void addEvent(String method,
+			ValueChangeEvent event)
+	{
+		events.add(new Entry(method, event));
+	}
+
+	/**
+	 * walk through list and fire collected events 
+	 */
+	public void fireEvents(FacesContext context)
+	{
+		try
+		{
+			Iterator iterEvents = events.iterator();
+			while (iterEvents.hasNext())
+			{
+				Entry entry = (Entry) iterEvents.next();
+	
+				try
+				{
+					MethodBinding mb = context.getApplication().createMethodBinding(entry.method, SIGNATURE);
+					mb.invoke(context, new Object[] { entry.event });
+					
+				}
+				catch (MethodNotFoundException e)
+				{
+					throw new FacesException(e);
+				}
+				catch (AbortProcessingException e)
+				{
+					// ignore any other value change event
+					return;
+				}
+			}
+		}
+		finally
+		{
+			events.clear();			
+		}
+	}
+
+	/**
+	 * check if the current request has a manager.<br />
+	 * The current request has a manager if, and only if it
+	 * collected valueChange events 
+	 */
+	public static boolean hasManager(FacesContext context)
+	{
+		Map requestMap = context.getExternalContext().getRequestMap();
+		return requestMap.get(VCL_MANAGER) != null;
+	}
+
+	/**
+	 * get the manager for this request. Create one if needed. 
+	 */
+	public static ValueChangeManager getManager(FacesContext context)
+	{
+		Map requestMap = context.getExternalContext().getRequestMap();
+		ValueChangeManager manager = (ValueChangeManager) requestMap
+				.get(VCL_MANAGER);
+		if (manager == null)
+		{
+			manager = new ValueChangeManager();
+			requestMap.put(VCL_MANAGER, manager);
+		}
+
+		return manager;
+	}
+}
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeNotifierTag.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeNotifierTag.java?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeNotifierTag.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangeNotifierTag.java Fri Feb 17 07:22:27 2006
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.valueChangeNotifier;
+
+import javax.faces.component.EditableValueHolder;
+import javax.faces.component.UIComponent;
+import javax.faces.webapp.UIComponentTag;
+import javax.servlet.jsp.JspException;
+import javax.servlet.jsp.tagext.Tag;
+import javax.servlet.jsp.tagext.TagSupport;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * <p>ValueChangeNotifier</p>
+ * 
+ * Like valueChangeListener, but will send valueChange events after the
+ * UPDATE_MODEL phase. <br />
+ * This simply means you CAN update your model values within such an event now.
+ * <br />
+ * <ul>
+ * <li>It wont be overwritten by the model update</li>
+ * <li>And wont trigger another valueChange if you update a value with an
+ * valueChangeListener attached</li>
+ * </ul>
+ * 
+ * @author Mario Ivankovits <imario - at - apache.org>  (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ValueChangeNotifierTag extends TagSupport
+{
+	private String method = null;
+	
+	private static Log log = LogFactory.getLog(ValueChangeNotifierTag.class);
+
+	public ValueChangeNotifierTag()
+	{
+	}
+
+	/**
+	 * The bean.method name of your valueChange method<br />
+	 * Currently only methods listeners are supported.<br />
+	 * e.g. myBean.myListenerMethod
+	 */
+	public void setMethod(String method)
+	{
+		this.method = method;
+	}
+
+	public int doStartTag() throws JspException
+	{
+		if (method == null)
+		{
+			throw new JspException("name attribute not set");
+		}
+
+		// Find parent UIComponentTag
+		UIComponentTag componentTag = UIComponentTag
+				.getParentUIComponentTag(pageContext);
+		if (componentTag == null)
+		{
+			throw new JspException(
+					"ValueChangeListenerTag has no UIComponentTag ancestor");
+		}
+
+		if (componentTag.getCreated())
+		{
+			// Component was just created, so we add the Listener
+			UIComponent component = componentTag.getComponentInstance();
+			if (component instanceof EditableValueHolder)
+			{
+				setupClassListener(component);
+			}
+			else
+			{
+				throw new JspException("Component " + component.getId()
+						+ " is no EditableValueHolder");
+			}
+		}
+
+		return Tag.SKIP_BODY;
+	}
+
+	protected void setupClassListener(UIComponent component)
+	{
+		if (UIComponentTag.isValueReference(method))
+		{
+				((EditableValueHolder) component)
+				.addValueChangeListener(new ValueChangeCollector(method));
+		} 
+		else
+		{
+			if(log.isErrorEnabled()){
+				log.error("Invalid expression " + method);
+			}
+		}
+	}
+}
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangePhaseListener.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangePhaseListener.java?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangePhaseListener.java (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/java/org/apache/myfaces/custom/valueChangeNotifier/ValueChangePhaseListener.java Fri Feb 17 07:22:27 2006
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2005-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.myfaces.custom.valueChangeNotifier;
+
+import javax.faces.event.PhaseEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.event.PhaseListener;
+
+/**
+ * PhaseListener to trigger the fireing of collected valueChange events.
+ *
+ * @author Mario Ivankovits <imario - at - apache.org>  (latest modification by $Author$)
+ * @version $Revision$ $Date$
+ */
+public class ValueChangePhaseListener implements PhaseListener
+{
+	public ValueChangePhaseListener()
+	{
+	}
+
+	public void afterPhase(PhaseEvent event)
+	{
+		if (ValueChangeManager.hasManager(event.getFacesContext()))
+		{
+			ValueChangeManager manager = ValueChangeManager.getManager(event.getFacesContext());
+			manager.fireEvents(event.getFacesContext());
+		}
+	}
+
+	public void beforePhase(PhaseEvent event)
+	{
+	}
+
+	public PhaseId getPhaseId()
+	{
+		return PhaseId.UPDATE_MODEL_VALUES;
+	}
+}
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/value_change_notifier_attributes.xml
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/value_change_notifier_attributes.xml?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/value_change_notifier_attributes.xml (added)
+++ myfaces/tomahawk/trunk/sandbox/core/src/main/tld/entities/value_change_notifier_attributes.xml Fri Feb 17 07:22:27 2006
@@ -0,0 +1,5 @@
+      <attribute>
+         <name>method</name>
+         <required>true</required>
+         <rtexprvalue>false</rtexprvalue>
+      </attribute>
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/debug/DebugPhaseListener.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/debug/DebugPhaseListener.java?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/debug/DebugPhaseListener.java (added)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/debug/DebugPhaseListener.java Fri Feb 17 07:22:27 2006
@@ -0,0 +1,40 @@
+package org.apache.myfaces.examples.debug;
+
+import javax.faces.event.PhaseEvent;
+import javax.faces.event.PhaseId;
+import javax.faces.event.PhaseListener;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * shows logger messages for each phase of the FacesLifecycle.
+ * @author matzew
+ *
+ */
+public class DebugPhaseListener implements PhaseListener
+{
+
+	private static Log log = LogFactory.getLog(DebugPhaseListener.class);
+	
+	public void afterPhase(PhaseEvent event)
+	{
+		if(log.isInfoEnabled())
+		{
+			log.info("AFTER PHASE " + event.getPhaseId());
+		}
+	}
+
+	public void beforePhase(PhaseEvent event)
+	{
+		if(log.isInfoEnabled())
+		{
+			log.info("BEFORE PHASE " + event.getPhaseId());
+		}
+	
+	}
+
+	public PhaseId getPhaseId() {
+		return PhaseId.ANY_PHASE;
+	}
+}
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/valueChangeNotifier/NotifierBean.java
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/valueChangeNotifier/NotifierBean.java?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/valueChangeNotifier/NotifierBean.java (added)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/java/org/apache/myfaces/examples/valueChangeNotifier/NotifierBean.java Fri Feb 17 07:22:27 2006
@@ -0,0 +1,51 @@
+package org.apache.myfaces.examples.valueChangeNotifier;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.ValueChangeEvent;
+import javax.faces.model.SelectItem;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class NotifierBean {
+	
+	private static Log log = LogFactory.getLog(NotifierBean.class);
+	
+	private String selectedCategory;
+	private List categories;
+	
+	public NotifierBean()
+	{
+		categories = new ArrayList();
+		categories.add(new SelectItem("a", "Category A"));
+		categories.add(new SelectItem("b", "Category b"));
+	}
+	
+	public void valueChange(ValueChangeEvent vce) throws AbortProcessingException 
+	{
+		if(log.isInfoEnabled()){
+            log.info("invoked valueChange method with " + vce.getNewValue() + " as its new value");
+		}
+		
+	}
+
+	public String getSelectedCategory() {
+		return selectedCategory;
+	}
+
+	public void setSelectedCategory(String selectedCategory) {
+		this.selectedCategory = selectedCategory;
+	}
+
+	public List getCategories() {
+		return categories;
+	}
+
+	public void setCategories(List categories) {
+		this.categories = categories;
+	}
+
+}
\ No newline at end of file

Added: myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/valueChangeNotifier.jsp
URL: http://svn.apache.org/viewcvs/myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/valueChangeNotifier.jsp?rev=378542&view=auto
==============================================================================
--- myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/valueChangeNotifier.jsp (added)
+++ myfaces/tomahawk/trunk/sandbox/examples/src/main/webapp/valueChangeNotifier.jsp Fri Feb 17 07:22:27 2006
@@ -0,0 +1,45 @@
+<%@ page session="false" contentType="text/html;charset=utf-8"%>
+<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
+<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
+<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
+<%@ taglib uri="http://myfaces.apache.org/sandbox" prefix="s"%>
+<html>
+
+<%@include file="inc/head.inc" %>
+
+<!--
+/*
+ * Copyright 2004 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+//-->
+
+<body>
+
+<f:view>
+    <h:form>
+      <h:selectOneMenu value="#{notifierBean.selectedCategory}" onchange="submit()">
+      
+	      <s:valueChangeNotifier method="#{notifierBean.valueChange}" />
+	      <f:selectItems value="#{notifierBean.categories}" />
+	  
+	  </h:selectOneMenu>    
+    </h:form>
+</f:view>
+
+<%@include file="inc/page_footer.jsp" %>
+
+</body>
+
+</html>
\ No newline at end of file