001: /* AbstractExecution.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Jun 6 12:18:25 2005, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2005 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zk.ui.impl;
020:
021: import java.util.List;
022: import java.util.LinkedList;
023: import java.util.Map;
024: import java.util.Collections;
025: import java.io.Reader;
026: import java.io.IOException;
027:
028: import org.zkoss.idom.Document;
029: import org.zkoss.web.servlet.Servlets;
030:
031: import org.zkoss.zk.ui.Execution;
032: import org.zkoss.zk.ui.Desktop;
033: import org.zkoss.zk.ui.Page;
034: import org.zkoss.zk.ui.Component;
035: import org.zkoss.zk.ui.UiException;
036: import org.zkoss.zk.ui.event.Event;
037: import org.zkoss.zk.ui.event.Events;
038: import org.zkoss.zk.ui.metainfo.PageDefinition;
039: import org.zkoss.zk.ui.metainfo.ComponentDefinition;
040: import org.zkoss.zk.ui.sys.WebAppCtrl;
041: import org.zkoss.zk.ui.sys.ExecutionCtrl;
042: import org.zkoss.zk.ui.sys.DesktopCtrl;
043: import org.zkoss.zk.ui.sys.Visualizer;
044: import org.zkoss.zk.ui.sys.UiEngine;
045: import org.zkoss.zk.au.AuResponse;
046:
047: /**
048: * A skeletal implementation of {@link Execution}.
049: *
050: * @author tomyeh
051: */
052: abstract public class AbstractExecution implements Execution,
053: ExecutionCtrl {
054: private Desktop _desktop;
055: private Page _curpage;
056: private PageDefinition _curpgdef;
057: private Visualizer _ei;
058: private List _events;
059: /** A stack of args being pushed by {@link #pushArg}. */
060: private List _args;
061: //private Event _evtInProc;
062: /** Which page is being created, or null if all in update mode. */
063: private final Page _creating;
064: /** Whether onPiggyback is checked for this execution. */
065: private boolean _piggybacked;
066:
067: /** Constructs an execution.
068: * @param creating which page is being creating for this execution, or
069: * null if none is being created.
070: * {@link #isAsyncUpdate} returns based on this.
071: */
072: protected AbstractExecution(Desktop desktop, Page creating) {
073: _desktop = desktop; //it is null if it is created by WebManager.newDesktop
074: _creating = creating;
075: }
076:
077: //-- Execution --//
078: public final boolean isAsyncUpdate(Page page) {
079: return _creating == null || (page != null && _creating != page);
080: }
081:
082: public Desktop getDesktop() {
083: return _desktop;
084: }
085:
086: public void postEvent(Event evt) {
087: if (evt == null)
088: throw new IllegalArgumentException("null");
089:
090: evt = ((DesktopCtrl) _desktop).beforePostEvent(evt);
091: if (evt == null)
092: return; //done
093:
094: if (_events == null)
095: _events = new LinkedList();
096: _events.add(evt);
097: //_piggybacked = false; //allow piggyback to be called again
098: }
099:
100: //-- ExecutionCtrl --//
101: public final Page getCurrentPage() {
102: return _curpage;
103: }
104:
105: public final void setCurrentPage(Page curpage) {
106: if (_curpage != null && curpage != null && _curpage != curpage
107: && _curpage.getDesktop() != curpage.getDesktop())
108: throw new IllegalStateException(
109: "Change current page to another desktop? "
110: + curpage);
111: _curpage = curpage;
112: }
113:
114: public PageDefinition getCurrentPageDefinition() {
115: return _curpgdef;
116: }
117:
118: public void setCurrentPageDefinition(PageDefinition pgdef) {
119: _curpgdef = pgdef;
120: }
121:
122: public Event getNextEvent() {
123: if (_events != null && !_events.isEmpty())
124: return (Event) _events.remove(0);
125:
126: if (!_piggybacked) { //handle piggyback only once
127: ((DesktopCtrl) _desktop).onPiggyback();
128: _piggybacked = true;
129: }
130:
131: return _events != null && !_events.isEmpty() ? (Event) _events
132: .remove(0) : null;
133: }
134:
135: public boolean isActivated() {
136: return _ei != null;
137: }
138:
139: public void onActivate() {
140: }
141:
142: public void onDeactivate() {
143: }
144:
145: public boolean isRecovering() {
146: return _ei != null && _ei.isRecovering();
147: }
148:
149: public void setVisualizer(Visualizer ei) {
150: _ei = ei;
151: }
152:
153: public Visualizer getVisualizer() {
154: return _ei;
155: }
156:
157: public String toAbsoluteURI(String uri, boolean skipInclude) {
158: if (uri != null && uri.length() > 0) {
159: final char cc = uri.charAt(0);
160: if (cc != '/' && cc != '~'
161: && !(skipInclude && isIncluded())
162: && !Servlets.isUniversalURL(uri)) {
163: final String dir = getDesktop().getCurrentDirectory();
164: if (dir != null)
165: return dir + uri;
166: }
167: }
168: return uri;
169: //we ignore _creating, because Servlet's include cannot handle
170: //related URI correctly (even though it is by the layout servlet)
171: }
172:
173: private final UiEngine getUiEngine() {
174: return ((WebAppCtrl) _desktop.getWebApp()).getUiEngine();
175: }
176:
177: public Component createComponents(String uri, Component parent,
178: Map arg) {
179: final Component[] cs = getUiEngine().createComponents(this ,
180: getPageDefinition(uri), _curpage, parent, arg);
181: return cs.length > 0 ? cs[0] : null;
182: }
183:
184: public Component createComponents(PageDefinition pagedef,
185: Component parent, Map arg) {
186: if (pagedef == null)
187: throw new IllegalArgumentException("pagedef cannot be null");
188: final Component[] cs = getUiEngine().createComponents(this ,
189: pagedef, _curpage, parent, arg);
190: return cs.length > 0 ? cs[0] : null;
191: }
192:
193: public Component createComponentsDirectly(String content,
194: String ext, Component parent, Map arg) {
195: final Component[] cs = getUiEngine().createComponents(this ,
196: getPageDefinitionDirectly(content, ext), _curpage,
197: parent, arg);
198: return cs.length > 0 ? cs[0] : null;
199: }
200:
201: public Component createComponentsDirectly(Document content,
202: String ext, Component parent, Map arg) {
203: final Component[] cs = getUiEngine().createComponents(this ,
204: getPageDefinitionDirectly(content, ext), _curpage,
205: parent, arg);
206: return cs.length > 0 ? cs[0] : null;
207: }
208:
209: public Component createComponentsDirectly(Reader reader,
210: String ext, Component parent, Map arg) throws IOException {
211: final Component[] cs = getUiEngine().createComponents(this ,
212: getPageDefinitionDirectly(reader, ext), _curpage,
213: parent, arg);
214: return cs.length > 0 ? cs[0] : null;
215: }
216:
217: public Component[] createComponents(String uri, Map arg) {
218: return getUiEngine().createComponents(this ,
219: getPageDefinition(uri), null, null, arg);
220: }
221:
222: public Component[] createComponents(PageDefinition pagedef, Map arg) {
223: if (pagedef == null)
224: throw new IllegalArgumentException("pagedef cannot be null");
225: return getUiEngine().createComponents(this , pagedef, null,
226: null, arg);
227: }
228:
229: public Component[] createComponentsDirectly(String content,
230: String ext, Map arg) {
231: return getUiEngine().createComponents(this ,
232: getPageDefinitionDirectly(content, ext), null, null,
233: arg);
234: }
235:
236: public Component[] createComponentsDirectly(Document content,
237: String ext, Map arg) {
238: return getUiEngine().createComponents(this ,
239: getPageDefinitionDirectly(content, ext), null, null,
240: arg);
241: }
242:
243: public Component[] createComponentsDirectly(Reader reader,
244: String ext, Map arg) throws IOException {
245: return getUiEngine()
246: .createComponents(this ,
247: getPageDefinitionDirectly(reader, ext), null,
248: null, arg);
249: }
250:
251: public void sendRedirect(String uri) {
252: getUiEngine().sendRedirect(uri, null);
253: }
254:
255: public void sendRedirect(String uri, String target) {
256: getUiEngine().sendRedirect(uri, target);
257: }
258:
259: public Map getArg() {
260: return _args != null ? (Map) _args.get(0)
261: : Collections.EMPTY_MAP;
262: }
263:
264: public void pushArg(Map arg) {
265: if (_args == null)
266: _args = new LinkedList();
267: _args.add(0, arg);
268: }
269:
270: public void popArg() {
271: if (_args != null) {
272: if (_args.size() == 1)
273: _args = null;
274: else
275: _args.remove(0);
276: }
277: }
278:
279: public void addAuResponse(String key, AuResponse response) {
280: getUiEngine().addResponse(key, response);
281: }
282:
283: public void setDesktop(Desktop desktop) {
284: if (desktop == null)
285: throw new IllegalArgumentException("null");
286: if (_desktop != null)
287: throw new IllegalStateException();
288: _desktop = desktop;
289: }
290:
291: //Object//
292: public String toString() {
293: return "[Exec" + System.identityHashCode(this ) + ": "
294: + _desktop + ']';
295: }
296: }
|