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