001package jmri.jmrix.swing;
002
003import javax.swing.AbstractAction;
004import jmri.SystemConnectionMemo;
005
006/**
007 * Base for {@link AbstractAction}s that need to be associated with a specific
008 * connection.
009 *
010 * @author Randall Wood Copyright 2020
011 * @param <M> the specific type of SystemConnectionMemo supported by this action
012 */
013public abstract class AbstractSystemConnectionAction<M extends SystemConnectionMemo> extends AbstractAction implements SystemConnectionAction<M> {
014
015    private transient M memo = null;
016
017    protected AbstractSystemConnectionAction(String name, M memo) {
018        super(name);
019        this.memo = memo;
020    }
021
022    /**
023     * {@inheritDoc}
024     */
025    @Override
026    public M getSystemConnectionMemo() {
027        return memo;
028    }
029
030    /**
031     * {@inheritDoc}
032     *
033     * @throws IllegalArgumentException if the memo is not assignable from any
034     *                                  class listed in
035     *                                  {@link #getSystemConnectionMemoClasses()}
036     */
037    @Override
038    public void setSystemConnectionMemo(M memo) {
039        if (getSystemConnectionMemoClasses().stream().anyMatch(memo.getClass()::isAssignableFrom)) {
040            this.memo = memo;
041        } else {
042            throw new IllegalArgumentException(memo.getClass() + " is not valid for " + this.getClass());
043        }
044    }
045}