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