View Javadoc
1 package org.apache.turbine.services.pull.util; 2 3 /* ==================================================================== 4 * The Apache Software License, Version 1.1 5 * 6 * Copyright (c) 2001 The Apache Software Foundation. All rights 7 * reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in 18 * the documentation and/or other materials provided with the 19 * distribution. 20 * 21 * 3. The end-user documentation included with the redistribution, 22 * if any, must include the following acknowledgment: 23 * "This product includes software developed by the 24 * Apache Software Foundation (http://www.apache.org/)." 25 * Alternately, this acknowledgment may appear in the software itself, 26 * if and wherever such third-party acknowledgments normally appear. 27 * 28 * 4. The names "Apache" and "Apache Software Foundation" and 29 * "Apache Turbine" must not be used to endorse or promote products 30 * derived from this software without prior written permission. For 31 * written permission, please contact apache@apache.org. 32 * 33 * 5. Products derived from this software may not be called "Apache", 34 * "Apache Turbine", nor may "Apache" appear in their name, without 35 * prior written permission of the Apache Software Foundation. 36 * 37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 48 * SUCH DAMAGE. 49 * ==================================================================== 50 * 51 * This software consists of voluntary contributions made by many 52 * individuals on behalf of the Apache Software Foundation. For more 53 * information on the Apache Software Foundation, please see 54 * <http://www.apache.org/>;. 55 */ 56 57 import java.io.FileInputStream; 58 import java.util.Properties; 59 import org.apache.turbine.om.security.User; 60 import org.apache.turbine.services.pull.ApplicationTool; 61 import org.apache.turbine.services.pull.TurbinePull; 62 import org.apache.turbine.services.resources.TurbineResources; 63 import org.apache.turbine.services.servlet.TurbineServlet; 64 import org.apache.turbine.util.ContentURI; 65 import org.apache.turbine.util.Log; 66 import org.apache.turbine.util.RunData; 67 68 69 /*** 70 * UIManager.java 71 * <br> 72 * Manages all UI elements for a Turbine Application. Any 73 * UI element can be accessed in any template using the 74 * $ui handle (assuming you use the default PullService 75 * configuration). So, for example, you could access 76 * the background colour for your pages by using 77 * $ui.bgcolor 78 * <p> 79 * <h3>Questions:</h3> 80 * What is the best way to allow an application 81 * to be skinned. And how to allow the flexible 82 * altering of a particular UI element in certain 83 * parts of the template hierarchy. For example 84 * on one section of your site you might like 85 * a certain bgcolor, on another part of your 86 * site you might want another. How can be let 87 * the designer specify these properties and 88 * still use the single $app.ui.bgcolor in 89 * all the templates. 90 * <p> 91 * It would also be very cool to use some form 92 * of inheritence for UI elements. Say a $ui.bgcolor 93 * is used in a template where the bgcolor is not 94 * set for that part of hierarch, it would be cool 95 * if it could find the setting for the bgcolor 96 * in the parent directory. So you could override 97 * a UI element where you wanted and the system 98 * would fall back to the parent when necessary. 99 * <p> 100 * How to specify skins, how to deal with images, 101 * how could this be handled with a web app. 102 * 103 * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a> 104 * @author <a href="mailto:james_coltman@majorband.co.uk">James Coltman</a> 105 * @version $Id: UIManager.java,v 1.3 2002/07/11 16:53:25 mpoeschl Exp $ 106 */ 107 public class UIManager implements ApplicationTool 108 { 109 /*** 110 * The location of the skins within the application 111 * resources directory. 112 */ 113 private static final String SKINS_DIRECTORY = "/ui/skins"; 114 115 /*** 116 * The name of the directory where images are 117 * stored for this skin. 118 */ 119 private static final String IMAGES_DIRECTORY = "/images"; 120 121 122 /*** 123 * Property tag for the skin that is to be 124 * used for the web application. 125 */ 126 private static final String SKIN_PROPERTY = "tool.ui.skin"; 127 128 /*** 129 * Default skin name. This name actually represents 130 * a directory in the WEBAPP/resources/ui/skins 131 * directory. There is a file called skin.props 132 * which actually contains the name/value pairs. 133 */ 134 private static final String SKIN_PROPERTY_DEFAULT = "default"; 135 136 /*** 137 * Attribute name of skinName value in User's temp hashmap. 138 */ 139 private static final String SKIN_ATTRIBUTE = 140 "org.apache.turbine.services.pull.util.UIManager.skin"; 141 142 /*** 143 * The actual skin being used for the webapp. 144 */ 145 private String skinName; 146 147 /*** 148 * The skins directory. 149 */ 150 private static String skinsDirectory; 151 152 /*** 153 * The file within the skin directory that actually 154 * contains the name/value pairs for the skin. 155 */ 156 private static final String SKIN_PROPS_FILE = "skin.props"; 157 158 /*** 159 * The file name for the skin style sheet. 160 */ 161 private static final String SKIN_CSS_FILE = "skin.css"; 162 163 /*** 164 * This the resources directory relative to the 165 * webapp context. Used for constructing correct 166 * URIs for retrieving images in image(). 167 */ 168 private static String resourcesDirectory; 169 170 /*** 171 * Properties to hold the name/value pairs 172 * for the skin. 173 */ 174 private static Properties skinProperties; 175 176 /*** 177 * Initialize the UIManager object. 178 * 179 * @param data This is null, RunData or User depending upon specified tool scope. 180 */ 181 public void init(Object data) 182 { 183 /*** 184 * Store the resources directory for use in image(). 185 */ 186 resourcesDirectory = TurbinePull.getResourcesDirectory(); 187 188 if (data == null) 189 { 190 Log.debug("UI Manager scope is global"); 191 setSkin(); 192 } 193 else if (data instanceof RunData) 194 { 195 Log.debug("UI Manager scope is request"); 196 setSkin((RunData) data); 197 } 198 else if (data instanceof User) 199 { 200 Log.debug("UI Manager scope is session"); 201 setSkin((User) data); 202 } 203 204 skinsDirectory = 205 TurbinePull.getAbsolutePathToResourcesDirectory() + SKINS_DIRECTORY; 206 207 loadSkin(); 208 } 209 210 /*** 211 * This lets the tool know that it should be 212 * refreshed. The tool can perform whatever actions 213 * are necessary to refresh itself. This is necessary 214 * for sane development where you probably want the 215 * tools to refresh themselves on every request. 216 */ 217 public void refresh() 218 { 219 Log.debug("Refreshing UI manager"); 220 221 loadSkin(); 222 } 223 224 /*** 225 * Retrieve a property from the properties held 226 * within the properties file for this skin. 227 */ 228 public String get(String key) 229 { 230 return skinProperties.getProperty(key); 231 } 232 233 /*** 234 * Retrieve the skin name. 235 */ 236 public String getSkin() 237 { 238 return skinName; 239 } 240 241 /*** 242 * Retrieve the URL for an image that is part 243 * of a skin. The images are stored in the 244 * WEBAPP/resources/ui/skins/<SKIN>/images 245 * directory. 246 * 247 * Use this if for some reason your server name, 248 * server scheme, or server port change on a 249 * per request basis. I'm not sure if this 250 * would happend in a load balanced situation. 251 * I think in most cases the image(String image) 252 * method would probably be enough, but I'm not 253 * absolutely positive. 254 */ 255 public String image(String imageId, RunData data) 256 { 257 ContentURI cu = new ContentURI(data); 258 StringBuffer sb = new StringBuffer(); 259 260 sb.append(resourcesDirectory). 261 append(SKINS_DIRECTORY). 262 append("/"). 263 append(getSkin()). 264 append(IMAGES_DIRECTORY). 265 append("/"). 266 append(imageId); 267 268 return cu.getURI(sb.toString()); 269 } 270 271 /*** 272 * Retrieve the URL for an image that is part 273 * of a skin. The images are stored in the 274 * WEBAPP/resources/ui/skins/<SKIN>/images 275 * directory. 276 */ 277 public String image(String imageId) 278 { 279 StringBuffer sb = new StringBuffer(); 280 281 sb.append(TurbineServlet.getServerScheme()). 282 append("://"). 283 append(TurbineServlet.getServerName()). 284 append(":"). 285 append(TurbineServlet.getServerPort()). 286 append(TurbineServlet.getContextPath()). 287 append(resourcesDirectory). 288 append(SKINS_DIRECTORY). 289 append("/"). 290 append(getSkin()). 291 append(IMAGES_DIRECTORY). 292 append("/"). 293 append(imageId); 294 295 return sb.toString(); 296 } 297 298 /*** 299 * Retrieve the URL for the style sheet that is part 300 * of a skin. The style is stored in the 301 * WEBAPP/resources/ui/skins/<SKIN> directory with the 302 * filename skin.css 303 * 304 * Use this if for some reason your server name, 305 * server scheme, or server port change on a 306 * per request basis. I'm not sure if this 307 * would happend in a load balanced situation. 308 * I think in most cases the style() 309 * method would probably be enough, but I'm not 310 * absolutely positive. 311 */ 312 public String getStylecss(RunData data) 313 { 314 ContentURI cu = new ContentURI(data); 315 StringBuffer sb = new StringBuffer(); 316 317 sb.append(resourcesDirectory). 318 append(SKINS_DIRECTORY). 319 append("/"). 320 append(getSkin()). 321 append("/"). 322 append(SKIN_CSS_FILE); 323 324 return cu.getURI(sb.toString()); 325 } 326 327 /*** 328 * Retrieve the URL for the style sheet that is part 329 * of a skin. The style is stored in the 330 * WEBAPP/resources/ui/skins/<SKIN> directory with the 331 * filename skin.css 332 */ 333 public String getStylecss() 334 { 335 StringBuffer sb = new StringBuffer(); 336 337 sb.append(TurbineServlet.getServerScheme()). 338 append("://"). 339 append(TurbineServlet.getServerName()). 340 append(":"). 341 append(TurbineServlet.getServerPort()). 342 append(TurbineServlet.getContextPath()). 343 append("/"). 344 append(resourcesDirectory). 345 append(SKINS_DIRECTORY). 346 append("/"). 347 append(getSkin()). 348 append("/"). 349 append(SKIN_CSS_FILE); 350 351 return sb.toString(); 352 } 353 354 /*** 355 * Load the specified skin. In development mode 356 * this may occur frequently as the skin properties 357 * are being changed. 358 */ 359 private void loadSkin() 360 { 361 skinProperties = new Properties(); 362 363 try 364 { 365 FileInputStream is = new FileInputStream( 366 skinsDirectory + "/" + getSkin() + "/" + SKIN_PROPS_FILE); 367 368 skinProperties.load(is); 369 } 370 catch (Exception e) 371 { 372 Log.error("Cannot load skin: " + skinName); 373 } 374 } 375 376 /*** 377 * Set the skin name to the skin from the TR.props 378 * file. If the property is not present use the 379 * default skin. 380 */ 381 public void setSkin() 382 { 383 this.skinName = TurbineResources.getString(SKIN_PROPERTY, 384 SKIN_PROPERTY_DEFAULT); 385 } 386 387 /*** 388 * Set the skin name to the specified skin. 389 * 390 * @param skinName the skin name to use. 391 */ 392 public void setSkin(String skinName) 393 { 394 this.skinName = skinName; 395 } 396 397 /*** 398 * Set the skin name when the tool is configured to be 399 * loaded on a per-request basis. By default it calls getSkin 400 * to return the skin specified in TR.properties. Developers can 401 * write a subclass of UIManager that overrides this method to 402 * determine the skin to use based on information held in the request. 403 * 404 * @param data a RunData instance 405 */ 406 protected void setSkin(RunData data) 407 { 408 setSkin(); 409 } 410 411 /*** 412 * Set the skin name when the tool is configured to be 413 * loaded on a per-session basis. It the user's temp hashmap contains 414 * a value in the attribute specified by the String constant SKIN_ATTRIBUTE 415 * then that is returned. Otherwise it calls getSkin to return the skin 416 * specified in TR.properties. 417 * 418 * @param user a User instance 419 */ 420 protected void setSkin(User user) 421 { 422 if (user.getTemp(SKIN_ATTRIBUTE) == null) 423 { 424 setSkin(); 425 } 426 else 427 { 428 setSkin((String) user.getTemp(SKIN_ATTRIBUTE)); 429 } 430 } 431 432 /*** 433 * Set the skin name user's temp hashmap for the current session. 434 * 435 * @param user a User instance 436 * @param skin the skin name for the session 437 */ 438 public static void setSkin(User user, String skin) 439 { 440 user.setTemp(SKIN_ATTRIBUTE, skin); 441 } 442 }

This page was automatically generated by Maven