Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
$URL: https://source.sakaiproject.org/svn/jsf/tags/sakai-10.0/jsf-widgets/src/java/org/sakaiproject/jsf/util/TagUtil.java $ $Id: TagUtil.java 105077 2012-02-24 22:54:29Z ottenhoff@longsight.com $ Copyright (c) 2003, 2004, 2005, 2006, 2008 The Sakai Foundation Licensed under the Educational Community 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.opensource.org/licenses/ECL-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.sakaiproject.jsf.util;
 
Common static utility methods that help in implementing JSF tags.
 
 public class TagUtil
 {

    
This class is meant for static use only
 
     private TagUtil()
     {
     }

   
Set a string value on a component - used by tags setProperties() method. Handles value bindings.
 
    public static void setString(UIComponent componentString nameString value)
    {
        if (value == null)
        {
            return;
        }
        if (UIComponentTag.isValueReference(value))
        {
            setValueBinding(componentnamevalue);
        } else
        {
            component.getAttributes().put(namevalue);
        }
    }

   
Set a string value on a component - used by tags setProperties() method. Handles value bindings.
 
    public static void setObject(UIComponent componentString nameString value)
    {
        if (value == null)
        {
            return;
        }
        if (UIComponentTag.isValueReference(value))
        {
            setValueBinding(componentnamevalue);
        } else
        {
            component.getAttributes().put(namevalue);
        }
    }

    
Set an integer value on a component - used by tags setProperties() method. Handles value bindings.
 
     public static void setInteger(UIComponent componentString nameString value)
     {
         if (value == null)
         {
             return;
         }
         if (UIComponentTag.isValueReference(value))
         {
             setValueBinding(componentnamevalue);
         } else
         {
             component.getAttributes().put(name, Integer.valueOf(value));
        }
    }

    
Set a Map value on a component - used by tags setProperties() method. Handles value bindings.
    public static void setMap(UIComponent componentString nameString value)
    {
        if (value == null)
        {
            return;
        }
        if (UIComponentTag.isValueReference(value))
        {
            setValueBinding(componentnamevalue);
        } else
        {
            component.getAttributes().put(namenew HashMap());
        }
    }

    
Set a double value on a component - used by tags setProperties() method. Handles value bindings.
    public static void setDouble(UIComponent componentString nameString value)
    {
        if (value == null)
        {
            return;
        }
        if (UIComponentTag.isValueReference(value))
        {
            setValueBinding(componentnamevalue);
        } else
        {
            component.getAttributes().put(name, Double.valueOf(value));
        }
    }

    
Set a boolean value on a component - used by tags setProperties() method. Handles value bindings.
    public static void setBoolean(UIComponent componentString nameString value)
    {
        if (value == null)
        {
            return;
        }
        if (UIComponentTag.isValueReference(value))
        {
            setValueBinding(componentnamevalue);
        } else
        {
            component.getAttributes().put(name, Boolean.valueOf(value));
        }
    }

    
Set a ValueBinding on a component - used by tags setProperties() method.
    public static void setValueBinding(UIComponent componentString nameString value)
    {
        FacesContext context = FacesContext.getCurrentInstance();
        Application app = context.getApplication();
        ValueBinding vb = app.createValueBinding(value);
        component.setValueBinding(namevb);
    }

    
Set an ActionListener on a component - used by tags setProperties() method. Handles method bindings.
    public static void setActionListener(UIComponent componentString value)
    {
        setMethodBinding(component"actionListener"valuenew Class[] { ActionEvent.class });
    }

    
Set a ValueChangeListener on a component - used by tags setProperties() method. Handles method bindings.
    public static void setValueChangeListener(UIComponent componentString value)
    {
        setMethodBinding(component"valueChangeListener"value,
                new Class[] { ValueChangeEvent.class });
    }

    
Set a Validator on a component - used by tags setProperties() method. Handles method bindings.
    public static void setValidator(UIComponent componentString value)
    {
        setMethodBinding(component"validator"valuenew Class[] { FacesContext.class,
                UIComponent.classObject.class });
    }

    
Set an action on a component - used by tags setProperties() method. Handles method bindings.
    public static void setAction(UIComponent componentString value)
    {
        if (value == null)
        {
            return;
        }
        if (UIComponentTag.isValueReference(value))
        {
            setMethodBinding(component"action"valuenew Class[] {});
        } else
        {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            MethodBinding mb = new ActionMethodBinding(value);
            component.getAttributes().put("action"mb);
        }
    }

    
Set a MethodBinding on a component - used by tags setProperties() method.
    public static void setMethodBinding(UIComponent componentString nameString value,
            Class[] paramTypes)
    {
        if (value == null)
        {
            return;
        }
        if (UIComponentTag.isValueReference(value))
        {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            MethodBinding mb = app.createMethodBinding(valueparamTypes);
            component.getAttributes().put(namemb);
        }
    }
    public static String eval(String expression)
    {
        if (expression == null)
        {
            return null;
        }
        if (UIComponentTag.isValueReference(expression))
        {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            return "" + app.createValueBinding(expression).getValue(context);
        } else
        {
            return expression;
        }
    }
    public static Integer evalInteger(String expression)
    {
        if (expression == null)
        {
            return null;
        }
        if (UIComponentTag.isValueReference(expression))
        {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            Object r = app.createValueBinding(expression).getValue(context);
            if (r == null)
            {
                return null;
            } else if (r instanceof Integer)
            {
                return (Integerr;
            } else
            {
                return Integer.valueOf(r.toString());
            }
        } else
        {
            return Integer.valueOf(expression);
        }
    }
    public static Double evalDouble(String expression)
    {
        if (expression == null)
        {
            return null;
        }
        if (UIComponentTag.isValueReference(expression))
        {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            Object r = app.createValueBinding(expression).getValue(context);
            if (r == null)
            {
                return null;
            } else if (r instanceof Double)
            {
                return (Doubler;
            } else
            {
                return Double.valueOf(r.toString());
            }
        } else
        {
            return Double.valueOf(expression);
        }
    }
    public static Boolean evalBoolean(String expression)
    {
        if (expression == null)
        {
            return null;
        }
        if (UIComponentTag.isValueReference(expression))
        {
            FacesContext context = FacesContext.getCurrentInstance();
            Application app = context.getApplication();
            Object r = app.createValueBinding(expression).getValue(context);
            if (r == null)
            {
                return null;
            } else if (r instanceof Boolean)
            {
                return (Booleanr;
            } else
            {
                return Boolean.valueOf(r.toString());
            }
        } else
        {
            return Boolean.valueOf(expression);
        }
    }

    
A shortcut MethodBinding which just returns a single string result - useful when an action should just return a certain result, not call a method.
    private static class ActionMethodBinding extends MethodBinding implements Serializable
    {
        private String result;
        public ActionMethodBinding(String result)
        {
            this. = result;
        }
        public Object invoke(FacesContext contextObject params[])
        {
            return ;
        }
        public String getExpressionString()
        {
            return ;
        }
        public Class getType(FacesContext context)
        {
            return String.class;
        }
    }
New to GrepCode? Check out our FAQ X