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