001package jmri.jmrit.withrottle;
002
003import java.awt.GraphicsEnvironment;
004import java.awt.event.ActionEvent;
005import javax.swing.Icon;
006import jmri.InstanceManager;
007import jmri.ThrottleManager;
008import jmri.util.swing.JmriAbstractAction;
009import jmri.util.swing.WindowInterface;
010
011/**
012 * Start, and create if needed, the WiThrottle server.
013 *
014 * @author Brett Hoffman Copyright (C) 2009
015 *
016 */
017public class WiThrottleCreationAction extends JmriAbstractAction {
018
019    public WiThrottleCreationAction(String s, WindowInterface wi) {
020        super(s, wi);
021    }
022
023    public WiThrottleCreationAction(String s, Icon i, WindowInterface wi) {
024        super(s, i, wi);
025    }
026
027    /**
028     * Create a new network server.
029     *
030     * @param name Labels frame in GUI
031     */
032    public WiThrottleCreationAction(String name) {
033        super(name);
034        if (InstanceManager.getNullableDefault(ThrottleManager.class) == null) {
035            super.setEnabled(false);
036        }
037    }
038
039    /**
040     * Create a new network server.
041     */
042    public WiThrottleCreationAction() {
043        this(Bundle.getMessage("MenuStartWiThrottleServer"));
044    }
045
046    /**
047     * Start the server end of WiThrottle.
048     *
049     * @param e The event causing the action.
050     */
051    @Override
052    public void actionPerformed(ActionEvent e) {
053        InstanceManager.getOptionalDefault(DeviceManager.class).orElseGet(() -> {
054            return InstanceManager.setDefault(DeviceManager.class, new FacelessServer());
055        });
056        // ensure the GUI is visible if we are not in headless mode.
057        if (!GraphicsEnvironment.isHeadless()) {
058           UserInterface ui = InstanceManager.getOptionalDefault(UserInterface.class).orElseGet(() -> {
059            return InstanceManager.setDefault(UserInterface.class, new UserInterface());
060           });
061           ui.setVisible(true);
062        }
063    }
064
065    // never invoked, because we overrode actionPerformed above
066    @Override
067    public jmri.util.swing.JmriPanel makePanel() {
068        throw new IllegalArgumentException("Should not be invoked");
069    }
070
071}