DelegatingActionProvider.java :  » Workflow-Engines » osbl-1_0 » org » osbl » client » action » Java Open Source

Java Open Source » Workflow Engines » osbl 1_0 
osbl 1_0 » org » osbl » client » action » DelegatingActionProvider.java
package org.osbl.client.action;

import javax.swing.*;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.HashMap;


public abstract class DelegatingActionProvider
    implements ActionProvider
{
    protected ActionProvider parent;
    Map<String,Action> cache = new HashMap<String, Action>();

    protected DelegatingActionProvider(ActionProvider parent) {
        this.parent = parent;
    }

    public Action getAction(String command) {
        Action action = cache.get(command);
        if (action == null) {
            action = parent.getAction(command);
            if (action == null)
                throw new IllegalArgumentException("No action for command " + command);
            action = clone(action);
            cache.put(command, action);
            configure(action);
        }
        return action;
    }

    public static Action clone(Action action) {
        try {
            Method clone = action.getClass().getMethod("clone");
            return (Action)clone.invoke(action);
        }
        catch (Exception e) {
            //e.printStackTrace();
            throw new RuntimeException("Action is not clonable");
        }

        /*
        if (action instanceof NavigationAction) {
            NavigationAction cloneable = (NavigationAction)action;
            return (Action)cloneable.clone();
        }
        else
            throw new RuntimeException("action is not an instance of " + NavigationAction.class.getName());
        */
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.