001: /*
002: * @author <a href="mailto:novotny@gridsphere.org">Jason Novotny</a>
003: * @version $Id: BaseBeanTag.java 6385 2007-10-25 14:02:26Z wehrens $
004: */
005: package org.gridsphere.provider.portletui.tags;
006:
007: import org.gridsphere.portlet.impl.SportletProperties;
008: import org.gridsphere.provider.portletui.beans.TagBean;
009:
010: import javax.servlet.jsp.PageContext;
011: import javax.servlet.jsp.tagext.BodyTagSupport;
012: import java.util.Enumeration;
013:
014: /**
015: * The abstract <code>BaseBeanTag</code> is a base class used by all UI tags that provides support for
016: * a bean identifier and a flag for indicating whether JavaScript is enabled.
017: */
018: public abstract class BaseBeanTag extends BodyTagSupport {
019:
020: protected String beanId = "";
021:
022: /**
023: * Returns the bean identifier
024: *
025: * @return the bean identifier
026: */
027: public String getBeanId() {
028: return beanId;
029: }
030:
031: /**
032: * Sets the bean identifier
033: *
034: * @param beanId the bean identifier
035: */
036: public void setBeanId(String beanId) {
037: this .beanId = beanId;
038: }
039:
040: public TagBean getTagBean() {
041: //System.err.println("looking for bean: " + getBeanKey());
042: return (TagBean) pageContext.findAttribute(getBeanKey());
043: }
044:
045: /**
046: * Returns the unique bean key
047: *
048: * @return the unique bean key
049: */
050: protected String getBeanKey() {
051: String cid = (String) pageContext
052: .findAttribute(SportletProperties.COMPONENT_ID);
053: String compId = (String) pageContext
054: .findAttribute(SportletProperties.GP_COMPONENT_ID);
055: String beanKey;
056: if (compId == null) {
057: beanKey = beanId + "_" + cid;
058: } else {
059: beanKey = compId + "%" + beanId + "_" + cid;
060: }
061: //System.err.println("in BaseBeanTag: getBeanKey(" + beanId + ") = " + beanKey);
062: return beanKey;
063: }
064:
065: /**
066: * Prints out all request attributes. Used for debugging
067: */
068: public void debug() {
069: Enumeration e = pageContext
070: .getAttributeNamesInScope(PageContext.REQUEST_SCOPE);
071: System.err.println("Printing attribues in request scope");
072: while (e.hasMoreElements()) {
073: System.err.println((String) e.nextElement());
074: }
075: e = pageContext
076: .getAttributeNamesInScope(PageContext.SESSION_SCOPE);
077: System.err.println("Printing attribues in session scope");
078: while (e.hasMoreElements()) {
079: System.err.println((String) e.nextElement());
080: }
081: e = pageContext
082: .getAttributeNamesInScope(PageContext.PAGE_SCOPE);
083: System.err.println("Printing attribues in page scope");
084: while (e.hasMoreElements()) {
085: System.err.println((String) e.nextElement());
086: }
087: e = pageContext
088: .getAttributeNamesInScope(PageContext.APPLICATION_SCOPE);
089: System.err.println("Printing attribues in application scope");
090: while (e.hasMoreElements()) {
091: System.err.println((String) e.nextElement());
092: }
093: }
094:
095: protected String getUniqueId(String varName) {
096: String uniqueId = (String) pageContext.getAttribute(varName,
097: PageContext.REQUEST_SCOPE);
098:
099: // use a counter to continually increase form number to provide unique form name
100: int ctr;
101: if (uniqueId == null) {
102: ctr = 1;
103: } else {
104: ctr = Integer.parseInt(uniqueId) + 1;
105: }
106: uniqueId = String.valueOf(ctr);
107: pageContext.setAttribute(varName, uniqueId,
108: PageContext.REQUEST_SCOPE);
109: return uniqueId;
110: }
111:
112: }
|