1 // WARNING: This file was automatically generated. Do not edit it directly,
2 // or you will lose your changes.
3
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 */
22 package org.apache.myfaces.trinidad.component;
23
24 import java.io.IOException;
25 import java.util.Collections;
26 import java.util.Iterator;
27 import javax.faces.component.UIComponent;
28 import javax.faces.context.FacesContext;
29 import org.apache.myfaces.trinidad.bean.FacesBean;
30 import org.apache.myfaces.trinidad.bean.PropertyKey;
31 import org.apache.myfaces.trinidad.util.ComponentUtils;
32
33 /**
34 *
35 * The switcher component dynamically decides which facet component
36 * should be rendered. It has two properties. The switcher will render
37 * the facet matching "facetName"; however, if no such facet exists
38 * (or "facetName" is null), and "defaultFacet" has been set, then
39 * that facet will be used instead. (It's possible to achieve this
40 * same functionality by using a panelGroup and binding the
41 * "rendered" property of each child, but this component can be
42 * simpler. Ordinary children of the switcher component are not rendered at
43 * all.)
44 *
45 * <h4>Events:</h4>
46 * <table border="1" width="100%" cellpadding="3" summary="">
47 * <tr bgcolor="#CCCCFF" class="TableHeadingColor">
48 * <th align="left">Type</th>
49 * <th align="left">Phases</th>
50 * <th align="left">Description</th>
51 * </tr>
52 * <tr class="TableRowColor">
53 * <td valign="top"><code>org.apache.myfaces.trinidad.event.AttributeChangeEvent</code></td>
54 * <td valign="top" nowrap>Invoke<br>Application<br>Apply<br>Request<br>Values</td>
55 * <td valign="top">Event delivered to describe an attribute change. Attribute change events are not delivered for any programmatic change to a property. They are only delivered when a renderer changes a property without the application's specific request. An example of an attribute change event might include the width of a column that supported client-side resizing.</td>
56 * </tr>
57 * </table>
58 */
59 public class UIXSwitcher extends UIXComponentBase
60 implements FlattenedComponent
61 {
62 static public final FacesBean.Type TYPE = new FacesBean.Type(
63 UIXComponentBase.TYPE);
64 static public final PropertyKey FACET_NAME_KEY =
65 TYPE.registerKey("facetName", String.class);
66 static public final PropertyKey DEFAULT_FACET_KEY =
67 TYPE.registerKey("defaultFacet", String.class);
68
69 static public final String COMPONENT_FAMILY =
70 "org.apache.myfaces.trinidad.Switcher";
71 static public final String COMPONENT_TYPE =
72 "org.apache.myfaces.trinidad.Switcher";
73
74 /**
75 * Construct an instance of the UIXSwitcher.
76 */
77 public UIXSwitcher()
78 {
79 super(null);
80 }
81
82 /**
83 * Processes the selected switcher facet
84 */
85 public <S> boolean processFlattenedChildren(
86 final FacesContext context,
87 ComponentProcessingContext cpContext,
88 final ComponentProcessor<S> childProcessor,
89 final S callbackContext
90 ) throws IOException
91 {
92 setupFlattenedContext(context, cpContext);
93
94 boolean abort;
95
96 try
97 {
98 UIComponent facet = _getFacet();
99
100 if (facet != null)
101 {
102 setupFlattenedChildrenContext(context, cpContext);
103
104 try
105 {
106 abort = UIXComponent.processFlattenedChildren(context,
107 cpContext,
108 childProcessor,
109 facet,
110 callbackContext);
111 }
112 finally
113 {
114 tearDownFlattenedChildrenContext(context, cpContext);
115 }
116 }
117 else
118 {
119 abort = false;
120 }
121 }
122 finally
123 {
124 tearDownFlattenedContext(context, cpContext);
125 }
126
127 return abort;
128 }
129
130 /**
131 * Returns <code>true</code> if this FlattenedComponent is currently flattening its children
132 * @param context FacesContext
133 * @return <code>true</code> if this FlattenedComponent is currently flattening its children
134 */
135 public boolean isFlatteningChildren(FacesContext context)
136 {
137 return true;
138 }
139
140 /**
141 * Only render the currently active facet.
142 */
143 @Override
144 public void encodeChildren(FacesContext context)
145 throws IOException
146 {
147 UIComponent facet = _getFacet();
148 if (facet != null)
149 {
150 facet.encodeAll(context);
151 }
152 }
153
154 /**
155 * Override to return true.
156 */
157 @Override
158 public boolean getRendersChildren()
159 {
160 return true;
161 }
162
163 protected Iterator<UIComponent> getRenderedFacetsAndChildren(FacesContext facesContext)
164 {
165 UIComponent facet = _getFacet();
166 if (facet == null)
167 {
168 return Collections.<UIComponent>emptyList().iterator();
169 }
170 else
171 {
172 return Collections.singleton(facet).iterator();
173 }
174 }
175
176 private UIComponent _getFacet()
177 {
178 if (!isRendered())
179 return null;
180
181 String facetName = getFacetName();
182 if (facetName != null)
183 {
184 UIComponent facet = getFacet(facetName);
185 if (facet != null)
186 return facet;
187 }
188
189 String defaultFacet = getDefaultFacet();
190 if (defaultFacet != null)
191 return getFacet(defaultFacet);
192
193 return null;
194 }
195
196 /**
197 * Gets the name of the facet to render and process.
198 *
199 * @return the new facetName value
200 */
201 final public String getFacetName()
202 {
203 return ComponentUtils.resolveString(getProperty(FACET_NAME_KEY));
204 }
205
206 /**
207 * Sets the name of the facet to render and process.
208 *
209 * @param facetName the new facetName value
210 */
211 final public void setFacetName(String facetName)
212 {
213 setProperty(FACET_NAME_KEY, (facetName));
214 }
215
216 /**
217 * Gets the name of the facet to render and process if "facetName"
218 * is null or otherwise does not refer to an existing facet.
219 *
220 * @return the new defaultFacet value
221 */
222 final public String getDefaultFacet()
223 {
224 return ComponentUtils.resolveString(getProperty(DEFAULT_FACET_KEY));
225 }
226
227 /**
228 * Sets the name of the facet to render and process if "facetName"
229 * is null or otherwise does not refer to an existing facet.
230 *
231 * @param defaultFacet the new defaultFacet value
232 */
233 final public void setDefaultFacet(String defaultFacet)
234 {
235 setProperty(DEFAULT_FACET_KEY, (defaultFacet));
236 }
237
238 @Override
239 public String getFamily()
240 {
241 return COMPONENT_FAMILY;
242 }
243
244 @Override
245 protected FacesBean.Type getBeanType()
246 {
247 return TYPE;
248 }
249
250 /**
251 * Construct an instance of the UIXSwitcher.
252 */
253 protected UIXSwitcher(
254 String rendererType
255 )
256 {
257 super(rendererType);
258 }
259
260 static
261 {
262 TYPE.lock();
263 }
264 }