001package jmri.jmrix.can.swing;
002
003import java.util.Arrays;
004import java.util.HashSet;
005import java.util.Set;
006
007import javax.swing.Icon;
008import jmri.SystemConnectionMemo;
009import jmri.jmrix.can.CanSystemConnectionMemo;
010import jmri.util.swing.JmriPanel;
011import jmri.jmrix.swing.SystemConnectionAction;
012import jmri.util.swing.WindowInterface;
013
014/**
015 * Action to create and load a CAN-specific JmriPanel
016 *
017 * @author Bob Jacobsen Copyright (C) 2012
018 */
019public class CanNamedPaneAction extends jmri.util.swing.JmriNamedPaneAction 
020    implements SystemConnectionAction<CanSystemConnectionMemo> {
021
022    /**
023     * Enhanced constructor for placing the pane in various GUIs.
024     * @param s Window Name
025     * @param wi JmriJFrameInterface
026     * @param paneClass Name of class to open
027     * @param memo System Connection
028     */
029    public CanNamedPaneAction(String s, WindowInterface wi, String paneClass, CanSystemConnectionMemo memo) {
030        super(s, wi, paneClass);
031        this.memo = memo;
032    }
033
034    /**
035     * Enhanced constructor for placing the pane in various GUIs.
036     * @param s Window Name
037     * @param i Icon to display
038     * @param wi JmriJFrameInterface
039     * @param paneClass Name of class to open
040     * @param memo System Connection
041     */
042    public CanNamedPaneAction(String s, Icon i, WindowInterface wi, String paneClass, CanSystemConnectionMemo memo) {
043        super(s, i, wi, paneClass);
044        this.memo = memo;
045    }
046
047    CanSystemConnectionMemo memo;
048
049    /**
050     * Makes Panel and calls initComponents
051     * {@inheritDoc}
052     */
053    @Override
054    public JmriPanel makePanel() {
055        JmriPanel p = super.makePanel();
056        if (p == null) {
057            return null;
058        }
059
060        try {
061            ((CanPanelInterface) p).initComponents(memo);
062            return p;
063        } catch (Exception ex) {
064            log.warn("could not init pane class: {}", paneClass, ex);
065        }
066
067        return p;
068    }
069
070    @Override
071    public CanSystemConnectionMemo getSystemConnectionMemo() {
072        return this.memo;
073    }
074
075    @Override
076    public void setSystemConnectionMemo(CanSystemConnectionMemo memo) throws IllegalArgumentException {
077        if (CanSystemConnectionMemo.class.isAssignableFrom(memo.getClass())) {
078            this.memo = memo;
079        } else {
080            throw new IllegalArgumentException();
081        }
082    }
083
084    @Override
085    public Set<Class<? extends SystemConnectionMemo>> getSystemConnectionMemoClasses() {
086        return new HashSet<>(Arrays.asList(CanSystemConnectionMemo.class));
087    }
088
089    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CanNamedPaneAction.class);
090
091}