2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * $Id: PropertyActionCommand.java 471910 2006-11-06 22:44:56Z gvanmatre $
20   */
21  package org.apache.shale.clay.component.chain;
22  
23  import javax.faces.component.ActionSource;
24  import javax.faces.component.UIComponent;
25  import javax.faces.context.FacesContext;
26  
27  import org.apache.commons.chain.Command;
28  import org.apache.commons.chain.Context;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.shale.clay.config.beans.AttributeBean;
32  import org.apache.shale.clay.config.beans.ComponentBean;
33  
34  /***
35   * <p>
36   * This <code>Command</code> will create an <code>action</code> method
37   * binding and assign it to the <code>UIComponent</code> implementing the
38   * <code>ActionSource</code> interface.
39   * </p>
40   */
41  
42  public class PropertyActionCommand extends AbstractCommand implements Command {
43  
44      /***
45       * <p>
46       * Common logger utility class.
47       * </p>
48       */
49      private static Log log;
50      static {
51          log = LogFactory.getLog(PropertyActionCommand.class);
52      }
53  
54      /***
55       * <p>
56       * Looks to see if the {@link AttributeBean} on the {@link ClayContext} is a
57       * <code>action</code> attribute. If it is, create a
58       * <code>MethodBinding</code> and assign it to the component returning a
59       * <code>true</code> value. Otherwise, return a <code>false</code>
60       * value. This <code>Command</code> is invoked from the
61       * {@link AssignPropertiesCommand} chain.
62       * </p>
63       *
64       * @param context common chains
65       * @return <code>true</code> if the chain is complete
66       * @exception Exception propagated up to the top of the chain
67       */
68      public boolean execute(Context context) throws Exception {
69  
70          boolean isFinal = false;
71  
72          ClayContext clayContext = (ClayContext) context;
73          if (clayContext == null) {
74              throw new NullPointerException(getMessages().getMessage("clay.null.clayContext"));
75          }
76          AttributeBean attributeBean = clayContext.getAttribute();
77          if (attributeBean == null) {
78              throw new NullPointerException(getMessages().getMessage("clay.null.attributeBean"));
79          }
80          ComponentBean displayElement = clayContext.getDisplayElement();
81          if (displayElement == null) {
82              throw new NullPointerException(getMessages().getMessage("clay.null.componentBean"));
83          }
84          FacesContext facesContext = clayContext.getFacesContext();
85          if (facesContext == null) {
86              throw new NullPointerException(getMessages().getMessage("clay.null.facesContext"));
87          }
88  
89          if (attributeBean.getName().equals("action") && attributeBean.getValue() != null) {
90              isFinal = true;
91  
92              UIComponent child = (UIComponent) clayContext.getChild();
93              if (child == null) {
94                  throw new NullPointerException(getMessages().getMessage("clay.null.childComponent"));
95              }
96  
97              if (child instanceof ActionSource) {
98                  String expr = null;
99                  expr = replaceMnemonic(clayContext);
100                 getTagUtils().setAction(child, expr);
101             } else {
102                 log.error(getMessages().getMessage("property.action.error", new Object[] {attributeBean}));
103             }
104         }
105 
106         return isFinal;
107     }
108 }