001/* 002 * Copyright (c) 2006-2010 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: berkley $' 006 * '$Date: 2010-04-28 00:12:36 +0000 (Wed, 28 Apr 2010) $' 007 * '$Revision: 24000 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 * 028 */ 029 030package org.kepler.util; 031 032import java.io.IOException; 033import java.util.List; 034 035import org.kepler.configuration.ConfigurationManager; 036import org.kepler.configuration.ConfigurationNamespace; 037import org.kepler.configuration.ConfigurationProperty; 038 039////////////////////////////////////////////////////////////////////////// 040//// StaticResources 041 042/** 043 * 044 * Static resources for accessing ResourceBundles. 045 * 046 * @author Matthew Brooke 047 * @version $Id: StaticResources.java 24000 2010-04-28 00:12:36Z berkley $ 048 * @since Ptolemy II 7.1 049 * @Pt.ProposedRating 050 * @Pt.AcceptedRating 051 */ 052public class StaticResources { 053 // kepler.gui.StaticGUIResources contains GUI specific code, 054 // this class should _not_ import GUI code. 055 056 // protected constructor - non-instantiable 057 protected StaticResources() { 058 } 059 060 /////////////////////////////////////////////////////////////////// 061 //// public methods //// 062 /////////////////////////////////////////////////////////////////// 063 064 /** 065 * Search the uiDisplayText resourcebundle for the property specified by the 066 * key parameter. Return the boolean value of the property String 067 * corresponding to the specified key. If the property is not found, the 068 * defaultVal parameter is returned. 069 * 070 * @param key 071 * the properties key identifying the value to be found 072 * @param defaultVal 073 * - the default boolean value to be returned if the requested 074 * property cannot be found or read 075 * @return boolean value of the property String corresponding to the 076 * specified key. If the property is not found, the defaultVal 077 * parameter is returned. 078 */ 079 public static boolean getBoolean(String key, boolean defaultVal) { 080 081 boolean val = defaultVal; 082 try { 083 val = Boolean.valueOf(_getString(key, getUISettingsProperty())) 084 .booleanValue(); 085 } catch (Exception ex) { 086 if (_isDebugging) { 087 System.out 088 .println("StaticResources could not find the property for the key: " 089 + key 090 + "\n; returning default value: " 091 + defaultVal); 092 } 093 } 094 return val; 095 } 096 097 /** 098 * Search the uiDisplayText resourcebundle for the property specified by the 099 * key parameter. Return the String value of the property value specified. 100 * If the property is not found, the default defaultString parameter is 101 * returned. 102 * 103 * @param key 104 * the properties key for the String to be found 105 * @param defaultString 106 * - the default String to be returned if the property cannot be 107 * found or read 108 * @return String value associated with the specified key, or the 109 * defaultString parameter if the property is not found. 110 */ 111 public static String getDisplayString(String key, String defaultString) { 112 113 String result = null; 114 try { 115 result = _getString(key, getDisplayTextProperty()); 116 } catch (Exception ex) { 117 if (_isDebugging) { 118 System.out 119 .println("StaticResources could not find String property for the key: " 120 + key 121 + "\n; returning default String: " 122 + defaultString); 123 } 124 return defaultString; 125 } 126 return result; 127 } 128 129 /** 130 * Search for the ResourceBundle containing the UI Display Text. 131 * @return The resource bundle that corresponds with 132 * {@link #UI_DISPLAY_TEXT_BUNDLE} 133 * @exception IOException If the bundle that corresponds with 134 * {@link #UI_DISPLAY_TEXT_BUNDLE} cannot be loaded. 135 */ 136 public static ConfigurationProperty getDisplayTextProperty() 137 throws IOException { 138 139 if (diplayTextProp == null) { 140 ConfigurationNamespace ns = new ConfigurationNamespace( 141 "uiDisplayText"); 142 diplayTextProp = ConfigurationManager.getInstance().getProperty( 143 ConfigurationManager.getModule("gui"), ns); 144 } 145 return diplayTextProp; 146 } 147 148 /** 149 * Search the uiSettings resourcebundle for the property specified by the 150 * key parameter. Return the String value of the property value specified. 151 * If the property is not found, the default defaultString parameter is 152 * returned. 153 * 154 * @param key 155 * the properties key for the String to be found 156 * @param defaultString 157 * - the default String to be returned if the property cannot be 158 * found or read 159 * @return String value associated with the specified key, or the 160 * defaultString parameter if the property is not found. 161 */ 162 public static String getSettingsString(String key, String defaultString) { 163 String result = null; 164 try { 165 result = _getString(key, getUISettingsProperty()); 166 } catch (Exception ex) { 167 if (_isDebugging) { 168 System.out 169 .println("StaticResources could not find String property for the key: " 170 + key 171 + "\n; returning default String: " 172 + defaultString); 173 } 174 return defaultString; 175 } 176 return result; 177 } 178 179 /** 180 * Search the uiSettings resourcebundle for the size property specified by 181 * the sizeKey. Return the integer (int) value of the size specified. If the 182 * property is not found, the defaultSize parameter is returned. 183 * 184 * @param sizeKey 185 * the properties key String for the size setting 186 * @param defaultSize 187 * - the default size to be used if the property cannot be found 188 * @return integer (int) value of the size specified. If the property is not 189 * found, the defaultSize parameter is returned. 190 */ 191 public static int getSize(String sizeKey, int defaultSize) { 192 193 int size = 0; 194 try { 195 size = _getInt(sizeKey, getUISettingsProperty()); 196 } catch (Exception ex) { 197 if (_isDebugging) { 198 System.out 199 .println("StaticResources could not find size property for the key: " 200 + sizeKey 201 + "\n; returning default size: " 202 + defaultSize); 203 } 204 return defaultSize; 205 } 206 return size; 207 } 208 209 /** 210 * Search for the ResourceBundle containing the ui settings. 211 * @return The resource bundle that corresponds with 212 * {@link #UI_SETTINGS_BUNDLE} 213 * @exception IOException If the bundle that corresponds with 214 * {@link #UI_SETTINGS_BUNDLE} cannot be loaded. 215 */ 216 public static ConfigurationProperty getUISettingsProperty() 217 throws IOException { 218 219 if (uiSettingsProp == null) { 220 ConfigurationNamespace ns = new ConfigurationNamespace("uiSettings"); 221 uiSettingsProp = ConfigurationManager.getInstance().getProperty( 222 ConfigurationManager.getModule("gui"), ns); 223 } 224 return uiSettingsProp; 225 } 226 227 /** 228 * get the configuration property for the svg icon class mappings 229 */ 230 public static ConfigurationProperty getUiSVGIconMappingsByClass() { 231 if (uiSVGIconMappingsByClassProp == null) { 232 ConfigurationNamespace ns = new ConfigurationNamespace( 233 "uiSVGIconMappingsByClass"); 234 uiSVGIconMappingsByClassProp = ConfigurationManager.getInstance() 235 .getProperty(ConfigurationManager.getModule("gui"), ns); 236 } 237 return uiSVGIconMappingsByClassProp; 238 } 239 240 /** 241 * get the configuraiton property for the svg icon lsid mappings 242 */ 243 public static ConfigurationProperty getUiSVGIconMappingsByLSID() { 244 if (uiSVGIconMappingsByLSIDProp == null) { 245 ConfigurationNamespace ns = new ConfigurationNamespace( 246 "uiSVGIconMappingsByLSID"); 247 uiSVGIconMappingsByLSIDProp = ConfigurationManager.getInstance() 248 .getProperty(ConfigurationManager.getModule("gui"), ns); 249 } 250 return uiSVGIconMappingsByLSIDProp; 251 } 252 253 /** 254 * get the value of a property based on the name 255 */ 256 public static String getValueFromName(ConfigurationProperty prop, 257 String name) { 258 //System.out.println("finding name=" + name + " in prop " + prop.getName()); 259 List l = prop.findProperties("name", name, true); 260 if (l != null && l.size() > 0) { 261 ConfigurationProperty p = (ConfigurationProperty) l.get(0); 262 String value = p.getProperty("value").getValue(); 263 return value; 264 } 265 return null; 266 } 267 268 /////////////////////////////////////////////////////////////////// 269 //// public variables //// 270 /////////////////////////////////////////////////////////////////// 271 272 static { 273 try { 274 getUISettingsProperty(); 275 } catch (IOException ex) { 276 // no worries - just try again when we actually need it 277 } 278 try { 279 getDisplayTextProperty(); 280 } catch (IOException ex) { 281 // no worries - just try again when we actually need it 282 } 283 } 284 285 /////////////////////////////////////////////////////////////////// 286 //// protected methods //// 287 /////////////////////////////////////////////////////////////////// 288 289 /** 290 * Get the String property denoted by the propertyKey. 291 * 292 * @param propertyKey 293 * the properties key String identifying the property 294 * 295 * @param property 296 * the ConfigurationProperty in which to search 297 * 298 * @return the String value identified by the propertyKey 299 * 300 * @exception java.lang.Exception 301 * if key is not found or cannot be read 302 */ 303 protected static String _getString(String propertyKey, 304 ConfigurationProperty property) throws Exception { 305 String s = getValueFromName(property, propertyKey); 306 return s; 307 } 308 309 /** 310 * Get the integer (int) property denoted by the propertyKey. 311 * 312 * @param propertyKey 313 * the properties key String identifying the property 314 * 315 * @param property 316 * the ConfigurationProperty in which to search 317 * 318 * @return the int value identified by the propertyKey 319 * 320 * @exception Exception 321 * if key is not found, cannot be read, or cannot be parsed as 322 * an integer 323 */ 324 protected static int _getInt(String propertyKey, 325 ConfigurationProperty property) throws Exception { 326 String val = _getString(propertyKey, property); 327 return new Integer(val).intValue(); 328 } 329 330 //TODO: get this path out of the code. 331 public final static String RESOURCEBUNDLE_DIR = "ptolemy/configs/kepler"; 332 333 /////////////////////////////////////////////////////////////////// 334 //// protected variables //// 335 /////////////////////////////////////////////////////////////////// 336 337 /** Set to true and recompile for debugging such as error messages.*/ 338 protected final static boolean _isDebugging = false; 339 340 /////////////////////////////////////////////////////////////////// 341 //// public variables //// 342 /////////////////////////////////////////////////////////////////// 343 344 private static ConfigurationProperty uiSettingsProp; 345 private static ConfigurationProperty diplayTextProp; 346 private static ConfigurationProperty uiSVGIconMappingsByClassProp; 347 private static ConfigurationProperty uiSVGIconMappingsByLSIDProp; 348}