package mil.navy.nps.logger; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Modal dialog box used to get user input for various networking * parameters, such as multicast group, port, and ttl.

* * This is called from a menu item in the main window.

* @author DMcG */ public class NetworkDialog extends JDialog { public static final int CANCEL = 0; public static final int OK = 1; private JFrame parentFrame; private JPanel left, right, buttons, drawArea; protected TextField ipaddress; protected TextField portNumber; protected JComboBox ttl; // did user hit OK or cancel? protected int result = CANCEL; /** * Constructor, set up GUI layout */ public NetworkDialog(JFrame pParentFrame, String pMcastAddress, int pPort, int pTTL) { super(pParentFrame, true); // superclass, sets us up as modal JButton ok, cancel; this.setSize(200, 200); parentFrame = pParentFrame; ipaddress = new TextField(25); // IP/mcast address to listen on portNumber = new TextField(20); // Port to listen on ttl = new JComboBox(); // Combo box for ttl // combo box entries ttl.setEditable(false); ttl.addItem(" Local (ttl=1)"); ttl.addItem(" Campus (ttl=16)"); ttl.addItem(" Regional (ttl=32)"); ttl.addItem(" World (ttl=64)"); // some knob-twiddling this.setTitle("Networking Parameters"); this.setSize(300, 150); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); // saves us from having to write an event handler drawArea = new JPanel(); drawArea.setLayout(new BorderLayout() ); left = new JPanel(); left.setLayout(new GridLayout(3,1)); left.add(new Label("Multicast address")); left.add(new Label("Multicast port")); left.add(new Label("Scope")); drawArea.add("West", left); right = new JPanel(); // Set up the right panel. right.setLayout(new GridLayout(3,1)); right.add(ipaddress); ipaddress.setText(pMcastAddress); right.add(portNumber); portNumber.setText((new Integer(pPort)).toString()); // Pick the right values for ttl if(pTTL <= 1) ttl.setSelectedIndex(0); else if (pTTL <=16) ttl.setSelectedIndex(1); else if (pTTL <= 32) ttl.setSelectedIndex(2); else if(pTTL >= 64) ttl.setSelectedIndex(3); right.add(ttl); drawArea.add("East", right); buttons = new JPanel(); ok = new JButton("OK"); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { WindowEvent we; result = OK; we = new WindowEvent(NetworkDialog.this, WindowEvent.WINDOW_CLOSING); NetworkDialog.this.processWindowEvent(we); } }); cancel = new JButton("Cancel"); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { WindowEvent we; result = CANCEL; we = new WindowEvent(NetworkDialog.this, WindowEvent.WINDOW_CLOSING); NetworkDialog.this.processWindowEvent(we); } }); buttons.add(ok); buttons.add(cancel); drawArea.add("South", buttons); this.getContentPane().add(drawArea); } /** * Returns what the user did after the dialog exits, OK * or cancel. */ public int getResult() { return result; } public String getMulticastAddress() { return ipaddress.getText(); } /** * Returns the port number selected by the user */ public int getPort() { int port = -1; try { port = Integer.parseInt(portNumber.getText()); } catch(NumberFormatException nfe) { System.out.println(nfe); } return port; } /** * Returns the ttl number selected by the user */ public int getTTL() { int ttlNo = 1; String selectedScope = (String)ttl.getSelectedItem(); if(selectedScope.compareToIgnoreCase("Local") == 0) ttlNo = 1; if(selectedScope.compareToIgnoreCase("Campus") == 0) ttlNo = 16; if(selectedScope.compareToIgnoreCase("Regional") == 0) ttlNo = 32; if(selectedScope.compareToIgnoreCase("World") == 0) ttlNo = 64; return ttlNo; } }