Coverage Report - javax.faces.component.UICommand
 
Classes in this File Line Coverage Branch Coverage Complexity
UICommand
0%
0/84
0%
0/34
0
 
 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 javax.faces.component;
 20  
 
 21  
 import javax.el.MethodExpression;
 22  
 import javax.el.ValueExpression;
 23  
 import javax.faces.context.FacesContext;
 24  
 import javax.faces.el.MethodBinding;
 25  
 import javax.faces.event.AbortProcessingException;
 26  
 import javax.faces.event.ActionEvent;
 27  
 import javax.faces.event.ActionListener;
 28  
 import javax.faces.event.FacesEvent;
 29  
 import javax.faces.event.PhaseId;
 30  
 
 31  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFComponent;
 32  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFListener;
 33  
 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFProperty;
 34  
 
 35  
 /**
 36  
  * 
 37  
  * UICommand is a base abstraction for components that implement ActionSource.
 38  
  * 
 39  
  */
 40  
 @JSFComponent(defaultRendererType = "javax.faces.Button")
 41  
 public class UICommand extends UIComponentBase implements ActionSource2
 42  
 {
 43  
     public static final String COMPONENT_TYPE = "javax.faces.Command";
 44  
     public static final String COMPONENT_FAMILY = "javax.faces.Command";
 45  
 
 46  
     private boolean _immediate;
 47  
     private boolean _immediateSet;
 48  
     private Object _value;
 49  
     private MethodExpression _actionExpression;
 50  
     private MethodBinding _actionListener;
 51  
 
 52  
     /**
 53  
      * Construct an instance of the UICommand.
 54  
      */
 55  
     public UICommand()
 56  0
     {
 57  0
         setRendererType("javax.faces.Button");
 58  0
     }
 59  
 
 60  
     /**
 61  
      * Specifies the action to take when this command is invoked.
 62  
      * <p>
 63  
      * If the value is an expression, it is expected to be a method 
 64  
      * binding EL expression that identifies an action method. An action method
 65  
      * accepts no parameters and has a String return value, called the action
 66  
      * outcome, that identifies the next view displayed. The phase that this
 67  
      * event is fired in can be controlled via the immediate attribute.
 68  
      * </p>
 69  
      * <p>
 70  
      * If the value is a string literal, it is treated as a navigation outcome
 71  
      * for the current view.  This is functionally equivalent to a reference to
 72  
      * an action method that returns the string literal.
 73  
      * </p>
 74  
      * 
 75  
      * @deprecated Use getActionExpression() instead.
 76  
      */
 77  
     public MethodBinding getAction()
 78  
     {
 79  0
         MethodExpression actionExpression = getActionExpression();
 80  0
         if (actionExpression instanceof _MethodBindingToMethodExpression)
 81  
         {
 82  0
             return ((_MethodBindingToMethodExpression) actionExpression)
 83  
                     .getMethodBinding();
 84  
         }
 85  0
         if (actionExpression != null)
 86  
         {
 87  0
             return new _MethodExpressionToMethodBinding(actionExpression);
 88  
         }
 89  0
         return null;
 90  
     }
 91  
 
 92  
     /**
 93  
      * @deprecated Use setActionExpression instead.
 94  
      */
 95  
     public void setAction(MethodBinding action)
 96  
     {
 97  0
         if (action != null)
 98  
         {
 99  0
             setActionExpression(new _MethodBindingToMethodExpression(action));
 100  
         }
 101  
         else
 102  
         {
 103  0
             setActionExpression(null);
 104  
         }
 105  0
     }
 106  
 
 107  
     @Override
 108  
     public void broadcast(FacesEvent event) throws AbortProcessingException
 109  
     {
 110  0
         super.broadcast(event);
 111  
 
 112  0
         if (event instanceof ActionEvent)
 113  
         {
 114  0
             FacesContext context = getFacesContext();
 115  
 
 116  0
             MethodBinding mb = getActionListener();
 117  0
             if (mb != null)
 118  
             {
 119  0
                 mb.invoke(context, new Object[]
 120  
                 { event });
 121  
             }
 122  
 
 123  0
             ActionListener defaultActionListener = context.getApplication()
 124  
                     .getActionListener();
 125  0
             if (defaultActionListener != null)
 126  
             {
 127  0
                 defaultActionListener.processAction((ActionEvent) event);
 128  
             }
 129  
         }
 130  0
     }
 131  
 
 132  
     @Override
 133  
     public void queueEvent(FacesEvent event)
 134  
     {
 135  0
         if (event != null && event instanceof ActionEvent)
 136  
         {
 137  0
             if (isImmediate())
 138  
             {
 139  0
                 event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
 140  
             }
 141  
             else
 142  
             {
 143  0
                 event.setPhaseId(PhaseId.INVOKE_APPLICATION);
 144  
             }
 145  
         }
 146  0
         super.queueEvent(event);
 147  0
     }
 148  
 
 149  
     /**
 150  
      * A boolean value that identifies the phase during which action events
 151  
      * should fire.
 152  
      * <p>
 153  
      * During normal event processing, action methods and action listener methods are fired during the
 154  
      * "invoke application" phase of request processing. If this attribute is set to "true", these methods
 155  
      * are fired instead at the end of the "apply request values" phase.
 156  
      * </p>
 157  
      */
 158  
     @JSFProperty
 159  
     public boolean isImmediate()
 160  
     {
 161  0
         if (_immediateSet)
 162  
         {
 163  0
             return _immediate;
 164  
         }
 165  0
         ValueExpression expression = getValueExpression("immediate");
 166  0
         if (expression != null)
 167  
         {
 168  0
             return (Boolean) expression.getValue(getFacesContext()
 169  
                     .getELContext());
 170  
         }
 171  0
         return false;
 172  
     }
 173  
 
 174  
     public void setImmediate(boolean immediate)
 175  
     {
 176  0
         this._immediate = immediate;
 177  0
         this._immediateSet = true;
 178  0
     }
 179  
 
 180  
     /**
 181  
      * The text to display to the user for this command component.
 182  
      */
 183  
     @JSFProperty
 184  
     public Object getValue()
 185  
     {
 186  0
         if (_value != null)
 187  
         {
 188  0
             return _value;
 189  
         }
 190  0
         ValueExpression expression = getValueExpression("value");
 191  0
         if (expression != null)
 192  
         {
 193  0
             return expression.getValue(getFacesContext().getELContext());
 194  
         }
 195  0
         return null;
 196  
     }
 197  
 
 198  
     public void setValue(Object value)
 199  
     {
 200  0
         this._value = value;
 201  0
     }
 202  
 
 203  
     /**
 204  
      * The action to take when this command is invoked.
 205  
      * <p>
 206  
      * If the value is an expression, it is expected to be a method binding EL expression that identifies
 207  
      * an action method. An action method accepts no parameters and has a String return value, called the
 208  
      * action outcome, that identifies the next view displayed. The phase that this event is fired in
 209  
      * can be controlled via the immediate attribute.
 210  
      * </p>
 211  
      * <p> 
 212  
      * If the value is a string literal, it is treated as a navigation outcome for the current view. This
 213  
      * is functionally equivalent to a reference to an action method that returns the string literal.
 214  
      * </p>
 215  
      */
 216  
     @JSFProperty(stateHolder = true, returnSignature = "java.lang.Object", jspName = "action")
 217  
     public MethodExpression getActionExpression()
 218  
     {
 219  0
         if (_actionExpression != null)
 220  
         {
 221  0
             return _actionExpression;
 222  
         }
 223  0
         ValueExpression expression = getValueExpression("actionExpression");
 224  0
         if (expression != null)
 225  
         {
 226  0
             return (MethodExpression) expression.getValue(getFacesContext()
 227  
                     .getELContext());
 228  
         }
 229  0
         return null;
 230  
     }
 231  
 
 232  
     public void setActionExpression(MethodExpression actionExpression)
 233  
     {
 234  0
         this._actionExpression = actionExpression;
 235  0
     }
 236  
 
 237  
     /**
 238  
      * A method binding EL expression that identifies an action listener method to be invoked if
 239  
      * this component is activated by the user.
 240  
      * <p>
 241  
      * An action listener method accepts a parameter of type javax.faces.event.ActionEvent and returns void.
 242  
      * The phase that this event is fired in can be controlled via the immediate attribute.
 243  
      * 
 244  
      * @deprecated
 245  
      */
 246  
     @JSFProperty(stateHolder = true, returnSignature = "void", methodSignature = "javax.faces.event.ActionEvent")
 247  
     public MethodBinding getActionListener()
 248  
     {
 249  0
         if (_actionListener != null)
 250  
         {
 251  0
             return _actionListener;
 252  
         }
 253  0
         ValueExpression expression = getValueExpression("actionListener");
 254  0
         if (expression != null)
 255  
         {
 256  0
             return (MethodBinding) expression.getValue(getFacesContext()
 257  
                     .getELContext());
 258  
         }
 259  0
         return null;
 260  
     }
 261  
 
 262  
     /**
 263  
      * @deprecated
 264  
      */
 265  
     @JSFProperty(returnSignature="void",methodSignature="javax.faces.event.ActionEvent")
 266  
     public void setActionListener(MethodBinding actionListener)
 267  
     {
 268  0
         this._actionListener = actionListener;
 269  0
     }
 270  
 
 271  
     public void addActionListener(ActionListener listener)
 272  
     {
 273  0
         addFacesListener(listener);
 274  0
     }
 275  
 
 276  
     public void removeActionListener(ActionListener listener)
 277  
     {
 278  0
         removeFacesListener(listener);
 279  0
     }
 280  
 
 281  
     /**
 282  
      * Event delivered when the "action" of the component has been
 283  
      * invoked; for example, by clicking on a button. The action may result 
 284  
      * in page navigation.
 285  
      */
 286  
     @JSFListener(event="javax.faces.event.ActionEvent",
 287  
             phases="Invoke Application, Apply Request Values")
 288  
     public ActionListener[] getActionListeners()
 289  
     {
 290  0
         return (ActionListener[]) getFacesListeners(ActionListener.class);
 291  
     }
 292  
 
 293  
     @Override
 294  
     public Object saveState(FacesContext facesContext)
 295  
     {
 296  0
         Object[] values = new Object[6];
 297  0
         values[0] = super.saveState(facesContext);
 298  0
         values[1] = _immediate;
 299  0
         values[2] = _immediateSet;
 300  0
         values[3] = _value;
 301  0
         values[4] = saveAttachedState(facesContext, _actionExpression);
 302  0
         values[5] = saveAttachedState(facesContext, _actionListener);
 303  
 
 304  0
         return values;
 305  
     }
 306  
 
 307  
     @Override
 308  
     public void restoreState(FacesContext facesContext, Object state)
 309  
     {
 310  0
         Object[] values = (Object[]) state;
 311  0
         super.restoreState(facesContext, values[0]);
 312  0
         _immediate = (Boolean) values[1];
 313  0
         _immediateSet = (Boolean) values[2];
 314  0
         _value = values[3];
 315  0
         _actionExpression = (MethodExpression) restoreAttachedState(
 316  
                 facesContext, values[4]);
 317  0
         _actionListener = (MethodBinding) restoreAttachedState(facesContext,
 318  
                 values[5]);
 319  0
     }
 320  
 
 321  
     @Override
 322  
     public String getFamily()
 323  
     {
 324  0
         return COMPONENT_FAMILY;
 325  
     }
 326  
 }