View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: Chat.java,v 1.9 2005/09/06 18:00:21 mat007 Exp $
29   */
30  
31  package jtge.sample.chat;
32  
33  import javax.swing.JFrame;
34  import javax.swing.SwingUtilities;
35  import jtge.sample.chat.client.core.ClientFacade;
36  import jtge.sample.chat.client.ui.ClientFrame;
37  import jtge.sample.chat.server.core.ServerFacade;
38  import jtge.sample.chat.server.ui.ServerFrame;
39  
40  /***
41   * Bootstraps the application.
42   *
43   * @author Mathieu Champlon
44   * @version $Revision: 1.9 $ $Date: 2005/09/06 18:00:21 $
45   */
46  public final class Chat
47  {
48      private Chat()
49      {
50      }
51  
52      /***
53       * Program entry point.
54       *
55       * @param args -s for a server and -c for a client (repeatable)
56       */
57      public static void main( final String[] args )
58      {
59          if( args.length == 0 )
60              usage( buildCommandLine( args ) );
61          for( int i = 0; i < args.length; i++ )
62          {
63              if( args[i].equals( "-s" ) )
64                  createServer();
65              else if( args[i].equals( "-c" ) )
66                  createClient();
67              else
68              {
69                  usage( buildCommandLine( args ) );
70                  return;
71              }
72          }
73      }
74  
75      private static void createServer()
76      {
77          show( new ServerFrame( "Chat Server", new ServerFacade() ) );
78      }
79  
80      private static void createClient()
81      {
82          show( new ClientFrame( "Chat Client", new ClientFacade() ) );
83      }
84  
85      private static void show( final JFrame frame )
86      {
87          final Runnable showFrame = new Runnable()
88          {
89              public void run()
90              {
91                  frame.show();
92              }
93          };
94          try
95          {
96              SwingUtilities.invokeLater( showFrame );
97          }
98          catch( Exception e )
99          {
100             System.out.println( e );
101             e.printStackTrace();
102         }
103     }
104 
105     private static String buildCommandLine( final String[] args )
106     {
107         final StringBuffer buffer = new StringBuffer();
108         for( int i = 0; i < args.length; i++ )
109             buffer.append( " " + args[i] );
110         return buffer.toString();
111     }
112 
113     private static void usage( final String commandLine )
114     {
115         System.out.println( "[ERROR] java Chat" + commandLine );
116         System.out.println();
117         System.out.println( "java Chat [-s] [-c]" );
118         System.out.println( "options :" );
119         System.out.println( " s   start a server" );
120         System.out.println( " c   start a client" );
121         System.out.println();
122     }
123 }