KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > theme > strategy > impl > DefaultStrategyImpl


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.portal.core.theme.strategy.impl;
9
10 import org.apache.log4j.Logger;
11 import org.jboss.portal.common.MediaType;
12 import org.jboss.portal.server.plugins.windowstate.WindowState;
13 import org.jboss.portal.server.theme.strategy.AbstractLayoutStrategy;
14 import org.jboss.portal.server.theme.strategy.LayoutStrategy;
15 import org.jboss.portal.server.theme.strategy.PortletContext;
16 import org.jboss.portal.server.theme.strategy.StrategyContext;
17 import org.jboss.portal.server.theme.strategy.StrategyException;
18 import org.jboss.portal.server.theme.strategy.StrategyResponse;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 /**
24  * This is am implementation of the LayoutStrategy to handle the maximized state of any portlet on a page.
25  * <p>This implementation allows only one maximized portlet at any given time. When in doubt , the currently targeted
26  * portlet will win (if a portet was moximized, then the back button was clicked, then another portlet was maximized, the
27  * strategy has to make sure that only one portlet is actually maximized. In this case, the last maximize will win, since
28  * the current request is targeted to this portlet.</p>
29  *
30  * @author <a HREF="mailto:mholzner@novell.com">Martin Holzner</a>.
31  * @version <tt>$Revision: 1.12 $</tt>
32  * @see LayoutStrategy
33  */

34 public class DefaultStrategyImpl extends AbstractLayoutStrategy
35 {
36    private static final Logger log = Logger.getLogger(DefaultStrategyImpl.class);
37
38    /**
39     * Create a new instance of this strategy implementation.
40     */

41    public DefaultStrategyImpl()
42    {
43       setMediaType(MediaType.HTML);
44    }
45
46    /**
47     * Evaluate the strategy and return a strategy response, containing the desired changes.
48     *
49     * @param context a context containing the portal's current information about portlets, states and the layout
50     * @return a strategy response with the desired changes
51     * @throws StrategyException if the strategy is missconfigured, or can't deal with the provided information
52     */

53    public StrategyResponse evaluate(StrategyContext context) throws StrategyException
54    {
55       if (context == null)
56       {
57          throw new NullPointerException JavaDoc("strategy context is null");
58       }
59
60       log.debug("evaluating strategy for: " + context.getLayout());
61
62       /*
63       // handle svg content differntly than xhtml
64       if (mediaType.equals(MediaType.SVG)){
65          //....
66       }else if (mediaType.equals(MediaType.XHTML)){
67          //....
68       }
69
70       // if you want to store information in the request or session (for example to use a Stack of maximized portlets):
71       HttpServletRequest request = context.getHttpServletRequest();
72       */

73
74       StrategyResponse response = context.createResponse();
75
76       ArrayList JavaDoc maximizedPortlets = new ArrayList JavaDoc();
77
78       // get the list of portlets and their window state, and check if one of the
79
// portlets is maximized
80
for (Iterator JavaDoc i = context.getPortletList().iterator(); i.hasNext();)
81       {
82          PortletContext portlet = (PortletContext)i.next();
83          log.debug("evaluating portlet: " + portlet.getPortletName());
84          if (WindowState.MAXIMIZED.equals(portlet.getWindowState()))
85          {
86             log.debug("found maximized portlet: " + portlet);
87             maximizedPortlets.add(portlet);
88          }
89          else
90          {
91             // exclude all, but the maximized portlet, from the render process
92
response.addNoRender(portlet);
93             /*
94             // exclude all but the maximized portlet, and those in the head region
95             if(!"head".equalsIgnoreCase(portlet.getRegionName())){
96                noRenderList.add(portlet);
97             }
98             */

99          }
100       }
101       // if there is at least one portlet with maximized window state, then
102
// switch the layout to the maximized layout, and
103
// flag all portlets that are not part of the header region (except the maximized one) as 'do-not-render'
104
// Note: it's better to flag them as do not render than to set their window states to minimized, since that
105
// could be interpreted differently on the layout / theme
106
// if one of the portlets is in maximized mode, then use the maximized layout
107
if (maximizedPortlets.size() > 0)
108       {
109          // there can only be one maximized portlet! when in doubt, the targeted portlet wins
110
if (context.getTargetPortlet() != null && maximizedPortlets.size() > 1)
111          {
112             if (maximizedPortlets.contains(context.getTargetPortlet()) &&
113                WindowState.MAXIMIZED.equals(context.getTargetPortlet().getWindowState()))
114             {
115                for (Iterator JavaDoc i = maximizedPortlets.iterator(); i.hasNext();)
116                {
117                   PortletContext portlet = (PortletContext)i.next();
118                   if (!portlet.equals(context.getTargetPortlet()))
119                   {
120                      log.debug("resetting maximized portlet to normal (there can only be one): " + portlet + " " +
121                         context.getTargetPortlet() + " will be maximized");
122                      // change the window state back to normal, and make sure the
123
// portlet is not rendered
124
response.addWindowStateChange(portlet, WindowState.NORMAL);
125                      response.addNoRender(portlet);
126                   }
127                }
128             }
129          }
130
131          // try to see if there is a layout uri specified for the maximized state
132
response.setState(WindowState.MAXIMIZED.toString());
133          maximizedPortlets.clear();
134       }
135       else
136       {
137          // if there is no maximized portlet, then clean up references
138
response.reset();
139       }
140
141       if (context.getLayout() != null)
142       {
143          response.setURI(context.getLayout().getURI());
144       }
145
146       return response;
147    }
148
149    /**
150     * @see java.lang.Object#toString()
151     */

152    public String JavaDoc toString()
153    {
154       return this.getClass().getName();
155    }
156 }
157
Popular Tags