You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Dmitry Beransky <db...@dembel.org> on 2006/12/05 18:07:48 UTC

[digester] converting resource uri to an object

Hi,

Could you guys tell me if I'm doing this right?..

for the following fragment:

<menu>
   <item>
      <action class="csaswing.action.MoveToFirstRowAction">
         <icon>/resources/pics/top.png</icon>
      </action>
   </item>
</menu>

I need to end up with roughly this sequence:

1. JMenu menu = new JMenu();
2. JMenuItem item = new JMenuItem();
3. Action action = new MoveToFirstRowAction();
4. Icon icon = new ImageIcon(getClass().getResource("/resources/pics/top.png"));
5. action.putValue(Action.SMALL_ICON, icon);
6. item.setAction(action);
7. menu.add(item);

and here's how I've implemented it:

   digester.addObjectCreate("*/menu/item", "javax.swing.JMenuItem", "class");
   digester.addObjectCreate("*/menu/item/action", null, "class");

   digester.addCallMethod("*/menu/item/action/title", "putValue", 2,
new Class[] {String.class, Object.class});
   digester.addObjectParam("*/menu/item/action/title", 0, Action.NAME);
   digester.addCallParam("*/menu/item/action/title", 1);
   digester.addRule("*/menu/item/action/icon", new IconRule());

   digester.addSetNext("*/menu/item/action", "setAction");

where IconRule is:

class IconRule extends Rule {
     Icon icon;

     public void body(String ns, String name, String text) throws Exception {
         icon = new ImageIcon(MenuBuilder.class.getResource(text));
     }

     public void end(String string, String string1) throws Exception {
         ((Action)getDigester().peek()).putValue(Action.SMALL_ICON, icon);
     }
}

My custom icon rule bugs me a little bit, but I'm not sure why.  Is
there a better, cleaner way of implementing this whole thing?

Thanks
Dmitry

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org