001package jmri.jmrix.loconet.swing;
002
003import java.util.Arrays;
004import java.util.HashSet;
005import java.util.Set;
006import javax.swing.Icon;
007import jmri.SystemConnectionMemo;
008import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
009import jmri.jmrix.swing.SystemConnectionAction;
010import jmri.util.swing.JmriNamedPaneAction;
011import jmri.util.swing.JmriPanel;
012import jmri.util.swing.WindowInterface;
013import org.slf4j.Logger;
014import org.slf4j.LoggerFactory;
015
016/**
017 * Action to create and load a JmriPanel from just its name.
018 *
019 * @author Bob Jacobsen Copyright (C) 2010
020 */
021public class LnNamedPaneAction extends JmriNamedPaneAction implements SystemConnectionAction<LocoNetSystemConnectionMemo> {
022
023    /**
024     * Enhanced constructor for placing the pane in various GUIs.
025     *
026     * @param s         Human-readable panel name for display by the action
027     * @param wi        Window into which to install the new panel. If you want it to be put into a existing
028     *                  one, provide a reference. To create a new window
029     *                  containing just this pane, use "new jmri.util.swing.sdi.JmriJFrameInterface()"
030     * @param paneClass Name of the panel's class, which must be a subclass of JmriPanel. That's not
031     *                  checked at compile time or when the constructor runs, but must be true
032     *                  for the action to be invoked successfully.
033     * @param memo      {@link jmri.jmrix.loconet.LocoNetSystemConnectionMemo} to be used by this object
034     */
035    public LnNamedPaneAction(String s, WindowInterface wi, String paneClass, LocoNetSystemConnectionMemo memo) {
036        super(s, wi, paneClass);
037        this.memo = memo;
038    }
039
040
041    /**
042     * Enhanced constructor for placing the pane in various GUIs.
043     *
044     * @param s         Human-readable panel name for display by the action
045     * @param i         Icon for display by the action
046     * @param wi        Window into which to install the new panel. If you want it to be put into a existing
047     *                  one, provide a reference. To create a new window
048     *                  containing just this pane, use "new jmri.util.swing.sdi.JmriJFrameInterface()"
049     * @param paneClass Name of the panel's class, which must be a subclass of JmriPanel. That's not
050     *                  checked at compile time or when the constructor runs, but must be true
051     *                  for the action to be invoked successfully.
052     * @param memo      {@link jmri.jmrix.loconet.LocoNetSystemConnectionMemo} to be used by this object
053     */
054
055
056    public LnNamedPaneAction(String s, Icon i, WindowInterface wi, String paneClass, LocoNetSystemConnectionMemo memo) {
057        super(s, i, wi, paneClass);
058        this.memo = memo;
059    }
060
061    LocoNetSystemConnectionMemo memo;
062
063    @Override
064    public JmriPanel makePanel() {
065        JmriPanel p = super.makePanel();
066        if (p == null) {
067            return null;
068        }
069
070        try {
071            if (LnPanelInterface.class.isAssignableFrom(p.getClass())) {
072                ((LnPanelInterface) p).initComponents(memo);
073            }
074            return p;
075        } catch (Exception ex) {
076            log.warn("Could not initialize class \"{}\"", paneClass, ex);
077        }
078
079        return p;
080    }
081
082    @Override
083    public LocoNetSystemConnectionMemo getSystemConnectionMemo() {
084        return this.memo;
085    }
086
087    @Override
088    public void setSystemConnectionMemo(LocoNetSystemConnectionMemo memo) throws IllegalArgumentException {
089        if (LocoNetSystemConnectionMemo.class.isAssignableFrom(memo.getClass())) {
090            this.memo = memo;
091        } else {
092            throw new IllegalArgumentException();
093        }
094    }
095
096    @Override
097    public Set<Class<? extends SystemConnectionMemo>> getSystemConnectionMemoClasses() {
098        return new HashSet<>(Arrays.asList(LocoNetSystemConnectionMemo.class));
099    }
100
101    private final static Logger log = LoggerFactory.getLogger(LnNamedPaneAction.class);
102
103}