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.component;
21  
22  import org.apache.myfaces.tobago.context.TobagoFacesContext;
23  import org.apache.myfaces.tobago.event.SheetStateChangeEvent;
24  import org.apache.myfaces.tobago.internal.util.Deprecation;
25  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
26  import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
27  import org.apache.myfaces.tobago.util.ComponentUtils;
28  import org.apache.myfaces.tobago.util.CreateComponentUtils;
29  import org.apache.myfaces.tobago.util.DebugUtils;
30  
31  import javax.faces.application.Application;
32  import javax.faces.component.ActionSource;
33  import javax.faces.component.EditableValueHolder;
34  import javax.faces.component.UICommand;
35  import javax.faces.component.UIComponent;
36  import javax.faces.component.UIGraphic;
37  import javax.faces.component.UIOutput;
38  import javax.faces.component.ValueHolder;
39  import javax.faces.context.FacesContext;
40  import javax.faces.el.MethodBinding;
41  import javax.faces.el.ValueBinding;
42  import javax.faces.event.ActionEvent;
43  import javax.faces.event.ActionListener;
44  import javax.faces.event.ValueChangeEvent;
45  import javax.faces.model.SelectItem;
46  import javax.faces.webapp.UIComponentTag;
47  import javax.servlet.jsp.JspException;
48  import java.util.ArrayList;
49  import java.util.Arrays;
50  import java.util.List;
51  
52  /**
53   * @deprecated Since Tobago 1.5 please use {@link ComponentUtils}
54   */
55  @Deprecated
56  public class ComponentUtil {
57  
58    public static final Class[] ACTION_ARGS = {};
59    public static final Class[] ACTION_LISTENER_ARGS = {ActionEvent.class};
60    public static final Class[] VALUE_CHANGE_LISTENER_ARGS = {ValueChangeEvent.class};
61    public static final Class[] VALIDATOR_ARGS = {FacesContext.class, UIComponent.class, Object.class};
62  
63    private ComponentUtil() {
64    }
65  
66    public static boolean hasErrorMessages(FacesContext context) {
67      return ComponentUtils.hasErrorMessages(context);
68    }
69  
70    public static boolean containsPopupActionListener(javax.faces.component.UICommand command) {
71      return ComponentUtils.containsPopupActionListener(command);
72    }
73  
74    public static String getFacesMessageAsString(FacesContext facesContext, UIComponent component) {
75      return ComponentUtils.getFacesMessageAsString(facesContext, component);
76    }
77  
78    public static boolean isInPopup(UIComponent component) {
79      return ComponentUtils.isInPopup(component);
80    }
81  
82    public static void resetPage(FacesContext context) {
83      ComponentUtils.resetPage(context);
84    }
85  
86    public static UIPage findPage(FacesContext context, UIComponent component) {
87      return (UIPage) ComponentUtils.findPage(context, component);
88    }
89  
90    public static UIPage findPage(UIComponent component) {
91      return (UIPage) ComponentUtils.findPage(component);
92    }
93  
94    public static void addStyles(UIComponent component, String[] styles) {
95      ((TobagoFacesContext) FacesContext.getCurrentInstance()).getStyleFiles().addAll(Arrays.asList(styles));
96    }
97  
98    public static void addScripts(UIComponent component, String[] scripts) {
99      ((TobagoFacesContext) FacesContext.getCurrentInstance()).getScriptFiles().addAll(Arrays.asList(scripts));
100   }
101 
102   public static void addOnloadCommands(UIComponent component, String[] cmds) {
103     ((TobagoFacesContext) FacesContext.getCurrentInstance()).getOnloadScripts().addAll(Arrays.asList(cmds));
104   }
105 
106   public static UIPage findPage(FacesContext facesContext) {
107     return (UIPage) ComponentUtils.findPage(facesContext);
108   }
109 
110   public static UIForm findForm(UIComponent component) {
111     return (UIForm) ComponentUtils.findForm(component);
112   }
113 
114   public static List<UIForm> findSubForms(UIComponent component) {
115     return new ArrayList<UIForm>((List) ComponentUtils.findSubForms(component));
116   }
117 
118   public static <T extends UIComponent> T findDescendant(UIComponent component, Class<T> type) {
119     return ComponentUtils.findDescendant(component, type);
120   }
121 
122   public static String findClientIdFor(UIComponent component, FacesContext facesContext) {
123     return ComponentUtils.findClientIdFor(component, facesContext);
124   }
125 
126   public static UIComponent findFor(UIComponent component) {
127     return ComponentUtils.findFor(component);
128   }
129 
130   public static boolean isInActiveForm(UIComponent component) {
131     return ComponentUtils.isInActiveForm(component);
132   }
133 
134   public static boolean isError(javax.faces.component.UIInput uiInput) {
135     return ComponentUtils.isError(uiInput);
136   }
137 
138   public static boolean isError(UIComponent component) {
139     return ComponentUtils.isError(component);
140   }
141 
142   public static boolean isOutputOnly(UIComponent component) {
143     return ComponentUtils.isOutputOnly(component);
144   }
145 
146   public static boolean mayValidate(UIComponent component) {
147     return ComponentUtils.mayValidate(component);
148   }
149 
150   public static boolean mayUpdateModel(UIComponent component) {
151     return ComponentUtils.mayUpdateModel(component);
152   }
153 
154   public static boolean getBooleanAttribute(UIComponent component, String name) {
155     return ComponentUtils.getBooleanAttribute(component, name);
156   }
157 
158   public static void setRenderedPartially(org.apache.myfaces.tobago.component.UICommand command, String renderers) {
159     ((SupportsRenderedPartially) command).setRenderedPartially(new String[]{renderers});
160   }
161 
162   public static void setStyleClasses(UIComponent component, String styleClasses) {
163     ComponentUtils.setStyleClasses(component, styleClasses);
164   }
165 
166   public static void setMarkup(UIComponent markupComponent, String markup) {
167     ComponentUtils.setMarkup(markupComponent, markup);
168   }
169 
170   public static Object getAttribute(UIComponent component, String name) {
171     return ComponentUtils.getAttribute(component, name);
172   }
173 
174   public static String getStringAttribute(UIComponent component, String name) {
175     return ComponentUtils.getStringAttribute(component, name);
176   }
177 
178   public static int getIntAttribute(UIComponent component, String name) {
179     return getIntAttribute(component, name, 0);
180   }
181 
182   public static int getIntAttribute(UIComponent component, String name, int defaultValue) {
183     return ComponentUtils.getIntAttribute(component, name, defaultValue);
184   }
185 
186   public static Character getCharacterAttribute(UIComponent component, String name) {
187     return ComponentUtils.getCharacterAttribute(component, name);
188   }
189 
190   public static boolean isFacetOf(UIComponent component, UIComponent parent) {
191     return ComponentUtils.isFacetOf(component, parent);
192   }
193 
194   public static LayoutComponentRendererBase getRenderer(FacesContext facesContext, UIComponent component) {
195     return (LayoutComponentRendererBase) ComponentUtils.getRenderer(facesContext, component);
196   }
197 
198   public static LayoutComponentRendererBase getRenderer(FacesContext facesContext, String family, String rendererType) {
199     return (LayoutComponentRendererBase) ComponentUtils.getRenderer(facesContext, family, rendererType);
200   }
201 
202   public static String currentValue(UIComponent component) {
203     return RenderUtils.currentValue(component);
204   }
205 
206   public static List<SelectItem> getSelectItems(UIComponent component) {
207     return RenderUtils.getSelectItems(component);
208   }
209 
210   public static Object findParameter(UIComponent component, String name) {
211     return ComponentUtils.findParameter(component, name);
212   }
213 
214   public static String toString(UIComponent component, int offset) {
215     return DebugUtils.toString(component, offset);
216   }
217 
218   public static ActionListener createActionListener(String type) throws JspException {
219     return ComponentUtils.createActionListener(type);
220   }
221 
222   public static UIGraphic getFirstGraphicChild(UIComponent component) {
223     return ComponentUtils.getFirstGraphicChild(component);
224   }
225 
226 /**
227  * @deprecated
228  */
229 @Deprecated  public static boolean isHoverEnabled(UIComponent component) {
230   Deprecation.LOG.error("no longer supported");
231   return ComponentUtils.getBooleanAttribute(component, Attributes.HOVER);
232   }
233 
234   public static UIOutput getFirstNonGraphicChild(UIComponent component) {
235     return ComponentUtils.getFirstNonGraphicChild(component);
236   }
237 
238   public static void setIntegerSizeProperty(UIComponent component, String name, String value) {
239     ComponentUtils.setIntegerSizeProperty(component, name, value);
240   }
241 
242   public static String removePx(String value) {
243     return ComponentUtils.removePx(value);
244   }
245 
246   public static void setIntegerProperty(UIComponent component, String name, String value) {
247     if (value != null) {
248       if (UIComponentTag.isValueReference(value)) {
249         component.setValueBinding(name, createValueBinding(value));
250       } else {
251         component.getAttributes().put(name, new Integer(value));
252       }
253     }
254   }
255 
256   public static void setBooleanProperty(UIComponent component, String name, String value) {
257     if (value != null) {
258       if (UIComponentTag.isValueReference(value)) {
259         component.setValueBinding(name, createValueBinding(value));
260       } else {
261         component.getAttributes().put(name, Boolean.valueOf(value));
262       }
263     }
264   }
265 
266   public static void setStringProperty(UIComponent component, String name, String value) {
267     if (value != null) {
268       if (UIComponentTag.isValueReference(value)) {
269         component.setValueBinding(name, createValueBinding(value));
270       } else {
271         component.getAttributes().put(name, value);
272       }
273     }
274   }
275 
276   public static void setValueForValueBinding(String name, Object value) {
277     ComponentUtils.setValueForValueBinding(name, value);
278   }
279 
280   public static ValueBinding createValueBinding(String value) {
281     return ComponentUtils.createValueBinding(value);
282   }
283 
284   public static String getValueFromEl(String script) {
285     if (UIComponentTag.isValueReference(script)) {
286       ValueBinding valueBinding = ComponentUtils.createValueBinding(script);
287       script = (String) valueBinding.getValue(FacesContext.getCurrentInstance());
288     }
289     return script;
290   }
291 
292   public static UIComponent createComponent(String componentType, String rendererType, String id) {
293     return CreateComponentUtils.createComponent(componentType, rendererType, id);
294   }
295 
296   public static UIComponent createComponent(FacesContext facesContext, String componentType,
297       String rendererType, String id) {
298     return CreateComponentUtils.createComponent(facesContext, componentType, rendererType, id);
299   }
300   
301   /**
302    * Please use createComponent(FacesContext facesContext, String componentType, String rendererType, String id)
303    *
304    * @deprecated
305    */
306   @Deprecated
307   public static UIComponent createComponent(FacesContext facesContext, String componentType, String rendererType) {
308     return createComponent(facesContext, componentType, rendererType, null);
309   }
310 
311   public static UIColumn createTextColumn(String label, String sortable, String align, String value, String id) {
312     return (UIColumn) CreateComponentUtils.createTextColumn(label, sortable, align, value, id);
313   }
314 
315   public static UIColumn createColumn(String label, String sortable, String align, UIComponent child) {
316     return (UIColumn) CreateComponentUtils.createColumn(label, sortable, align, child);
317   }
318 
319   public static UIColumn createColumn(String label, String sortable, String align, UIComponent child, String id) {
320     return (UIColumn) CreateComponentUtils.createColumn(label, sortable, align, child, id);
321   }
322 
323   public static UIMenuSelectOne createUIMenuSelectOneFacet(FacesContext facesContext, UICommand command, String id) {
324     return CreateComponentUtils.createUIMenuSelectOneFacet(facesContext, command, id);
325   }
326 
327   public static boolean hasSelectedValue(List<SelectItem> items, Object value) {
328     return ComponentUtils.hasSelectedValue(items, value);
329   }
330 
331   public static UIComponent createUISelectBooleanFacet(FacesContext facesContext, UICommand command, String id) {
332     return CreateComponentUtils.createUISelectBooleanFacet(facesContext, command, id);
333   }
334 
335   public static int getIntValue(ValueBinding valueBinding) {
336     return ComponentUtils.getIntValue(valueBinding);
337   }
338 
339   public static String createPickerId(FacesContext facesContext, UIComponent component, String postfix) {
340     return ComponentUtils.createPickerId(facesContext, component, postfix);
341   }
342 
343   public static String getComponentId(FacesContext facesContext, UIComponent component) {
344     return ComponentUtils.getComponentId(facesContext, component);
345   }
346 
347   public static UIComponent provideLabel(FacesContext facesContext, UIComponent component) {
348     return ComponentUtils.provideLabel(facesContext, component);
349   }
350 
351   public static List<SelectItem> getItemsToRender(javax.faces.component.UISelectOne component) {
352     return RenderUtils.getItemsToRender(component);
353   }
354 
355   public static List<SelectItem> getItemsToRender(javax.faces.component.UISelectMany component) {
356     return RenderUtils.getItemsToRender(component);
357   }
358 
359   public static void setValidator(EditableValueHolder editableValueHolder, String validator) {
360     ComponentUtils.setValidator(editableValueHolder, validator);
361   }
362 
363   public static void setConverter(ValueHolder valueHolder, String converterId) {
364     ComponentUtils.setConverter(valueHolder, converterId);
365   }
366 
367   public static void setAction(UICommand component, String type, String action) {
368     ComponentUtils.setAction(component, action);
369   }
370 
371   public static void setSuggestMethodBinding(UIIn component, String suggestMethod) {
372     if (suggestMethod != null) {
373       if (UIComponentTag.isValueReference(suggestMethod)) {
374         final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
375             .createMethodBinding(suggestMethod, new Class[]{String.class});
376         component.setSuggestMethod(methodBinding);
377       } else {
378         throw new IllegalArgumentException(
379             "Must be a valueReference (suggestMethod): " + suggestMethod);
380       }
381     }
382   }
383 
384   public static void setActionListener(ActionSource command, String actionListener) {
385     ComponentUtils.setActionListener(command, actionListener);
386   }
387 
388   public static void setValueChangeListener(EditableValueHolder valueHolder, String valueChangeListener) {
389     ComponentUtils.setValueChangeListener(valueHolder, valueChangeListener);
390   }
391 
392   public static void setValueBinding(UIComponent component, String name, String state) {
393     ComponentUtils.setValueBinding(component, name, state);
394   }
395 
396   public static void setStateChangeListener(UISheet data, String stateChangeListener) {
397     final FacesContext facesContext = FacesContext.getCurrentInstance();
398     final Application application = facesContext.getApplication();
399 
400     if (stateChangeListener != null) {
401       if (UIComponentTag.isValueReference(stateChangeListener)) {
402         Class[] arguments = {SheetStateChangeEvent.class};
403         MethodBinding binding
404             = application.createMethodBinding(stateChangeListener, arguments);
405         data.setStateChangeListener(binding);
406       } else {
407         throw new IllegalArgumentException(
408             "Must be a valueReference (actionListener): " + stateChangeListener);
409       }
410     }
411   }
412 
413   @Deprecated
414   public static String[] getMarkupBinding(FacesContext facesContext, SupportsMarkup component) {
415     return ComponentUtils.getMarkupBinding(facesContext, component);
416   }
417 
418   public static UIComponent findComponent(UIComponent from, String relativeId) {
419     return ComponentUtils.findComponent(from, relativeId);
420   }
421 }