| 1 |
// Copyright 2003-2009, FreeHEP. |
| 2 |
package org.freehep.graphicsbase.util; |
| 3 |
|
| 4 |
import java.awt.Color; |
| 5 |
import java.awt.Dimension; |
| 6 |
import java.awt.Insets; |
| 7 |
import java.util.ArrayList; |
| 8 |
import java.util.Collections; |
| 9 |
import java.awt.Rectangle; |
| 10 |
import java.util.Enumeration; |
| 11 |
import java.util.List; |
| 12 |
import java.util.Properties; |
| 13 |
|
| 14 |
/** |
| 15 |
* Special property class which allows typed properties to be set and |
| 16 |
* returned. It also allows the hookup of two default property objects |
| 17 |
* to be searched if this property object does not contain the property. |
| 18 |
* |
| 19 |
* FIXME check what org.freehep.application.PropertyUtilities.java |
| 20 |
* has to offer and merge, or not |
| 21 |
* |
| 22 |
* FIXME: This class does not seem general enough to be a "public" utility. Should be |
| 23 |
* improved and merged with PropertyUtilities, or moved into the graphicsio package (tonyj) |
| 24 |
* |
| 25 |
* @author Mark Donszelmann |
| 26 |
* @version $Id: UserProperties.java 8584 2006-08-10 23:06:37Z duns $ |
| 27 |
*/ |
| 28 |
public class UserProperties extends Properties { |
| 29 |
|
| 30 |
/** |
| 31 |
* |
| 32 |
*/ |
| 33 |
private static final long serialVersionUID = 3297483251864528952L; |
| 34 |
protected Properties altDefaults; |
| 35 |
|
| 36 |
public UserProperties() { |
| 37 |
super(); |
| 38 |
altDefaults = new Properties(); |
| 39 |
} |
| 40 |
|
| 41 |
public UserProperties(Properties defaults) { |
| 42 |
super(defaults); |
| 43 |
altDefaults = new Properties(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Constructs UserProperties with a defaults and altDefaults table, |
| 48 |
* which are searched in that order. |
| 49 |
*/ |
| 50 |
public UserProperties(Properties defaults, Properties altDefaults) { |
| 51 |
super(defaults); |
| 52 |
this.altDefaults = altDefaults; |
| 53 |
} |
| 54 |
|
| 55 |
public Enumeration<?> propertyNames() { |
| 56 |
|
| 57 |
List<String> list = new ArrayList<String>(); |
| 58 |
|
| 59 |
for (Enumeration<?> e = super.propertyNames(); e.hasMoreElements(); ) { |
| 60 |
list.add((String)e.nextElement()); |
| 61 |
} |
| 62 |
if (altDefaults != null) { |
| 63 |
for (Enumeration<?> e = altDefaults.propertyNames(); e.hasMoreElements(); ) { |
| 64 |
list.add((String)e.nextElement()); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return Collections.enumeration(list); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Copies properties, including its defaults into this UserProperties |
| 73 |
*/ |
| 74 |
public void setProperties(Properties properties) { |
| 75 |
for (Enumeration<?> e=properties.propertyNames(); e.hasMoreElements(); ) { |
| 76 |
String key = (String)e.nextElement(); |
| 77 |
setProperty(key, properties.getProperty(key)); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
public Object setProperty(String key, String value) { |
| 82 |
if (value == null) return super.setProperty(key, "null"); |
| 83 |
return super.setProperty(key, value); |
| 84 |
} |
| 85 |
|
| 86 |
public Object setProperty(String key, String[] value) { |
| 87 |
return setProperty(this, key, value); |
| 88 |
} |
| 89 |
|
| 90 |
public static Object setProperty(Properties properties, String key, String[] value) { |
| 91 |
if (value == null) return properties.setProperty(key, "null"); |
| 92 |
|
| 93 |
StringBuffer sb = new StringBuffer(); |
| 94 |
for (int i=0; i<value.length; i++) { |
| 95 |
if (i != 0) sb.append(", "); |
| 96 |
sb.append(value[i]); |
| 97 |
} |
| 98 |
return properties.setProperty(key, sb.toString()); |
| 99 |
} |
| 100 |
|
| 101 |
public Object setProperty(String key, Color value) { |
| 102 |
return setProperty(this, key, value); |
| 103 |
} |
| 104 |
|
| 105 |
public static Object setProperty(Properties properties, String key, Color value) { |
| 106 |
if (value == null) return properties.setProperty(key, "null"); |
| 107 |
return properties.setProperty(key, value.getRed()+", "+value.getGreen()+", "+value.getBlue()+", "+value.getAlpha()); |
| 108 |
} |
| 109 |
|
| 110 |
public Object setProperty(String key, Rectangle value) { |
| 111 |
return setProperty(this, key, value); |
| 112 |
} |
| 113 |
|
| 114 |
public static Object setProperty(Properties properties, String key, Rectangle value) { |
| 115 |
if (value == null) return properties.setProperty(key, "null"); |
| 116 |
return properties.setProperty(key, value.x+", "+value.y+", "+value.width+", "+value.height); |
| 117 |
} |
| 118 |
|
| 119 |
public Object setProperty(String key, Insets value) { |
| 120 |
return setProperty(this, key, value); |
| 121 |
} |
| 122 |
|
| 123 |
public static Object setProperty(Properties properties, String key, Insets value) { |
| 124 |
if (value == null) return properties.setProperty(key, "null"); |
| 125 |
return properties.setProperty(key, value.top+", "+value.left+", "+value.bottom+", "+value.right); |
| 126 |
} |
| 127 |
|
| 128 |
public Object setProperty(String key, Dimension value) { |
| 129 |
return setProperty(this, key, value); |
| 130 |
} |
| 131 |
|
| 132 |
public static Object setProperty(Properties properties, String key, Dimension value) { |
| 133 |
if (value == null) return properties.setProperty(key, "null"); |
| 134 |
return properties.setProperty(key, value.width+", "+value.height); |
| 135 |
} |
| 136 |
|
| 137 |
public Object setProperty(String key, int value) { |
| 138 |
return setProperty(this, key, value); |
| 139 |
} |
| 140 |
|
| 141 |
public static Object setProperty(Properties properties, String key, int value) { |
| 142 |
return properties.setProperty(key, Integer.toString(value)); |
| 143 |
} |
| 144 |
|
| 145 |
public Object setProperty(String key, double value) { |
| 146 |
return setProperty(this, key, value); |
| 147 |
} |
| 148 |
|
| 149 |
public static Object setProperty(Properties properties, String key, double value) { |
| 150 |
return properties.setProperty(key, Double.toString(value)); |
| 151 |
} |
| 152 |
|
| 153 |
public Object setProperty(String key, float value) { |
| 154 |
return setProperty(this, key, value); |
| 155 |
} |
| 156 |
|
| 157 |
public static Object setProperty(Properties properties, String key, float value) { |
| 158 |
return properties.setProperty(key, Float.toString(value)); |
| 159 |
} |
| 160 |
|
| 161 |
public Object setProperty(String key, boolean value) { |
| 162 |
return setProperty(this, key, value); |
| 163 |
} |
| 164 |
|
| 165 |
public static Object setProperty(Properties properties, String key, boolean value) { |
| 166 |
return properties.setProperty(key, Boolean.toString(value)); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public String getProperty(String key) { |
| 172 |
String value = super.getProperty(key); |
| 173 |
return value != null ? value : altDefaults.getProperty(key); |
| 174 |
} |
| 175 |
|
| 176 |
public String getProperty(String key, String def) { |
| 177 |
String value = getProperty(key); |
| 178 |
return value != null ? value : def; |
| 179 |
} |
| 180 |
|
| 181 |
public String[] getPropertyStringArray(String key) { |
| 182 |
return getPropertyStringArray(key, null); |
| 183 |
} |
| 184 |
|
| 185 |
public String[] getPropertyStringArray(String key, String[] def) { |
| 186 |
String s = getProperty(key); |
| 187 |
if (s == null) return def; |
| 188 |
if (s.equals("null")) return null; |
| 189 |
|
| 190 |
return s.split(", "); |
| 191 |
} |
| 192 |
|
| 193 |
public Color getPropertyColor(String key) { |
| 194 |
return getPropertyColor(key, null); |
| 195 |
} |
| 196 |
|
| 197 |
public Color getPropertyColor(String key, Color def) { |
| 198 |
String s = getProperty(key); |
| 199 |
if (s == null) return def; |
| 200 |
if (s.equals("null")) return null; |
| 201 |
|
| 202 |
String[] r = s.split(", "); |
| 203 |
return new Color( |
| 204 |
Integer.parseInt(r[0]), |
| 205 |
Integer.parseInt(r[1]), |
| 206 |
Integer.parseInt(r[2]), |
| 207 |
Integer.parseInt(r[3]) |
| 208 |
); |
| 209 |
} |
| 210 |
|
| 211 |
public Rectangle getPropertyRectangle(String key) { |
| 212 |
return getPropertyRectangle(key, null); |
| 213 |
} |
| 214 |
|
| 215 |
public Rectangle getPropertyRectangle(String key, Rectangle def) { |
| 216 |
String s = getProperty(key); |
| 217 |
if (s == null) return def; |
| 218 |
if (s.equals("null")) return null; |
| 219 |
|
| 220 |
String[] r = s.split(", "); |
| 221 |
return new Rectangle( |
| 222 |
Integer.parseInt(r[0]), |
| 223 |
Integer.parseInt(r[1]), |
| 224 |
Integer.parseInt(r[2]), |
| 225 |
Integer.parseInt(r[3]) |
| 226 |
); |
| 227 |
} |
| 228 |
|
| 229 |
public Insets getPropertyInsets(String key) { |
| 230 |
return getPropertyInsets(key, null); |
| 231 |
} |
| 232 |
|
| 233 |
public Insets getPropertyInsets(String key, Insets def) { |
| 234 |
String s = getProperty(key); |
| 235 |
if (s == null) return def; |
| 236 |
if (s.equals("null")) return null; |
| 237 |
|
| 238 |
String[] r = s.split(", "); |
| 239 |
return new Insets( |
| 240 |
Integer.parseInt(r[0]), |
| 241 |
Integer.parseInt(r[1]), |
| 242 |
Integer.parseInt(r[2]), |
| 243 |
Integer.parseInt(r[3]) |
| 244 |
); |
| 245 |
} |
| 246 |
|
| 247 |
public Dimension getPropertyDimension(String key) { |
| 248 |
return getPropertyDimension(key, null); |
| 249 |
} |
| 250 |
|
| 251 |
public Dimension getPropertyDimension(String key, Dimension def) { |
| 252 |
String s = getProperty(key); |
| 253 |
if (s == null) return def; |
| 254 |
if (s.equals("null")) return null; |
| 255 |
|
| 256 |
String[] d = s.split(", "); |
| 257 |
return new Dimension( |
| 258 |
Integer.parseInt(d[0]), |
| 259 |
Integer.parseInt(d[1]) |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
public int getPropertyInt(String key) { |
| 264 |
return getPropertyInt(key, 0); |
| 265 |
} |
| 266 |
|
| 267 |
public int getPropertyInt(String key, int def) { |
| 268 |
// FIXME: Yow!, two unnecessary object creations for each key lookup. |
| 269 |
return new Integer(getProperty(key, Integer.toString(def))).intValue(); |
| 270 |
} |
| 271 |
|
| 272 |
public double getPropertyDouble(String key) { |
| 273 |
return getPropertyDouble(key, 0.0); |
| 274 |
} |
| 275 |
|
| 276 |
public double getPropertyDouble(String key, double def) { |
| 277 |
return new Double(getProperty(key, Double.toString(def))).doubleValue(); |
| 278 |
} |
| 279 |
|
| 280 |
public float getPropertyFloat(String key) { |
| 281 |
return getPropertyFloat(key, 0.0f); |
| 282 |
} |
| 283 |
|
| 284 |
public float getPropertyFloat(String key, float def) { |
| 285 |
return new Float(getProperty(key, Float.toString(def))).floatValue(); |
| 286 |
} |
| 287 |
|
| 288 |
public boolean isProperty(String key) { |
| 289 |
return isProperty(key, false); |
| 290 |
} |
| 291 |
|
| 292 |
public boolean isProperty(String key, boolean def) { |
| 293 |
return new Boolean(getProperty(key, Boolean.toString(def))).booleanValue(); |
| 294 |
} |
| 295 |
|
| 296 |
} |