0

I use xml for persisting data about UI elements. For example, I have a tag that stands for JButton, - for JMenuItem and I handle them with the help of Java SAX classes. But I want to build my UI element using Action like this:

Then I'll handle such tag and build what I want. But I have a problem with passing different amount of options to this Action object. In one case I want to pass name like:

putValue(NAME, "Element name");

In another case it will be:

putValue(NAME, "Some name");
putValue(SHORT_DESCRIPTION, "Tooltip");
putValue(MNEMONIC_KEY, new Integer('A'));

In a code it will look like:

class NewAction extends AbstractAtion {
    public NewAction() {
            putValue(NAME, "Some name");
            putValue(SHORT_DESCRIPTION, "Tooltip");
            putValue(MNEMONIC_KEY, new Integer('A'));
    }
}

But how to do it with xml? Are there any ideas on how to store and handle different amount of Action key:value args. in xml?

4

1 回答 1

0

May be this ?

<action type="NewAction">
    <property>
        <name>NAME</name>
        <value>Some name</value>      
    </property>
    <property>
        <name>SHORT_DESCRIPTION</name>
        <value>Tooltip</value>      
    </property>
    ...
</action>

The action could be instantiated via reflection. However its works only if the action have no any dependencies. If it become too complex may be you could use spring.

于 2012-09-13T12:16:13.367 回答