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