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.PortalTheme;
027: import org.jboss.portal.theme.ThemeElement;
028: import org.jboss.portal.theme.impl.render.dynamic.DynaConstants;
029: import org.jboss.portal.theme.render.RendererContext;
030: import org.jboss.portal.theme.render.ThemeContext;
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.util.Iterator;
039:
040: /**
041: * Tag handler for the theme tag. <p>The theme tag injects theme resource links into the page. Themes are CSS files, and
042: * their resources (images). The theme tag can inject only the resources of one theme at a time. The href and src
043: * attributes of the link and script tags that are being injected are automatically adjusted to the war context they
044: * reside in. A theme is defined in the /WEB-INF/portal-themes.xml</p>
045: *
046: * @author <a href="mailto:mholzner@novell.com>Martin Holzner</a>
047: * @version $LastChangedRevision: 8784 $, $LastChangedDate: 2007-10-27 19:01:46 -0400 (Sat, 27 Oct 2007) $
048: * @see org.jboss.portal.theme.PortalTheme
049: * @see org.jboss.portal.theme.ThemeLink
050: * @see org.jboss.portal.theme.ThemeScript
051: */
052: public class ThemeTagHandler extends SimpleTagSupport {
053:
054: /** . */
055: private static Logger log = Logger.getLogger(ThemeTagHandler.class);
056:
057: /** . */
058: private String themeName;
059:
060: /**
061: * Render the markup for theme injection into the HEAD tag. <p>This tag expects that the theme name is provided via a
062: * http request attribute, keyed by <code>LayoutConstants.ATTR_PAGE</code>. The request attribute is expected to
063: * contain a PageResult, which in turn exposes an accessor method for the active theme's name. The theme server
064: * (registry of all available themes) also has to be available via a request attribute
065: * (ThemeConstants.ATTR_THEMESERVER).</p>
066: *
067: * @throws JspException
068: * @throws IOException
069: * @see org.jboss.portal.theme.PortalTheme
070: */
071: public void doTag() throws JspException, IOException {
072: JspWriter out = this .getJspContext().getOut();
073:
074: // get page and region
075: PageContext app = (PageContext) getJspContext();
076: HttpServletRequest request = (HttpServletRequest) app
077: .getRequest();
078:
079: // Get the theme provided as a render context attribute
080: RendererContext rendererContext = (RendererContext) request
081: .getAttribute(LayoutConstants.ATTR_RENDERCONTEXT);
082: ThemeContext themeContext = rendererContext.getThemeContext();
083:
084: PortalTheme theme = themeContext.getTheme();
085:
086: // Hard code here for now
087: // todo remove that hack
088: String jsBase = rendererContext
089: .getProperty(DynaConstants.RESOURCE_BASE_URL);
090: if (jsBase != null) {
091: out
092: .print("<link rel=\"stylesheet\" id=\"dyna_css\" href=\""
093: + jsBase
094: + "/style.css\" type=\"text/css\"/>\n");
095: }
096:
097: // If no theme provided we use what may be on the tag
098: if (theme == null && themeName != null
099: && themeName.length() > 0) {
100: theme = themeContext.getTheme(getThemeName());
101: }
102:
103: //
104: if (theme != null) {
105: for (Iterator i = theme.getElements().iterator(); i
106: .hasNext();) {
107: ThemeElement el = (ThemeElement) i.next();
108: out.println(el.getElement());
109: }
110: }
111: }
112:
113: // ----- attribute handlers
114:
115: public String getThemeName() {
116: return themeName;
117: }
118:
119: public void setThemeName(String name) {
120: themeName = name;
121: }
122: }
|