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.custom.updateactionlistener;
20  
21  import java.io.IOException;
22  
23  import javax.el.ELException;
24  import javax.faces.FacesException;
25  import javax.faces.application.Application;
26  import javax.faces.component.ActionSource;
27  import javax.faces.component.UIComponent;
28  import javax.faces.convert.Converter;
29  import javax.faces.event.ActionListener;
30  import javax.faces.webapp.UIComponentTag;
31  
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  
35  import com.sun.facelets.FaceletContext;
36  import com.sun.facelets.FaceletException;
37  import com.sun.facelets.tag.TagAttribute;
38  import com.sun.facelets.tag.TagConfig;
39  import com.sun.facelets.tag.TagHandler;
40  import com.sun.facelets.tag.jsf.ComponentConfig;
41  
42  /**
43   * Tag handler for the Tomahawk UpdateActionListener tag
44   * 
45   * @since 1.1.7
46   * @author arobinson74
47   */
48  public class UpdateActionListenerTagHandler extends TagHandler
49  {
50      private static Log logger = LogFactory
51              .getLog(UpdateActionListenerTagHandler.class);
52  
53      private TagAttribute converterAttr;
54      private TagAttribute propertyAttr;
55      private TagAttribute valueAttr;
56  
57      /**
58       * @param config
59       */
60      public UpdateActionListenerTagHandler(ComponentConfig config)
61      {
62          this((TagConfig) config);
63      }
64  
65      /**
66       * @param config
67       */
68      public UpdateActionListenerTagHandler(TagConfig config)
69      {
70          super(config);
71          valueAttr = getRequiredAttribute("value");
72          propertyAttr = getRequiredAttribute("property");
73          converterAttr = getAttribute("converter");
74      }
75  
76      /**
77       * @see com.sun.facelets.FaceletHandler#apply(com.sun.facelets.FaceletContext, javax.faces.component.UIComponent)
78       */
79      public void apply(FaceletContext ctx, UIComponent parent)
80              throws IOException, FacesException, FaceletException, ELException
81      {
82          logger.debug("Apply called. Component: " + parent);
83          ActionSource actionSource = (ActionSource) parent;
84  
85          if (sourceHasProperty(actionSource))
86              return;
87  
88          UpdateActionListener al = new UpdateActionListener();
89  
90          Application app = ctx.getFacesContext().getApplication();
91          if (converterAttr != null)
92          {
93              Converter converter = app.createConverter(converterAttr
94                      .getValue(ctx));
95              al.setConverter(converter);
96          }
97  
98          String value = valueAttr.getValue();
99          if (UIComponentTag.isValueReference(value))
100             al.setValueBinding(app.createValueBinding(valueAttr.getValue()));
101         else
102             al.setValue(value);
103 
104         al.setPropertyBinding(app.createValueBinding(propertyAttr.getValue()));
105 
106         actionSource.addActionListener(al);
107     }
108 
109     private boolean sourceHasProperty(ActionSource source)
110     {
111         ActionListener[] listeners = source.getActionListeners();
112         for (int i = 0; i < listeners.length; i++)
113         {
114             ActionListener listener = listeners[i];
115             if (listener instanceof UpdateActionListener == false)
116                 continue;
117             UpdateActionListener al = (UpdateActionListener) listener;
118             if (al.getPropertyBinding().getExpressionString().equals(
119                     this.propertyAttr.getValue()))
120             {
121                 logger.debug("Action listener already has a listener for "
122                         + this.propertyAttr.getValue());
123                 return true;
124             }
125         }
126         logger.debug("Action listener for property is not present. Property: "
127                 + this.propertyAttr.getValue());
128         return false;
129     }
130 }