001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.script.common;
020:
021: import java.util.Stack;
022: import javax.servlet.jsp.JspContext;
023:
024: /**
025: * This class is used by the framework to store a stack of {@link IDataAccessProvider} objevcts. This
026: * is used when nesting user interface elements that all expose some {@link IDataAccessProvider} in
027: * order to keep track of a parent's current item in a data set.
028: */
029: public class DataAccessProviderStack {
030:
031: private static final String KEY = DataAccessProviderStack.class
032: .getName();
033:
034: private Stack _stack = null;
035:
036: public static void addDataAccessProvider(
037: IDataAccessProvider provider, JspContext jspContext) {
038: assert jspContext != null;
039:
040: DataAccessProviderBean bean = new DataAccessProviderBean(
041: provider);
042:
043: Object val = jspContext.getAttribute(KEY);
044: DataAccessProviderStack curStack = null;
045: if (val == null) {
046: curStack = new DataAccessProviderStack();
047:
048: jspContext.setAttribute(KEY, curStack);
049: } else
050: curStack = (DataAccessProviderStack) val;
051:
052: curStack.push(bean);
053:
054: jspContext.setAttribute("container", bean);
055: }
056:
057: public static DataAccessProviderBean removeDataAccessProvider(
058: JspContext jspContext) {
059: assert jspContext != null;
060:
061: Object val = jspContext.getAttribute(KEY);
062: if (val != null) {
063: DataAccessProviderStack curStack = (DataAccessProviderStack) val;
064: DataAccessProviderBean lastTop = curStack.pop();
065:
066: if (!curStack.isEmpty())
067: jspContext.setAttribute("container", curStack.peek());
068: else
069: jspContext.removeAttribute("container");
070:
071: return lastTop;
072: }
073:
074: // todo: should this throw an IllegalStateException?
075:
076: return null;
077: }
078:
079: public DataAccessProviderStack() {
080: _stack = new Stack();
081: }
082:
083: public boolean isEmpty() {
084: assert _stack != null;
085: return _stack.empty();
086: }
087:
088: public DataAccessProviderBean peek() {
089: assert _stack != null;
090: return (DataAccessProviderBean) _stack.peek();
091: }
092:
093: public DataAccessProviderBean pop() {
094: assert _stack != null;
095: return (DataAccessProviderBean) _stack.pop();
096: }
097:
098: public void push(DataAccessProviderBean bean) {
099: assert _stack != null;
100: _stack.push(bean);
101: }
102:
103: }
|