001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.theme.tag;
023:
024: import org.jboss.logging.Logger;
025: import org.jboss.portal.theme.LayoutConstants;
026: import org.jboss.portal.theme.Orientation;
027: import org.jboss.portal.theme.impl.JSPRendererContext;
028: import org.jboss.portal.theme.render.RenderException;
029: import org.jboss.portal.theme.render.renderer.PageRendererContext;
030: import org.jboss.portal.theme.render.renderer.RegionRendererContext;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.jsp.JspException;
034: import javax.servlet.jsp.JspWriter;
035: import javax.servlet.jsp.PageContext;
036: import javax.servlet.jsp.tagext.SimpleTagSupport;
037: import java.io.IOException;
038: import java.io.PrintWriter;
039: import java.util.Collection;
040: import java.util.Collections;
041: import java.util.Map;
042:
043: /**
044: * Tag handler for the region tag. <p>A region represents a subsection of a portal page. A region can host several
045: * portlets. The portlets can be arranged horizontally or vertically. The region tag utilizes a render set to create the
046: * markup around the individual portlets.</p>
047: *
048: * @author <a href="mailto:mholzner@novell.com>Martin Holzner</a>
049: * @author <a href="mailto:roy@jboss.org>Roy Russo</a>
050: * @version $LastChangedRevision: 8784 $, $LastChangedDate: 2007-10-27 19:01:46 -0400 (Sat, 27 Oct 2007) $
051: * @see org.jboss.portal.theme.PortalRenderSet
052: */
053: public class RegionTagHandler extends SimpleTagSupport {
054: private static Logger log = Logger
055: .getLogger(RegionTagHandler.class);
056: //default to vertical
057:
058: private Orientation regionOrientation;
059: private String regionName = null;
060: private String regionCssId = null;
061:
062: /**
063: * create the markup of this tag
064: *
065: * @throws JspException
066: * @throws IOException
067: */
068: public void doTag() throws JspException, IOException {
069: if (regionCssId == null) {
070: regionCssId = regionName;
071: }
072: log.debug("rendering " + regionName + " [" + regionOrientation
073: + "] cssId[" + regionCssId + "]");
074:
075: // get page and region
076: PageContext app = (PageContext) getJspContext();
077: HttpServletRequest request = (HttpServletRequest) app
078: .getRequest();
079:
080: PageRendererContext page = (PageRendererContext) request
081: .getAttribute(LayoutConstants.ATTR_PAGE);
082: JspWriter out = this .getJspContext().getOut();
083: if (page == null) {
084: out.write("<p bgcolor='red'>No page to render!</p>");
085: out
086: .write("<p bgcolor='red'>The page to render (PageResult) must be set in the request attribute '"
087: + LayoutConstants.ATTR_PAGE + "'</p>");
088: out.flush();
089: return;
090: }
091:
092: final String cssId = regionCssId == null ? regionName
093: : regionCssId;
094: final Orientation orientation = this .regionOrientation == null ? Orientation.DEFAULT
095: : this .regionOrientation;
096:
097: if (page.getRegion(regionName) == null) // non-window display of content
098: {
099: // Create a dummy region obj, based on region name
100: RegionRendererContext rrc = new RegionRendererContext() {
101: public String getId() {
102: return regionName;
103: }
104:
105: public Collection getWindows() {
106: return Collections.EMPTY_LIST;
107: }
108:
109: public PageRendererContext getPage() {
110: return null;
111: }
112:
113: public Orientation getOrientation() {
114: return orientation;
115: }
116:
117: public String getCSSId() {
118: return cssId;
119: }
120:
121: public String getProperty(String name) {
122: return null;
123: }
124:
125: public Map getProperties() {
126: return Collections.EMPTY_MAP;
127: }
128: };
129:
130: JSPRendererContext renderContext = (JSPRendererContext) request
131: .getAttribute(LayoutConstants.ATTR_RENDERCONTEXT);
132: try {
133: PrintWriter pw = new PrintWriter(out);
134: renderContext.setWriter(pw);
135: renderContext.render(rrc);
136: pw.flush();
137: } catch (RenderException e) {
138: throw new JspException(e);
139: }
140: } else {
141: // window-centric display of content
142: JSPRendererContext renderContext = (JSPRendererContext) request
143: .getAttribute(LayoutConstants.ATTR_RENDERCONTEXT);
144: if (renderContext == null) {
145: log.debug("no render context available in request");
146: return;
147: }
148:
149: final RegionRendererContext region = page
150: .getRegion(regionName);
151: RegionRendererContext rrc = new RegionRendererContext() {
152: public String getId() {
153: return region.getId();
154: }
155:
156: public Collection getWindows() {
157: return region.getWindows();
158: }
159:
160: public Orientation getOrientation() {
161: return orientation;
162: }
163:
164: public String getCSSId() {
165: return cssId;
166: }
167:
168: public String getProperty(String name) {
169: return region.getProperty(name);
170: }
171:
172: public Map getProperties() {
173: return region.getProperties();
174: }
175: };
176: try {
177: PrintWriter pw = new PrintWriter(out);
178: renderContext.setWriter(pw);
179: renderContext.render(rrc);
180: pw.flush();
181: } catch (RenderException e) {
182: throw new JspException(e);
183: }
184:
185: log
186: .debug("done rendering page region [" + regionName
187: + "]");
188: }
189: }
190:
191: // ------ attribute handlers
192:
193: /**
194: * Attribute handler for the orientation attribute of this tag
195: *
196: * @param orientation the orientation attribute value set in the hosting jsp
197: */
198: public void setOrientation(String orientation) {
199: if (orientation == null) {
200: log.error("no null value allowed");
201: }
202:
203: try {
204: this .regionOrientation = Orientation.parse(orientation);
205: } catch (IllegalArgumentException e) {
206: log.error(e);
207: this .regionOrientation = Orientation.DEFAULT;
208: }
209: }
210:
211: /**
212: * Attribute handler for the region name attribute of this tag
213: *
214: * @param regionName the name of the region this tag should render the markup for
215: */
216: public void setRegionName(String regionName) {
217: this .regionName = regionName;
218: }
219:
220: /**
221: * Attribute handler for the region id attribute of this tag <p>The region Id can be used as the value of the id
222: * attribute of the region container tag. In case of the DivRenderer render set, this is the id attribute of the div
223: * tag that represents this region on the page.</p>
224: *
225: * @param regionID the id attribute value to set for this tag
226: */
227: public void setRegionID(String regionID) {
228: this.regionCssId = regionID;
229: }
230: }
|