1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }