private Object removeFromMap(Serializable key, String mapKey) { Object ret = null; if (component.initialStateMarked()) { Map<String, Object> dMap = (Map<String, Object>) deltaMap.get(key); if (dMap != null) { ret = dMap.remove(mapKey); if (dMap.isEmpty()) { deltaMap.remove(key); } } } Map<String, Object> map = (Map<String, Object>) get(key); if (map != null) { if (ret == null) { ret = map.remove(mapKey); } else { map.remove(mapKey); } if (map.isEmpty()) { defaultMap.remove(key); } } if (ret != null && !component.initialStateMarked()) { deltaMap.remove(key); } return ret; }
public static UIComponent addTransient( FacesContext context, ServletRequest req, UIComponent parent, String prevId, Class childClass) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); BodyContent body = parentTag.getBodyContent(); if (body != null) addVerbatim(parent, body); } UIComponent child = null; ; if (child == null) child = (UIComponent) childClass.newInstance(); child.setTransient(true); addChild(parent, prevId, child); return child; }
protected void assignUniqueId(FaceletContext ctx, UIComponent parent, String id, UIComponent c) { // If the id is specified as a literal, and the component is being // repeated (by c:forEach, for example), use generated unique ids // after the first instance if (this.id != null && !(this.id.isLiteral() && ComponentSupport.getNeedUniqueIds(ctx))) { c.setId(this.id.getValue(ctx)); } else { UIViewRoot root = ComponentSupport.getViewRoot(ctx, parent); if (root != null) { String uid; IdMapper mapper = IdMapper.getMapper(ctx.getFacesContext()); String mid = ((mapper != null) ? mapper.getAliasedId(id) : id); UIComponent ancestorNamingContainer = parent.getNamingContainer(); if (null != ancestorNamingContainer && ancestorNamingContainer instanceof UniqueIdVendor) { uid = ((UniqueIdVendor) ancestorNamingContainer).createUniqueId(ctx.getFacesContext(), mid); } else { uid = root.createUniqueId(ctx.getFacesContext(), mid); } c.setId(uid); } } if (this.rendererType != null) { c.setRendererType(this.rendererType); } }
public static UIComponent findPersistent( FacesContext context, ServletRequest req, UIComponent parent, String id) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); BodyContent body = null; if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); body = parentTag.getBodyContent(); } if (parent != null) { List<UIComponent> children = parent.getChildren(); int size = children.size(); String prevId = null; for (int i = 0; i < size; i++) { UIComponent child = children.get(i); if (id.equals(child.getId())) { if (body != null) addVerbatim(parent, prevId, body); return child; } if (child.getId() != null) prevId = child.getId(); } } return null; }
public void render(Graphics g) { g.setColor(color); g.fillRect(position.x, position.y, size.x, size.y); for (UIComponent component : components) { component.render(g); } }
public static UIComponent findParentForm(FacesContext context, UIComponent component) { UIComponent parent = component; while (parent != null) if (parent instanceof UIForm) break; else parent = parent.getParent(); return parent; }
protected void setProperties(UIComponent component) { if (_binding != null) component.setValueExpression("binding", _binding); if (_rendered != null) component.setValueExpression("rendered", _rendered); String type = getRendererType(); if (type != null) component.setRendererType(type); }
public static String findComponentClientId(String id) { UIComponent component = null; FacesContext facesContext = FacesContext.getCurrentInstance(); component = findComponent(facesContext.getViewRoot(), id); return component.getClientId(facesContext); }
public void renderBlockToUpdate(UIComponent uicomponent, WebuiRequestContext context, Writer w) throws Exception { w.write("<div class=\"BlockToUpdate\">"); w.append("<div class=\"BlockToUpdateId\">").append(uicomponent.getId()).append("</div>"); w.write("<div class=\"BlockToUpdateData\">"); uicomponent.processRender(context); w.write("</div>"); w.write("</div>"); }
@SuppressWarnings({"UnusedDeclaration"}) protected UIComponent findReparentedComponent( FaceletContext ctx, UIComponent parent, String tagId) { UIComponent facet = parent.getFacets().get(UIComponent.COMPOSITE_FACET_NAME); if (facet != null) { UIComponent newParent = facet.findComponent((String) parent.getAttributes().get(tagId)); if (newParent != null) return ComponentSupport.findChildByTagId(newParent, tagId); } return null; }
private void doSetBindings(ELContext elContext, UIComponent component) { if (component == null) return; ValueExpression binding = component.getValueExpression("binding"); if (binding != null) binding.setValue(elContext, component); Iterator<UIComponent> iter = component.getFacetsAndChildren(); while (iter.hasNext()) doSetBindings(elContext, iter.next()); }
/** * Docs * * @param key * @param defaultValue value to return if key is not present in main or delta-map * @return value - if null, defaultValue */ public Object eval(Serializable key, Object defaultValue) { Object retVal = get(key); if (retVal == null) { ValueExpression ve = component.getValueExpression(key.toString()); if (ve != null) { retVal = ve.getValue(component.getFacesContext().getELContext()); } } return ((retVal != null) ? retVal : defaultValue); }
protected boolean pushComponentToEL( FaceletContext ctx, UIComponent c, CompositeComponentStackManager ccStackManager) { c.pushComponentToEL(ctx.getFacesContext(), c); boolean compcompPushed = false; if (UIComponent.isCompositeComponent(c)) { compcompPushed = ccStackManager.push(c, TreeCreation); } return compcompPushed; }
protected List getRenderedChildColumnsList(UIComponent component) { List results = new ArrayList(); Iterator kids = component.getChildren().iterator(); while (kids.hasNext()) { UIComponent kid = (UIComponent) kids.next(); if ((kid instanceof UIColumn) && kid.isRendered()) { results.add(kid); } else if (kid instanceof UIColumns) { results.add(kid); } } return results; }
public static String getWidgetVar(String id) { UIComponent component = findComponent(FacesContext.getCurrentInstance().getViewRoot(), id); Renderer renderer = FacesContext.getCurrentInstance() .getRenderKit() .getRenderer(component.getFamily(), component.getRendererType()); if (component == null) throw new FacesException("Cannot find component " + id + " in view."); else if (!(renderer instanceof CoreRenderer)) throw new FacesException("Component with id " + id + " is not derived from CoreRenderer."); return ((CoreRenderer) renderer).resolveWidgetVar(component); }
/** * Override the {@link UIComponent#getContainerClientId} to allow users to disable this form from * prepending its <code>clientId</code> to its descendent's <code>clientIds</code> depending on * the value of this form's {@link #isPrependId} property. */ public String getContainerClientId(FacesContext context) { if (this.isPrependId()) { return super.getContainerClientId(context); } else { UIComponent parent = this.getParent(); while (parent != null) { if (parent instanceof NamingContainer) { return parent.getContainerClientId(context); } parent = parent.getParent(); } } return null; }
public static void decorateAttribute(UIComponent component, String attribute, String value) { String attributeValue = (String) component.getAttributes().get(attribute); if (attributeValue != null) { if (attributeValue.indexOf(value) == -1) { String decoratedValue = attributeValue + ";" + value; component.getAttributes().put(attribute, decoratedValue); } else { component.getAttributes().put(attribute, attributeValue); } } else { component.getAttributes().put(attribute, value); } }
public static UIComponent addFacet( FacesContext context, ServletRequest req, UIComponent parent, String facetName, ValueExpression binding, Class childClass) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); } UIComponent child = null; if (binding != null) child = (UIComponent) binding.getValue(context.getELContext()); if (child == null) child = (UIComponent) childClass.newInstance(); if (parent != null) parent.getFacets().put(facetName, child); if (binding != null) binding.setValue(context.getELContext(), child); return child; }
/** * Override {@link UIComponent#processUpdates} to ensure that the children of this <code>UIForm * </code> instance are only processed if {@link #isSubmitted} returns <code>true</code>. * * @throws NullPointerException {@inheritDoc} */ public void processUpdates(FacesContext context) { if (context == null) { throw new NullPointerException(); } if (!isSubmitted()) { return; } // Process all facets and children of this component Iterator kids = getFacetsAndChildren(); while (kids.hasNext()) { UIComponent kid = (UIComponent) kids.next(); kid.processUpdates(context); } }
public static List<SelectItem> createSelectItems(UIComponent component) { List<SelectItem> items = new ArrayList<SelectItem>(); Iterator<UIComponent> children = component.getChildren().iterator(); while (children.hasNext()) { UIComponent child = children.next(); if (child instanceof UISelectItem) { UISelectItem selectItem = (UISelectItem) child; items.add(new SelectItem(selectItem.getItemValue(), selectItem.getItemLabel())); } else if (child instanceof UISelectItems) { Object selectItems = ((UISelectItems) child).getValue(); if (selectItems instanceof SelectItem[]) { SelectItem[] itemsArray = (SelectItem[]) selectItems; for (SelectItem item : itemsArray) items.add(new SelectItem(item.getValue(), item.getLabel())); } else if (selectItems instanceof Collection) { Collection<SelectItem> collection = (Collection<SelectItem>) selectItems; for (SelectItem item : collection) items.add(new SelectItem(item.getValue(), item.getLabel())); } } } return items; }
private void adjustIndexOfDynamicChildren(FacesContext context, UIComponent parent) { StateContext stateContext = StateContext.getStateContext(context); if (!stateContext.hasOneOrMoreDynamicChild(parent)) { return; } List<UIComponent> children = parent.getChildren(); List<UIComponent> dynamicChildren = Collections.emptyList(); for (UIComponent cur : children) { if (stateContext.componentAddedDynamically(cur)) { if (dynamicChildren.isEmpty()) { dynamicChildren = new ArrayList<UIComponent>(children.size()); } dynamicChildren.add(cur); } } for (UIComponent cur : dynamicChildren) { int i = stateContext.getIndexOfDynamicallyAddedChildInParent(cur); if (-1 != i) { children.remove(cur); children.add(i, cur); } } }
private static void renderPassThruAttributesUnoptimized( FacesContext context, ResponseWriter writer, UIComponent component, Attribute[] knownAttributes, Map<String, List<ClientBehavior>> behaviors) throws IOException { boolean isXhtml = XHTML_CONTENT_TYPE.equals(writer.getContentType()); Map<String, Object> attrMap = component.getAttributes(); for (Attribute attribute : knownAttributes) { String attrName = attribute.getName(); String[] events = attribute.getEvents(); boolean hasBehavior = ((events != null) && (events.length > 0) && (behaviors.containsKey(events[0]))); Object value = attrMap.get(attrName); if (value != null && shouldRenderAttribute(value) && !hasBehavior) { writer.writeAttribute(prefixAttribute(attrName, isXhtml), value, attrName); } else if (hasBehavior) { renderHandler(context, component, null, attrName, value, events[0]); } } }
/** * One and only implementation of save-state - makes all other implementations unnecessary. * * @param context * @return the saved state */ public Object saveState(FacesContext context) { if (component.initialStateMarked()) { return saveMap(deltaMap); } else { return saveMap(defaultMap); } }
private static String getSubmitHandler( FacesContext context, UIComponent component, Collection<ClientBehaviorContext.Parameter> params, boolean preventDefault) { StringBuilder builder = new StringBuilder(256); String formClientId = getFormClientId(component, context); String componentClientId = component.getClientId(context); builder.append("qab.sf(document.getElementById('"); builder.append(formClientId); builder.append("'),{"); appendProperty(builder, componentClientId, componentClientId); if ((null != params) && (!params.isEmpty())) { for (ClientBehaviorContext.Parameter param : params) { appendProperty(builder, param.getName(), param.getValue()); } } builder.append("})"); if (preventDefault) { builder.append(";return false"); } return builder.toString(); }
/** * One and only implementation of restore state. Makes all other implementations unnecessary. * * @param context FacesContext * @param state the state to be restored. */ public void restoreState(FacesContext context, Object state) { FacesContext fc = FacesContext.getCurrentInstance(); Object[] savedState = (Object[]) state; component.initialState = (Boolean) savedState[savedState.length - 1]; int length = (savedState.length - 1) / 2; for (int i = 0; i < length; i++) { Object value = savedState[i * 2 + 1]; if (Void.TYPE.equals(value)) { value = null; } Serializable serializable = (Serializable) savedState[i * 2]; if (value != null) { if (value instanceof Collection) { value = restoreAttachedState(fc, value); } else if (value instanceof StateHolderSaver) { value = ((StateHolderSaver) value).restore(context); } else { value = (value instanceof Serializable ? value : restoreAttachedState(fc, value)); } } if (value instanceof Map) { for (Map.Entry<String, Object> entry : ((Map<String, Object>) value).entrySet()) { this.put(serializable, entry.getKey(), entry.getValue()); } } else if (value instanceof List) { for (Object o : ((List<Object>) value)) { this.add(serializable, o); } } else { put(serializable, value); } } }
public static UIComponent addPersistent( FacesContext context, ServletRequest req, UIComponent parent, ValueExpression binding, Class childClass) throws Exception { if (context == null) context = FacesContext.getCurrentInstance(); if (parent == null) { UIComponentClassicTagBase parentTag = (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent"); parent = parentTag.getComponentInstance(); BodyContent body = parentTag.getBodyContent(); addVerbatim(parent, body); } UIComponent child = null; if (binding != null) child = (UIComponent) binding.getValue(context.getELContext()); if (child == null) { child = (UIComponent) childClass.newInstance(); // jsf/3251 if (binding != null) binding.setValue(context.getELContext(), child); } if (parent != null) parent.getChildren().add(child); return child; }
public static String getFormClientId(UIComponent component, FacesContext context) { UIComponent parent = component.getParent(); while (parent != null) { if (parent instanceof UIForm) { break; } parent = parent.getParent(); } UIForm form = (UIForm) parent; if (form != null) { return form.getClientId(context); } return null; }
@Override protected UIComponent createComponent(FacesContext context, String newId) throws JspException { Application app = context.getApplication(); UIComponent component; if (_binding != null) { component = app.createComponent(_binding, context, getComponentType()); component.setValueExpression("binding", _binding); } else component = app.createComponent(getComponentType()); component.setId(getId()); setProperties(component); return component; }
// 编写处理Action事件的方法 public void processAction(ActionEvent event) { // 获取当前的FacesContext对象 FacesContext context = FacesContext.getCurrentInstance(); // 获取JSF页面中<f:view.../>元素 UIViewRoot viewRoot = context.getViewRoot(); // 通过ID获取<f:view.../>内的<h:form.../>子元素。 UIComponent comp = viewRoot.findComponent("addForm"); // 通过ID获取<h:form.../>内的第一个<h:inputText.../>子元素。 UIInput input = (UIInput) comp.findComponent("name"); // 通过ID获取<h:form.../>内的第二个<h:inputText.../>子元素。 HtmlInputText price = (HtmlInputText) comp.findComponent("price"); if (input.getValue().equals("疯狂Java讲义")) { price.setSize(60); price.setValue("99.0元"); price.setStyle("background-color:#9999ff;" + "font-weight:bold"); } }
@SuppressWarnings("unchecked") public static void renderPassThruAttributes( FacesContext context, ResponseWriter writer, UIComponent component, Attribute[] attributes, Map<String, List<ClientBehavior>> behaviors) throws IOException { if (behaviors == null) { behaviors = Collections.emptyMap(); } if ((behaviors.size() < 2)) { List<String> setAttributes = (List<String>) component.getAttributes().get(ATTRIBUTES_THAT_ARE_SET_KEY); if (setAttributes != null) { renderPassThruAttributesOptimized( context, writer, component, attributes, setAttributes, behaviors); } } else { renderPassThruAttributesUnoptimized(context, writer, component, attributes, behaviors); } List<String> html5PassThru = (List<String>) component.getAttributes().get(HTML5DataRule.HTML5DATAATTRIBUTES); if (html5PassThru != null) { for (String attribute : html5PassThru) { String value; if (component.getValueExpression(attribute) != null) { Object valueObject = component.getValueExpression(attribute).getValue(context.getELContext()); value = valueObject != null ? valueObject.toString() : null; } else { value = (String) component.getAttributes().get(attribute); } if (value != null) { writer.writeAttribute(attribute, value, null); } } } }