forum  

[Forum] Allow to register a converter for actions

List for Users of Carlsbad Cubes' Technologies and Products
Sun, 30 Oct 2005 08:52:16 -0800

Hi all,

I had done a little change in Parser.applyAttributes(Object, Factory, List) and i would like to know if it is safe and if potentially it could be included in the source pool eventually. The purpose is to allow to register Converters for Action and so allow us to inject any action which match the value of the action attribute from the converter. For instance i have (but perhaps this is a bad design) a ActionManager into which i register actions by name (this is a sort of HashMap) and that i use to fetch actions when the parser encounter them. This allow to not be mandatory to declare the actions as public fields in the client object of the SwingEngine. Attached to this mail are the patch and an example converter. To use it, i add the following lines before parsing the xml ui descriptor:
...
               swingEngine = new SwingEngine(this);
               actionManager = new AndroMDAModuleActionManager( this );
               ConverterLibrary.getInstance().register(Action.class,
                       actionManager);
               URL uiDef = ClassLoader.getSystemResource(UI_DESCRIPTOR);
               File f = new File(uiDef.getFile());
               swingEngine.render(f);
...
(i work on a module for argouml which integrate andromda into it - this explain the name of the classes)
Thanks for any feedback,
Best regards,

--
Cordialement,
Ludo - http://www.ubik-products.com
---
"L'amour pour principe et l'ordre pour base; le progres pour but" (A.Comte)
Index: Parser.java
===================================================================
RCS file: /cvs/swixml/src/org/swixml/Parser.java,v
retrieving revision 1.5
diff -u -r1.5 Parser.java
--- Parser.java 22 Aug 2005 21:20:02 -0000      1.5
+++ Parser.java 29 Oct 2005 16:43:15 -0000
@@ -677,18 +677,22 @@
             //  a getClass().getFields lookup has to be done to find the 
correct fields.
             //
             if (Action.class.equals(paraType)) {
-              para = 
engine.getClient().getClass().getField(attr.getValue()).get(engine.getClient());
-              action = (Action) para;
+                try {
+                    para = 
engine.getClient().getClass().getField(attr.getValue()).get(engine.getClient());
+                    action = (Action) para;                    
+                } catch (NoSuchFieldException e) {
+                    para = converter.convert(paraType, attr, 
engine.getLocalizer());
+                    action = (Action) para;
+                    if (para == null && SwingEngine.DEBUG_MODE) {
+                        System.err.println("Action '" + attr.getValue() + "' 
not set. Public Action '" + attr.getValue() + "' not found in " + 
engine.getClient().getClass().getName());
+                    }                    
+                }
             } else {
               para = converter.convert(paraType, attr, engine.getLocalizer());
             }
 
             method.invoke(obj, new Object[]{para}); // ATTR SET
 
-          } catch (NoSuchFieldException e) {
-            if (SwingEngine.DEBUG_MODE) {
-              System.err.println("Action '" + attr.getValue() + "' not set. 
Public Action '" + attr.getValue() + "' not found in " + 
engine.getClient().getClass().getName());
-            }
           } catch (InvocationTargetException e) {
             //
             // The JFrame class is slightly incompatible with Frame.
/**
 * 
 */
package org.argouml.modules.andromda.ui.actions;

import java.util.HashMap;
import java.util.Map;

import javax.swing.Action;

import org.apache.log4j.Logger;
import org.jdom.Attribute;
import org.swixml.Converter;
import org.swixml.Localizer;

/**
 * @author lmaitre
 *
 */
public class AbstractActionManager implements ActionManager, Converter {

    private Logger LOG = Logger.getLogger(AbstractActionManager.class);
    
    protected Map actionsByName;
    
    /**
     * 
     */
    public AbstractActionManager() {
        super();
        actionsByName = new HashMap();
    }

    public void addAction(String id, Action action) {
        //
        actionsByName.put(id,action);
    }
    
    public void removeAction(String id) {
        //
        actionsByName.remove(id);
    }
    
    /**
     * @see 
org.argouml.modules.andromda.ui.actions.ActionManager#getAction(java.lang.String)
     */
    public Action getAction(String actionName) {
        return (Action) actionsByName.get(actionName);
    }

    /**
     * @see org.swixml.Converter#convert(java.lang.Class, org.jdom.Attribute, 
org.swixml.Localizer)
     */
    public Object convert(Class arg0, Attribute arg1, Localizer arg2) throws 
Exception {
        LOG.info("Return action for 
"+arg1.getValue()+":"+getAction(arg1.getValue()));
        return getAction(arg1.getValue());
    }

    /**
     * @see org.swixml.Converter#convertsTo()
     */
    public Class convertsTo() {
        return Action.class;
    }

}
package org.argouml.modules.andromda.ui.actions;

import org.argouml.modules.andromda.ui.container.ModuleContainer;

public class AndroMDAModuleActionManager extends AbstractActionManager 
implements ActionManager {

    private ModuleContainer parent;
    
    public AndroMDAModuleActionManager(ModuleContainer p) {
        super();
        parent = p;
        addAction("launchMavenAction",new ActionLaunchAndroMDA(parent));
        addAction("createProjectAction",new 
ActionCreateProjectAndroMDA(parent));
    }
    
}
_______________________________________________
Forum mailing list
Forum@carlsbadcubes.com
http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com