Allow to register a converter for actions

List for Users of Carlsbad Cubes' Technologies and Products <[email protected]> Sat, 29 Oct 2005 18:52:48 +0200
Newsgroups gmane.comp.embedded.carlsbad-cubes
Organization Ubik Products
Message-ID <[email protected]>
This is a multi-part message in MIME format.
--------------030701030008050801090606
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

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) 


--------------030701030008050801090606
Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0";
	name="Parser.java.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Parser.java.diff"

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.

--------------030701030008050801090606
Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0";
	name="AbstractActionManager.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="AbstractActionManager.java"

/**
 * 
 */
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;
    }

}

--------------030701030008050801090606
Content-Type: text/plain; x-mac-type="0"; x-mac-creator="0";
	name="AndroMDAModuleActionManager.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="AndroMDAModuleActionManager.java"

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));
    }
    
}

--------------030701030008050801090606
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Forum mailing list
[email protected]
http://carlsbadcubes.com/mailman/listinfo/forum_carlsbadcubes.com

--------------030701030008050801090606--