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: package org.apache.cocoon.portal.layout.renderer.aspect.impl;
018:
019: import javax.portlet.PortletMode;
020: import javax.portlet.WindowState;
021: import javax.servlet.ServletConfig;
022: import javax.servlet.http.HttpServletRequest;
023:
024: import org.apache.avalon.framework.context.Context;
025: import org.apache.avalon.framework.context.ContextException;
026: import org.apache.avalon.framework.context.Contextualizable;
027: import org.apache.avalon.framework.parameters.ParameterException;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.cocoon.portal.PortalService;
030: import org.apache.cocoon.portal.coplet.CopletInstanceData;
031: import org.apache.cocoon.portal.coplet.adapter.impl.PortletAdapter;
032: import org.apache.cocoon.portal.layout.Layout;
033: import org.apache.cocoon.portal.layout.impl.CopletLayout;
034: import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
035: import org.apache.cocoon.portal.pluto.PortletURLProviderImpl;
036: import org.apache.cocoon.portal.pluto.om.PortletWindowImpl;
037: import org.apache.cocoon.servlet.CocoonServlet;
038: import org.apache.cocoon.xml.XMLUtils;
039: import org.apache.pluto.om.portlet.PortletDefinition;
040: import org.apache.pluto.om.window.PortletWindow;
041: import org.apache.pluto.services.PortletContainerEnvironment;
042: import org.apache.pluto.services.information.DynamicInformationProvider;
043: import org.apache.pluto.services.information.InformationProviderService;
044: import org.xml.sax.ContentHandler;
045: import org.xml.sax.SAXException;
046:
047: /**
048: * This aspect draws a portlet window for a JSR-168 implementation.
049: *
050: * <h2>Example XML:</h2>
051: * <pre>
052: * <window>
053: * <title>title</title>
054: * <maximize-uri>event</maximize-uri>
055: * <minimize-uri>event</minimize-uri>
056: * <fullscreen-uri>event</fullscreen-uri>
057: * <edit-uri>event</edit-uri>
058: * <help-uri>event</help-uri>
059: * <view-uri>event</view-uri>
060: * <!-- output of following renderers -->
061: * </window>
062: * </pre>
063: *
064: * <h2>Applicable to:</h2>
065: * <ul>
066: * <li>{@link org.apache.cocoon.portal.layout.impl.CopletLayout}</li>
067: * </ul>
068: *
069: * <h2>Parameters</h2>
070: * <table><tbody>
071: * <tr><th>root-tag</th><td>Should a root tag surrounding the following output
072: * be generated?</td><td></td><td>boolean</td><td><code>true</code></td></tr>
073: * <tr><th>tag-name</th><td>Name of the root tag if requested.
074: * </td><td></td><td>String</td><td><code>"window"</code></td></tr>
075: * </tbody></table>
076: *
077: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
078: *
079: * @version CVS $Id: PortletWindowAspect.java 433543 2006-08-22 06:22:54Z crossley $
080: */
081: public final class PortletWindowAspect extends AbstractAspect implements
082: Contextualizable {
083:
084: /** The environment */
085: protected PortletContainerEnvironment environment;
086:
087: /* (non-Javadoc)
088: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
089: */
090: public void contextualize(Context context) throws ContextException {
091: try {
092: // now get the portal manager
093: ServletConfig servletConfig = (ServletConfig) context
094: .get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
095: PortletAdapter portalManager = (PortletAdapter) servletConfig
096: .getServletContext().getAttribute(
097: PortletAdapter.class.getName());
098: if (portalManager != null) {
099: this .environment = portalManager
100: .getPortletContainerEnvironment();
101: }
102: } catch (ContextException ignore) {
103: // we ignore the context exception
104: // this avoids startup errors if the portal is configured for the CLI
105: // environment
106: this
107: .getLogger()
108: .warn(
109: "The JSR-168 support is disabled as the servlet context is not available.",
110: ignore);
111: }
112: }
113:
114: /* (non-Javadoc)
115: * @see org.apache.cocoon.portal.layout.renderer.RendererAspect#toSAX(org.apache.cocoon.portal.layout.renderer.RendererAspectContext, org.apache.cocoon.portal.layout.Layout, org.apache.cocoon.portal.PortalService, org.xml.sax.ContentHandler)
116: */
117: public void toSAX(RendererAspectContext context, Layout layout,
118: PortalService service, ContentHandler contenthandler)
119: throws SAXException {
120: final PreparedConfiguration config = (PreparedConfiguration) context
121: .getAspectConfiguration();
122: final CopletInstanceData copletInstanceData = ((CopletLayout) layout)
123: .getCopletInstanceData();
124:
125: if (config.rootTag) {
126: XMLUtils.startElement(contenthandler, config.tagName);
127: }
128: final PortletWindow window = (PortletWindow) copletInstanceData
129: .getTemporaryAttribute("window");
130: if (window == null) {
131: // no portlet window, so use a default behaviour
132: XMLUtils.createElement(contenthandler, "title",
133: copletInstanceData.getTitle());
134: } else {
135: if (((PortletWindowImpl) window).getLayout() == null) {
136: ((PortletWindowImpl) window)
137: .setLayout((CopletLayout) layout);
138: }
139:
140: String title = (String) copletInstanceData
141: .getTemporaryAttribute("dynamic-title");
142: if (title == null) {
143: final PortletDefinition def = window.getPortletEntity()
144: .getPortletDefinition();
145: try {
146: title = def.getDisplayName(
147: def.getLanguageSet().getDefaultLocale())
148: .getDisplayName();
149: } catch (Exception ignore) {
150: title = copletInstanceData.getTitle();
151: }
152: }
153: XMLUtils.createElement(contenthandler, "title", title);
154:
155: if (this .environment != null) {
156: InformationProviderService ips = (InformationProviderService) this .environment
157: .getContainerService(InformationProviderService.class);
158: DynamicInformationProvider dip = ips
159: .getDynamicProvider((HttpServletRequest) context
160: .getObjectModel()
161: .get("portlet-request"));
162:
163: // Sizing
164: final String wsString = (String) copletInstanceData
165: .getTemporaryAttribute("window-state");
166: WindowState ws;
167: if (wsString != null) {
168: ws = new WindowState(wsString);
169: } else {
170: ws = WindowState.NORMAL;
171: }
172:
173: if (!ws.equals(WindowState.MINIMIZED)
174: && !ws.equals(WindowState.MAXIMIZED)) {
175: PortletURLProviderImpl url = (PortletURLProviderImpl) dip
176: .getPortletURLProvider(window);
177: url.clearParameters();
178: url.setWindowState(WindowState.MINIMIZED);
179:
180: XMLUtils.createElement(contenthandler,
181: "minimize-uri", url.toString());
182: }
183:
184: if (!ws.equals(WindowState.NORMAL)) {
185: PortletURLProviderImpl url = (PortletURLProviderImpl) dip
186: .getPortletURLProvider(window);
187: url.clearParameters();
188: url.setWindowState(WindowState.NORMAL);
189: XMLUtils.createElement(contenthandler,
190: "maximize-uri", url.toString());
191: }
192:
193: if (!ws.equals(WindowState.MAXIMIZED)) {
194: PortletURLProviderImpl url = (PortletURLProviderImpl) dip
195: .getPortletURLProvider(window);
196: url.clearParameters();
197: url.setWindowState(WindowState.MAXIMIZED);
198: XMLUtils.createElement(contenthandler,
199: "fullscreen-uri", url.toString());
200: }
201:
202: // portlet modes
203: final String pmString = (String) copletInstanceData
204: .getTemporaryAttribute("portlet-mode");
205: PortletMode pm;
206: if (pmString == null) {
207: pm = PortletMode.VIEW;
208: } else {
209: pm = new PortletMode(pmString);
210: }
211: if (!pm.equals(PortletMode.EDIT)) {
212: PortletURLProviderImpl url = (PortletURLProviderImpl) dip
213: .getPortletURLProvider(window);
214: url.clearParameters();
215: url.setPortletMode(PortletMode.EDIT);
216: XMLUtils.createElement(contenthandler, "edit-uri",
217: url.toString());
218: }
219: if (!pm.equals(PortletMode.HELP)) {
220: PortletURLProviderImpl url = (PortletURLProviderImpl) dip
221: .getPortletURLProvider(window);
222: url.clearParameters();
223: url.setPortletMode(PortletMode.HELP);
224: XMLUtils.createElement(contenthandler, "help-uri",
225: url.toString());
226: }
227: if (!pm.equals(PortletMode.VIEW)) {
228: PortletURLProviderImpl url = (PortletURLProviderImpl) dip
229: .getPortletURLProvider(window);
230: url.clearParameters();
231: url.setPortletMode(PortletMode.VIEW);
232: XMLUtils.createElement(contenthandler, "view-uri",
233: url.toString());
234: }
235: }
236: }
237:
238: context.invokeNext(layout, service, contenthandler);
239:
240: if (config.rootTag) {
241: XMLUtils.endElement(contenthandler, config.tagName);
242: }
243: }
244:
245: protected static class PreparedConfiguration {
246: public String tagName;
247: public boolean rootTag;
248:
249: public void takeValues(PreparedConfiguration from) {
250: this .tagName = from.tagName;
251: this .rootTag = from.rootTag;
252: }
253: }
254:
255: /* (non-Javadoc)
256: * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
257: */
258: public Object prepareConfiguration(Parameters configuration)
259: throws ParameterException {
260: PreparedConfiguration pc = new PreparedConfiguration();
261: pc.tagName = configuration.getParameter("tag-name", "window");
262: pc.rootTag = configuration.getParameterAsBoolean("root-tag",
263: true);
264: return pc;
265: }
266:
267: }
|