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.suggestajax.tablesuggestajax;
20  
21  import javax.faces.application.Application;
22  import javax.faces.el.ValueBinding;
23  import javax.faces.webapp.UIComponentTag;
24  
25  import org.apache.myfaces.custom.suggestajax.AbstractSuggestAjaxTag;
26  import org.apache.myfaces.custom.suggestajax.SuggestAjax;
27  
28  import com.sun.facelets.FaceletContext;
29  import com.sun.facelets.tag.TagAttribute;
30  import com.sun.facelets.tag.jsf.ComponentConfig;
31  import com.sun.facelets.tag.jsf.html.HtmlComponentHandler;
32  
33  public class TableSuggestAjaxTagHandler extends HtmlComponentHandler
34  {
35      private static final String SUGGESTED_ITEMS_METHOD = "suggestedItemsMethod";
36      private static final String MAX_SUGGESTED_ITEMS = "maxSuggestedItems";
37  
38      private TagAttribute suggestedItemsMethodAttr;
39      private TagAttribute maxSuggestedItemsAttr;
40      
41      public TableSuggestAjaxTagHandler(ComponentConfig config)
42      {
43          super(config);
44          suggestedItemsMethodAttr = getAttribute(SUGGESTED_ITEMS_METHOD);
45          maxSuggestedItemsAttr = getAttribute(MAX_SUGGESTED_ITEMS);
46      }
47      
48      protected void setAttributes(FaceletContext ctx, Object instance)
49      {
50          super.setAttributes(ctx, instance);
51  
52          Application app = ctx.getFacesContext().getApplication();
53          
54          SuggestAjax comp = (SuggestAjax) instance;
55          
56          if (maxSuggestedItemsAttr != null){
57              String maxSuggestedItems = maxSuggestedItemsAttr.getValue();
58              
59              if (maxSuggestedItems != null)
60              {
61                  if (UIComponentTag.isValueReference(maxSuggestedItems))
62                  {
63                      ValueBinding vb = app.createValueBinding(maxSuggestedItems);
64                      comp.setValueBinding(MAX_SUGGESTED_ITEMS, vb);
65                  }
66                  else
67                  {
68                      comp.getAttributes().put(MAX_SUGGESTED_ITEMS, Integer.valueOf(maxSuggestedItems));
69                  }
70              } 
71          }
72          
73          if (suggestedItemsMethodAttr != null){
74              String suggestedItemsMethod = suggestedItemsMethodAttr.getValue();
75              if (suggestedItemsMethod != null)
76              {
77                  AbstractSuggestAjaxTag.setSuggestedItemsMethodProperty(ctx.getFacesContext(),
78                          comp,suggestedItemsMethod);
79              }
80          }
81      }
82      
83  }