-
Property Sets
I couldn't find any standard way to load property sets (groups of properties which can then be referenced as a single unit) so implemented one.
Usage:
Code:
<bean id="defaultProperties"
class="ui.PropertySet">
<property name="properties">
<map>
<entry key="property1">
<ref bean="valueOne"/>
</entry>
<entry key="property2">
<ref bean="valueTwo"/>
</entry>
</map>
</property>
</bean>
<bean id="machineView"
class="ui.PropertySetViewDescriptor">
<property name="viewClass">
<value>ui.view.MachineView</value>
</property>
<property name="propertySet">
<ref bean="defaultProperties"/>
</property>
</bean>
<bean id="lotView"
class="ui.PropertySetViewDescriptor">
<property name="viewClass">
<value>ui.view.LotView</value>
</property>
<property>
<map>
<entry key="propertySet">
<ref bean="defaultProperties"/>
</entry>
<entry key="property3">
<ref bean="propertyThree"/>
</entry>
</map>
</property>
</bean>
You can currently nest property sets, though I don't see much of a use case for it w/out a well-defined ordering of precedence, which there currently is not.
The change was fairly minor, but did require a modification to DefaultViewDescriptor; specifically, I extracted a setPropertyValues method from createView in order to get a hook before ((InitializingBean)view).afterPropertiesSet() was called.
If there is an already-established way of grouping properties, I'd very much appreciate hearing about it. If not and anyone else needs this behavior...
(I would've posted attachments, but didn't see the option)
Code:
public class PropertySet {
private Map _properties;
public void setProperties( Map properties ) {
_properties = properties;
}
public Map getProperties() {
return _properties;
}
}
public class PropertySetViewDescriptor extends DefaultViewDescriptor {
private PropertySet _propertySet;
public void setPropertySet( PropertySet propertySet ) {
_propertySet = propertySet;
}
protected void setPropertyValues( View view, Map properties ) {
BeanWrapper wrapper = null;
if( properties != null ) {
wrapper = new BeanWrapperImpl( view );
setProperties( wrapper, properties );
}
if( _propertySet != null ) {
if( wrapper == null )
wrapper = new BeanWrapperImpl( view );
setProperties( wrapper, _propertySet );
}
}
private void setProperties( BeanWrapper wrapper, Map properties) {
for( Iterator keys = properties.keySet().iterator(); keys.hasNext(); ) {
String key = (String) keys.next();
Object value = properties.get( key );
if( value instanceof PropertySet )
setProperties( wrapper, (PropertySet) value );
else
wrapper.setPropertyValue( key, value );
}
}
private void setProperties( BeanWrapper wrapper, PropertySet set ) {
Map properties = set.getProperties();
if( properties != null ) {
setProperties( wrapper, properties );
}
}
}
Index: DefaultViewDescriptor.java
===================================================================
RCS file: /cvsroot/spring-rich-c/spring-richclient/src/org/springframework/richclient/application/support/DefaultViewDescriptor.java,v
retrieving revision 1.9
diff -u -r1.9 DefaultViewDescriptor.java
--- DefaultViewDescriptor.java 12 Nov 2004 02:36:53 -0000 1.9
+++ DefaultViewDescriptor.java 18 Dec 2004 16:32:34 -0000
@@ -97,10 +97,7 @@
}
getApplicationEventMulticaster().addApplicationListener((ApplicationListener)view);
}
- if (viewProperties != null) {
- BeanWrapper wrapper = new BeanWrapperImpl(view);
- wrapper.setPropertyValues(viewProperties);
- }
+ setProperties( view );
if (view instanceof InitializingBean) {
try {
@@ -113,6 +110,13 @@
return view;
}
+ private void setProperties( View view ) {
+ if (viewProperties != null) {
+ BeanWrapper wrapper = new BeanWrapperImpl(view);
+ wrapper.setPropertyValues(viewProperties);
+ }
+ }
+
public CommandButtonLabelInfo getShowViewCommandLabel() {
return getLabel();
}
-
I can propose the next way to do almost the same
Code:
<bean name="properties" class="java.util.HashMap">
<constructor-arg index="0">
<map>
<entry key="a">
<value>A</value>
</entry>
<entry key="b">
<value>B</value>
</entry>
</map>
</constructor-arg>
</bean>
...
...
<property="viewProperties">
<ref bean="properties"/>
</property>
...
Then if you override standard HashMap you are get all you need. I imply the next functionality:
Code:
MyMap map = new MyMap(otherMap);
map.setExtraProperties(thirdMap);