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