Announcement Announcement Module
Collapse
No announcement yet.
Property Sets Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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 &#123;
      private Map _properties;
      
      public void setProperties&#40; Map properties &#41; &#123;
        _properties = properties;
      &#125;
      
      public Map getProperties&#40;&#41; &#123;
        return _properties;
      &#125;
    &#125;
    
    
    public class PropertySetViewDescriptor extends DefaultViewDescriptor &#123;
      private PropertySet _propertySet;
      
      public void setPropertySet&#40; PropertySet propertySet &#41; &#123;
        _propertySet = propertySet;
      &#125;
      
      protected void setPropertyValues&#40; View view, Map properties &#41; &#123;
        BeanWrapper wrapper = null;
        
        if&#40; properties != null &#41; &#123;
          wrapper = new BeanWrapperImpl&#40; view &#41;;
          
          setProperties&#40; wrapper, properties &#41;;
        &#125;
        
        if&#40; _propertySet != null &#41; &#123;
          if&#40; wrapper == null &#41;
            wrapper = new BeanWrapperImpl&#40; view &#41;;
          
          setProperties&#40; wrapper, _propertySet &#41;;
        &#125;
      &#125;
      
      private void setProperties&#40; BeanWrapper wrapper, Map properties&#41; &#123;
        for&#40; Iterator keys = properties.keySet&#40;&#41;.iterator&#40;&#41;; keys.hasNext&#40;&#41;; &#41; &#123;
          String key = &#40;String&#41; keys.next&#40;&#41;;
          Object value = properties.get&#40; key &#41;;
          
          if&#40; value instanceof PropertySet &#41;
            setProperties&#40; wrapper, &#40;PropertySet&#41; value &#41;;
          else
            wrapper.setPropertyValue&#40; key, value &#41;;
        &#125;
      &#125;
      
      private void setProperties&#40; BeanWrapper wrapper, PropertySet set &#41; &#123;
        Map properties = set.getProperties&#40;&#41;;
        
        if&#40; properties != null &#41; &#123;
          setProperties&#40; wrapper, properties &#41;;
        &#125;
      &#125;
    &#125;
    
    
    Index&#58; DefaultViewDescriptor.java
    ===================================================================
    RCS file&#58; /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&#58;36&#58;53 -0000	1.9
    +++ DefaultViewDescriptor.java	18 Dec 2004 16&#58;32&#58;34 -0000
    @@ -97,10 +97,7 @@
                 &#125;
                 getApplicationEventMulticaster&#40;&#41;.addApplicationListener&#40;&#40;ApplicationListener&#41;view&#41;;
             &#125;
    -        if &#40;viewProperties != null&#41; &#123;
    -            BeanWrapper wrapper = new BeanWrapperImpl&#40;view&#41;;
    -            wrapper.setPropertyValues&#40;viewProperties&#41;;
    -        &#125;
    +        setProperties&#40; view &#41;;
     
             if &#40;view instanceof InitializingBean&#41; &#123;
                 try &#123;
    @@ -113,6 +110,13 @@
             return view;
         &#125;
     
    +    private void setProperties&#40; View view &#41; &#123;
    +      if &#40;viewProperties != null&#41; &#123;
    +          BeanWrapper wrapper = new BeanWrapperImpl&#40;view&#41;;
    +          wrapper.setPropertyValues&#40;viewProperties&#41;;
    +      &#125;
    +    &#125;
    +
         public CommandButtonLabelInfo getShowViewCommandLabel&#40;&#41; &#123;
             return getLabel&#40;&#41;;
         &#125;

  • #2
    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&#40;otherMap&#41;;
    map.setExtraProperties&#40;thirdMap&#41;;

    Comment

    Working...
    X