Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The reason why I prefer this one is because it is generic and can be reused for confirmation dialogs. You want to open some sort of dialog before doing any action, just chain it.

Prototype ChainedAction

 

 

Code Block
languagejava
titleChainedAction Prototype
linenumberstrue
public class ChainedAction<D extends ChainedActionDefinition> extends AbstractAction<ChainedActionDefinition> implements ChainedActionCallback {
    private final Item item;
    private final Context context;
    private final ComponentProvider componentProvider;
    private Iterator<Action> it;
    public ChainedAction(D definition, final Item item, ComponentProvider componentProvider) {
        super(definition);
        this.item = item;
        this.componentProvider = componentProvider;
        this.context = new SimpleContext();
    }
    private void prepareExecution() throws ActionExecutionException {
        Collection<Action> actions = new LinkedList<Action>();
        for (ActionDefinition actionDefinition : getDefinition().getActions()) {
            Action action = createAction(actionDefinition, item, context, this);
            actions.add(action);
        }
        this.it = actions.iterator();
    }
    @Override
    public void execute() throws ActionExecutionException {
        prepareExecution();
        executeNext();
    }
    private void executeNext() throws ActionExecutionException {
        while(it.hasNext()) {
            Action action = it.next();
            action.execute();
            if (action instanceof DeferredAction) {
                break;
            }
        }
    }

    private Action createAction(ActionDefinition actionDefinition, Object... args) throws ActionExecutionException {
        Class<? extends Action> implementationClass = actionDefinition.getImplementationClass();
        if (implementationClass == null) {
            throw new ActionExecutionException("No action class set for action: " + actionDefinition.getName());
        }
        Object[] combinedParameters = new Object[args.length + 1];
        combinedParameters[0] = actionDefinition;
        System.arraycopy(args, 0, combinedParameters, 1, args.length);
        try {
            return componentProvider.newInstance(implementationClass, combinedParameters);
        } catch (MgnlInstantiationException e) {
            throw new ActionExecutionException("Could not instantiate action class for action: " + actionDefinition.getName(), e);
        }
    }

    @Override
    public void proceed() {
        try {
            executeNext();
        } catch (ActionExecutionException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    @Override
    public void abort() {
    }
}

 

Action Composition

Meaning building more complex code and the current Action API is not flexible enough to do it the right way IMO.