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.switchcase;
23  
24  import java.io.IOException;
25  import java.util.Objects;
26  
27  import javax.faces.FacesException;
28  import javax.faces.component.UIComponent;
29  import javax.faces.component.UIComponentBase;
30  import javax.faces.component.visit.VisitCallback;
31  import javax.faces.component.visit.VisitContext;
32  import javax.faces.context.FacesContext;
33  import javax.faces.event.FacesEvent;
34  import javax.faces.event.PhaseId;
35  
36  /**
37   * Component class for the <code>Switch</code> component.
38   *
39   * @author Michael Gmeiner / last modified by Melloware
40   * @since 0.6
41   */
42  public class Switch extends UIComponentBase {
43  
44      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.Switch";
45      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
46  
47      @SuppressWarnings("java:S115")
48      protected enum PropertyKeys {
49          value
50      }
51  
52      public Switch() {
53          setRendererType(null);
54      }
55  
56      @Override
57      public String getFamily() {
58          return COMPONENT_FAMILY;
59      }
60  
61      public Object getValue() {
62          return getStateHelper().eval(PropertyKeys.value, null);
63      }
64  
65      public void setValue(final Object value) {
66          getStateHelper().put(PropertyKeys.value, value);
67      }
68  
69      private void evaluate() {
70          DefaultCase caseToRender = null;
71          DefaultCase defaultCase = null;
72  
73          for (final UIComponent child : getChildren()) {
74              child.setRendered(false);
75  
76              if (child instanceof Case) {
77                  final Case caseComponent = (Case) child;
78                  final Object evaluate = getValue();
79                  final Object caseValue = caseComponent.getValue();
80  
81                  if (Objects.equals(evaluate, caseValue)) {
82                      caseToRender = caseComponent;
83                  }
84              }
85              else if (child instanceof DefaultCase) {
86                  defaultCase = (DefaultCase) child;
87              }
88              else {
89                  throw new FacesException("Switch only accepts case or defaultCase as children.");
90              }
91          }
92  
93          if (caseToRender == null) {
94              caseToRender = defaultCase;
95          }
96  
97          if (caseToRender != null) {
98              caseToRender.setRendered(true);
99          }
100     }
101 
102     @Override
103     public void processDecodes(final FacesContext context) {
104         evaluate();
105         super.processDecodes(context);
106     }
107 
108     @Override
109     public void processValidators(final FacesContext context) {
110         evaluate();
111         super.processValidators(context);
112     }
113 
114     @Override
115     public void processUpdates(final FacesContext context) {
116         evaluate();
117         super.processUpdates(context);
118     }
119 
120     @Override
121     public void broadcast(final FacesEvent event) {
122         evaluate();
123         super.broadcast(event);
124     }
125 
126     @Override
127     public boolean visitTree(final VisitContext context, final VisitCallback callback) {
128         // mustn't evaluate cases during Restore View
129         if (context.getFacesContext().getCurrentPhaseId() != PhaseId.RESTORE_VIEW) {
130             evaluate();
131         }
132         return super.visitTree(context, callback);
133     }
134 
135     @Override
136     public void encodeBegin(final FacesContext context) throws IOException {
137         evaluate();
138         super.encodeBegin(context);
139     }
140 
141 }