Package se.datadosen.util
Class BeanBinder
java.lang.Object
se.datadosen.util.BeanBinder
public class BeanBinder
extends java.lang.Object
Utility class for synchronizing a Swing GUI with JavaBean properties. Also
synchronizes a bean with a property file or command line parameters.
Synchronization is performed when calling getProperties() and
setProperties(). Matching is done where the name of a property corresponds to
the name of a GUI element.
Copyright (c) 2005, David Ekholm Datadosen
Copyright (c) 2005, David Ekholm Datadosen
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
BeanBinder.Bindeable
Support for other component types -
Constructor Summary
Constructors Constructor Description BeanBinder()
-
Method Summary
Modifier and Type Method Description void
addBinderListener(BinderListener listener)
void
addUI(java.awt.Container ui)
Add user interface to participate in readModel and writeModel callsvoid
addUI(java.awt.Container... newUIs)
Add user interface to participate in readModel and writeModel callsstatic java.lang.Object
cloneProperties(java.lang.Object fromBean, java.lang.Object toBean)
static void
debug(java.lang.Object bean, java.lang.Object jComponentHolder)
protected void
fireModelRead()
protected void
fireWriteModel()
static java.util.Map<java.lang.String,java.lang.Object>
getMap(java.lang.Object bean)
Return an unprocessed String to Object Map of this bean Static methods are ignoredstatic java.util.Properties
getProperties(java.lang.Object bean)
Return a java.util.Property object representing the state of a JavaBeanstatic java.util.Properties
getProperties(java.lang.Object bean, boolean nullProperties)
Return a java.util.Property object representing the state of a JavaBeanstatic void
getProperties(java.lang.Object bean, java.awt.Container jComponentHolder)
Update a Swing GUI with the properties from a JavaBean.static void
getProperties(java.lang.Object bean, java.util.List<java.awt.Container> holders)
static java.util.Properties
getProperties(java.lang.Object bean, java.util.Properties existing)
Return a java.util.Property object representing the state of a JavaBeanstatic boolean
isSynchingUI()
Return true if we're currently synching a UI from a bean on the same thread as calling thread (see getProperties(Object bean, Object jComponentHolder)void
onRead(java.lang.Runnable r)
void
onWrite(java.lang.Runnable r)
static java.util.Properties
parseArgsOptions(java.lang.String[] args)
Utility method to generate a Property object from command line parametersvoid
readModel(java.lang.Object bean)
Like getProperties, but non static (uses user interface array from constructor) and fires modelRead to listenersvoid
removeBinderListener(BinderListener listener)
void
removeUI(java.lang.Object ui)
Remove user interface from participating in readModel and writeModel callsstatic void
setProperties(java.lang.Object bean, java.awt.Container jComponentHolder)
Update a JavaBean from a Swing GUI.static void
setProperties(java.lang.Object bean, java.util.List<java.awt.Container> uis)
Update a JavaBean from a Swing GUI.static void
setProperties(java.lang.Object bean, java.util.Map props)
Set JavaBean properties given a Map objectvoid
walkComponents(java.util.function.BiConsumer<java.lang.String,javax.swing.JComponent> visitor)
Visit all accessible UI componentsvoid
writeModel(java.lang.Object bean)
Like setProperties, but non static (uses user interface array from constructor) and fires writeModel to listeners
-
Constructor Details
-
BeanBinder
public BeanBinder()
-
-
Method Details
-
onRead
public void onRead(java.lang.Runnable r) -
onWrite
public void onWrite(java.lang.Runnable r) -
addBinderListener
-
removeBinderListener
-
addUI
public void addUI(java.awt.Container... newUIs)Add user interface to participate in readModel and writeModel calls- Since:
- 13.1
-
addUI
public void addUI(java.awt.Container ui)Add user interface to participate in readModel and writeModel calls- Parameters:
ui
-- Since:
- 13.1
-
removeUI
public void removeUI(java.lang.Object ui)Remove user interface from participating in readModel and writeModel calls- Parameters:
ui
-- Since:
- 13.1
-
fireWriteModel
protected void fireWriteModel() -
fireModelRead
protected void fireModelRead() -
debug
public static void debug(java.lang.Object bean, java.lang.Object jComponentHolder) throws java.lang.Exception- Throws:
java.lang.Exception
-
getProperties
public static void getProperties(java.lang.Object bean, java.util.List<java.awt.Container> holders) throws java.lang.Exception- Throws:
java.lang.Exception
-
readModel
public void readModel(java.lang.Object bean) throws java.lang.ExceptionLike getProperties, but non static (uses user interface array from constructor) and fires modelRead to listeners- Parameters:
bean
-- Throws:
java.lang.Exception
-
writeModel
public void writeModel(java.lang.Object bean)Like setProperties, but non static (uses user interface array from constructor) and fires writeModel to listeners- Parameters:
bean
-
-
walkComponents
public void walkComponents(java.util.function.BiConsumer<java.lang.String,javax.swing.JComponent> visitor)Visit all accessible UI components- Parameters:
visitor
- visitor accepting component name and component- Since:
- 27.1
-
getProperties
public static void getProperties(java.lang.Object bean, java.awt.Container jComponentHolder)Update a Swing GUI with the properties from a JavaBean. Currently supports the following types of GUI elements:- JTextField
- JComboBox
- JCheckBox
- ButtonGroup (radio buttons)
- Parameters:
bean
- JavaBean to read properties fromjComponentHolder
- object with GUI elements (public instances of JComponent objects)
-
-
isSynchingUI
public static boolean isSynchingUI()Return true if we're currently synching a UI from a bean on the same thread as calling thread (see getProperties(Object bean, Object jComponentHolder) -
cloneProperties
public static java.lang.Object cloneProperties(java.lang.Object fromBean, java.lang.Object toBean) throws BeanBinderException- Returns:
- toBean
- Throws:
BeanBinderException
- Since:
- v12
-
getProperties
public static java.util.Properties getProperties(java.lang.Object bean) throws java.lang.ExceptionReturn a java.util.Property object representing the state of a JavaBean- Throws:
java.lang.Exception
-
getMap
public static java.util.Map<java.lang.String,java.lang.Object> getMap(java.lang.Object bean) throws BeanBinderExceptionReturn an unprocessed String to Object Map of this bean Static methods are ignored- Parameters:
bean
-- Returns:
- Throws:
BeanBinderException
- Since:
- 15.2.3
-
getProperties
public static java.util.Properties getProperties(java.lang.Object bean, java.util.Properties existing) throws java.lang.ExceptionReturn a java.util.Property object representing the state of a JavaBean- Parameters:
bean
- JavaBean to read properties fromexisting
- Existing properties object to add properties to- Throws:
java.lang.Exception
-
getProperties
public static java.util.Properties getProperties(java.lang.Object bean, boolean nullProperties) throws java.lang.ExceptionReturn a java.util.Property object representing the state of a JavaBean- Parameters:
bean
- JavaBean to read properties fromnullProperties
- if null-value properties are to be included- Throws:
java.lang.Exception
-
setProperties
public static void setProperties(java.lang.Object bean, java.util.Map props) throws BeanBinderExceptionSet JavaBean properties given a Map object- Throws:
BeanBinderException
-
setProperties
public static void setProperties(java.lang.Object bean, java.util.List<java.awt.Container> uis)Update a JavaBean from a Swing GUI. See getProperties() for a list of supported GUI elements.- Parameters:
bean
- JavaBean to set properties onuis
- objects with GUI elements (public instances of JComponent objects)
-
setProperties
public static void setProperties(java.lang.Object bean, java.awt.Container jComponentHolder) throws BeanBinderExceptionUpdate a JavaBean from a Swing GUI. See getProperties() for a list of supported GUI elements.- Parameters:
bean
- JavaBean to set properties onjComponentHolder
- object with GUI elements (public instances of JComponent objects)- Throws:
BeanBinderException
-
parseArgsOptions
public static java.util.Properties parseArgsOptions(java.lang.String[] args)Utility method to generate a Property object from command line parameters