Package org.opentrafficsim.swing.gui
Class PropertiesStore
java.lang.Object
org.opentrafficsim.swing.gui.PropertiesStore
Class that can be used within a program to load and save properties. This class adheres to the XDG Base Directory
Specification regarding where setting files are stored.
Copyright (c) 2026-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionPropertiesStore(Properties properties, String context, String description) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd value to list.voidclearProperty(String key) Removes key from the store.contextKey(String key, Object subContext) Returns a key specific for the sub-context.getBoolean(String key) Returns boolean for given key.Returns color of given key.getColorOrDefault(String key, Color defaultValue) Returns the property value, or the provided default if there is no value mapped to the key.getInteger(String key) Returns int for given key.getIntegerOrDefault(String key, boolean defaultValue) Returns the property value, or the provided default if there is no value mapped to the key.getIntegerOrDefault(String key, int defaultValue) Returns the property value, or the provided default if there is no value mapped to the key.Returns list property.getOptionalBoolean(String key) Returns boolean that might not be given for given key.getOptionalColor(String key) Returns color that might not be given for given key.getOptionalInteger(String key) Returns int that might not be given for given key.Returns property that might not be given.getProperty(String key) Returns the property value.getPropertyOrDefault(String key, String defaultValue) Returns the property value, or the provided default if there is no value mapped to the key.static StringReturns a key that complies to upper/lower case convention.voidremoveFromList(String key, String value) Remove value from list.voidsave()Saves properties.voidsetBoolean(String key, boolean value) Set boolean value.voidSet color.voidSet int value.voidsetMaxSubContexts(int maxSubContexts) Sets the maximum number of sub-contexts.voidsetProperty(String key, String value) Sets a property value.voidsetProperty(String key, String value, boolean save) Sets a property value.static ColorstringToColor(String colorString) Returns color from string.static StringvalueToString(boolean value) Converts boolean to String.static StringvalueToString(int value) Converts int to String.static StringvalueToString(Color color) Returns string from color.
-
Constructor Details
-
PropertiesStore
Constructor. To populate the defaultPropertiesuse the various staticvalueToStringmethods.- Parameters:
properties- properties pre-loaded with defaultscontext- context of the properties, e.g."appearance"or"editor"description- description of the properties, which is saved as a comment in the file that stores the properties
-
-
Method Details
-
setMaxSubContexts
public void setMaxSubContexts(int maxSubContexts) Sets the maximum number of sub-contexts. The default value is 50.- Parameters:
maxSubContexts- maximum number of sub-contexts
-
save
public void save()Saves properties. -
getProperty
Returns the property value. If the program has not saved any value, a default value should have been given via the input properties.- Parameters:
key- key- Returns:
- property value
-
getOptionalProperty
Returns property that might not be given.- Parameters:
key- key- Returns:
- property that might not be given
-
getPropertyOrDefault
Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.- Parameters:
key- keydefaultValue- default value- Returns:
- property value, or the provided default if there is no value mapped to the key
-
setProperty
Sets a property value.- Parameters:
key- keyvalue- valuesave- whether to save the properties (typically yes, but only on last if multiple properties are set)
-
setProperty
Sets a property value.- Parameters:
key- keyvalue- value
-
clearProperty
Removes key from the store.- Parameters:
key- key
-
key
Returns a key that complies to upper/lower case convention.- Parameters:
key- key- Returns:
- key that complies to upper/lower case convention
-
contextKey
Returns a key specific for the sub-context. The resulting key iscontext.{hashCode}.{key}, using the hash code of the sub-context. This method also deals with limiting the number of saved sub-contexts.- Parameters:
key- keysubContext- sub-context- Returns:
- contextual key
-
getList
Returns list property.- Parameters:
key- key under which list is stored- Returns:
- list (recent to old)
-
addToList
Add value to list. If the value is already in the list, it is moved to the front. If the list does not exist it will be created. The resulting list is saved.- Parameters:
key- key under which list is storedvalue- value to add to the listmaxNumber- maximum number of elements in the list- Throws:
IllegalArgumentException- when the value contains a '|'
-
removeFromList
Remove value from list. The resulting list is saved.- Parameters:
key- keyvalue- value
-
getColor
Returns color of given key.- Parameters:
key- key- Returns:
- color
-
getOptionalColor
Returns color that might not be given for given key.- Parameters:
key- key- Returns:
- color that might not be given
-
getColorOrDefault
Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.- Parameters:
key- keydefaultValue- default value- Returns:
- property value, or the provided default if there is no value mapped to the key
-
stringToColor
Returns color from string.- Parameters:
colorString- color as string- Returns:
- color
-
setColor
Set color.- Parameters:
key- keycolor- color
-
valueToString
Returns string from color.- Parameters:
color- color- Returns:
- string from color
-
getInteger
Returns int for given key.- Parameters:
key- key- Returns:
- int
-
getOptionalInteger
Returns int that might not be given for given key.- Parameters:
key- key- Returns:
- int that might not be given
-
getIntegerOrDefault
Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.- Parameters:
key- keydefaultValue- default value- Returns:
- property value, or the provided default if there is no value mapped to the key
-
setInt
Set int value.- Parameters:
key- keyvalue- value
-
valueToString
Converts int to String.- Parameters:
value- value- Returns:
- string
-
getBoolean
Returns boolean for given key.- Parameters:
key- key- Returns:
- boolean
-
getOptionalBoolean
Returns boolean that might not be given for given key.- Parameters:
key- key- Returns:
- boolean that might not be given
-
getIntegerOrDefault
Returns the property value, or the provided default if there is no value mapped to the key. In the latter case, the default value will be stored as the property value.- Parameters:
key- keydefaultValue- default value- Returns:
- property value, or the provided default if there is no value mapped to the key
-
setBoolean
Set boolean value.- Parameters:
key- keyvalue- value
-
valueToString
Converts boolean to String.- Parameters:
value- value- Returns:
- string
-