001    /*
002     * $Id: ClientDialog.java,v 1.2 2003/09/23 16:12:31 gus Exp $
003     *
004     * Developed for "Rethinking CS101", a project of Lynn Andrea Stein's AP Group.
005     * For more information, see <a href="http://www.ai.mit.edu/projects/cs101/">the
006     * CS101 homepage</a> or email <las@ai.mit.edu>.
007     *
008     * Copyright (C) 1996 Massachusetts Institute of Technology.
009     * Please do not redistribute without obtaining permission.
010     */
011    package cs101.awt;
012    
013    import java.net.InetAddress;
014    import java.net.UnknownHostException;
015    
016    /**
017     * Manages a top level query dialog box to get hostName and port
018     * connection information.  To display the dialog box, use ask(); to
019     * extract the hostName or port number, use getHostName or getPort().<p>
020     *
021     * Relies heavily on QueryDialog.<p>
022     *
023     * Copyright (c) 1998 Massachusetts Institute of Technology
024     *
025     * @see cs101.awt.QueryDialog
026     * @see #ask
027     * @see #getHostName
028     * @see #getPort
029     *
030     * @author Todd C. Parnell, tparnell@ai.mit.edu
031     * @author Nathan Williams <nathanw@mit.edu>
032     * @author Lynn Andrea Stein <las@ai.mit.edu>
033     * @version $Id: ClientDialog.java,v 1.2 2003/09/23 16:12:31 gus Exp $
034     */
035    public class ClientDialog {
036      
037      protected QueryDialog qd;
038      protected String[] answers, questions;
039      
040      /**
041       * Creates the dialog information.
042       * To show the dialog, call ask().
043       * To retrieve information, use int getPort().
044       */
045      public ClientDialog() {
046        this.questions = new String[2];
047        this.questions[0] = "HostName";
048        this.questions[1] = "Port";
049        this.answers = new String[2];
050        try{ 
051          this.answers[0] = InetAddress.getLocalHost().getHostName();
052        } catch (UnknownHostException e) {}
053        this.answers[1] = "4321";
054        this.qd = new QueryDialog("Please enter the server host and port.",
055                                  questions,
056                                  answers);
057      }
058    
059      /**
060       * Actually display the query dialog and get the answers from the user.
061       *
062       * @see #getHostName()
063       * @see #getPort()
064       */ 
065      public synchronized void ask() {
066        this.answers = this.qd.ask();
067      }
068    
069      /**
070       * Return the host machine name from the user.  Not guaranteed to be 
071       * sensible if ask hasn't aready been called.
072       *
073       * @see #ask()
074       */
075      public synchronized String getHostName() {
076        return( this.answers[0] );
077      }
078    
079      /**
080       * Return the port number from the user.  Not guaranteed to be sensible
081       * if ask hasn't aready been called.
082       *
083       * @see #ask()
084       */
085      public synchronized int getPort() {
086        try {
087          return Integer.parseInt( this.answers[1] );
088        } catch ( NumberFormatException e ) {
089          throw new RuntimeException("Bad port number '"+answers[1]+"'");
090        }
091      }
092    
093    }
094        
095    /*
096     * $Log: ClientDialog.java,v $
097     * Revision 1.2  2003/09/23 16:12:31  gus
098     * javadoc fix
099     *
100     * Revision 1.1.1.1  2002/06/05 21:56:32  root
101     * CS101 comes to Olin finally.
102     *
103     * Revision 1.7  1998/07/24 17:06:26  tparnell
104     * Placate new javadoc behavior
105     *
106     * Revision 1.6  1998/07/22 18:18:33  tparnell
107     * migration from cs101.util to cs101.*
108     *
109     * Revision 1.5  1998/07/21 20:19:53  tparnell
110     * bugfix javadoc
111     *
112     * Revision 1.4  1998/06/24 21:27:21  tparnell
113     * code cleanup
114     *
115     * Revision 1.3  1998/06/22 21:26:54  tparnell
116     * merge from 6004 lab tparnell/rcddisk/cs101/util
117     *
118     * Revision 1.2  1998/06/03 21:48:42  tparnell
119     * update from Java 1.0 to 1.1
120     *
121     * Revision 1.1  1998/03/13 22:18:06  tparnell
122     * Import from server crash.  I think the src and class files match up.
123     *
124     * Revision 1.1  1996/11/18 17:25:00  las
125     * Added revised SharedWhiteboard support classes.  These versions of
126     * Client and Server supercede the previous ones and are not directly
127     * backwards compatible.  In particular, Server is an instantiable class
128     * rather than a primarily static one (use RunServer to run it), and
129     * Client uses StringHandler rather than subclassing to specialize it.
130     * Line.java just picked up some obscure documentation along the way.
131     * Otherwise, classes are direct imports from SharedWhiteboard.
132     *
133     * Revision 1.5  1996/11/18 16:41:19  las
134     * Client, Server, their dependencies (e.g. the Dialogs and their
135     * invokers) all work.  ClientTester is a bit awkward in that it doesn't
136     * echo received strings until something is read.  Oh, well.
137     * Moving them to cs101.util in the next revision.
138     *
139     * Revision 1.4  1996/11/17 22:28:13  las
140     * Everything compiles (now).  Client, Server, ClientDialog, ServerDialog,
141     * StringHandler, and RunServer need to be moved to cs101.util.  But
142     * first, to test....
143     *
144     * Revision 1.3  1996/11/17 21:26:53  las
145     * Client, ClientDialog writen, not yet tested.
146     *
147     */
148