001    package org.apache.myfaces.tobago.component;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one or more
005     * contributor license agreements.  See the NOTICE file distributed with
006     * this work for additional information regarding copyright ownership.
007     * The ASF licenses this file to You under the Apache License, Version 2.0
008     * (the "License"); you may not use this file except in compliance with
009     * the License.  You may obtain a copy of the License at
010     *
011     *      http://www.apache.org/licenses/LICENSE-2.0
012     *
013     * Unless required by applicable law or agreed to in writing, software
014     * distributed under the License is distributed on an "AS IS" BASIS,
015     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016     * See the License for the specific language governing permissions and
017     * limitations under the License.
018     */
019    
020    import org.apache.myfaces.tobago.context.TobagoFacesContext;
021    import org.apache.myfaces.tobago.event.SheetStateChangeEvent;
022    import org.apache.myfaces.tobago.internal.util.Deprecation;
023    import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
024    import org.apache.myfaces.tobago.renderkit.util.RenderUtils;
025    import org.apache.myfaces.tobago.util.ComponentUtils;
026    import org.apache.myfaces.tobago.util.CreateComponentUtils;
027    import org.apache.myfaces.tobago.util.DebugUtils;
028    
029    import javax.faces.application.Application;
030    import javax.faces.component.ActionSource;
031    import javax.faces.component.EditableValueHolder;
032    import javax.faces.component.UICommand;
033    import javax.faces.component.UIComponent;
034    import javax.faces.component.UIGraphic;
035    import javax.faces.component.UIOutput;
036    import javax.faces.component.ValueHolder;
037    import javax.faces.context.FacesContext;
038    import javax.faces.el.MethodBinding;
039    import javax.faces.el.ValueBinding;
040    import javax.faces.event.ActionEvent;
041    import javax.faces.event.ActionListener;
042    import javax.faces.event.ValueChangeEvent;
043    import javax.faces.model.SelectItem;
044    import javax.faces.webapp.UIComponentTag;
045    import javax.servlet.jsp.JspException;
046    import java.util.ArrayList;
047    import java.util.Arrays;
048    import java.util.List;
049    
050    /**
051     * @deprecated please use {@link ComponentUtils}
052     */
053    @Deprecated
054    public class ComponentUtil {
055    
056      public static final Class[] ACTION_ARGS = {};
057      public static final Class[] ACTION_LISTENER_ARGS = {ActionEvent.class};
058      public static final Class[] VALUE_CHANGE_LISTENER_ARGS = {ValueChangeEvent.class};
059      public static final Class[] VALIDATOR_ARGS = {FacesContext.class, UIComponent.class, Object.class};
060    
061      private ComponentUtil() {
062      }
063    
064      public static boolean hasErrorMessages(FacesContext context) {
065        return ComponentUtils.hasErrorMessages(context);
066      }
067    
068      public static boolean containsPopupActionListener(javax.faces.component.UICommand command) {
069        return ComponentUtils.containsPopupActionListener(command);
070      }
071    
072      public static String getFacesMessageAsString(FacesContext facesContext, UIComponent component) {
073        return ComponentUtils.getFacesMessageAsString(facesContext, component);
074      }
075    
076      public static boolean isInPopup(UIComponent component) {
077        return ComponentUtils.isInPopup(component);
078      }
079    
080      public static void resetPage(FacesContext context) {
081        ComponentUtils.resetPage(context);
082      }
083    
084      public static UIPage findPage(FacesContext context, UIComponent component) {
085        return (UIPage) ComponentUtils.findPage(context, component);
086      }
087    
088      public static UIPage findPage(UIComponent component) {
089        return (UIPage) ComponentUtils.findPage(component);
090      }
091    
092      public static void addStyles(UIComponent component, String[] styles) {
093        ((TobagoFacesContext) FacesContext.getCurrentInstance()).getStyleFiles().addAll(Arrays.asList(styles));
094      }
095    
096      public static void addScripts(UIComponent component, String[] scripts) {
097        ((TobagoFacesContext) FacesContext.getCurrentInstance()).getScriptFiles().addAll(Arrays.asList(scripts));
098      }
099    
100      public static void addOnloadCommands(UIComponent component, String[] cmds) {
101        ((TobagoFacesContext) FacesContext.getCurrentInstance()).getOnloadScripts().addAll(Arrays.asList(cmds));
102      }
103    
104      public static UIPage findPage(FacesContext facesContext) {
105        return (UIPage) ComponentUtils.findPage(facesContext);
106      }
107    
108      public static UIForm findForm(UIComponent component) {
109        return (UIForm) ComponentUtils.findForm(component);
110      }
111    
112      public static List<UIForm> findSubForms(UIComponent component) {
113        return new ArrayList<UIForm>((List) ComponentUtils.findSubForms(component));
114      }
115    
116      public static <T extends UIComponent> T findDescendant(UIComponent component, Class<T> type) {
117        return ComponentUtils.findDescendant(component, type);
118      }
119    
120      public static String findClientIdFor(UIComponent component, FacesContext facesContext) {
121        return ComponentUtils.findClientIdFor(component, facesContext);
122      }
123    
124      public static UIComponent findFor(UIComponent component) {
125        return ComponentUtils.findFor(component);
126      }
127    
128      public static boolean isInActiveForm(UIComponent component) {
129        return ComponentUtils.isInActiveForm(component);
130      }
131    
132      public static boolean isError(javax.faces.component.UIInput uiInput) {
133        return ComponentUtils.isError(uiInput);
134      }
135    
136      public static boolean isError(UIComponent component) {
137        return ComponentUtils.isError(component);
138      }
139    
140      public static boolean isOutputOnly(UIComponent component) {
141        return ComponentUtils.isOutputOnly(component);
142      }
143    
144      public static boolean mayValidate(UIComponent component) {
145        return ComponentUtils.mayValidate(component);
146      }
147    
148      public static boolean mayUpdateModel(UIComponent component) {
149        return ComponentUtils.mayUpdateModel(component);
150      }
151    
152      public static boolean getBooleanAttribute(UIComponent component, String name) {
153        return ComponentUtils.getBooleanAttribute(component, name);
154      }
155    
156      public static void setRenderedPartially(org.apache.myfaces.tobago.component.UICommand command, String renderers) {
157        ((SupportsRenderedPartially) command).setRenderedPartially(new String[]{renderers});
158      }
159    
160      public static void setStyleClasses(UIComponent component, String styleClasses) {
161        ComponentUtils.setStyleClasses(component, styleClasses);
162      }
163    
164      public static void setMarkup(UIComponent markupComponent, String markup) {
165        ComponentUtils.setMarkup(markupComponent, markup);
166      }
167    
168      public static Object getAttribute(UIComponent component, String name) {
169        return ComponentUtils.getAttribute(component, name);
170      }
171    
172      public static String getStringAttribute(UIComponent component, String name) {
173        return ComponentUtils.getStringAttribute(component, name);
174      }
175    
176      public static int getIntAttribute(UIComponent component, String name) {
177        return getIntAttribute(component, name, 0);
178      }
179    
180      public static int getIntAttribute(UIComponent component, String name, int defaultValue) {
181        return ComponentUtils.getIntAttribute(component, name, defaultValue);
182      }
183    
184      public static Character getCharacterAttribute(UIComponent component, String name) {
185        return ComponentUtils.getCharacterAttribute(component, name);
186      }
187    
188      public static boolean isFacetOf(UIComponent component, UIComponent parent) {
189        return ComponentUtils.isFacetOf(component, parent);
190      }
191    
192      public static LayoutComponentRendererBase getRenderer(FacesContext facesContext, UIComponent component) {
193        return (LayoutComponentRendererBase) ComponentUtils.getRenderer(facesContext, component);
194      }
195    
196      public static LayoutComponentRendererBase getRenderer(FacesContext facesContext, String family, String rendererType) {
197        return (LayoutComponentRendererBase) ComponentUtils.getRenderer(facesContext, family, rendererType);
198      }
199    
200      public static String currentValue(UIComponent component) {
201        return RenderUtils.currentValue(component);
202      }
203    
204      public static List<SelectItem> getSelectItems(UIComponent component) {
205        return RenderUtils.getSelectItems(component);
206      }
207    
208      public static Object findParameter(UIComponent component, String name) {
209        return ComponentUtils.findParameter(component, name);
210      }
211    
212      public static String toString(UIComponent component, int offset) {
213        return DebugUtils.toString(component, offset);
214      }
215    
216      public static ActionListener createActionListener(String type) throws JspException {
217        return ComponentUtils.createActionListener(type);
218      }
219    
220      public static UIGraphic getFirstGraphicChild(UIComponent component) {
221        return ComponentUtils.getFirstGraphicChild(component);
222      }
223    
224    /**
225     * @deprecated
226     */
227    @Deprecated  public static boolean isHoverEnabled(UIComponent component) {
228      Deprecation.LOG.error("no longer supported");
229      return ComponentUtils.getBooleanAttribute(component, Attributes.HOVER);
230      }
231    
232      public static UIOutput getFirstNonGraphicChild(UIComponent component) {
233        return ComponentUtils.getFirstNonGraphicChild(component);
234      }
235    
236      public static void setIntegerSizeProperty(UIComponent component, String name, String value) {
237        ComponentUtils.setIntegerSizeProperty(component, name, value);
238      }
239    
240      public static String removePx(String value) {
241        return ComponentUtils.removePx(value);
242      }
243    
244      public static void setIntegerProperty(UIComponent component, String name, String value) {
245        if (value != null) {
246          if (UIComponentTag.isValueReference(value)) {
247            component.setValueBinding(name, createValueBinding(value));
248          } else {
249            component.getAttributes().put(name, new Integer(value));
250          }
251        }
252      }
253    
254      public static void setBooleanProperty(UIComponent component, String name, String value) {
255        if (value != null) {
256          if (UIComponentTag.isValueReference(value)) {
257            component.setValueBinding(name, createValueBinding(value));
258          } else {
259            component.getAttributes().put(name, Boolean.valueOf(value));
260          }
261        }
262      }
263    
264      public static void setStringProperty(UIComponent component, String name, String value) {
265        if (value != null) {
266          if (UIComponentTag.isValueReference(value)) {
267            component.setValueBinding(name, createValueBinding(value));
268          } else {
269            component.getAttributes().put(name, value);
270          }
271        }
272      }
273    
274      public static void setValueForValueBinding(String name, Object value) {
275        ComponentUtils.setValueForValueBinding(name, value);
276      }
277    
278      public static ValueBinding createValueBinding(String value) {
279        return ComponentUtils.createValueBinding(value);
280      }
281    
282      public static String getValueFromEl(String script) {
283        if (UIComponentTag.isValueReference(script)) {
284          ValueBinding valueBinding = ComponentUtils.createValueBinding(script);
285          script = (String) valueBinding.getValue(FacesContext.getCurrentInstance());
286        }
287        return script;
288      }
289    
290      public static UIComponent createComponent(String componentType, String rendererType, String id) {
291        return CreateComponentUtils.createComponent(componentType, rendererType, id);
292      }
293    
294      public static UIComponent createComponent(FacesContext facesContext, String componentType,
295          String rendererType, String id) {
296        return CreateComponentUtils.createComponent(facesContext, componentType, rendererType, id);
297      }
298      
299      /**
300       * Please use createComponent(FacesContext facesContext, String componentType, String rendererType, String id)
301       *
302       * @deprecated
303       */
304      @Deprecated
305      public static UIComponent createComponent(FacesContext facesContext, String componentType, String rendererType) {
306        return createComponent(facesContext, componentType, rendererType, null);
307      }
308    
309      public static UIColumn createTextColumn(String label, String sortable, String align, String value, String id) {
310        return (UIColumn) CreateComponentUtils.createTextColumn(label, sortable, align, value, id);
311      }
312    
313      public static UIColumn createColumn(String label, String sortable, String align, UIComponent child) {
314        return (UIColumn) CreateComponentUtils.createColumn(label, sortable, align, child);
315      }
316    
317      public static UIColumn createColumn(String label, String sortable, String align, UIComponent child, String id) {
318        return (UIColumn) CreateComponentUtils.createColumn(label, sortable, align, child, id);
319      }
320    
321      public static UIMenuSelectOne createUIMenuSelectOneFacet(FacesContext facesContext, UICommand command, String id) {
322        return CreateComponentUtils.createUIMenuSelectOneFacet(facesContext, command, id);
323      }
324    
325      public static boolean hasSelectedValue(List<SelectItem> items, Object value) {
326        return ComponentUtils.hasSelectedValue(items, value);
327      }
328    
329      public static UIComponent createUISelectBooleanFacet(FacesContext facesContext, UICommand command, String id) {
330        return CreateComponentUtils.createUISelectBooleanFacet(facesContext, command, id);
331      }
332    
333      public static int getIntValue(ValueBinding valueBinding) {
334        return ComponentUtils.getIntValue(valueBinding);
335      }
336    
337      public static String createPickerId(FacesContext facesContext, UIComponent component, String postfix) {
338        return ComponentUtils.createPickerId(facesContext, component, postfix);
339      }
340    
341      public static String getComponentId(FacesContext facesContext, UIComponent component) {
342        return ComponentUtils.getComponentId(facesContext, component);
343      }
344    
345      public static UIComponent provideLabel(FacesContext facesContext, UIComponent component) {
346        return ComponentUtils.provideLabel(facesContext, component);
347      }
348    
349      public static List<SelectItem> getItemsToRender(javax.faces.component.UISelectOne component) {
350        return RenderUtils.getItemsToRender(component);
351      }
352    
353      public static List<SelectItem> getItemsToRender(javax.faces.component.UISelectMany component) {
354        return RenderUtils.getItemsToRender(component);
355      }
356    
357      public static void setValidator(EditableValueHolder editableValueHolder, String validator) {
358        ComponentUtils.setValidator(editableValueHolder, validator);
359      }
360    
361      public static void setConverter(ValueHolder valueHolder, String converterId) {
362        ComponentUtils.setConverter(valueHolder, converterId);
363      }
364    
365      public static void setAction(UICommand component, String type, String action) {
366        ComponentUtils.setAction(component, action);
367      }
368    
369      public static void setSuggestMethodBinding(UIInput component, String suggestMethod) {
370        if (suggestMethod != null) {
371          if (UIComponentTag.isValueReference(suggestMethod)) {
372            final MethodBinding methodBinding = FacesContext.getCurrentInstance().getApplication()
373                .createMethodBinding(suggestMethod, new Class[]{String.class});
374            component.setSuggestMethod(methodBinding);
375          } else {
376            throw new IllegalArgumentException(
377                "Must be a valueReference (suggestMethod): " + suggestMethod);
378          }
379        }
380      }
381    
382      public static void setActionListener(ActionSource command, String actionListener) {
383        ComponentUtils.setActionListener(command, actionListener);
384      }
385    
386      public static void setValueChangeListener(EditableValueHolder valueHolder, String valueChangeListener) {
387        ComponentUtils.setValueChangeListener(valueHolder, valueChangeListener);
388      }
389    
390      public static void setSortActionListener(UIData data, String actionListener) {
391        final FacesContext facesContext = FacesContext.getCurrentInstance();
392        final Application application = facesContext.getApplication();
393        if (actionListener != null) {
394          if (UIComponentTag.isValueReference(actionListener)) {
395            MethodBinding binding = application.createMethodBinding(
396                actionListener, ACTION_LISTENER_ARGS);
397            data.setSortActionListener(binding);
398          } else {
399            throw new IllegalArgumentException(
400                "Must be a valueReference (sortActionListener): " + actionListener);
401          }
402        }
403      }
404    
405      public static void setValueBinding(UIComponent component, String name, String state) {
406        ComponentUtils.setValueBinding(component, name, state);
407      }
408    
409      public static void setStateChangeListener(UISheet data, String stateChangeListener) {
410        final FacesContext facesContext = FacesContext.getCurrentInstance();
411        final Application application = facesContext.getApplication();
412    
413        if (stateChangeListener != null) {
414          if (UIComponentTag.isValueReference(stateChangeListener)) {
415            Class[] arguments = {SheetStateChangeEvent.class};
416            MethodBinding binding
417                = application.createMethodBinding(stateChangeListener, arguments);
418            data.setStateChangeListener(binding);
419          } else {
420            throw new IllegalArgumentException(
421                "Must be a valueReference (actionListener): " + stateChangeListener);
422          }
423        }
424      }
425    
426      @Deprecated
427      public static String[] getMarkupBinding(FacesContext facesContext, SupportsMarkup component) {
428        return ComponentUtils.getMarkupBinding(facesContext, component);
429      }
430    
431      public static UIComponent findComponent(UIComponent from, String relativeId) {
432        return ComponentUtils.findComponent(from, relativeId);
433      }
434    }