📄 uicomponentbase.java
字号:
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 * as published by the Free Software Foundation. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package javax.faces.component;import java.beans.*;import java.lang.reflect.*;import java.lang.annotation.Annotation;import java.io.*;import java.util.*;import java.util.logging.*;import javax.el.*;import javax.faces.*;import javax.faces.application.*;import javax.faces.component.html.*;import javax.faces.context.*;import javax.faces.el.*;import javax.faces.event.*;import javax.faces.render.*;public abstract class UIComponentBase extends UIComponent{ private static final Logger log = Logger.getLogger(UIComponentBase.class.getName()); private static final UIComponent []NULL_FACETS_AND_CHILDREN = new UIComponent[0]; private static final FacesListener []NULL_FACES_LISTENERS = new FacesListener[0]; private static final HashMap<String,Integer> _rendererToCodeMap = new HashMap<String,Integer>(); private static final HashMap<Integer,String> _codeToRendererMap = new HashMap<Integer,String>(); private static final WeakHashMap<Class,HashMap<String,Property>> _compMap = new WeakHashMap<Class,HashMap<String,Property>>(); private String _id; private String _clientId; private UIComponent _parent; private String _rendererType; private ValueExpression _rendererTypeExpr; private boolean _isTransient; private Boolean _isRendered; private ValueExpression _isRenderedExpr; private ValueExpression _bindingExpr; private ComponentList _children; private ComponentMap _facets; private UIComponent []_facetsAndChildren; private AttributeMap _attributeMap; protected Map<String,ValueExpression> _bindings; private FacesListener []_facesListeners = NULL_FACES_LISTENERS; public Map<String,Object> getAttributes() { if (_attributeMap == null) _attributeMap = new AttributeMap(this); return _attributeMap; } @Deprecated public ValueBinding getValueBinding(String name) { ValueExpression expr = getValueExpression(name); if (expr == null) return null; else if (expr instanceof ValueExpressionAdapter) return ((ValueExpressionAdapter) expr).getBinding(); else return new ValueBindingAdapter(expr); } @Deprecated public void setValueBinding(String name, ValueBinding binding) { setValueExpression(name, new ValueExpressionAdapter(binding)); } /** * Returns the value expression for an attribute * * @param name the name of the attribute to get */ @Override public ValueExpression getValueExpression(String name) { if (name == null) throw new NullPointerException(); if ("rendered".equals(name)) return _isRenderedExpr; else if ("rendererType".equals(name)) return _rendererTypeExpr; else if ("binding".equals(name)) return _bindingExpr; if (_bindings != null) return _bindings.get(name); else return null; } /** * Sets the value expression for an attribute * * @param name the name of the attribute to set * @param expr the value expression */ @Override public void setValueExpression(String name, ValueExpression expr) { if (name.equals("id")) throw new IllegalArgumentException("'id' is not a valid ValueExpression name."); else if (name.equals("parent")) throw new IllegalArgumentException("'parent' is not a valid ValueExpression name."); if ("rendered".equals(name)) { if (expr.isLiteralText()) { _isRendered = Util.booleanValueOf(expr.getValue(null)); return; } else _isRenderedExpr = expr; } else if ("rendererType".equals(name)) { if (expr.isLiteralText()) { _rendererType = String.valueOf(expr.getValue(null)); return; } else _rendererTypeExpr = expr; } else if ("binding".equals(name)) { _bindingExpr = expr; } try { if (expr != null) { if (expr.isLiteralText()) { getAttributes().put(name, expr.getValue(null)); } else { if (_bindings == null) _bindings = new HashMap<String,ValueExpression>(); _bindings.put(name, expr); } } else if (_bindings != null) _bindings.remove(name); } catch (ELException e) { throw new FacesException(e); } } /** * Returns the client-specific id for the component. */ @Override public String getClientId(FacesContext context) { if (context == null) throw new NullPointerException(); if (_clientId != null) return _clientId; String parentId = null; for (UIComponent ptr = getParent(); ptr != null; ptr = ptr.getParent()) { if (ptr instanceof NamingContainer) { parentId = ptr.getContainerClientId(context); break; } } String myId = _id; if (myId == null) { myId = context.getViewRoot().createUniqueId(); } if (parentId != null) myId = parentId + NamingContainer.SEPARATOR_CHAR + myId; Renderer renderer = getRenderer(context); if (renderer != null) _clientId = renderer.convertClientId(context, myId); else _clientId = myId; return _clientId; } public String getFamily() { return null; } public String getId() { return _id; } public void setId(String id) { if (id == null) { _id = null; _clientId = null; return; } int len = id.length(); if (len == 0) throw new IllegalArgumentException(); char ch = id.charAt(0); if (! ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || ch == '_')) throw new IllegalArgumentException(); for (int i = 1; i < len; i++) { ch = id.charAt(i); if (! ('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || '0' <= ch && ch <= '9' || ch == '_' || ch == '-')) throw new IllegalArgumentException(); } _id = id; _clientId = null; } public UIComponent getParent() { return _parent; } public void setParent(UIComponent parent) { _parent = parent; } public boolean isRendered() { if (_isRendered != null) return _isRendered; else if (_isRenderedExpr != null) return Util.evalBoolean(_isRenderedExpr, getFacesContext()); else return true; } public void setRendered(boolean isRendered) { _isRendered = isRendered; } public String getRendererType() { if (_rendererType != null) return _rendererType; else if (_rendererTypeExpr != null) return Util.evalString(_rendererTypeExpr, getFacesContext()); else return null; } public void setRendererType(String rendererType) { _rendererType = rendererType; } public boolean getRendersChildren() { Renderer renderer = getRenderer(getFacesContext()); if (renderer != null) return renderer.getRendersChildren(); else return false; } public List<UIComponent> getChildren() { if (_children == null) _children = new ComponentList(this); return _children; } public int getChildCount() { if (_children != null) return _children.size(); else return 0; } public UIComponent findComponent(String expr) { UIComponent base = null; String []values = expr.split(":"); if (values[0].equals("")) { for (base = this; base.getParent() != null; base = base.getParent()) { } } else { for (base = this; base.getParent() != null && ! (base instanceof NamingContainer); base = base.getParent()) { } } for (int i = 0; i < values.length; i++) { String v = values[i]; if ("".equals(v)) continue; base = findComponent(base, v); if (i + 1 == values.length) return base; if (base == null) return base; if (! (base instanceof NamingContainer)) { throw new IllegalArgumentException("'" + v + "' in expression '" + expr + "' does not match an intermediate NamingContainer."); } } return base; } private static UIComponent findComponent(UIComponent comp, String id) { if (id.equals(comp.getId())) return comp; Iterator iter = comp.getFacetsAndChildren(); while (iter.hasNext()) { UIComponent child = (UIComponent) iter.next(); if (id.equals(child.getId())) return child; if (! (child instanceof NamingContainer)) { UIComponent desc = findComponent(child, id); if (desc != null) return desc; } } return null; } public Map<String,UIComponent> getFacets() { if (_facets == null) _facets = new ComponentMap(this); return _facets; } public UIComponent getFacet(String name) { if (_facets != null) return _facets.get(name); else return null; } public Iterator<UIComponent> getFacetsAndChildren() { return new FacetAndChildIterator(getFacetsAndChildrenArray()); } UIComponent []getFacetsAndChildrenArray() { if (_facetsAndChildren == null) { if (_children == null && _facets == null) _facetsAndChildren = NULL_FACETS_AND_CHILDREN; else { int facetCount = getFacetCount(); int childCount = getChildCount(); _facetsAndChildren = new UIComponent[facetCount + childCount]; int i = 0; if (_facets != null) { for (UIComponent facet : _facets.values()) { _facetsAndChildren[i++] = facet; } } for (int j = 0; j < childCount; j++) { _facetsAndChildren[i++] = _children.get(j); } } } return _facetsAndChildren; } // // Listeners, broadcast and event handling // protected void addFacesListener(FacesListener listener) { if (listener == null) throw new NullPointerException(); int length = _facesListeners.length; FacesListener[] newListeners = new FacesListener[length + 1]; System.arraycopy(_facesListeners, 0, newListeners, 0, length); newListeners[length] = listener; _facesListeners = newListeners; } protected FacesListener []getFacesListeners(Class cl) { if (FacesListener.class.equals(cl)) return _facesListeners; int count = 0; for (int i = _facesListeners.length - 1; i >= 0; i--) { if (cl.isAssignableFrom(_facesListeners[i].getClass())) count++; } FacesListener []array = (FacesListener []) Array.newInstance(cl, count); count = 0; for (int i = _facesListeners.length - 1; i >= 0; i--) { if (cl.isAssignableFrom(_facesListeners[i].getClass())) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -