1 package org.apache.turbine.tool; 2 3 56 57 import java.io.FileInputStream ; 58 import java.util.Properties ; 59 60 import org.apache.commons.logging.Log; 61 import org.apache.commons.logging.LogFactory; 62 import org.apache.fulcrum.TurbineServices; 63 import org.apache.fulcrum.security.entity.User; 64 import org.apache.turbine.RunData; 65 import org.apache.turbine.Turbine; 66 import org.apache.turbine.services.pull.ApplicationTool; 67 import org.apache.turbine.services.pull.PullService; 68 69 107 public class UIManager implements ApplicationTool 108 { 109 private static final Log log = LogFactory.getLog( UIManager.class ); 110 111 115 private static final String SKINS_DIRECTORY = "/ui/skins"; 116 117 121 private static final String IMAGES_DIRECTORY = "/images"; 122 123 124 128 private static final String SKIN_PROPERTY = "tool.ui.skin"; 129 130 136 private static final String SKIN_PROPERTY_DEFAULT = "default"; 137 138 141 private static final String SKIN_ATTRIBUTE = 142 "org.apache.fulcrum.pull.util.UIManager.skin"; 143 144 147 private String skinName; 148 149 152 private static String skinsDirectory; 153 154 158 private static final String SKIN_PROPS_FILE = "skin.props"; 159 160 163 private static final String SKIN_CSS_FILE = "skin.css"; 164 165 170 private static String resourcesDirectory; 171 172 176 private static Properties skinProperties; 177 178 183 public void init(Object data) 184 { 185 188 189 resourcesDirectory = getPullService().getResourcesDirectory(); 190 191 if (data == null) 192 { 193 log.debug("UI Manager scope is global"); 194 setSkin(); 195 } 196 else if (data instanceof RunData) 197 { 198 log.debug("UI Manager scope is request"); 199 setSkin((RunData) data); 200 } 201 else if (data instanceof User) 202 { 203 log.debug("UI Manager scope is session"); 204 setSkin((User) data); 205 } 206 207 skinsDirectory = 208 getPullService().getAbsolutePathToResourcesDirectory() + SKINS_DIRECTORY; 209 210 loadSkin(); 211 } 212 213 220 public void refresh() 221 { 222 log.debug("Refreshing UI manager"); 223 224 loadSkin(); 225 } 226 227 231 public String get(String key) 232 { 233 return skinProperties.getProperty(key); 234 } 235 236 239 public String getSkin() 240 { 241 return skinName; 242 } 243 244 258 public String image(String imageId, RunData data) 259 { 260 ContentURI cu = new ContentURI(data); 261 StringBuffer sb = new StringBuffer (); 262 263 sb.append(resourcesDirectory). 264 append(SKINS_DIRECTORY). 265 append("/"). 266 append(getSkin()). 267 append(IMAGES_DIRECTORY). 268 append("/"). 269 append(imageId); 270 271 return cu.getURI(sb.toString()); 272 } 273 274 280 public String image(String imageId) 281 { 282 StringBuffer sb = new StringBuffer (); 283 284 sb.append(Turbine.getServerScheme()). 285 append("://"). 286 append(Turbine.getServerName()). 287 append(":"). 288 append(Turbine.getServerPort()). 289 append(Turbine.getApplicationRoot()). 290 append("/"). 291 append(resourcesDirectory). 292 append(SKINS_DIRECTORY). 293 append("/"). 294 append(getSkin()). 295 append(IMAGES_DIRECTORY). 296 append("/"). 297 append(imageId); 298 299 return sb.toString(); 300 } 301 302 316 public String getStylecss(RunData data) 317 { 318 ContentURI cu = new ContentURI(data); 319 StringBuffer sb = new StringBuffer (); 320 321 sb.append(resourcesDirectory). 322 append(SKINS_DIRECTORY). 323 append("/"). 324 append(getSkin()). 325 append("/"). 326 append(SKIN_CSS_FILE); 327 328 return cu.getURI(sb.toString()); 329 } 330 331 337 public String getStylecss() 338 { 339 StringBuffer sb = new StringBuffer (); 340 341 sb.append(Turbine.getServerScheme()). 342 append("://"). 343 append(Turbine.getServerName()). 344 append(":"). 345 append(Turbine.getServerPort()). 346 append(Turbine.getApplicationRoot()). 347 append("/"). 348 append(resourcesDirectory). 349 append(SKINS_DIRECTORY). 350 append("/"). 351 append(getSkin()). 352 append("/"). 353 append(SKIN_CSS_FILE); 354 355 return sb.toString(); 356 } 357 358 363 private void loadSkin() 364 { 365 skinProperties = new Properties (); 366 367 try 368 { 369 FileInputStream is = new FileInputStream ( 370 skinsDirectory + "/" + getSkin() + "/" + SKIN_PROPS_FILE); 371 372 skinProperties.load(is); 373 } 374 catch (Exception e) 375 { 376 log.error("Cannot load skin: " + skinName); 377 } 378 } 379 380 385 public void setSkin() 386 { 387 this.skinName = Turbine.getConfiguration().getString(SKIN_PROPERTY, 388 SKIN_PROPERTY_DEFAULT); 389 } 390 391 396 public void setSkin(String skinName) 397 { 398 this.skinName = skinName; 399 } 400 401 410 protected void setSkin(RunData data) 411 { 412 setSkin(); 413 } 414 415 424 protected void setSkin(User user) 425 { 426 if (user.getTemp(SKIN_ATTRIBUTE) == null) 427 { 428 setSkin(); 429 } 430 else 431 { 432 setSkin((String ) user.getTemp(SKIN_ATTRIBUTE)); 433 } 434 } 435 436 442 public static void setSkin(User user, String skin) 443 { 444 user.setTemp(SKIN_ATTRIBUTE, skin); 445 } 446 447 453 protected static PullService getPullService() 454 { 455 return (PullService)TurbineServices 456 .getInstance().getService(PullService.SERVICE_NAME); 457 } 458 } 459 | Popular Tags |