1 package org.apache.turbine.services.pull.util; 2 3 18 19 import java.io.FileInputStream ; 20 21 import java.util.Properties ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import org.apache.turbine.Turbine; 27 import org.apache.turbine.om.security.User; 28 import org.apache.turbine.services.pull.ApplicationTool; 29 import org.apache.turbine.services.pull.TurbinePull; 30 import org.apache.turbine.util.RunData; 31 import org.apache.turbine.util.ServerData; 32 import org.apache.turbine.util.uri.DataURI; 33 34 78 public class UIManager implements ApplicationTool 79 { 80 81 private static Log log = LogFactory.getLog(UIManager.class); 82 83 87 private static final String SKINS_DIRECTORY = "/ui/skins"; 88 89 93 private static final String IMAGES_DIRECTORY = "/images"; 94 95 99 private static final String SKIN_PROPERTY = "tool.ui.skin"; 100 101 107 private static final String SKIN_PROPERTY_DEFAULT = "default"; 108 109 112 private static final String SKIN_ATTRIBUTE = 113 UIManager.class.getName()+ ".skin"; 114 115 118 private String skinName; 119 120 123 private String skinsDirectory; 124 125 129 private static final String SKIN_PROPS_FILE = "skin.props"; 130 131 134 private static final String SKIN_CSS_FILE = "skin.css"; 135 136 141 private String resourcesDirectory; 142 143 147 private Properties skinProperties; 148 149 154 public void init(Object data) 155 { 156 159 resourcesDirectory = TurbinePull.getResourcesDirectory(); 160 161 if (data == null) 162 { 163 log.debug("UI Manager scope is global"); 164 setSkin(); 165 } 166 else if (data instanceof RunData) 167 { 168 log.debug("UI Manager scope is request"); 169 setSkin((RunData) data); 170 } 171 else if (data instanceof User) 172 { 173 log.debug("UI Manager scope is session"); 174 setSkin((User) data); 175 } 176 177 skinsDirectory = 178 TurbinePull.getAbsolutePathToResourcesDirectory() + SKINS_DIRECTORY; 179 180 loadSkin(); 181 } 182 183 190 public void refresh() 191 { 192 log.debug("Refreshing UI manager"); 193 194 loadSkin(); 195 } 196 197 201 public String get(String key) 202 { 203 return skinProperties.getProperty(key); 204 } 205 206 209 public String getSkin() 210 { 211 return skinName; 212 } 213 214 228 public String image(String imageId, RunData data) 229 { 230 DataURI du = new DataURI(data); 231 232 StringBuffer sb = new StringBuffer (); 233 234 sb.append(resourcesDirectory). 235 append(SKINS_DIRECTORY). 236 append("/"). 237 append(getSkin()). 238 append(IMAGES_DIRECTORY). 239 append("/"). 240 append(imageId); 241 242 du.setScriptName(sb.toString()); 243 return du.getAbsoluteLink(); 244 } 245 246 252 public String image(String imageId) 253 { 254 ServerData sd = Turbine.getDefaultServerData(); 255 DataURI du = new DataURI(sd); 256 257 StringBuffer sb = new StringBuffer (); 258 259 sb.append(resourcesDirectory). 260 append(SKINS_DIRECTORY). 261 append("/"). 262 append(getSkin()). 263 append(IMAGES_DIRECTORY). 264 append("/"). 265 append(imageId); 266 267 du.setScriptName(sb.toString()); 268 return du.getAbsoluteLink(); 269 } 270 271 285 public String getStylecss(RunData data) 286 { 287 DataURI du = new DataURI(data); 288 StringBuffer sb = new StringBuffer (); 289 290 sb.append(resourcesDirectory). 291 append(SKINS_DIRECTORY). 292 append("/"). 293 append(getSkin()). 294 append("/"). 295 append(SKIN_CSS_FILE); 296 297 du.setScriptName(sb.toString()); 298 return du.getAbsoluteLink(); 299 } 300 301 307 public String getStylecss() 308 { 309 ServerData sd = Turbine.getDefaultServerData(); 310 DataURI du = new DataURI(sd); 311 312 StringBuffer sb = new StringBuffer (); 313 314 sb.append(resourcesDirectory). 315 append(SKINS_DIRECTORY). 316 append("/"). 317 append(getSkin()). 318 append("/"). 319 append(SKIN_CSS_FILE); 320 321 du.setScriptName(sb.toString()); 322 return du.getAbsoluteLink(); 323 } 324 325 330 private void loadSkin() 331 { 332 skinProperties = new Properties (); 333 334 try 335 { 336 FileInputStream is = new FileInputStream ( 337 skinsDirectory + "/" + getSkin() + "/" + SKIN_PROPS_FILE); 338 339 skinProperties.load(is); 340 } 341 catch (Exception e) 342 { 343 log.error("Cannot load skin: " + skinName); 344 } 345 } 346 347 352 public void setSkin() 353 { 354 this.skinName = Turbine.getConfiguration() 355 .getString(SKIN_PROPERTY, 356 SKIN_PROPERTY_DEFAULT); 357 } 358 359 364 public void setSkin(String skinName) 365 { 366 this.skinName = skinName; 367 } 368 369 378 protected void setSkin(RunData data) 379 { 380 setSkin(); 381 } 382 383 392 protected void setSkin(User user) 393 { 394 if (user.getTemp(SKIN_ATTRIBUTE) == null) 395 { 396 setSkin(); 397 } 398 else 399 { 400 setSkin((String ) user.getTemp(SKIN_ATTRIBUTE)); 401 } 402 } 403 404 410 public static void setSkin(User user, String skin) 411 { 412 user.setTemp(SKIN_ATTRIBUTE, skin); 413 } 414 } 415 | Popular Tags |