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            if (java.awt.GraphicsEnvironment.isHeadless()) {
063                // don't want this to be handled via Swing
064                return null;
065            }
066        } catch (Exception ex) {
067            log.warn("could not init pane class: {}", paneClass, ex);
068        }
069
070        return p;
071    }
072
073    @Override
074    public CanSystemConnectionMemo getSystemConnectionMemo() {
075        return this.memo;
076    }
077
078    @Override
079    public void setSystemConnectionMemo(CanSystemConnectionMemo memo) throws IllegalArgumentException {
080        if (CanSystemConnectionMemo.class.isAssignableFrom(memo.getClass())) {
081            this.memo = memo;
082        } else {
083            throw new IllegalArgumentException();
084        }
085    }
086
087    @Override
088    public Set<Class<? extends SystemConnectionMemo>> getSystemConnectionMemoClasses() {
089        return new HashSet<>(Arrays.asList(CanSystemConnectionMemo.class));
090    }
091
092    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CanNamedPaneAction.class);
093
094}