📄 pageimpl.java
字号:
/* PageImpl.java{{IS_NOTE Purpose: Description: History: Fri Jun 3 18:17:32 2005, Created by tomyeh}}IS_NOTECopyright (C) 2005 Potix Corporation. All Rights Reserved.{{IS_RIGHT This program is distributed under GPL Version 2.0 in the hope that it will be useful, but WITHOUT ANY WARRANTY.}}IS_RIGHT*/package org.zkoss.zk.ui.impl;import java.lang.reflect.Method;import java.util.Iterator;import java.util.List;import java.util.LinkedList;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.AbstractMap;import java.util.Set;import java.util.HashSet;import java.util.Collection;import java.util.Collections;import java.io.Writer;import java.io.IOException;import javax.servlet.jsp.el.FunctionMapper;import org.zkoss.lang.D;import org.zkoss.lang.Objects;import org.zkoss.lang.Strings;import org.zkoss.lang.Exceptions;import org.zkoss.lang.Expectable;import org.zkoss.util.CollectionsX;import org.zkoss.util.logging.Log;import org.zkoss.zk.mesg.MZk;import org.zkoss.zk.ui.Desktop;import org.zkoss.zk.ui.Page;import org.zkoss.zk.ui.Session;import org.zkoss.zk.ui.IdSpace;import org.zkoss.zk.ui.Component;import org.zkoss.zk.ui.Executions;import org.zkoss.zk.ui.Execution;import org.zkoss.zk.ui.UiException;import org.zkoss.zk.ui.ComponentNotFoundException;import org.zkoss.zk.ui.event.EventListener;import org.zkoss.zk.ui.event.Events;import org.zkoss.zk.ui.metainfo.PageDefinition;import org.zkoss.zk.ui.metainfo.LanguageDefinition;import org.zkoss.zk.ui.metainfo.ComponentDefinition;import org.zkoss.zk.ui.metainfo.ComponentDefinitionMap;import org.zkoss.zk.ui.metainfo.DefinitionNotFoundException;import org.zkoss.zk.ui.util.Interpreter;import org.zkoss.zk.ui.util.Namespace;import org.zkoss.zk.ui.util.VariableResolver;import org.zkoss.zk.ui.sys.ExecutionCtrl;import org.zkoss.zk.ui.sys.WebAppCtrl;import org.zkoss.zk.ui.sys.DesktopCtrl;import org.zkoss.zk.ui.sys.PageCtrl;import org.zkoss.zk.ui.sys.ComponentCtrl;import org.zkoss.zk.ui.sys.ComponentsCtrl;import org.zkoss.zk.ui.sys.Variables;import org.zkoss.zk.ui.sys.UiEngine;import org.zkoss.zk.ui.impl.bsh.BshInterpreter;import org.zkoss.zk.au.AuSetTitle;/** * An implmentation of {@link Page} and {@link PageCtrl}. * Refer to them for more details. * * <p>Note: though {@link PageImpl} is serializable, it is designed * to work with Web container to enable the serialization of sessions. * It is not suggested to serialize and desrialize it directly since * many fields might be lost. * * <p>On the other hand, it is OK to serialize and deserialize * {@link Component}. * * <p>Implementation Notes:<br> * It is not thread-safe because it is protected by the spec: * at most one thread can access a page and all its components at the same time. * * @author tomyeh */public class PageImpl implements Page, PageCtrl, java.io.Serializable { private static final Log log = Log.lookup(PageImpl.class); private static final Log _zklog = Log.lookup("org.zkoss.zk.log"); private static final long serialVersionUID = 20060707L; /** URI for redrawing as a desktop or part of another desktop. */ private final String _dkUri, _pgUri; /** The component that includes this page, or null if not included. */ private transient Component _owner; /** Used to retore _owner. */ private transient String _ownerUuid; private transient Desktop _desktop; private String _id; private transient Interpreter _ip; private String _title = "", _style = ""; private final String _path; /** A list of root components. */ private final List _roots = new LinkedList(); private transient List _roRoots; /** A map of fellows. */ private transient Map _fellows; /** A map of attributes. */ private transient Map _attrs; //don't create it dynamically because _ip bind it at constructor /** A map of event listener: Map(evtnm, List(EventListener)). */ private transient Map _listeners; /** The default parent. */ private transient Component _defparent; /** The reason to store it is PageDefinition is not serializable. */ private FunctionMapper _funmap; /** The reason to store it is PageDefinition is not serializable. */ private ComponentDefinitionMap _compdefs; /** The reason to store it is PageDefinition is not serializable. */ private transient LanguageDefinition _langdef; /** The header tags. */ private String _headers = ""; /** Constructs a page by giving the page definition. * * <p>Note: when a page is constructed, it doesn't belong to a desktop * yet. Caller has to invoke {@link #init} to complete * the creation of a page. * Why two phase? Contructor could be called before execution * is activated, but {@link #init} must be called in an execution. * * <p>Also note that {@link #getId} and {@link #getTitle} * are not ready until {@link #init} is called. * * @param pgdef the page definition (never null). */ public PageImpl(PageDefinition pgdef) { _langdef = pgdef.getLanguageDefinition(); _dkUri = _langdef.getDesktopURI(); _pgUri = _langdef.getPageURI(); _compdefs = pgdef.getComponentDefinitionMap(); _path = pgdef.getRequestPath(); init(); } /** Constructs a page by giving the language definition. It is mainly * used to create a page for {@link org.zkoss.zk.ui.Richlet}. * * <p>Note: when a page is constructed, it doesn't belong to a desktop * yet. Caller has to invoke {@link #init} to complete * the creation of a page. * * <p>Also note that {@link #getId} and {@link #getTitle} * are not ready until {@link #init} is called. * * @param langdef the language definition (never null). * @param path the request path, or null if not available */ public PageImpl(LanguageDefinition langdef, String path) { _langdef = langdef; _dkUri = _langdef.getDesktopURI(); _pgUri = _langdef.getPageURI(); _compdefs = new ComponentDefinitionMap(); _path = path != null ? path: ""; init(); } /** Initialized the page when contructed or deserialized. */ protected void init() { _ip = new BshInterpreter(); _roRoots = Collections.unmodifiableList(_roots); _attrs = new HashMap(); _fellows = new HashMap(); } /** Returns the UI engine. */ private final UiEngine getUiEngine() { return ((WebAppCtrl)_desktop.getWebApp()).getUiEngine(); } //-- Page --// private final Execution getExecution() { return _desktop != null ? _desktop.getExecution(): Executions.getCurrent(); } public final FunctionMapper getFunctionMapper() { return _funmap; } public void addFunctionMapper(FunctionMapper funmap) { if (funmap != null) if (_funmap != null) _funmap = new DualFuncMapper(funmap, _funmap); else _funmap = funmap; } public String getRequestPath() { return _path; } public final String getId() { return _id; } public void setId(String id) { if (_desktop != null) throw new UiException("Unable to change the identifier after the page is initialized"); if (id != null && id.length() > 0) _id = id; //No need to update client since it is allowed only before init(...) } public String getTitle() { return _title; } public void setTitle(String title) { if (title == null) title = ""; if (!_title.equals(title)) { _title = title; if (_desktop != null) { final Execution exec = getExecution(); if (_title.length() > 0) { _title = (String)exec.evaluate(this, _title, String.class); if (_title == null) _title = ""; } if (exec.isAsyncUpdate(this)) getUiEngine().addResponse("setTitle", new AuSetTitle(_title)); } } } public String getStyle() { return _style; } public void setStyle(String style) { if (style == null) style = ""; if (!_style.equals(style)) { _style = style; if (_desktop != null) { final Execution exec = getExecution(); if (_style.length() > 0) { _style = (String)exec.evaluate(this, _style, String.class); if (_style == null) _style = ""; } //FUTURE: might support the change of style dynamically } } } public Collection getRoots() { return _roRoots; } public Map getAttributes(int scope) { switch (scope) { case DESKTOP_SCOPE: return _desktop != null ? _desktop.getAttributes(): Collections.EMPTY_MAP; case SESSION_SCOPE: return _desktop != null ? _desktop.getSession().getAttributes(): Collections.EMPTY_MAP; case APPLICATION_SCOPE: return _desktop != null ? _desktop.getWebApp().getAttributes(): Collections.EMPTY_MAP; case PAGE_SCOPE: return _attrs; case REQUEST_SCOPE: final Execution exec = getExecution(); if (exec != null) return exec.getAttributes(); //fall thru default: return Collections.EMPTY_MAP; } } public Object getAttribute(String name, int scope) { return getAttributes(scope).get(name); } public Object setAttribute(String name, Object value, int scope) { if (value != null) { final Map attrs = getAttributes(scope); if (attrs == Collections.EMPTY_MAP) throw new IllegalStateException("This component doesn't belong to any ID space: "+this); return attrs.put(name, value); } else { return removeAttribute(name, scope); } } public Object removeAttribute(String name, int scope) { final Map attrs = getAttributes(scope); if (attrs == Collections.EMPTY_MAP) throw new IllegalStateException("This component doesn't belong to any ID space: "+this); return attrs.remove(name); } public Map getAttributes() { return _attrs; } public Object getAttribute(String name) { return _attrs.get(name); } public Object setAttribute(String name, Object value) { return value != null ? _attrs.put(name, value): removeAttribute(name); } public Object removeAttribute(String name) { return _attrs.remove(name); } public void invalidate() { getUiEngine().addInvalidate(this); } public void removeComponents() { for (Iterator it = new ArrayList(getRoots()).iterator(); it.hasNext();) ((Component)it.next()).detach(); } public void setVariable(String name, Object val) { _ip.setVariable(name, val); } public Object getVariable(String name) { return _ip.getVariable(name); } public void unsetVariable(String name) { _ip.unsetVariable(name); } public boolean addVariableResolver(VariableResolver resolver) { return _ip.addVariableResolver(resolver); } public boolean removeVariableResolver(VariableResolver resolver) { return _ip.removeVariableResolver(resolver); } public Object resolveElVariable(String name) { try { final javax.servlet.jsp.el.VariableResolver resolv = getExecution().getVariableResolver(); return resolv != null ? resolv.resolveVariable(name): null; } catch (javax.servlet.jsp.el.ELException ex) { throw UiException.Aide.wrap(ex); } } public boolean addEventListener(String evtnm, EventListener listener) { if (evtnm == null || listener == null) throw new IllegalArgumentException("null"); if (!Events.isValid(evtnm)) throw new IllegalArgumentException("Invalid event name: "+evtnm); if (listener.isAsap()) log.warning("Ignored: ASAP is meaningless if an event listener added to a page: "+listener); if (_listeners == null) _listeners = new HashMap(3); List l = (List)_listeners.get(evtnm); if (l != null) { for (Iterator it = l.iterator(); it.hasNext();) { final EventListener li = (EventListener)it.next(); if (listener.equals(li))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -