001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033: /* Original Copyright:
034: * Copyright 2004 The Apache Software Foundation.
035: *
036: * Licensed under the Apache License, Version 2.0 (the "License");
037: * you may not use this file except in compliance with the License.
038: * You may obtain a copy of the License at
039: *
040: * http://www.apache.org/licenses/LICENSE-2.0
041: *
042: * Unless required by applicable law or agreed to in writing, software
043: * distributed under the License is distributed on an "AS IS" BASIS,
044: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
045: * See the License for the specific language governing permissions and
046: * limitations under the License.
047: */
048:
049: package com.icesoft.faces.component.ext.taglib;
050:
051: import com.icesoft.faces.component.CSS_DEFAULT;
052: import com.icesoft.faces.component.IceExtended;
053: import com.icesoft.faces.component.ext.HtmlInputText;
054: import com.icesoft.faces.component.ext.renderkit.FormRenderer;
055: import com.icesoft.faces.context.effects.Effect;
056: import com.icesoft.faces.context.effects.EffectBuilder;
057: import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
058: import org.apache.commons.logging.Log;
059: import org.apache.commons.logging.LogFactory;
060:
061: import javax.faces.component.UIComponent;
062: import javax.faces.component.UISelectItem;
063: import javax.faces.component.UISelectItems;
064: import javax.faces.context.FacesContext;
065: import javax.faces.el.ValueBinding;
066: import javax.faces.model.SelectItem;
067: import java.util.ArrayList;
068: import java.util.Collection;
069: import java.util.Iterator;
070: import java.util.List;
071: import java.util.Map;
072: import java.util.StringTokenizer;
073:
074: public class Util extends Object {
075: private static Log log = LogFactory.getLog(Util.class);
076:
077: /**
078: * Gets the comma separated list of visibility user roles from the given
079: * component and checks if current user is in one of these roles.
080: *
081: * @param component a user role aware component
082: * @return true if no user roles are defined for this component or user is
083: * in one of these roles, false otherwise
084: */
085: public static boolean isRenderedOnUserRole(UIComponent component) {
086: String userRole;
087: if (component instanceof IceExtended) {
088: userRole = ((IceExtended) component)
089: .getRenderedOnUserRole();
090: } else {
091: userRole = (String) component.getAttributes().get(
092: IceExtended.RENDERED_ON_USER_ROLE_ATTR);
093: }
094:
095: //there is no restriction
096: if (userRole == null) {
097: return true;
098: }
099:
100: FacesContext facesContext = FacesContext.getCurrentInstance();
101: StringTokenizer st = new StringTokenizer(userRole, ",");
102: while (st.hasMoreTokens()) {
103: if (facesContext.getExternalContext().isUserInRole(
104: st.nextToken().trim())) {
105: return true;
106: }
107: }
108: return false;
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 userRole;
121: if (component instanceof IceExtended) {
122: userRole = ((IceExtended) component).getEnabledOnUserRole();
123: } else {
124: userRole = (String) component.getAttributes().get(
125: 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 st = new StringTokenizer(userRole, ",");
135: while (st.hasMoreTokens()) {
136: if (facesContext.getExternalContext().isUserInRole(
137: 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 getSelectItems(FacesContext context,
162: UIComponent uiComponent) {
163:
164: List selectItems = new ArrayList();
165: Iterator children = uiComponent.getChildren().iterator();
166:
167: while (children.hasNext()) {
168: UIComponent nextSelectItemChild = (UIComponent) children
169: .next();
170: if (nextSelectItemChild instanceof UISelectItem) {
171: Object selectItemValue = ((UISelectItem) nextSelectItemChild)
172: .getValue();
173: if (selectItemValue != null
174: && selectItemValue instanceof SelectItem) {
175: selectItems.add(selectItemValue);
176: } else {
177: selectItems.add(new SelectItem(
178: ((UISelectItem) nextSelectItemChild)
179: .getItemValue(),
180: ((UISelectItem) nextSelectItemChild)
181: .getItemLabel(),
182: ((UISelectItem) nextSelectItemChild)
183: .getItemDescription(),
184: ((UISelectItem) nextSelectItemChild)
185: .isItemDisabled()));
186: }
187: } else if (nextSelectItemChild instanceof UISelectItems) {
188: Object selectItemsValue = ((UISelectItems) nextSelectItemChild)
189: .getValue();
190:
191: if (selectItemsValue != null) {
192: if (selectItemsValue instanceof SelectItem) {
193: selectItems.add(selectItemsValue);
194: } else if (selectItemsValue instanceof Collection) {
195: Iterator selectItemsIterator = ((Collection) selectItemsValue)
196: .iterator();
197: while (selectItemsIterator.hasNext()) {
198: selectItems.add(selectItemsIterator.next());
199: }
200: } else if (selectItemsValue instanceof SelectItem[]) {
201: SelectItem selectItemArray[] = (SelectItem[]) selectItemsValue;
202: for (int i = 0; i < selectItemArray.length; i++) {
203: selectItems.add(selectItemArray[i]);
204: }
205: } else if (selectItemsValue instanceof Map) {
206: Iterator selectItemIterator = ((Map) selectItemsValue)
207: .keySet().iterator();
208: while (selectItemIterator.hasNext()) {
209: Object nextKey = selectItemIterator.next();
210: if (nextKey != null) {
211: Object nextValue = ((Map) selectItemsValue)
212: .get(nextKey);
213: if (nextValue != null) {
214: selectItems.add(new SelectItem(
215: nextValue.toString(),
216: nextKey.toString()));
217: }
218: }
219: }
220: }
221: }
222: }
223: }
224: return selectItems;
225: }
226:
227: public static void addEffect(String effect, UIComponent panel) {
228: if (effect != null) {
229: if (isValueReference(effect)) {
230: ValueBinding vb = Util.getValueBinding(effect);
231: panel.setValueBinding("effect", vb);
232: } else {
233: Effect fx = (Effect) panel.getAttributes()
234: .get("effect");
235: if (fx == null) {
236: fx = EffectBuilder.build(effect);
237: panel.getAttributes().put("effect", fx);
238: }
239: }
240: }
241: }
242:
243: public static void addVisible(String visible, UIComponent panel) {
244: if (visible != null) {
245: if (isValueReference(visible)) {
246: ValueBinding vb = Util.getValueBinding(visible);
247: panel.setValueBinding("visible", vb);
248: } else {
249: Boolean boolVisible = new Boolean(visible);
250: panel.getAttributes().put("visible", boolVisible);
251:
252: }
253: }
254: }
255:
256: public static void addLocalEffect(String effect, String name,
257: UIComponent panel) {
258:
259: if (effect != null) {
260: if (log.isTraceEnabled()) {
261: log.trace("AddLocalEffect. String [" + effect
262: + "] name [" + name + "] class ["
263: + panel.getClass().getName() + "]");
264: }
265: if (isValueReference(effect)) {
266: if (log.isTraceEnabled()) {
267: log.trace("Adding Value Binding");
268: }
269: ValueBinding vb = Util.getValueBinding(effect);
270: panel.setValueBinding(name, vb);
271: } else {
272: if (log.isTraceEnabled()) {
273: log.trace("Adding Literal String");
274: }
275:
276: Effect fx = (Effect) panel.getAttributes().get(name);
277: if (fx == null) {
278: fx = EffectBuilder.build(effect);
279: panel.getAttributes().put(name, fx);
280: }
281: }
282: } else {
283: if (log.isTraceEnabled()) {
284: log.trace("AddLocalEffect. Effect is null");
285: }
286: }
287: }
288:
289: public static boolean isValueReference(String v) {
290: if (v == null) {
291: return false;
292: }
293: if (v.startsWith("#{") && v.endsWith("}")) {
294: return true;
295: }
296: return false;
297: }
298:
299: public static String stripPx(String s) {
300: if (s == null) {
301: return null;
302: }
303: int i = s.indexOf("px");
304: if (i != -1) {
305: return s.substring(0, i);
306: }
307: return s;
308: }
309:
310: /**
311: * This method should be used by the component/renderer class to
312: * get the qualifiedStyleClass for a class which has been used
313: * internally. This would return a qualifiedStyleClass string using the
314: * following pattern.
315: * componentDefaultClass + noneAttributeClass
316: * @param uiComponent
317: * @param noneAttributeClass
318: * @return qualifiedStyleClass string
319: */
320: public static String getQualifiedStyleClass(
321: UIComponent uiComponent, String noneAttributeClass) {
322: return getQualifiedStyleClass(uiComponent, noneAttributeClass,
323: null, null, false, true);
324:
325: }
326:
327: /**
328: * This method should be used by the renderer/component class that
329: * can be disabled, to get the qualifiedStyleClass for a class which has
330: * been used internally. This would return a qualifiedStyleClass string
331: * using the following pattern.
332: * defaulStyleClass + noneAttributeClass[-dis]
333: * @param uiComponent
334: * @param noneAttributeClass
335: * @return qualifiedStyleClass string
336: */
337: public static String getQualifiedStyleClass(
338: UIComponent uiComponent, String noneAttributeClass,
339: boolean disabled) {
340: return getQualifiedStyleClass(uiComponent, noneAttributeClass,
341: null, null, disabled, true);
342:
343: }
344:
345: /**
346: * This method should be used by the renderer/component class to get the
347: * qualifiedStyleClass for a "styleClass" or any other class related attribute
348: * on the component (e.g.) inputText on inputFile component.
349: * This would return a qualifiedStyleClass string using the following pattern.
350: * defaulStyleClass [userDefinedStyleClass]
351: * @param uiComponent
352: * @param userDefinedStyleClass
353: * @param defaulStyleClass
354: * @param classAttributeName
355: * @return qualifiedStyleClass string
356: */
357:
358: public static String getQualifiedStyleClass(
359: UIComponent uiComponent, String userDefinedStyleClass,
360: String defaulStyleClass, String classAttributeName) {
361: return getQualifiedStyleClass(uiComponent,
362: userDefinedStyleClass, defaulStyleClass,
363: classAttributeName, false, false);
364:
365: }
366:
367: /**
368: * This method should be used by the renderer/component class that can be
369: * disabled, to get the qualifiedStyleClass for a "styleClass" or any other
370: * class related attribute on the component (e.g.) "inputText" attribute on
371: * "inputFile" component. This methods returns a qualifiedStyleClass string
372: * using the following pattern.
373: * defaulStyleClass[-dis] [userDefinedStyleClass[-dis]]
374: * @param uiComponent
375: * @param userDefinedStyleClass
376: * @param defaulStyleClass
377: * @param classAttributeName
378: * @return qualifiedStyleClass string
379: */
380: public static String getQualifiedStyleClass(
381: UIComponent uiComponent, String userDefinedStyleClass,
382: String defaulStyleClass, String classAttributeName,
383: boolean disabled) {
384: return getQualifiedStyleClass(uiComponent,
385: userDefinedStyleClass, defaulStyleClass,
386: classAttributeName, disabled, false);
387:
388: }
389:
390: private static String getQualifiedStyleClass(
391: UIComponent uiComponent, String userDefinedStyleClass,
392: String defaulStyleClass, String classAttributeName,
393: boolean disabled, boolean isNoneAttributeClass) {
394: if (isNoneAttributeClass) {
395: //1- This requested class is used internally by the component and
396: //not visible to the developer (or can not be set using an attribute)
397: return appendLocalClassToStyleClass(uiComponent,
398: userDefinedStyleClass);
399: }
400:
401: String disabledSuffix = disabled ? CSS_DEFAULT.DIS_SUFFIX : "";
402: String styleClass = null;
403:
404: if ("styleClass".equalsIgnoreCase(classAttributeName)) {
405: //2- The following code is responsible to assembled the qualified
406: //value for "styleClass" attribute using the following syntax:
407: // defaultStyleClass[-dis] [userDefinedStyleClass[-dis]]
408: styleClass = getValue(uiComponent, userDefinedStyleClass,
409: "styleClass");
410: styleClass = defaulStyleClass
411: + disabledSuffix
412: + " "
413: + ((styleClass != null) ? styleClass
414: + disabledSuffix : "");
415: return styleClass.trim();
416: } else {
417: //3- the following code is to deal with other style class
418: //attributes that has been defined on the components (e.g.)
419: // "inputTextClass" attribute on "inputFile" component.
420: styleClass = String.valueOf(uiComponent.getAttributes()
421: .get("styleClass"));
422: String subClass = getValue(uiComponent,
423: userDefinedStyleClass, classAttributeName);
424:
425: String newClass = appendLocalClassToStyleClass(uiComponent,
426: defaulStyleClass);
427: if (subClass != null) {
428: newClass += " " + subClass + disabledSuffix;
429: }
430: return newClass.trim();
431: }
432: }
433:
434: private static String getValue(UIComponent uiComponent,
435: String value, String attributeName) {
436: if (value != null) {
437: return value;
438: }
439: ValueBinding vb = uiComponent.getValueBinding(attributeName);
440: return vb != null ? (String) vb.getValue(FacesContext
441: .getCurrentInstance()) : null;
442: }
443:
444: private static String appendLocalClassToStyleClass(
445: UIComponent uiComponent, String localClass) {
446: String styleClass = String.valueOf(
447: uiComponent.getAttributes().get("styleClass")).trim();
448: String[] classes = styleClass.split(" ");
449: StringBuffer name = new StringBuffer();
450: for (int i = 0; i < classes.length; i++) {
451: if (classes[i] != null
452: && classes[i].endsWith(CSS_DEFAULT.DIS_SUFFIX)) {
453: name.append(classes[i].replaceAll(
454: CSS_DEFAULT.DIS_SUFFIX, localClass
455: + CSS_DEFAULT.DIS_SUFFIX));
456: } else {
457: name.append(classes[i] + localClass);
458: }
459: name.append(" ");
460: }
461: return name.toString().trim();
462: }
463:
464: public static String getClassName(UIComponent uiComponent,
465: String styleClass, String styleClassAsString,
466: String defaultStyleClass) {
467: if (styleClass != null) {
468: return styleClass;
469: }
470: ValueBinding vb = uiComponent
471: .getValueBinding(styleClassAsString);
472: return vb != null ? (String) vb.getValue(FacesContext
473: .getCurrentInstance()) : defaultStyleClass;
474: }
475:
476: public static ValueBinding getValueBinding(String valueRef) {
477: FacesContext facesContext = FacesContext.getCurrentInstance();
478: return facesContext.getApplication().createValueBinding(
479: valueRef);
480: }
481:
482: public static boolean isEventSource(FacesContext facesContext,
483: UIComponent uiComponent) {
484: Object focusId = facesContext.getExternalContext()
485: .getRequestParameterMap().get(
486: FormRenderer.getFocusElementId());
487: if (focusId != null) {
488: if (focusId.toString().equals(
489: uiComponent.getClientId(facesContext))) {
490: ((HtmlInputText) uiComponent).setFocus(true);
491: } else {
492: ((HtmlInputText) uiComponent).setFocus(false);
493: }
494: }
495: Object componenetId = facesContext.getExternalContext()
496: .getRequestParameterMap().get("ice.event.captured");
497: if (componenetId != null) {
498: if (componenetId.toString().equals(
499: uiComponent.getClientId(facesContext))) {
500: return true;
501: } else {
502:
503: return false;
504: }
505: }
506: return false;
507: }
508: } // end of class Util
|