001    /*
002     * NetworkSettingsDialog.java
003     *
004     * Created on October 29, 2003, 9:08 AM
005     */
006    
007    package cs101.awt;
008    
009    import java.net.InetAddress;
010    import java.net.UnknownHostException;
011    
012    import javax.swing.JOptionPane;
013    
014    /**
015     * A dialog box that will allow the user to select wether the program should
016     * run in standalone mode, client mode or server mode. It is typically
017     * displayed before the program does anything, and in some cases it may be the
018     * first window that the user sees. This class is partly
019     * based on a similar class written by Ben Vandiver in March 2003, but has
020     * been entirely re-coded from scratch.
021     *
022     * @author  Patrick G. Heck (gus.heck@olin.edu)
023     * @version $Id: NetworkSettingsDialog.java,v 1.2 2003/10/29 17:04:11 gus Exp $
024     */
025    public class NetworkSettingsDialog extends javax.swing.JDialog {
026        
027        /** Run in standalone mode */
028        public static final int NETWORK_NONE =   0;
029        
030        /** Run as a client */
031        public static final int NETWORK_CLIENT = 1;
032        
033        /** Run as a server */
034        public static final int NETWORK_SERVER = 2;
035        
036        // default state is standalone
037        private int mode = NETWORK_NONE;
038        
039        // toggles debugging output
040        private static boolean debug;
041        
042        /** 
043         * Creates new form NetworkSettingsDialog. 
044         *
045         * @param parent The parent frame, or null if this dialog applies
046         *               to the entire application, not just one frame.
047         * @param modal  If true, and parent is null, the entire application
048         *               will block until ok is pressed. If true and parent 
049         *               is defined, then only the parent window will be blocked.
050         *               If false, no blocking will occur.
051         */
052        public NetworkSettingsDialog(java.awt.Frame parent, boolean modal) {
053            super(parent, modal);
054            initComponents();
055        }
056        
057        /** This method is called from within the constructor to
058         * initialize the form.
059         * WARNING: Do NOT modify this code. The content of this method is
060         * always regenerated by the Form Editor.
061         */
062        private void initComponents() {//GEN-BEGIN:initComponents
063            java.awt.GridBagConstraints gridBagConstraints;
064    
065            modeButtons = new javax.swing.ButtonGroup();
066            jLabel3 = new javax.swing.JLabel();
067            jPanel1 = new javax.swing.JPanel();
068            leftPanel = new javax.swing.JPanel();
069            standaloneRadioButton = new javax.swing.JRadioButton();
070            clientRadioButton = new javax.swing.JRadioButton();
071            serverRadioButton = new javax.swing.JRadioButton();
072            jPanel3 = new javax.swing.JPanel();
073            hostLabel = new javax.swing.JLabel();
074            hostTextField = new javax.swing.JTextField();
075            portLabel = new javax.swing.JLabel();
076            portTextField = new javax.swing.JTextField();
077            okButton = new javax.swing.JButton();
078    
079            jLabel3.setText("jLabel3");
080    
081            setTitle("Network Settings");
082            addWindowListener(new java.awt.event.WindowAdapter() {
083                public void windowClosing(java.awt.event.WindowEvent evt) {
084                    closeDialog(evt);
085                }
086            });
087    
088            jPanel1.setLayout(new java.awt.GridBagLayout());
089    
090            jPanel1.setBorder(new javax.swing.border.EmptyBorder(new java.awt.Insets(10, 10, 10, 10)));
091            leftPanel.setLayout(new javax.swing.BoxLayout(leftPanel, javax.swing.BoxLayout.Y_AXIS));
092    
093            leftPanel.setBorder(new javax.swing.border.TitledBorder("Mode"));
094            standaloneRadioButton.setText(" Standalone");
095            standaloneRadioButton.setToolTipText("Run with no network features enabled");
096            modeButtons.add(standaloneRadioButton);
097            standaloneRadioButton.addActionListener(new java.awt.event.ActionListener() {
098                public void actionPerformed(java.awt.event.ActionEvent evt) {
099                    standaloneRadioButtonActionPerformed(evt);
100                }
101            });
102    
103            leftPanel.add(standaloneRadioButton);
104    
105            clientRadioButton.setText("Client");
106            clientRadioButton.setToolTipText("Run as a networked client");
107            modeButtons.add(clientRadioButton);
108            clientRadioButton.addActionListener(new java.awt.event.ActionListener() {
109                public void actionPerformed(java.awt.event.ActionEvent evt) {
110                    clientRadioButtonActionPerformed(evt);
111                }
112            });
113    
114            leftPanel.add(clientRadioButton);
115    
116            serverRadioButton.setText("Server");
117            serverRadioButton.setToolTipText("Run as a networked server");
118            modeButtons.add(serverRadioButton);
119            serverRadioButton.addActionListener(new java.awt.event.ActionListener() {
120                public void actionPerformed(java.awt.event.ActionEvent evt) {
121                    serverRadioButtonActionPerformed(evt);
122                }
123            });
124    
125            leftPanel.add(serverRadioButton);
126    
127            gridBagConstraints = new java.awt.GridBagConstraints();
128            gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
129            jPanel1.add(leftPanel, gridBagConstraints);
130    
131            jPanel3.setLayout(new java.awt.GridBagLayout());
132    
133            hostLabel.setText("Host:");
134            hostLabel.setToolTipText("The host to connect to");
135            hostLabel.setEnabled(false);
136            gridBagConstraints = new java.awt.GridBagConstraints();
137            gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
138            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
139            jPanel3.add(hostLabel, gridBagConstraints);
140    
141            hostTextField.setText("localhost");
142            hostTextField.setToolTipText("Enter the host name here");
143            hostTextField.setPreferredSize(new java.awt.Dimension(100, 19));
144            hostTextField.setEnabled(false);
145            gridBagConstraints = new java.awt.GridBagConstraints();
146            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
147            jPanel3.add(hostTextField, gridBagConstraints);
148    
149            portLabel.setText("Port:");
150            portLabel.setToolTipText("The port to listen on or connect to");
151            portLabel.setEnabled(false);
152            gridBagConstraints = new java.awt.GridBagConstraints();
153            gridBagConstraints.gridx = 0;
154            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
155            jPanel3.add(portLabel, gridBagConstraints);
156    
157            portTextField.setText("4321");
158            portTextField.setToolTipText("Enter the port number here");
159            portTextField.setPreferredSize(new java.awt.Dimension(100, 19));
160            portTextField.setEnabled(false);
161            gridBagConstraints = new java.awt.GridBagConstraints();
162            gridBagConstraints.gridx = 1;
163            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
164            jPanel3.add(portTextField, gridBagConstraints);
165    
166            okButton.setText("OK");
167            okButton.setMargin(new java.awt.Insets(2, 28, 2, 28));
168            okButton.addActionListener(new java.awt.event.ActionListener() {
169                public void actionPerformed(java.awt.event.ActionEvent evt) {
170                    okButtonActionPerformed(evt);
171                }
172            });
173    
174            gridBagConstraints = new java.awt.GridBagConstraints();
175            gridBagConstraints.gridx = 0;
176            gridBagConstraints.gridwidth = 2;
177            gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
178            jPanel3.add(okButton, gridBagConstraints);
179    
180            gridBagConstraints = new java.awt.GridBagConstraints();
181            gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
182            jPanel1.add(jPanel3, gridBagConstraints);
183    
184            getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
185    
186            pack();
187        }//GEN-END:initComponents
188        
189        private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
190            // Add your handling code here:
191            if (!validateInput()) return;
192            this.hide();
193            System.out.println("getMode() will return: " + getMode());
194            if (debug) {
195                try {
196                    System.out.println("getHost() will return: "+ getHost());
197                } catch (RuntimeException re) {
198                    System.out.println("getHost() will throw " + re);
199                }
200                try {
201                    System.out.println("getPort() will return: " + getPort());
202                } catch (RuntimeException re) {
203                    System.out.println("getPort() will throw " + re);
204                }
205            }
206        }//GEN-LAST:event_okButtonActionPerformed
207        
208        private void standaloneRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_standaloneRadioButtonActionPerformed
209            // Add your handling code here:
210            mode = NETWORK_NONE;
211            hostLabel.setEnabled(false);
212            hostTextField.setEnabled(false);
213            portLabel.setEnabled(false);
214            portTextField.setEnabled(false);
215            
216        }//GEN-LAST:event_standaloneRadioButtonActionPerformed
217        
218        private void clientRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clientRadioButtonActionPerformed
219            // Add your handling code here:
220            mode = NETWORK_CLIENT;
221            hostLabel.setEnabled(true);
222            hostTextField.setEnabled(true);
223            portLabel.setEnabled(true);
224            portTextField.setEnabled(true);
225            
226        }//GEN-LAST:event_clientRadioButtonActionPerformed
227        
228        private void serverRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_serverRadioButtonActionPerformed
229            // Add your handling code here:
230            mode = NETWORK_SERVER;
231            hostLabel.setEnabled(false);
232            hostTextField.setEnabled(false);
233            portLabel.setEnabled(true);
234            portTextField.setEnabled(true);
235            
236        }//GEN-LAST:event_serverRadioButtonActionPerformed
237        
238        /** Closes the dialog */
239        private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
240            setVisible(false);
241            dispose();
242        }//GEN-LAST:event_closeDialog
243        
244        private boolean validateInput() {
245            try {
246                if (this.mode == NETWORK_CLIENT) {
247                    Integer.parseInt(portTextField.getText());
248                    InetAddress.getByName(hostTextField.getText());
249                } else if (this.mode == NETWORK_SERVER)
250                    Integer.parseInt(portTextField.getText());
251            } catch (NumberFormatException e) {
252                JOptionPane.showMessageDialog(this,"Non-numeric value for port!");
253                return false;
254            } catch (UnknownHostException e) {
255                JOptionPane.showMessageDialog(this,"Unknown host: " + hostTextField.getText());
256                return false;
257            }
258            return true;
259        }
260        /**
261         * Returns the requested state of network connectivity
262         *
263         * @return An integer constant drawn from NETWORK_NONE,
264         * NETWORK_CLIENT, or NETWORK_SERVER.
265         */
266        public int getMode() {
267            return this.mode;
268        }
269        
270        /**
271         * returns the requested port.  The text box the value is drawn from
272         * depends on the state.
273         *
274         * @return port number to serve on / connect to.
275         * @throws RuntimeException if the state was NETWORK_NONE.
276         */
277        public int getPort() {
278            if (mode == NETWORK_NONE) {
279                throw new RuntimeException("Invalid Network settings for getPort()");
280            }
281            return Integer.parseInt(portTextField.getText());
282        }
283        
284        /**
285         * returns the name of the host to connect to as a client.
286         *
287         * @return String name of host to connect to.
288         * @throws RuntimeException if the state was not NETWORK_CLIENT.
289         */
290        public String getHost() {
291            if (this.mode == NETWORK_CLIENT)
292                return this.hostTextField.getText();
293            else
294                throw new RuntimeException("Invalid Network settings for getHost()");
295        }
296        
297        /**
298         * Main method for visual layout inspection.
299         *
300         * @param args the command line arguments
301         */
302        public static void main(String args[]) {
303            debug = true;
304            NetworkSettingsDialog d = 
305            new NetworkSettingsDialog(new javax.swing.JFrame(), true);
306            
307            d.show();
308            System.exit(0);
309        }
310        
311        
312        // Variables declaration - do not modify//GEN-BEGIN:variables
313        private javax.swing.JRadioButton clientRadioButton;
314        private javax.swing.JLabel hostLabel;
315        private javax.swing.JTextField hostTextField;
316        private javax.swing.JLabel jLabel3;
317        private javax.swing.JPanel jPanel1;
318        private javax.swing.JPanel jPanel3;
319        private javax.swing.JPanel leftPanel;
320        private javax.swing.ButtonGroup modeButtons;
321        private javax.swing.JButton okButton;
322        private javax.swing.JLabel portLabel;
323        private javax.swing.JTextField portTextField;
324        private javax.swing.JRadioButton serverRadioButton;
325        private javax.swing.JRadioButton standaloneRadioButton;
326        // End of variables declaration//GEN-END:variables
327        
328    }
329    
330    /*
331     * $Log: NetworkSettingsDialog.java,v $
332     * Revision 1.2  2003/10/29 17:04:11  gus
333     * add a title to the dialog
334     *
335     * Revision 1.1  2003/10/29 15:55:50  gus
336     * An initial version of a rewrite of the network settings dialog from breakout
337     * spring 2003.
338     *
339     */