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.SupportsHelp;
23  import org.apache.myfaces.tobago.component.SupportsLabelLayout;
24  import org.apache.myfaces.tobago.component.Visual;
25  import org.apache.myfaces.tobago.util.MessageUtils;
26  
27  import javax.faces.application.FacesMessage;
28  import javax.faces.component.behavior.ClientBehaviorHolder;
29  import javax.faces.context.FacesContext;
30  
31  /**
32   * Base class for select one.
33   */
34  public abstract class AbstractUISelectOneBase extends javax.faces.component.UISelectOne
35      implements Visual, SupportsLabelLayout, ClientBehaviorHolder, SupportsHelp {
36  
37    public static final String MESSAGE_VALUE_REQUIRED = "org.apache.myfaces.tobago.UISelectOne.REQUIRED";
38  
39    private transient boolean nextToRenderIsLabel;
40  
41    @Override
42    public void validate(final FacesContext facesContext) {
43      if (isRequired() && !isReadonly()) {
44        final Object submittedValue = getSubmittedValue();
45        if (submittedValue == null || "".equals(submittedValue)) {
46          if (getRequiredMessage() != null) {
47            final String requiredMessage = getRequiredMessage();
48            facesContext.addMessage(getClientId(facesContext), new FacesMessage(FacesMessage.SEVERITY_ERROR,
49                requiredMessage, requiredMessage));
50          } else {
51            facesContext.addMessage(getClientId(facesContext),
52                MessageUtils.getMessage(facesContext, FacesMessage.SEVERITY_ERROR, MESSAGE_VALUE_REQUIRED,
53                    MessageUtils.getLabel(facesContext, this)));
54          }
55          setValid(false);
56        }
57      }
58      super.validate(facesContext);
59    }
60  
61    public abstract boolean isReadonly();
62  
63    public abstract boolean isDisabled();
64  
65    public boolean isError() {
66      final FacesContext facesContext = FacesContext.getCurrentInstance();
67      return !isValid()
68          || !facesContext.getMessageList(getClientId(facesContext)).isEmpty();
69    }
70  
71    public abstract boolean isFocus();
72  
73    public abstract Integer getTabIndex();
74  
75    @Override
76    public boolean isNextToRenderIsLabel() {
77      return nextToRenderIsLabel;
78    }
79  
80    @Override
81    public void setNextToRenderIsLabel(final boolean nextToRenderIsLabel) {
82      this.nextToRenderIsLabel = nextToRenderIsLabel;
83    }
84  }