View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.tristatemanycheckbox;
23  
24  import java.util.Collection;
25  import java.util.Iterator;
26  import java.util.Map;
27  
28  import javax.faces.application.FacesMessage;
29  import javax.faces.application.ResourceDependency;
30  import javax.faces.component.UISelectMany;
31  import javax.faces.component.html.HtmlSelectManyCheckbox;
32  import javax.faces.context.FacesContext;
33  import javax.faces.validator.Validator;
34  import javax.faces.validator.ValidatorException;
35  
36  import org.primefaces.component.api.Widget;
37  import org.primefaces.extensions.util.MessageFactory;
38  import org.primefaces.util.Constants;
39  
40  /**
41   * TriStateManyCheckbox
42   *
43   * @author Mauricio Fenoglio / last modified by $Author$
44   * @version $Revision$
45   * @since 0.3
46   */
47  @ResourceDependency(library = "primefaces", name = "components.css")
48  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
49  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
50  @ResourceDependency(library = "primefaces", name = "core.js")
51  @ResourceDependency(library = "primefaces-extensions", name = "primefaces-extensions.js")
52  @ResourceDependency(library = "primefaces-extensions", name = "tristatemanycheckbox/tristatemanycheckbox.js")
53  public class TriStateManyCheckbox extends HtmlSelectManyCheckbox implements Widget {
54  
55      public static final String UI_ICON = "ui-icon ";
56  
57      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.TriStateManyCheckbox";
58      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
59      private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.";
60  
61      /**
62       * PropertyKeys
63       *
64       * @author Oleg Varaksin / last modified by $Author$
65       * @version $Revision$
66       */
67      @SuppressWarnings("java:S115")
68      protected enum PropertyKeys {
69  
70          //@formatter:off CHECKSTYLE:OFF
71          widgetVar,
72          layout,
73          stateOneIcon,
74          stateTwoIcon,
75          stateThreeIcon,
76          stateOneTitle,
77          stateTwoTitle,
78          stateThreeTitle
79          //@formatter:on CHECKSTYLE:ON
80      }
81  
82      public TriStateManyCheckbox() {
83          setRendererType(DEFAULT_RENDERER);
84      }
85  
86      @Override
87      public String getFamily() {
88          return COMPONENT_FAMILY;
89      }
90  
91      public String getWidgetVar() {
92          return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.widgetVar, null);
93      }
94  
95      public void setWidgetVar(final String widgetVar) {
96          getStateHelper().put(TriStateManyCheckbox.PropertyKeys.widgetVar, widgetVar);
97      }
98  
99      @Override
100     public String getLayout() {
101         return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.layout, null);
102     }
103 
104     @Override
105     public void setLayout(final String layout) {
106         getStateHelper().put(TriStateManyCheckbox.PropertyKeys.layout, layout);
107     }
108 
109     public String getStateOneIcon() {
110         return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.stateOneIcon, null);
111     }
112 
113     public void setStateOneIcon(final String stateOneIcon) {
114         getStateHelper().put(TriStateManyCheckbox.PropertyKeys.stateOneIcon, stateOneIcon);
115     }
116 
117     public String getStateTwoIcon() {
118         return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.stateTwoIcon, null);
119     }
120 
121     public void setStateTwoIcon(final String stateTwoIcon) {
122         getStateHelper().put(TriStateManyCheckbox.PropertyKeys.stateTwoIcon, stateTwoIcon);
123     }
124 
125     public String getStateThreeIcon() {
126         return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.stateThreeIcon, null);
127     }
128 
129     public void setStateThreeIcon(final String stateThreeIcon) {
130         getStateHelper().put(TriStateManyCheckbox.PropertyKeys.stateThreeIcon, stateThreeIcon);
131     }
132 
133     public String getStateOneTitle() {
134         return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.stateOneTitle, Constants.EMPTY_STRING);
135     }
136 
137     public void setStateOneTitle(final String stateOneTitle) {
138         getStateHelper().put(TriStateManyCheckbox.PropertyKeys.stateOneTitle, stateOneTitle);
139     }
140 
141     public String getStateTwoTitle() {
142         return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.stateTwoTitle, Constants.EMPTY_STRING);
143     }
144 
145     public void setStateTwoTitle(final String stateTwoTitle) {
146         getStateHelper().put(TriStateManyCheckbox.PropertyKeys.stateTwoTitle, stateTwoTitle);
147     }
148 
149     public String getStateThreeTitle() {
150         return (String) getStateHelper().eval(TriStateManyCheckbox.PropertyKeys.stateThreeTitle, Constants.EMPTY_STRING);
151     }
152 
153     public void setStateThreeTitle(final String stateThreeTitle) {
154         getStateHelper().put(TriStateManyCheckbox.PropertyKeys.stateThreeTitle, stateThreeTitle);
155     }
156 
157     @Override
158     protected void validateValue(final FacesContext context, final Object value) {
159         final Map<Object, Object> mapValues = (Map) value;
160 
161         // call all validators
162         final Validator[] validators = getValidators();
163         if (getValidators() != null) {
164             for (final Validator validator : validators) {
165                 final Iterator<Object> it = mapValues.values().iterator();
166                 while (it.hasNext()) {
167                     final Object newValue = it.next();
168                     try {
169                         validator.validate(context, this, newValue);
170                     }
171                     catch (final ValidatorException ve) {
172                         // If the validator throws an exception, we're
173                         // invalid, and we need to add a message
174                         setValid(false);
175 
176                         FacesMessage message;
177                         final String validatorMessageString = getValidatorMessage();
178 
179                         if (null != validatorMessageString) {
180                             message = new FacesMessage(FacesMessage.SEVERITY_ERROR, validatorMessageString, validatorMessageString);
181                             message.setSeverity(FacesMessage.SEVERITY_ERROR);
182                         }
183                         else {
184                             final Collection<FacesMessage> messages = ve.getFacesMessages();
185                             if (null != messages) {
186                                 message = null;
187 
188                                 final String cid = getClientId(context);
189                                 for (final FacesMessage m : messages) {
190                                     context.addMessage(cid, m);
191                                 }
192                             }
193                             else {
194                                 message = ve.getFacesMessage();
195                             }
196                         }
197 
198                         if (message != null) {
199                             context.addMessage(getClientId(context), message);
200                         }
201                     }
202                 }
203             }
204         }
205 
206         boolean doAddMessage = false;
207 
208         // Ensure that if the state are all 0 and a
209         // value is required, a message is queued
210         if (isRequired()
211                     && isValid()) {
212             final Iterator<Object> it = mapValues.values().iterator();
213             boolean cCheck = true;
214             while (it.hasNext() && cCheck) {
215                 final Object val = it.next();
216                 if (!"0".equals(getConverter().getAsString(context, this, val))) {
217                     cCheck = false;
218                 }
219             }
220 
221             if (cCheck) {
222                 doAddMessage = true;
223             }
224         }
225 
226         if (doAddMessage) {
227             final Object[] params = new Object[2];
228             params[0] = MessageFactory.getLabel(context, this);
229 
230             // Enqueue an error message if an invalid value was specified
231             final FacesMessage message = org.primefaces.util.MessageFactory.getFacesMessage(UISelectMany.INVALID_MESSAGE_ID,
232                         FacesMessage.SEVERITY_ERROR, params);
233             context.addMessage(getClientId(context), message);
234             setValid(false);
235         }
236     }
237 }