KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > layout > renderer > aspect > impl > PortletWindowAspect


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.layout.renderer.aspect.impl;
17
18 import javax.portlet.PortletMode;
19 import javax.portlet.WindowState;
20 import javax.servlet.ServletConfig JavaDoc;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.Contextualizable;
26 import org.apache.avalon.framework.parameters.ParameterException;
27 import org.apache.avalon.framework.parameters.Parameters;
28 import org.apache.cocoon.portal.PortalService;
29 import org.apache.cocoon.portal.coplet.CopletInstanceData;
30 import org.apache.cocoon.portal.coplet.adapter.impl.PortletAdapter;
31 import org.apache.cocoon.portal.layout.Layout;
32 import org.apache.cocoon.portal.layout.impl.CopletLayout;
33 import org.apache.cocoon.portal.layout.renderer.aspect.RendererAspectContext;
34 import org.apache.cocoon.portal.pluto.PortletURLProviderImpl;
35 import org.apache.cocoon.portal.pluto.om.PortletWindowImpl;
36 import org.apache.cocoon.servlet.CocoonServlet;
37 import org.apache.cocoon.xml.XMLUtils;
38 import org.apache.pluto.om.portlet.PortletDefinition;
39 import org.apache.pluto.om.window.PortletWindow;
40 import org.apache.pluto.services.PortletContainerEnvironment;
41 import org.apache.pluto.services.information.DynamicInformationProvider;
42 import org.apache.pluto.services.information.InformationProviderService;
43 import org.xml.sax.ContentHandler JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46 /**
47  * This aspect draws a portlet window for a JSR-168 implementation.
48  *
49  * <h2>Example XML:</h2>
50  * <pre>
51  * &lt;window&gt;
52  * &lt;title&gt;title&lt;/title&gt;
53  * &lt;maximize-uri&gt;event&lt;/maximize-uri&gt;
54  * &lt;minimize-uri&gt;event&lt;/minimize-uri&gt;
55  * &lt;fullscreen-uri&gt;event&lt;/fullscreen-uri&gt;
56  * &lt;edit-uri&gt;event&lt;/edit-uri&gt;
57  * &lt;help-uri&gt;event&lt;/help-uri&gt;
58  * &lt;view-uri&gt;event&lt;/view-uri&gt;
59  * &lt;!-- output of following renderers --&gt;
60  * &lt;/window&gt;
61  * </pre>
62  *
63  * <h2>Applicable to:</h2>
64  * <ul>
65  * <li>{@link org.apache.cocoon.portal.layout.impl.CopletLayout}</li>
66  * </ul>
67  *
68  * <h2>Parameters</h2>
69  * <table><tbody>
70  * <tr><th>root-tag</th><td>Should a root tag surrounding the following output
71  * be generated?</td><td></td><td>boolean</td><td><code>true</code></td></tr>
72  * <tr><th>tag-name</th><td>Name of the root tag if requested.
73  * </td><td></td><td>String</td><td><code>"window"</code></td></tr>
74  * </tbody></table>
75  *
76  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
77  *
78  * @version CVS $Id: PortletWindowAspect.java 331660 2005-11-08 01:22:30Z rgoers $
79  */

80 public final class PortletWindowAspect
81 extends AbstractAspect
82 implements Contextualizable {
83
84     /** The environment */
85     protected PortletContainerEnvironment environment;
86     
87     /* (non-Javadoc)
88      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
89      */

90     public void contextualize(Context context) throws ContextException {
91         try {
92             // now get the portal manager
93
ServletConfig JavaDoc servletConfig = (ServletConfig JavaDoc) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
94             PortletAdapter portalManager = (PortletAdapter) servletConfig.getServletContext().getAttribute(PortletAdapter.class.getName());
95             if ( portalManager != null ) {
96                 this.environment = portalManager.getPortletContainerEnvironment();
97             }
98         } catch (ContextException ignore) {
99             // we ignore the context exception
100
// this avoids startup errors if the portal is configured for the CLI
101
// environment
102
this.getLogger().warn("The JSR-168 support is disabled as the servlet context is not available.", ignore);
103         }
104     }
105
106     /* (non-Javadoc)
107      * @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)
108      */

109     public void toSAX(RendererAspectContext context,
110                       Layout layout,
111                       PortalService service,
112                       ContentHandler JavaDoc contenthandler)
113     throws SAXException JavaDoc {
114         final PreparedConfiguration config = (PreparedConfiguration)context.getAspectConfiguration();
115         final CopletInstanceData copletInstanceData = ((CopletLayout)layout).getCopletInstanceData();
116
117         if ( config.rootTag ) {
118             XMLUtils.startElement(contenthandler, config.tagName);
119         }
120         final PortletWindow window = (PortletWindow)copletInstanceData.getTemporaryAttribute("window");
121         if ( window == null ) {
122             // no portlet window, so use a default behaviour
123
XMLUtils.createElement(contenthandler, "title", copletInstanceData.getTitle());
124         } else {
125             if ( ((PortletWindowImpl)window).getLayout() == null ) {
126                 ((PortletWindowImpl)window).setLayout((CopletLayout)layout);
127             }
128             
129             String JavaDoc title = (String JavaDoc) copletInstanceData.getTemporaryAttribute("dynamic-title");
130             if ( title == null ) {
131                 final PortletDefinition def = window.getPortletEntity().getPortletDefinition();
132                 try {
133                     title = def.getDisplayName(def.getLanguageSet().getDefaultLocale()).getDisplayName();
134                 } catch (Exception JavaDoc ignore) {
135                     title = copletInstanceData.getTitle();
136                 }
137             }
138             XMLUtils.createElement(contenthandler, "title", title);
139         
140
141             if ( this.environment != null ) {
142                 InformationProviderService ips = (InformationProviderService) this.environment.getContainerService(InformationProviderService.class);
143                 DynamicInformationProvider dip = ips.getDynamicProvider((HttpServletRequest JavaDoc) context.getObjectModel().get("portlet-request"));
144                 
145                 // Sizing
146
WindowState ws = (WindowState)copletInstanceData.getTemporaryAttribute("window-state");
147                 if ( ws == null ) {
148                     ws = WindowState.NORMAL;
149                 }
150                 
151                 if ( !ws.equals(WindowState.MINIMIZED) && !ws.equals(WindowState.MAXIMIZED)) {
152                     PortletURLProviderImpl url = (PortletURLProviderImpl)dip.getPortletURLProvider(window);
153                     url.clearParameters();
154                     url.setWindowState(WindowState.MINIMIZED);
155                     
156                     XMLUtils.createElement(contenthandler, "minimize-uri", url.toString());
157                 }
158
159                 if ( !ws.equals(WindowState.NORMAL)) {
160                     PortletURLProviderImpl url = (PortletURLProviderImpl)dip.getPortletURLProvider(window);
161                     url.clearParameters();
162                     url.setWindowState(WindowState.NORMAL);
163                     XMLUtils.createElement(contenthandler, "maximize-uri", url.toString());
164                 }
165
166                 if ( !ws.equals(WindowState.MAXIMIZED)) {
167                     PortletURLProviderImpl url = (PortletURLProviderImpl)dip.getPortletURLProvider(window);
168                     url.clearParameters();
169                     url.setWindowState(WindowState.MAXIMIZED);
170                     XMLUtils.createElement(contenthandler, "fullscreen-uri", url.toString());
171                 }
172
173                 // portlet modes
174
PortletMode pm = (PortletMode)copletInstanceData.getTemporaryAttribute("portlet-mode");
175                 if ( pm == null ) {
176                     pm = PortletMode.VIEW;
177                 }
178                 if ( !pm.equals(PortletMode.EDIT) ) {
179                     PortletURLProviderImpl url = (PortletURLProviderImpl)dip.getPortletURLProvider(window);
180                     url.clearParameters();
181                     url.setPortletMode(PortletMode.EDIT);
182                     XMLUtils.createElement(contenthandler, "edit-uri", url.toString());
183                 }
184                 if ( !pm.equals(PortletMode.HELP) ) {
185                     PortletURLProviderImpl url = (PortletURLProviderImpl)dip.getPortletURLProvider(window);
186                     url.clearParameters();
187                     url.setPortletMode(PortletMode.HELP);
188                     XMLUtils.createElement(contenthandler, "help-uri", url.toString());
189                 }
190                 if ( !pm.equals(PortletMode.VIEW) ) {
191                     PortletURLProviderImpl url = (PortletURLProviderImpl)dip.getPortletURLProvider(window);
192                     url.clearParameters();
193                     url.setPortletMode(PortletMode.VIEW);
194                     XMLUtils.createElement(contenthandler, "view-uri", url.toString());
195                 }
196             }
197         }
198
199         context.invokeNext( layout, service, contenthandler );
200         
201         if ( config.rootTag ) {
202             XMLUtils.endElement(contenthandler, config.tagName);
203         }
204     }
205
206     protected static class PreparedConfiguration {
207         public String JavaDoc tagName;
208         public boolean rootTag;
209         
210         public void takeValues(PreparedConfiguration from) {
211             this.tagName = from.tagName;
212             this.rootTag = from.rootTag;
213         }
214     }
215     
216     /* (non-Javadoc)
217      * @see org.apache.cocoon.portal.layout.renderer.aspect.RendererAspect#prepareConfiguration(org.apache.avalon.framework.parameters.Parameters)
218      */

219     public Object JavaDoc prepareConfiguration(Parameters configuration)
220     throws ParameterException {
221         PreparedConfiguration pc = new PreparedConfiguration();
222         pc.tagName = configuration.getParameter("tag-name", "window");
223         pc.rootTag = configuration.getParameterAsBoolean("root-tag", true);
224         return pc;
225     }
226
227 }
228
Popular Tags