KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > webapp > tiles > skin > LayoutSwitchAction


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

18
19 package org.apache.struts.webapp.tiles.skin;
20
21 import javax.servlet.RequestDispatcher JavaDoc;
22 import javax.servlet.ServletContext JavaDoc;
23 import javax.servlet.ServletException JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
26 import javax.servlet.http.HttpSession JavaDoc;
27 import org.apache.struts.action.ActionForm;
28 import org.apache.struts.action.ActionForward;
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.tiles.ComponentContext;
31 import org.apache.struts.tiles.ComponentDefinition;
32 import org.apache.struts.tiles.DefinitionsFactoryException;
33 import org.apache.struts.tiles.actions.TilesAction;
34
35   /**
36    * Customize layouts according to predefined "skin"
37    * A "skin" is a set of layouts used together to provide a consistent
38    * Look & Feel.
39    * This action is called when inserting a definition's layout. It replaces
40    * definition's layout by the one set in selected skin.
41    * the appropriate
42    * Available skins are stored in application context. They are initialized from
43    * a Tile definition.
44    * Currently selected skin is stored under user session, if any. Otherwise,
45    * the default skin is used.
46    * This action act for all layouts. A Tile's attribute (skinLayout) is used as a key to
47    * know which layouts is concerned.
48    */

49 public class LayoutSwitchAction extends TilesAction
50 {
51     /** debug flag */
52   public static boolean debug = true;
53     /** Tile's attribute containing layout key */
54   public static final String JavaDoc LAYOUT_ATTRIBUTE = "layout.attribute";
55     /** Tile attribute containing name used to store user settings in session context */
56   public static String JavaDoc USER_SETTINGS_NAME_ATTRIBUTE = "userSettingsName";
57     /** Default name used to store settings in session context */
58   public static String JavaDoc DEFAULT_USER_SETTINGS_NAME = "examples.tiles.skin.SELECTED_DEFINITION";
59
60     /** Name of catalog in application context */
61   public static final String JavaDoc CATALOG_NAME = "examples.tiles.skin.CATALOG_NAME";
62
63     /** Default name used to store menu catalog in application scope */
64   public static String JavaDoc DEFAULT_CATALOG_NAME = "tiles.examples.skin.layoutCatalog";
65     /** Tile attribute containing name used to store menu catalog in application scope */
66   public static String JavaDoc CATALOG_NAME_ATTRIBUTE = "catalogName";
67     /** Tile attribute containing name of the settings definition used to initialize catalog */
68   public static final String JavaDoc CATALOG_SETTING_ATTRIBUTE = "catalogSettings";
69
70     /**
71      * Process the specified HTTP request, and create the corresponding HTTP
72      * response (or forward to another web component that will create it).
73      * Return an <code>ActionForward</code> instance describing where and how
74      * control should be forwarded, or <code>null</code> if the response has
75      * already been completed.
76      * This method should be implemented by subclasses.
77      *
78      * @param context The current Tile context, containing Tile attributes.
79      * @param mapping The ActionMapping used to select this instance.
80      * @param form The optional ActionForm bean for this request (if any).
81      * @param request The HTTP request we are processing.
82      * @param response The HTTP response we are creating.
83      *
84      * @exception Exception if the application business logic throws
85      * an exception
86      * @since Struts 1.1
87      */

88     public ActionForward execute(
89         ComponentContext context,
90         ActionMapping mapping,
91         ActionForm form,
92         HttpServletRequest JavaDoc request,
93         HttpServletResponse JavaDoc response)
94         throws Exception JavaDoc
95     {
96     if(debug)
97       System.out.println( "EnterLayoutSwitchAction" );
98       // Get attribute value indicating which layout we want
99
String JavaDoc layoutKey = (String JavaDoc)context.getAttribute( LAYOUT_ATTRIBUTE );
100     if(layoutKey==null)
101       throw new ServletException JavaDoc( "Error - CustomSkinAction : attribute '"
102                                   + LAYOUT_ATTRIBUTE
103                                   + "' not found in Tile's attributes. Need it to select appropriate layout" );
104
105       // Get user current skin
106
ComponentDefinition definition = getCurrentDefinition( context, request, getServlet().getServletContext() );
107       // get requested layout from definition
108
String JavaDoc layout = (String JavaDoc)definition.getAttribute(layoutKey);
109     if(layout==null)
110       throw new ServletException JavaDoc( "Error - CustomSkinAction : no layout defined for key '"
111                                   + layoutKey
112                                   + "' in currently selected skin '"
113                                   + getUserSetting(context, request ) + "'." );
114       // set path to forward to
115
// Not very nice solution, need to improve it
116
/*
117     ComponentDefinition forwarDefinition = new ComponentDefinition( "", layout, new HashMap() );
118     DefinitionsUtil.setActionDefinition( request, forwarDefinition );
119     */

120     if(debug)
121       System.out.println( "Switch to : " + layout );
122     RequestDispatcher JavaDoc rd = getServlet().getServletContext().getRequestDispatcher( layout );
123     if(rd==null)
124       throw new ServletException JavaDoc( "LayoutSwitch error : Can't find layout '"
125                                   + layout + "'." );
126     rd.include(request, response);
127     if(debug)
128       System.out.println( "Exit LayoutSwitchAction" );
129     return null;
130     }
131
132     /**
133      * Retrieve key associated to user.
134      * This key denote a definition in catalog.
135      * Return user selected key, or "default" if none is set.
136      */

137   public static String JavaDoc getUserSetting( ComponentContext context, HttpServletRequest JavaDoc request )
138   {
139   HttpSession JavaDoc session = request.getSession( false );
140   if( session == null )
141     return null;
142
143     // Retrieve attribute name used to store settings.
144
String JavaDoc userSettingsName = (String JavaDoc)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE );
145   if( userSettingsName == null )
146     userSettingsName = DEFAULT_USER_SETTINGS_NAME;
147
148   return (String JavaDoc)session.getAttribute(userSettingsName);
149   }
150
151     /**
152      * Set user setting value.
153      * This key denote a definition in catalog.
154      * Return user selected key, or "default" if none is set.
155      */

156   public static void setUserSetting( ComponentContext context, HttpServletRequest JavaDoc request, String JavaDoc setting )
157   {
158   HttpSession JavaDoc session = request.getSession();
159
160     // Retrieve attribute name used to store settings.
161
String JavaDoc userSettingsName = (String JavaDoc)context.getAttribute( USER_SETTINGS_NAME_ATTRIBUTE );
162   if( userSettingsName == null )
163     userSettingsName = DEFAULT_USER_SETTINGS_NAME;
164
165   session.setAttribute(userSettingsName, setting);
166   }
167
168     /**
169      * Get currently selected skin definition.
170      */

171   public static ComponentDefinition getCurrentDefinition( ComponentContext context, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext )
172     throws ServletException JavaDoc
173   {
174     // Get selected key
175
String JavaDoc selected = getUserSetting(context, request);
176
177   DefinitionCatalog catalog = getCatalog( context, request, servletContext);
178   ComponentDefinition definition = (ComponentDefinition)catalog.get( selected );
179   if( definition == null )
180     definition = (ComponentDefinition)catalog.getDefault();
181
182   return definition;
183   }
184
185     /**
186      * Get catalog of available skins.
187      */

188   public static DefinitionCatalog getCatalog( ComponentContext context, HttpServletRequest JavaDoc request, ServletContext JavaDoc servletContext )
189     throws ServletException JavaDoc
190   {
191     // Retrieve name used to store catalog in application context.
192
// If not found, use default name
193
String JavaDoc catalogName = (String JavaDoc)context.getAttribute( CATALOG_NAME_ATTRIBUTE );
194   if(catalogName == null)
195     catalogName = DEFAULT_CATALOG_NAME;
196
197   if(debug)
198     System.out.println( "Catalog name=" + catalogName );
199   try
200     {
201     DefinitionCatalog catalog = (DefinitionCatalog)servletContext.getAttribute( catalogName );
202     if(catalog == null)
203       { // create catalog
204
if(debug)
205         System.out.println( "Create catalog" );
206       String JavaDoc catalogSettings = (String JavaDoc)context.getAttribute( CATALOG_SETTING_ATTRIBUTE );
207       if(catalogSettings == null)
208         throw new ServletException JavaDoc( "Error - CustomSkinAction : attribute '"
209                                   + CATALOG_SETTING_ATTRIBUTE
210                                   + "' not found in Tile's attributes. Need it to initialize catalog" );
211       catalog = new DefinitionCatalog( catalogSettings, request, servletContext );
212       if(debug)
213         System.out.println( "Catalog created" );
214       servletContext.setAttribute( catalogName, catalog );
215       } // end if
216
return catalog;
217     }
218    catch(DefinitionsFactoryException ex )
219     {
220     if(debug)
221         System.out.println( "Exception : " + ex.getMessage() );
222     throw new ServletException JavaDoc( ex.getMessage() );
223     }
224   }
225 }
226
Popular Tags