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(final 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 (final Exception e) {
56          throw new TagAttributeException(tag, type, e);
57        }
58      }
59    }
60  
61    @Override
62    public void apply(final FaceletContext faceletContext, final UIComponent parent) throws IOException {
63      if (parent instanceof TabChangeSource2) {
64        // only process if parent was just created
65        if (parent.getParent() == null) {
66          final TabChangeSource2 changeSource = (TabChangeSource2) parent;
67          TabChangeListener listener = null;
68          ValueExpression valueExpression = null;
69          if (binding != null) {
70            valueExpression = binding.getValueExpression(faceletContext, TabChangeListener.class);
71            listener = (TabChangeListener) valueExpression.getValue(faceletContext);
72          }
73          if (listener == null) {
74            try {
75              listener = (TabChangeListener) listenerType.newInstance();
76            } catch (final Exception e) {
77              throw new TagAttributeException(tag, type, e.getCause());
78            }
79            if (valueExpression != null) {
80              valueExpression.setValue(faceletContext, listener);
81            }
82          }
83          if (valueExpression != null) {
84            changeSource.addTabChangeListener(
85                new ValueExpressionTabChangeListener(type.getValue(), valueExpression));
86          } else {
87            changeSource.addTabChangeListener(listener);
88          }
89        }
90      } else {
91        throw new TagException(tag, "Parent is not of type TabChangeSource, type is: " + parent);
92      }
93    }
94  }