KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > ext > taglib > Util


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33 /* Original Copyright:
34  * Copyright 2004 The Apache Software Foundation.
35  *
36  * Licensed under the Apache License, Version 2.0 (the "License");
37  * you may not use this file except in compliance with the License.
38  * You may obtain a copy of the License at
39  *
40  * http://www.apache.org/licenses/LICENSE-2.0
41  *
42  * Unless required by applicable law or agreed to in writing, software
43  * distributed under the License is distributed on an "AS IS" BASIS,
44  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
45  * See the License for the specific language governing permissions and
46  * limitations under the License.
47  */

48
49 package com.icesoft.faces.component.ext.taglib;
50
51 import com.icesoft.faces.component.CSS_DEFAULT;
52 import com.icesoft.faces.component.IceExtended;
53 import com.icesoft.faces.component.ext.HtmlInputText;
54 import com.icesoft.faces.component.ext.renderkit.FormRenderer;
55 import com.icesoft.faces.context.effects.Effect;
56 import com.icesoft.faces.context.effects.EffectBuilder;
57 import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
58 import org.apache.commons.logging.Log;
59 import org.apache.commons.logging.LogFactory;
60
61 import javax.faces.component.UIComponent;
62 import javax.faces.component.UISelectItem;
63 import javax.faces.component.UISelectItems;
64 import javax.faces.context.FacesContext;
65 import javax.faces.el.ValueBinding;
66 import javax.faces.model.SelectItem;
67 import java.util.ArrayList JavaDoc;
68 import java.util.Collection JavaDoc;
69 import java.util.Iterator JavaDoc;
70 import java.util.List JavaDoc;
71 import java.util.Map JavaDoc;
72 import java.util.StringTokenizer JavaDoc;
73
74 public class Util extends Object JavaDoc {
75     private static Log log = LogFactory.getLog(Util.class);
76
77     /**
78      * Gets the comma separated list of visibility user roles from the given
79      * component and checks if current user is in one of these roles.
80      *
81      * @param component a user role aware component
82      * @return true if no user roles are defined for this component or user is
83      * in one of these roles, false otherwise
84      */

85     public static boolean isRenderedOnUserRole(UIComponent component) {
86         String JavaDoc userRole;
87         if (component instanceof IceExtended) {
88             userRole = ((IceExtended) component).getRenderedOnUserRole();
89         } else {
90             userRole = (String JavaDoc) component.getAttributes()
91                     .get(IceExtended.RENDERED_ON_USER_ROLE_ATTR);
92         }
93
94         //there is no restriction
95
if (userRole == null) {
96             return true;
97         }
98
99         FacesContext facesContext = FacesContext.getCurrentInstance();
100         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(userRole, ",");
101         while (st.hasMoreTokens()) {
102             if (facesContext.getExternalContext()
103                     .isUserInRole(st.nextToken().trim())) {
104                 return true;
105             }
106         }
107         return false;
108     }
109
110
111     /**
112      * Gets the comma separated list of enabling user roles from the given
113      * component and checks if current user is in one of these roles.
114      *
115      * @param component a user role aware component
116      * @return true if no user roles are defined for this component or user is
117      * in one of these roles, false otherwise
118      */

119     public static boolean isEnabledOnUserRole(UIComponent component) {
120         String JavaDoc userRole;
121         if (component instanceof IceExtended) {
122             userRole = ((IceExtended) component).getEnabledOnUserRole();
123         } else {
124             userRole = (String JavaDoc) component.getAttributes()
125                     .get(IceExtended.ENABLED_ON_USER_ROLE_ATTR);
126         }
127
128         // there is no restriction
129
if (userRole == null) {
130             return true;
131         }
132
133         FacesContext facesContext = FacesContext.getCurrentInstance();
134         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(userRole, ",");
135         while (st.hasMoreTokens()) {
136             if (facesContext.getExternalContext()
137                     .isUserInRole(st.nextToken().trim())) {
138                 return true;
139             }
140         }
141         return false;
142     }
143
144     public static boolean isParentPartialSubmit(UIComponent uiComponent) {
145         UIComponent form = DomBasicRenderer.findForm(uiComponent);
146         if (form instanceof IceExtended) {
147             if (((IceExtended) form).getPartialSubmit()) {
148                 return true;
149             }
150         }
151         return false;
152     }
153
154     /**
155      * <p>Return an Iterator over representing the available options for this
156      * component, assembled from the set of UISelectItem and/or UISelectItems
157      * components that are direct children of this component. If there are no
158      * such children, a zero-length array is returned.</p>
159      */

160
161     public static List JavaDoc getSelectItems(FacesContext context,
162                                       UIComponent uiComponent) {
163
164         List JavaDoc selectItems = new ArrayList JavaDoc();
165         Iterator JavaDoc children = uiComponent.getChildren().iterator();
166
167         while (children.hasNext()) {
168             UIComponent nextSelectItemChild = (UIComponent) children.next();
169             if (nextSelectItemChild instanceof UISelectItem) {
170                 Object JavaDoc selectItemValue =
171                         ((UISelectItem) nextSelectItemChild).getValue();
172                 if (selectItemValue != null &&
173                     selectItemValue instanceof SelectItem) {
174                     selectItems.add(selectItemValue);
175                 } else {
176                     selectItems.add(
177                             new SelectItem(
178                                     ((UISelectItem) nextSelectItemChild).getItemValue(),
179                                     ((UISelectItem) nextSelectItemChild).getItemLabel(),
180                                     ((UISelectItem) nextSelectItemChild).getItemDescription(),
181                                     ((UISelectItem) nextSelectItemChild).isItemDisabled()));
182                 }
183             } else if (nextSelectItemChild instanceof UISelectItems) {
184                 Object JavaDoc selectItemsValue =
185                         ((UISelectItems) nextSelectItemChild).getValue();
186
187                 if (selectItemsValue != null) {
188                     if (selectItemsValue instanceof SelectItem) {
189                         selectItems.add(selectItemsValue);
190                     } else if (selectItemsValue instanceof Collection JavaDoc) {
191                         Iterator JavaDoc selectItemsIterator =
192                                 ((Collection JavaDoc) selectItemsValue).iterator();
193                         while (selectItemsIterator.hasNext()) {
194                             selectItems.add(selectItemsIterator.next());
195                         }
196                     } else if (selectItemsValue instanceof SelectItem[]) {
197                         SelectItem selectItemArray[] =
198                                 (SelectItem[]) selectItemsValue;
199                         for (int i = 0; i < selectItemArray.length; i++) {
200                             selectItems.add(selectItemArray[i]);
201                         }
202                     } else if (selectItemsValue instanceof Map JavaDoc) {
203                         Iterator JavaDoc selectItemIterator =
204                                 ((Map JavaDoc) selectItemsValue).keySet().iterator();
205                         while (selectItemIterator.hasNext()) {
206                             Object JavaDoc nextKey = selectItemIterator.next();
207                             if (nextKey != null) {
208                                 Object JavaDoc nextValue =
209                                         ((Map JavaDoc) selectItemsValue).get(nextKey);
210                                 if (nextValue != null) {
211                                     selectItems.add(
212                                             new SelectItem(
213                                                     nextValue.toString(),
214                                                     nextKey.toString()));
215                                 }
216                             }
217                         }
218                     }
219                 }
220             }
221         }
222         return selectItems;
223     }
224
225
226     public static void addEffect(String JavaDoc effect, UIComponent panel) {
227         if (effect != null) {
228             if (isValueReference(effect)) {
229                 ValueBinding vb = Util.getValueBinding(effect);
230                 panel.setValueBinding("effect", vb);
231             } else {
232                 Effect fx = (Effect) panel.getAttributes().get("effect");
233                 if (fx == null) {
234                     fx = EffectBuilder.build(effect);
235                     panel.getAttributes().put("effect", fx);
236                 }
237             }
238         }
239     }
240
241     public static void addVisible(String JavaDoc visible, UIComponent panel) {
242         if (visible != null) {
243             if (isValueReference(visible)) {
244                 ValueBinding vb = Util.getValueBinding(visible);
245                 panel.setValueBinding("visible", vb);
246             } else {
247                 Boolean JavaDoc boolVisible = new Boolean JavaDoc(visible);
248                 panel.getAttributes().put("visible", boolVisible);
249
250             }
251         }
252     }
253
254     public static void addLocalEffect(String JavaDoc effect, String JavaDoc name,
255                                       UIComponent panel) {
256
257         if (effect != null) {
258             if (log.isTraceEnabled()) {
259                 log.trace("AddLocalEffect. String [" + effect + "] name [" +
260                           name + "] class [" + panel.getClass().getName() +
261                           "]");
262             }
263             if (isValueReference(effect)) {
264                 if (log.isTraceEnabled()) {
265                     log.trace("Adding Value Binding");
266                 }
267                 ValueBinding vb = Util.getValueBinding(effect);
268                 panel.setValueBinding(name, vb);
269             } else {
270                 if (log.isTraceEnabled()) {
271                     log.trace("Adding Literal String");
272                 }
273
274                 Effect fx = (Effect) panel.getAttributes().get(name);
275                 if (fx == null) {
276                     fx = EffectBuilder.build(effect);
277                     panel.getAttributes().put(name, fx);
278                 }
279             }
280         } else {
281             if (log.isTraceEnabled()) {
282                 log.trace("AddLocalEffect. Effect is null");
283             }
284         }
285     }
286
287     public static boolean isValueReference(String JavaDoc v) {
288         if (v == null) {
289             return false;
290         }
291         if (v.startsWith("#{") && v.endsWith("}")) {
292             return true;
293         }
294         return false;
295     }
296
297     public static String JavaDoc stripPx(String JavaDoc s) {
298         if (s == null) {
299             return null;
300         }
301         int i = s.indexOf("px");
302         if (i != -1) {
303             return s.substring(0, i);
304         }
305         return s;
306     }
307
308
309     /**
310      * This method should be used by the component/renderer class to
311      * get the qualifiedStyleClass for a class which has been used
312      * internally. This would return a qualifiedStyleClass string using the
313      * following pattern.
314      * componentDefaultClass + noneAttributeClass
315      * @param uiComponent
316      * @param noneAttributeClass
317      * @return qualifiedStyleClass string
318      */

319     public static String JavaDoc getQualifiedStyleClass(UIComponent uiComponent,
320                                           String JavaDoc noneAttributeClass) {
321         return getQualifiedStyleClass(uiComponent,
322                                 noneAttributeClass,
323                                 null,
324                                 null,
325                                 false,
326                                 true);
327         
328     }
329
330     /**
331      * This method should be used by the renderer/component class that
332      * can be disabled, to get the qualifiedStyleClass for a class which has
333      * been used internally. This would return a qualifiedStyleClass string
334      * using the following pattern.
335      * defaulStyleClass + noneAttributeClass[-dis]
336      * @param uiComponent
337      * @param noneAttributeClass
338      * @return qualifiedStyleClass string
339      */

340     public static String JavaDoc getQualifiedStyleClass(UIComponent uiComponent,
341                                           String JavaDoc noneAttributeClass,
342                                           boolean disabled) {
343         return getQualifiedStyleClass(uiComponent,
344                                 noneAttributeClass,
345                                 null,
346                                 null,
347                                 disabled,
348                                 true);
349         
350     }
351
352     /**
353      * This method should be used by the renderer/component class to get the
354      * qualifiedStyleClass for a "styleClass" or any other class related attribute
355      * on the component (e.g.) inputText on inputFile component.
356      * This would return a qualifiedStyleClass string using the following pattern.
357      * defaulStyleClass [userDefinedStyleClass]
358      * @param uiComponent
359      * @param userDefinedStyleClass
360      * @param defaulStyleClass
361      * @param classAttributeName
362      * @return qualifiedStyleClass string
363      */

364     
365     public static String JavaDoc getQualifiedStyleClass(UIComponent uiComponent,
366                                           String JavaDoc userDefinedStyleClass,
367                                           String JavaDoc defaulStyleClass,
368                                           String JavaDoc classAttributeName) {
369         return getQualifiedStyleClass(uiComponent,
370                 userDefinedStyleClass,
371                 defaulStyleClass,
372                 classAttributeName,
373                 false,
374                 false);
375         
376     }
377
378     /**
379      * This method should be used by the renderer/component class that can be
380      * disabled, to get the qualifiedStyleClass for a "styleClass" or any other
381      * class related attribute on the component (e.g.) "inputText" attribute on
382      * "inputFile" component. This methods returns a qualifiedStyleClass string
383      * using the following pattern.
384      * defaulStyleClass[-dis] [userDefinedStyleClass[-dis]]
385      * @param uiComponent
386      * @param userDefinedStyleClass
387      * @param defaulStyleClass
388      * @param classAttributeName
389      * @return qualifiedStyleClass string
390      */

391     public static String JavaDoc getQualifiedStyleClass(UIComponent uiComponent,
392                                           String JavaDoc userDefinedStyleClass,
393                                           String JavaDoc defaulStyleClass,
394                                           String JavaDoc classAttributeName,
395                                           boolean disabled) {
396         return getQualifiedStyleClass(uiComponent,
397                 userDefinedStyleClass,
398                 defaulStyleClass,
399                 classAttributeName,
400                 disabled,
401                 false);
402         
403     }
404
405       private static String JavaDoc getQualifiedStyleClass(UIComponent uiComponent,
406                                           String JavaDoc userDefinedStyleClass,
407                                           String JavaDoc defaulStyleClass,
408                                           String JavaDoc classAttributeName,
409                                           boolean disabled,
410                                           boolean isNoneAttributeClass) {
411           if (isNoneAttributeClass) {
412               //1- This requested class is used internally by the component and
413
//not visible to the developer (or can not be set using an attribute)
414
return appendLocalClassToStyleClass(uiComponent, userDefinedStyleClass);
415           }
416
417           String JavaDoc disabledSuffix = disabled ? CSS_DEFAULT.DIS_SUFFIX: "";
418           String JavaDoc styleClass = null;
419           
420           if ("styleClass".equalsIgnoreCase(classAttributeName)) {
421               //2- The following code is responsible to assembled the qualified
422
//value for "styleClass" attribute using the following syntax:
423
// defaultStyleClass[-dis] [userDefinedStyleClass[-dis]]
424
styleClass = getValue(uiComponent,
425                       userDefinedStyleClass,
426                       "styleClass");
427               styleClass = defaulStyleClass +
428               disabledSuffix+
429               " " + ((styleClass!=null)? styleClass + disabledSuffix :"");
430               return styleClass.trim();
431           } else {
432               //3- the following code is to deal with other style class
433
//attributes that has been defined on the components (e.g.)
434
// "inputTextClass" attribute on "inputFile" component.
435
styleClass = String.valueOf(uiComponent.getAttributes()
436                       .get("styleClass"));
437               String JavaDoc subClass = getValue(uiComponent,
438                       userDefinedStyleClass,
439                       classAttributeName);
440               
441               String JavaDoc newClass = appendLocalClassToStyleClass(uiComponent,
442                                       defaulStyleClass);
443               if (subClass != null) {
444                   newClass += " " + subClass + disabledSuffix;
445               }
446               return newClass.trim();
447           }
448        }
449
450     private static String JavaDoc getValue(UIComponent uiComponent,
451                                     String JavaDoc value,
452                                     String JavaDoc attributeName) {
453         if (value != null) {
454             return value;
455         }
456         ValueBinding vb = uiComponent.getValueBinding(attributeName);
457         return vb != null ?
458                (String JavaDoc) vb.getValue(FacesContext.getCurrentInstance()) :
459                null;
460     }
461     
462     private static String JavaDoc appendLocalClassToStyleClass(UIComponent uiComponent,
463                                         String JavaDoc localClass) {
464         String JavaDoc styleClass = String.valueOf(uiComponent.getAttributes()
465                 .get("styleClass")).trim();
466         String JavaDoc[] classes = styleClass.split(" ");
467         StringBuffer JavaDoc name = new StringBuffer JavaDoc();
468         for (int i =0; i <classes.length; i++) {
469             if (classes[i] != null && classes[i].endsWith(CSS_DEFAULT.DIS_SUFFIX)) {
470                 name.append(classes[i].replaceAll(CSS_DEFAULT.DIS_SUFFIX,
471                         localClass + CSS_DEFAULT.DIS_SUFFIX));
472             } else {
473                 name.append(classes[i]+ localClass);
474             }
475             name.append(" ");
476         }
477        return name.toString().trim();
478     }
479     
480     public static String JavaDoc getClassName(UIComponent uiComponent,
481                                       String JavaDoc styleClass,
482                                       String JavaDoc styleClassAsString,
483                                       String JavaDoc defaultStyleClass) {
484         if (styleClass != null) {
485             return styleClass;
486         }
487         ValueBinding vb = uiComponent.getValueBinding(styleClassAsString);
488         return vb != null ?
489                (String JavaDoc) vb.getValue(FacesContext.getCurrentInstance()) :
490                defaultStyleClass;
491     }
492
493     public static ValueBinding getValueBinding(String JavaDoc valueRef) {
494         FacesContext facesContext = FacesContext.getCurrentInstance();
495         return facesContext.getApplication().createValueBinding(valueRef);
496     }
497
498     public static boolean isEventSource(FacesContext facesContext, UIComponent uiComponent) {
499         Object JavaDoc focusId = facesContext.getExternalContext()
500                 .getRequestParameterMap().get(FormRenderer.getFocusElementId());
501         if (focusId != null) {
502             if (focusId.toString()
503                     .equals(uiComponent.getClientId(facesContext))) {
504                 ((HtmlInputText) uiComponent).setFocus(true);
505             } else {
506                 ((HtmlInputText) uiComponent).setFocus(false);
507             }
508         }
509         Object JavaDoc componenetId = facesContext.getExternalContext()
510                 .getRequestParameterMap().get("ice.event.captured");
511         if (componenetId != null) {
512             if (componenetId.toString()
513                     .equals(uiComponent.getClientId(facesContext))) {
514                 return true;
515             } else {
516
517                 return false;
518             }
519         }
520         return false;
521     }
522 } // end of class Util
523
Popular Tags