001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions;
003
004import java.awt.event.ActionEvent;
005import java.beans.PropertyChangeListener;
006import java.util.HashMap;
007import java.util.Map;
008
009import javax.swing.Action;
010
011/**
012 * Action wrapper that delegates to a {@link ParameterizedAction} object using
013 * a specific set of parameters.
014 */
015public class ParameterizedActionDecorator implements Action {
016
017    private final ParameterizedAction action;
018    private final Map<String, Object> parameters;
019
020    /**
021     * Constructs a new ParameterizedActionDecorator.
022     * @param action the action that is invoked by this wrapper
023     * @param parameters parameters used for invoking the action
024     */
025    public ParameterizedActionDecorator(ParameterizedAction action, Map<String, Object> parameters) {
026        this.action = action;
027        this.parameters = new HashMap<>(parameters);
028    }
029
030    @Override
031    public void addPropertyChangeListener(PropertyChangeListener listener) {
032        action.addPropertyChangeListener(listener);
033    }
034
035    @Override
036    public Object getValue(String key) {
037        return action.getValue(key);
038    }
039
040    @Override
041    public boolean isEnabled() {
042        return action.isEnabled();
043    }
044
045    @Override
046    public void putValue(String key, Object value) {
047        action.putValue(key, value);
048    }
049
050    @Override
051    public void removePropertyChangeListener(PropertyChangeListener listener) {
052        action.removePropertyChangeListener(listener);
053    }
054
055    @Override
056    public void setEnabled(boolean b) {
057        action.setEnabled(b);
058    }
059
060    @Override
061    public void actionPerformed(ActionEvent e) {
062        action.actionPerformed(e, parameters);
063    }
064
065    /**
066     * Get the parameters used to invoke the wrapped action.
067     * @return the parameters used to invoke the wrapped action
068     */
069    public Map<String, Object> getParameters() {
070        return parameters;
071    }
072}