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.component.InputSuggest;
23  import org.apache.myfaces.tobago.component.Visual;
24  import org.apache.myfaces.tobago.config.TobagoConfig;
25  import org.apache.myfaces.tobago.event.SheetStateChangeSource;
26  import org.apache.myfaces.tobago.event.SortActionSource;
27  import org.apache.myfaces.tobago.event.TabChangeSource;
28  import org.apache.myfaces.tobago.internal.component.AbstractUISegmentLayout;
29  import org.apache.myfaces.tobago.internal.config.TobagoConfigImpl;
30  
31  import javax.faces.component.EditableValueHolder;
32  import javax.faces.component.UIComponent;
33  import javax.faces.context.FacesContext;
34  import javax.faces.validator.Validator;
35  import javax.faces.view.facelets.ComponentConfig;
36  import javax.faces.view.facelets.ComponentHandler;
37  import javax.faces.view.facelets.FaceletContext;
38  import javax.faces.view.facelets.MetaRuleset;
39  import java.util.HashSet;
40  import java.util.Map;
41  import java.util.Set;
42  
43  public class TobagoComponentHandler extends ComponentHandler {
44  
45    public TobagoComponentHandler(final ComponentConfig componentConfig) {
46      super(componentConfig);
47    }
48  
49    @Override
50    protected MetaRuleset createMetaRuleset(final Class aClass) {
51      final MetaRuleset metaRuleset = super.createMetaRuleset(aClass);
52      if (SortActionSource.class.isAssignableFrom(aClass)) {
53        metaRuleset.addRule(SortActionSourceRule.INSTANCE);
54      }
55      if (AbstractUISegmentLayout.class.isAssignableFrom(aClass)) {
56        metaRuleset.addRule(SegmentLayoutRule.INSTANCE);
57      }
58      if (TabChangeSource.class.isAssignableFrom(aClass)) {
59        metaRuleset.addRule(TabChangeSourceRule.INSTANCE);
60      }
61      if (SheetStateChangeSource.class.isAssignableFrom(aClass)) {
62        metaRuleset.addRule(SheetStateChangeSourceRule.INSTANCE);
63      }
64      if (Visual.class.isAssignableFrom(aClass)) {
65        metaRuleset.addRule(SupportsMarkupRule.INSTANCE);
66      }
67      if (InputSuggest.class.isAssignableFrom(aClass)) {
68        metaRuleset.addRule(SuggestMethodRule.INSTANCE);
69      }
70  
71      return metaRuleset;
72    }
73  
74    @Override
75    public void onComponentPopulated(
76        final FaceletContext context, final UIComponent component, final UIComponent parent) {
77  
78      // TODO call only if component was created
79      if (component instanceof EditableValueHolder) {
80        addDefaultValidators(context.getFacesContext(), (EditableValueHolder) component);
81      }
82    }
83  
84    private void addDefaultValidators(final FacesContext context, final EditableValueHolder component) {
85      final TobagoConfigImpl tobagoConfig = (TobagoConfigImpl) TobagoConfig.getInstance(context);
86      final Map<String, String> validatorInfoMap = tobagoConfig.getDefaultValidatorInfo();
87      if (validatorInfoMap.isEmpty()) {
88        return;
89      }
90      final Validator[] validators = component.getValidators();
91      if (validators.length > 0) {
92        final Set<String> classNames = new HashSet<String>();
93        // collect classNames of validators
94        for (Validator validator : validators) {
95          classNames.add(validator.getClass().getName());
96        }
97        for (Map.Entry<String, String> entry : validatorInfoMap.entrySet()) {
98          if (!classNames.contains(entry.getValue())) {
99            component.addValidator(context.getApplication().createValidator((String) entry.getKey()));
100         }
101       }
102     } else {
103       for (String next : validatorInfoMap.keySet()) {
104         component.addValidator(context.getApplication().createValidator(next));
105       }
106     }
107   }
108 }