001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: Config.java,v $ 023 Revision 1.10 2004/05/12 18:03:42 markl 024 javadoc updates 025 026 Revision 1.9 2004/05/05 21:22:45 markl 027 Comment header updates. 028 029 Revision 1.8 2004/03/10 00:48:01 markl 030 added getObject() / putObject() 031 032 Revision 1.7 2003/01/19 09:42:38 markl 033 Javadoc & comment header updates. 034 035 Revision 1.6 2002/08/11 09:49:44 markl 036 Typo fix. 037 038 Revision 1.5 2001/10/31 23:08:05 markl 039 Added new constructor. 040 041 Revision 1.4 2001/03/12 02:57:38 markl 042 Source code cleanup. 043 044 Revision 1.3 1999/04/19 05:31:25 markl 045 Added support for Fonts. 046 047 Revision 1.2 1999/01/10 03:41:46 markl 048 added GPL header & RCS tag 049 ---------------------------------------------------------------------------- 050*/ 051 052package kiwi.util; 053 054import java.awt.Color; 055import java.awt.Font; 056import java.beans.*; 057import java.io.*; 058import java.util.*; 059import javax.swing.event.*; 060 061import kiwi.event.*; 062import kiwi.text.*; 063 064/** Configuration object. This class extends 065 * <code>Properties</code>, adding convenience methods for storing and 066 * retrieving properties as strings, integers, booleans, and 067 * <code>Color</code>s. All values are stored internally as strings, so that 068 * persisting the object will produce a human-readable and 069 * -modifiable file. 070 * <p> 071 * Whenever the contents of the <code>Config</code> object change, a 072 * <code>ChangeEvent</code> is fired. Also, when a specific property in the 073 * object changes, a <code>PropertyChangeEvent</code> is fired. 074 * 075 * @see java.util.Properties 076 * @see kiwi.io.ConfigFile 077 * @see javax.swing.event.ChangeEvent 078 * @see java.beans.PropertyChangeEvent 079 * 080 * @author Mark Lindner 081 */ 082 083public class Config extends Properties implements PropertyChangeSource 084 { 085 private static final String DEFAULT_DESCRIPTION = "Configuration Parameters"; 086 /** The description for this set of configuration parameters. */ 087 protected String description = DEFAULT_DESCRIPTION; 088 /** The support object for firing <code>ChangeEvent</code>s when the object 089 * changes. 090 */ 091 protected ChangeSupport support; 092 /** The support object for firing <coode>PropertyChangeEvent</code>s when 093 * a property changes. 094 */ 095 protected PropertyChangeSupport psupport; 096 097 /** Construct a new <code>Config</code> with a default description. 098 */ 099 100 public Config() 101 { 102 this(DEFAULT_DESCRIPTION); 103 } 104 105 /** Construct a new <code>Config</code> object. 106 * 107 * @param description The description of the configuration parameters that 108 * will be stored in this object (one line of text). 109 */ 110 111 public Config(String description) 112 { 113 this.description = description; 114 support = new ChangeSupport(this); 115 psupport = new PropertyChangeSupport(this); 116 } 117 118 /** Construct a new <code>Config</code> object from a <code>Properties</code> 119 * list. 120 * 121 * @since Kiwi 1.3.2 122 */ 123 124 public Config(Properties properties, String description) 125 { 126 this(description); 127 128 Enumeration e = properties.propertyNames(); 129 while(e.hasMoreElements()) 130 { 131 String key = (String)e.nextElement(); 132 put(key, properties.get(key)); 133 } 134 } 135 136 /** Get the description for this set of configuration parameters. 137 * 138 * @return The description. 139 * @see #setDescription 140 */ 141 142 public String getDescription() 143 { 144 return(description); 145 } 146 147 /** Set the description for this set of configuration parameters. 148 * 149 * @param description The new description, or <code>null</code> if a default 150 * description should be used. 151 * @see #getDescription 152 */ 153 154 public void setDescription(String description) 155 { 156 this.description = (description == null ? DEFAULT_DESCRIPTION 157 : description); 158 } 159 160 /** Look up a <code>String</code> property. 161 * 162 * @param key The name of the property. 163 * @return The property's value, as a <code>String</code>, or 164 * <code>null</code> if a property with the specified name does not exist. 165 * @see #putString 166 */ 167 168 public String getString(String key) 169 { 170 return((String)get(key)); 171 } 172 173 /** Look up a <code>String</code> property. 174 * 175 * @param key The name of the property. 176 * @param defaultValue The default value to return. 177 * @return The property's value, as a <code>String</code>, or 178 * <code>defaultValue</code> if a property with the specified name does not 179 * exist. 180 * @see #putString 181 */ 182 183 public String getString(String key, String defaultValue) 184 { 185 String s = (String)get((Object)key); 186 if(s == null) s = defaultValue; 187 188 return(s); 189 } 190 191 /** Store a <code>String</code> property. 192 * 193 * @param key The name of the property. 194 * @param value The value of the property. 195 * @return The old value associated with this key, or <code>null</code> if 196 * there was no previous value. 197 * @see #getString 198 */ 199 200 public String putString(String key, String value) 201 { 202 String old = getString(key); 203 put(key, value); 204 205 return(old); 206 } 207 208 /** Look up an integer property. 209 * 210 * @param key The name of the property. 211 * @return The property's value, as an <code>int</code>, or <code>0</code> 212 * if a property with the specified name does not exist. 213 * @see #putInt 214 */ 215 216 public int getInt(String key) 217 { 218 return(getInt(key, 0)); 219 } 220 221 /** Look up an integer property. 222 * 223 * @param key The name of the property. 224 * @param defaultValue The default value to return. 225 * @return The property's value, as an <code>String</code>, or 226 * <code>defaultValue</code> if a property with the specified name does not 227 * exist. 228 * @see #putInt 229 */ 230 231 public int getInt(String key, int defaultValue) 232 { 233 String s = (String)get(key); 234 if(s == null) return(defaultValue); 235 236 return(Integer.parseInt(s)); 237 } 238 239 /** Store an integer property. 240 * 241 * @param key The name of the property. 242 * @param value The value of the property. 243 * @return The old value associated with this key, or 0 if there 244 * was no previous value. 245 * @see #getInt 246 */ 247 248 public int putInt(String key, int value) 249 { 250 int old = getInt(key); 251 String s = String.valueOf(value); 252 put(key, s); 253 254 return(old); 255 } 256 257 /** Look up an boolean property. 258 * 259 * @param key The name of the property. 260 * @return The property's value, as a <code>boolean</code>. Returns 261 * <code>false</code> if a property with the specified name does not exist. 262 * @see #putBoolean 263 */ 264 265 public boolean getBoolean(String key) 266 { 267 return(getBoolean(key, false)); 268 } 269 270 /** Look up a boolean property. 271 * 272 * @param key The name of the property. 273 * @param defaultValue The default value to return. 274 * @return The property's value, as a <code>boolean</code>, or 275 * <code>defaultValue</code> if a property with the specified name does not 276 * exist. 277 * @see #putBoolean 278 */ 279 280 public boolean getBoolean(String key, boolean defaultValue) 281 { 282 String s = (String)get(key); 283 if(s == null) return(defaultValue); 284 285 return(Boolean.valueOf(s).booleanValue()); 286 } 287 288 /** Store a boolean property. 289 * 290 * @param key The name of the property. 291 * @param value The value of the property. 292 * @return The old value associated with this key, or <code>false</code> if 293 * there was no previous value. 294 * @see #getBoolean 295 */ 296 297 public boolean putBoolean(String key, boolean value) 298 { 299 boolean old = getBoolean(key); 300 String s = String.valueOf(value); 301 put(key, s); 302 303 return(old); 304 } 305 306 /** Look up a <code>Color</code> property. 307 * 308 * @param key The name of the property. 309 * 310 * @return The property's value, as a <code>Color</code>. Returns 311 * <code>null</code> if a property with the specified name does not exist, 312 * or is not a properly formatted color specification. 313 * @see #putColor 314 */ 315 316 public Color getColor(String key) 317 { 318 return(getColor(key, null)); 319 } 320 321 /** Look up a <code>Color</code> property. 322 * 323 * @param key The name of the property. 324 * @param defaultValue The default value to return. 325 * @return The property's value, as a <code>Color</code>, or 326 * <code>defaultValue</code> if a property with the specified name does not 327 * exist. 328 * @see #putColor 329 */ 330 331 public Color getColor(String key, Color defaultValue) 332 { 333 String s = (String)get(key); 334 if(s == null) return(defaultValue); 335 336 try 337 { 338 return(ColorFormatter.parse(s)); 339 } 340 catch(ParsingException ex) 341 { 342 return(null); 343 } 344 } 345 346 /** Store a <code>Color</code> property. 347 * 348 * @param key The name of the property. 349 * @param value The value of the property. 350 * @return The old value associated with this key, or <code>null</code> if 351 * there was no previous value. 352 * @see #getColor 353 */ 354 355 public Color putColor(String key, Color value) 356 { 357 Color old = getColor(key); 358 String s = ColorFormatter.format(value); 359 put(key, s); 360 361 return(old); 362 } 363 364 /** Look up a <code>Font</code> property. 365 * 366 * @param key The name of the property. 367 * 368 * @return The property's value, as a <code>Font</code>. Returns 369 * <code>null</code> if a property with the specified name does not exist, 370 * or is not a properly formatted font specification. 371 * @see #putFont 372 */ 373 374 public Font getFont(String key) 375 { 376 return(getFont(key, null)); 377 } 378 379 /** Look up a <code>Font</code> property. 380 * 381 * @param key The name of the property. 382 * @param defaultValue The default value to return. 383 * @return The property's value, as a <code>Font</code>, or 384 * <code>defaultValue</code> if a property with the specified name does not 385 * exist. 386 * @see #putFont 387 */ 388 389 public Font getFont(String key, Font defaultValue) 390 { 391 String s = (String)get(key); 392 if(s == null) return(defaultValue); 393 394 try 395 { 396 return(FontFormatter.parse(s)); 397 } 398 catch(ParsingException ex) 399 { 400 return(null); 401 } 402 } 403 404 /** Store a <code>Font</code> property. 405 * 406 * @param key The name of the property. 407 * @param value The value of the property. 408 * @return The old value associated with this key, or <code>null</code> if 409 * there was no previous value. 410 * @see #getFont 411 */ 412 413 public Font putFont(String key, Font value) 414 { 415 Font old = getFont(key); 416 String s = FontFormatter.format(value); 417 put(key, s); 418 419 return(old); 420 } 421 422 /** Store an arbitrary object property. 423 * 424 * @param key The name of the property. 425 * @param value The value of the property. 426 * @return The old value associated with this key, or <code>null</code> if 427 * there was no previous value. 428 * @see #getObject 429 * @since Kiwi 2.0 430 */ 431 432 public Object putObject(String key, Object value) 433 { 434 Object old = get(key); 435 put(key, value); 436 437 return(old); 438 } 439 440 /** Look up an arbitrary object property. 441 * 442 * @param key The name of the property. 443 * @param defaultValue The default value to return. 444 * @return The property's value, or <code>defaultValue</code> if a 445 * property with the specified name does not exist. 446 * @see #putObject 447 * @since Kiwi 2.0 448 */ 449 450 public Object getObject(String key, Object defaultValue) 451 { 452 Object o = get(key); 453 if(o == null) return(defaultValue); 454 455 return(o); 456 } 457 458 /** Store an arbitrary property. 459 * 460 * @param key The object that identifies the property. 461 * @param value The value of the property. 462 * @return The old value associated with this key, or <code>null</code> if 463 * there was no previous value. 464 */ 465 466 public Object put(Object key, Object value) 467 { 468 Object o = super.put(key, value); 469 support.fireChangeEvent(); 470 psupport.firePropertyChange(key.toString(), o, value); 471 472 return(o); 473 } 474 475 /** Remove a property. Removes the property for the given key. 476 * 477 * @param key The object that identifies the property. 478 * @return The value associated with this key, or <code>null</code> if there 479 * was no property with the given key in this object. 480 * @see #clear 481 */ 482 483 public Object remove(Object key) 484 { 485 Object o = super.remove(key); 486 if(o != null) 487 { 488 support.fireChangeEvent(); 489 psupport.firePropertyChange(key.toString(), o, null); 490 } 491 492 return(o); 493 } 494 495 /** Remove all properties. Removes all properties from this object. 496 */ 497 498 public void clear() 499 { 500 if(size() > 0) 501 { 502 super.clear(); 503 support.fireChangeEvent(); 504 } 505 } 506 507 /** Get a list of properties. 508 * 509 * @return A list of the property names as an <code>Enumeration</code>. 510 */ 511 512 public Enumeration list() 513 { 514 return(keys()); 515 } 516 517 /** Add a <code>ChangeListener</code> to this object's list of listeners. 518 * 519 * @param listener The listener to add. 520 */ 521 522 public void addChangeListener(ChangeListener listener) 523 { 524 support.addChangeListener(listener); 525 } 526 527 /** Remove a <code>ChangeListener</code> from this object's list of 528 * listeners. 529 * 530 * @param listener The listener to remove. 531 */ 532 533 public void removeChangeListener(ChangeListener listener) 534 { 535 support.removeChangeListener(listener); 536 } 537 538 /** Add a <code>PropertyChangeListener</code> to this object's list of 539 * listeners. 540 * 541 * @since Kiwi 1.3 542 * 543 * @param listener The listener to add. 544 */ 545 546 public void addPropertyChangeListener(PropertyChangeListener listener) 547 { 548 psupport.addPropertyChangeListener(listener); 549 } 550 551 /** Remove a <code>PropertyChangeListener</code> from this object's list of 552 * listeners. 553 * 554 * @since Kiwi 1.3 555 * 556 * @param listener The listener to remove. 557 */ 558 559 public void removePropertyChangeListener(PropertyChangeListener listener) 560 { 561 psupport.removePropertyChangeListener(listener); 562 } 563 564 } 565 566/* end of source file */