001package jmri.jmrit.z21server;
002
003import jmri.InstanceManager;
004import jmri.jmrit.throttle.LargePowerManagerButton;
005import jmri.jmrit.throttle.StopAllButton;
006import jmri.util.FileUtil;
007import jmri.util.JmriJFrame;
008import jmri.util.prefs.JmriPreferencesActionFactory;
009
010import javax.swing.*;
011import java.awt.*;
012import java.awt.event.ActionEvent;
013import java.net.InetAddress;
014
015public class UserInterface extends JmriJFrame {
016
017
018    JMenuBar menuBar;
019    JMenuItem serverOnOff;
020    JPanel panel;
021    JLabel manualPortLabel = new JLabel();
022
023    //keep a reference to the actual server
024    private FacelessServer facelessServer;
025
026    // Server iVars
027    boolean isListen;
028
029    /**
030     * Save the last known size and the last known location since 4.15.4.
031     */
032    UserInterface() {
033        super(true, true);
034
035        isListen = true;
036        facelessServer = FacelessServer.getInstance();
037        String host = "";
038        try {
039            host = InetAddress.getLocalHost().getHostAddress();
040        } catch (Exception e) { host = "unknown ip"; }
041
042        this.manualPortLabel.setText("<html>" + host + "</html>"); // NOI18N
043
044        createWindow();
045
046    } // End of constructor
047
048
049    protected void createWindow() {
050        panel = new JPanel();
051        panel.setLayout(new GridBagLayout());
052        GridBagConstraints con = new GridBagConstraints();
053        getContentPane().add(panel);
054        con.fill = GridBagConstraints.NONE;
055        con.weightx = 0.5;
056        con.weighty = 0;
057
058        JLabel label = new JLabel(Bundle.getMessage("LabelListening"));
059        con.gridx = 0;
060        con.gridy = 0;
061        con.gridwidth = 2;
062        panel.add(label, con);
063
064        con.gridx = 0;
065        con.gridy = 1;
066        con.gridwidth = 2;
067
068        con.gridy = 2;
069        panel.add(manualPortLabel, con);
070
071
072        JToolBar toolBar = new JToolBar();
073        toolBar.setFloatable(false);
074        toolBar.add(new StopAllButton());
075        toolBar.add(new LargePowerManagerButton());
076        con.weightx = 0.5;
077        con.ipadx = 0;
078        con.gridx = 1;
079        con.gridy = 3;
080        con.gridwidth = 2;
081        panel.add(toolBar, con);
082
083        JLabel icon;
084        java.net.URL imageURL = FileUtil.findURL("resources/z21appIcon.png");
085
086        if (imageURL != null) {
087            ImageIcon image = new ImageIcon(imageURL);
088            icon = new JLabel(image);
089            con.weightx = 0.5;
090            con.gridx = 2;
091            con.gridy = 0;
092            con.ipady = 5;
093            con.gridheight = 2;
094            panel.add(icon, con);
095        }
096
097        con.gridx = 0;
098        con.gridy = 4;
099        con.weighty = 1.0;
100        con.ipadx = 10;
101        con.ipady = 10;
102        con.gridheight = 3;
103        con.gridwidth = GridBagConstraints.REMAINDER;
104        con.fill = GridBagConstraints.BOTH;
105
106        //  Create the menu to use with the window. Has to be before pack() for Windows.
107        buildMenu();
108
109        //  Set window size & location
110        this.setTitle("Z21 App Server");
111        this.pack();
112
113        this.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
114
115        setVisible(true);
116        setMinimumSize(new Dimension(400, 100));
117
118    }
119
120    protected void buildMenu() {
121        this.setJMenuBar(new JMenuBar());
122
123        JMenu menu = new JMenu(Bundle.getMessage("MenuMenu"));
124        serverOnOff = new JMenuItem(Bundle.getMessage("MenuMenuStop"));
125        serverOnOff.addActionListener(new AbstractAction() {
126
127            @Override
128            public void actionPerformed(ActionEvent event) {
129                if (isListen) { // Stop server, remove addresses from UI
130                    disableServer();
131                    serverOnOff.setText(Bundle.getMessage("MenuMenuStart"));
132                    manualPortLabel.setText(null);
133                } else { // Restart server
134                    enableServer();
135                    serverOnOff.setText(Bundle.getMessage("MenuMenuStop"));
136                    String host = "";
137                    try {
138                        host = InetAddress.getLocalHost().getHostAddress();
139                    } catch (Exception e) { host = "unknown ip"; }
140                    manualPortLabel.setText("<html>" + host + "</html>");
141                }
142            }
143        });
144
145        menu.add(serverOnOff);
146
147        //menu.add(new ControllerFilterAction());
148
149        Action prefsAction = InstanceManager.getDefault(JmriPreferencesActionFactory.class).getCategorizedAction(
150                Bundle.getMessage("MenuMenuPrefs"),
151                "Z21 App Server");
152
153        menu.add(prefsAction);
154
155        this.getJMenuBar().add(menu);
156
157        // add help menu
158        addHelpMenu("package.jmri.jmrit.z21server.UserInterface", true);
159    }
160
161
162    void disableServer() {
163        facelessServer.stop();
164        isListen = false;
165    }
166
167    private void enableServer() {
168        facelessServer.start();
169        isListen = true;
170    }
171
172}