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.SupportsHelp;
26  import org.apache.myfaces.tobago.component.SupportsLabelLayout;
27  import org.apache.myfaces.tobago.component.Visual;
28  import org.apache.myfaces.tobago.util.ComponentUtils;
29  import org.apache.myfaces.tobago.util.MessageUtils;
30  
31  import javax.faces.application.FacesMessage;
32  import javax.faces.component.UISelectBoolean;
33  import javax.faces.component.behavior.ClientBehaviorHolder;
34  import javax.faces.context.FacesContext;
35  
36  public abstract class AbstractUISelectBoolean extends UISelectBoolean
37      implements Visual, ClientBehaviorHolder, SupportFieldId, SupportsAccessKey, SupportsLabelLayout, SupportsHelp {
38  
39    private transient boolean nextToRenderIsLabel;
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          facesContext.addMessage(getClientId(facesContext),
61              MessageUtils.getMessage(facesContext, FacesMessage.SEVERITY_ERROR, REQUIRED_MESSAGE_ID, 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 boolean isError() {
74      final FacesContext facesContext = FacesContext.getCurrentInstance();
75      return !isValid()
76          || !facesContext.getMessageList(getClientId(facesContext)).isEmpty();
77    }
78  
79    public abstract boolean isFocus();
80  
81    public abstract Integer getTabIndex();
82  
83    public abstract String getItemLabel();
84  
85    public abstract void setItemLabel(String itemLabel);
86  
87    public abstract String getItemImage();
88  
89    @Override
90    public abstract String getLabel();
91  
92    @Override
93    public String getFieldId(final FacesContext facesContext) {
94      return getClientId(facesContext) + ComponentUtils.SUB_SEPARATOR + "field";
95    }
96  
97    public boolean isLabelLayoutSkip() {
98      return getLabelLayout() == LabelLayout.skip;
99    }
100 
101   @Override
102   public boolean isNextToRenderIsLabel() {
103     return nextToRenderIsLabel;
104   }
105 
106   @Override
107   public void setNextToRenderIsLabel(final boolean nextToRenderIsLabel) {
108     this.nextToRenderIsLabel = nextToRenderIsLabel;
109   }
110 }