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  
20  package org.apache.myfaces.tobago.facelets;
21  
22  import org.apache.myfaces.tobago.event.TabChangeListener;
23  import org.apache.myfaces.tobago.event.TabChangeSource;
24  import org.apache.myfaces.tobago.event.ValueExpressionTabChangeListener;
25  
26  import javax.el.ELException;
27  import javax.el.ValueExpression;
28  import javax.faces.FacesException;
29  import javax.faces.component.UIComponent;
30  import javax.faces.view.facelets.FaceletContext;
31  import javax.faces.view.facelets.TagAttribute;
32  import javax.faces.view.facelets.TagAttributeException;
33  import javax.faces.view.facelets.TagConfig;
34  import javax.faces.view.facelets.TagException;
35  import javax.faces.view.facelets.TagHandler;
36  import java.io.IOException;
37  
38  public class TabChangeListenerHandler extends TagHandler {
39  
40    private Class listenerType;
41  
42    private final TagAttribute type;
43  
44    private final TagAttribute binding;
45  
46  
47    public TabChangeListenerHandler(TagConfig config) {
48      super(config);
49      binding = getAttribute("binding");
50      type = getAttribute("type");
51      if (type != null) {
52        if (!type.isLiteral()) {
53          throw new TagAttributeException(tag, type, "Must be literal");
54        }
55        try {
56          this.listenerType = Class.forName(type.getValue());
57        } catch (Exception e) {
58          throw new TagAttributeException(tag, type, e);
59        }
60      }
61    }
62  
63    public void apply(FaceletContext faceletContext, UIComponent parent)
64        throws IOException, FacesException, ELException {
65      if (parent instanceof TabChangeSource) {
66        // only process if parent was just created
67        if (parent.getParent() == null) {
68          TabChangeSource changeSource = (TabChangeSource) parent;
69          TabChangeListener listener = null;
70          ValueExpression valueExpression = null;
71          if (binding != null) {
72            valueExpression = binding.getValueExpression(faceletContext, TabChangeListener.class);
73            listener = (TabChangeListener) valueExpression.getValue(faceletContext);
74          }
75          if (listener == null) {
76            try {
77              listener = (TabChangeListener) listenerType.newInstance();
78            } catch (Exception e) {
79              throw new TagAttributeException(tag, type, e.getCause());
80            }
81            if (valueExpression != null) {
82              valueExpression.setValue(faceletContext, listener);
83            }
84          }
85          if (valueExpression != null) {
86            changeSource.addTabChangeListener(
87                new ValueExpressionTabChangeListener(type.getValue(), valueExpression));
88          } else {
89            changeSource.addTabChangeListener(listener);
90          }
91        }
92      } else {
93        throw new TagException(tag, "Parent is not of type TabChangeSource, type is: " + parent);
94      }
95    }
96  }