001package jmri.jmrix.openlcb.swing.lccpro;
002
003import java.awt.Dimension;
004import java.awt.event.ActionEvent;
005import javax.swing.Icon;
006import javax.swing.WindowConstants;
007import jmri.InstanceManager;
008import jmri.UserPreferencesManager;
009import jmri.util.swing.JmriAbstractAction;
010import jmri.util.swing.WindowInterface;
011
012/**
013 * AbstractAction for the LccProFrane so that multiple windows can be opened
014 *
015 * @author Kevin Dickerson Copyright (C) 2011
016 * @author Randall Wood Copyright (C) 2012
017 * @author Bob Jacobsen  Copyright (C) 2024
018 */
019public class LccProFrameAction extends JmriAbstractAction {
020
021    public LccProFrameAction(String s, WindowInterface wi) {
022        super(s, wi);
023    }
024
025    public LccProFrameAction(String s, WindowInterface wi, boolean allowQuit) {
026        super(s, wi);
027        this.allowQuit = allowQuit;
028        checkAndSetEnabled();
029    }
030
031    public LccProFrameAction(String s, Icon i, WindowInterface wi) {
032        super(s, i, wi);
033        checkAndSetEnabled();
034    }
035
036    /**
037     * Default constructor used when instantiating via startup action or button
038     * configured in user preferences
039     */
040    public LccProFrameAction() {
041        super(Bundle.getMessage("LccProFrameAction")); // NOI18N
042        allowQuit = false;
043        checkAndSetEnabled();
044    }
045
046    /**
047     * Method for opening a new window via the classic JMRI interface.
048     * @param pName action name.
049     * @param allowQuit Set state to either close JMRI or just the roster window.
050     */
051    public LccProFrameAction(String pName, boolean allowQuit) {
052        super(pName);
053        this.allowQuit = allowQuit;
054        checkAndSetEnabled();
055    }
056
057    boolean allowQuit = true;
058
059    void checkAndSetEnabled() {
060        // if there's no connection, disable this
061        var memo = jmri.InstanceManager.getNullableDefault(jmri.jmrix.can.CanSystemConnectionMemo.class);
062        setEnabled(memo != null);
063    }
064    
065    @Override
066    public void actionPerformed(ActionEvent event) {
067        var mainFrame = new LccProFrame("LCC Pro");
068        UserPreferencesManager p = InstanceManager.getDefault(jmri.UserPreferencesManager.class);
069        if (!p.hasProperties(mainFrame.getWindowFrameRef())) {
070            mainFrame.setSize(new Dimension(1024, 600));
071            mainFrame.setPreferredSize(new Dimension(1024, 600));
072        }
073        mainFrame.setVisible(true);
074        mainFrame.setAllowQuit(allowQuit);
075        mainFrame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
076    }
077
078    // never invoked, because we overrode actionPerformed above
079    @Override
080    public jmri.util.swing.JmriPanel makePanel() {
081        throw new IllegalArgumentException("Should not be invoked");
082    }
083}