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.internal.component;
21  
22  import org.apache.myfaces.tobago.component.LabelLayout;
23  import org.apache.myfaces.tobago.component.SupportFieldId;
24  import org.apache.myfaces.tobago.component.SupportsAccessKey;
25  import org.apache.myfaces.tobago.component.SupportsLabelLayout;
26  import org.apache.myfaces.tobago.component.Visual;
27  import org.apache.myfaces.tobago.util.ComponentUtils;
28  import org.apache.myfaces.tobago.util.MessageUtils;
29  
30  import javax.faces.application.FacesMessage;
31  import javax.faces.component.UISelectBoolean;
32  import javax.faces.component.behavior.ClientBehaviorHolder;
33  import javax.faces.context.FacesContext;
34  
35  /**
36   * {@link org.apache.myfaces.tobago.internal.taglib.component.SelectBooleanCheckboxTagDeclaration}
37   */
38  public abstract class AbstractUISelectBooleanCheckbox extends UISelectBoolean
39      implements Visual, ClientBehaviorHolder, SupportFieldId, SupportsAccessKey, SupportsLabelLayout {
40  
41    @Override
42    public boolean isSelected() {
43      Object value = getSubmittedValue();
44      if (value == null) {
45        value = getValue();
46      }
47      if (value instanceof Boolean) {
48        return (Boolean) value;
49      } else {
50        return value != null && Boolean.valueOf(value.toString());
51      }
52    }
53  
54    @Override
55    protected void validateValue(final FacesContext facesContext, final Object convertedValue) {
56      if (isRequired()) {
57        if (convertedValue instanceof Boolean && !((Boolean) convertedValue)
58            // String: e. g. if there is no ValueExpression
59            || convertedValue instanceof String && !Boolean.parseBoolean((String) convertedValue)) {
60          MessageUtils.addMessage(
61              facesContext, this, FacesMessage.SEVERITY_ERROR, REQUIRED_MESSAGE_ID, new Object[]{getId()});
62          setValid(false);
63          return;
64        }
65      }
66      super.validateValue(facesContext, convertedValue);
67    }
68  
69    public abstract boolean isDisabled();
70  
71    public abstract boolean isReadonly();
72  
73    public abstract boolean isFocus();
74  
75    public abstract Integer getTabIndex();
76  
77    public abstract String getLabel();
78  
79    public abstract String getItemLabel();
80  
81    public abstract void setItemLabel(String itemLabel);
82  
83    @Override
84    public String getFieldId(final FacesContext facesContext) {
85      return getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "field";
86    }
87  
88    public boolean isLabelLayoutSkip() {
89      return getLabelLayout() == LabelLayout.skip;
90    }
91  }