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