View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.event;
20  
21  import javax.el.ValueExpression;
22  
23  import javax.faces.component.StateHolder;
24  import javax.faces.context.FacesContext;
25  import javax.faces.event.ActionEvent;
26  import javax.faces.event.ActionListener;
27  
28  import org.apache.myfaces.trinidad.bean.FacesBean;
29  import org.apache.myfaces.trinidad.bean.FacesBeanImpl;
30  import org.apache.myfaces.trinidad.bean.PropertyKey;
31  import org.apache.myfaces.trinidad.logging.TrinidadLogger;
32  
33  /**
34   * ActionListener that supports getting a value from
35   * one binding and setting it on another.
36   */
37  public class SetActionListener implements ActionListener, StateHolder
38  {
39    /**
40     * Creates a SetActionListener.
41     */
42    public SetActionListener()
43    {
44      _bean = new Bean();
45    }
46  
47    /**
48     * Gets the value from the "from" property and sets it on 
49     * the ValueBinding for the "to" property
50     */
51    public void processAction(ActionEvent event)
52    {
53      ValueExpression to = _bean.getValueExpression(Bean.TO_KEY);
54      if (to != null)
55      {
56        Object from = getFrom();
57        try
58        {
59          to.setValue(FacesContext.getCurrentInstance().getELContext(), from);
60        }
61        catch (RuntimeException e)
62        {
63          if (_LOG.isWarning())
64          {
65            ValueExpression fromExpression = _bean.getValueExpression(Bean.FROM_KEY);
66            String mes = "Error setting:'"+to.getExpressionString() +
67              "' to value:"+from;
68            if (fromExpression != null)
69              mes += " from:'"+fromExpression.getExpressionString()+"'";
70              
71            _LOG.warning(mes, e);
72          }
73          throw e;
74        }
75      }
76    }
77  
78    public ValueExpression getValueExpression(String name)
79    {
80      PropertyKey key = Bean.TYPE.findKey(name);
81      if (key == null)
82        return null;
83  
84      return _bean.getValueExpression(key);
85    }
86  
87    public void setValueExpression(String name, ValueExpression binding)
88    {
89      PropertyKey key = Bean.TYPE.findKey(name);
90      if (key == null)
91        throw new IllegalArgumentException();
92      _bean.setValueExpression(key, binding);
93    }
94  
95    public Object getFrom()
96    {
97      return _bean.getProperty(Bean.FROM_KEY);
98    }
99  
100   public void setFrom(Object from)
101   {
102     _bean.setProperty(Bean.FROM_KEY, from);
103   }
104 
105   public Object saveState(FacesContext context)
106   {
107     return _bean.saveState(context);
108   }
109 
110   public void restoreState(FacesContext context, Object state)
111   {
112     _bean.restoreState(context, state);
113   }
114 
115   public boolean isTransient()
116   {
117     return false;
118   }
119 
120   public void setTransient(boolean newTransientValue)
121   {
122     throw new UnsupportedOperationException();
123   }
124 
125   // saveState() and restoreState() come from FacesBeanImpl
126   static private class Bean extends FacesBeanImpl
127   {
128     static public final FacesBean.Type TYPE = new FacesBean.Type();
129     static public final PropertyKey FROM_KEY =
130       TYPE.registerKey("from");
131     // Must be a ValueExpression
132     static public final PropertyKey TO_KEY =
133       TYPE.registerKey("to");
134 
135 
136     @Override
137     public Type getType()
138     {
139       return TYPE;
140     }
141 
142     static
143     {
144       TYPE.lock();
145     }
146   }
147 
148   private Bean _bean;
149 
150   private static final TrinidadLogger _LOG = TrinidadLogger.createTrinidadLogger(SetActionListener.class);
151 }