//
// $Id: MiCasaApp.java,v 1.13 2004/03/06 11:29:19 mdb Exp $

package com.threerings.micasa.client;

import java.io.IOException;

import com.samskivert.swing.util.SwingUtil;
import com.threerings.util.Name;

import com.threerings.presents.client.Client;
import com.threerings.presents.net.UsernamePasswordCreds;

import com.threerings.micasa.Log;

/**
 * The micasa app is the main point of entry for the MiCasa client
 * application. It creates and initializes the myriad components of the
 * client and sets all the proper wheels in motion.
 */
public class MiCasaApp
{
    public void init ()
        throws IOException
    {
        // create a frame
        _frame = new MiCasaFrame();

        // create our client instance
        String cclass = getProperty("client");
        if (cclass == null) {
            cclass = MiCasaClient.class.getName();
        }

        try {
            _client = (MiCasaClient)Class.forName(cclass).newInstance();
        } catch (Exception e) {
            Log.warning("Unable to instantiate client class " +
                        "[cclass=" + cclass + "].");
            Log.logStackTrace(e);
        }

        // initialize our client instance
        _client.init(_frame);
    }

    public void run ()
    {
        // position everything and show the frame
        _frame.setSize(800, 600);
        SwingUtil.centerWindow(_frame);
        _frame.setVisible(true);

        Client client = _client.getContext().getClient();

        // read our server and port settings
        String server = getProperty("server");
        if (server == null) {
            server = "localhost";
        }
        int port = Client.DEFAULT_SERVER_PORT;
        String portstr = getProperty("port");
        if (portstr != null) {
            try {
                port = Integer.parseInt(portstr);
            } catch (NumberFormatException nfe) {
                Log.warning("Invalid port specification '" + portstr + "'.");
            }
        }

        // pass them on to the client
        client.setServer(server, port);

        // configure the client with some credentials and logon
        String username = getProperty("username");
        String password = getProperty("password");
        if (username != null && password != null) {
            // create and set our credentials
            client.setCredentials(
                new UsernamePasswordCreds(new Name(username), password));
            client.logon();
        }
    }

    /**
     * Attempts to get a property but doesn't freak out if we fail
     * security check.
     */
    protected String getProperty (String key)
    {
        try {
            return System.getProperty(key);
        } catch (Throwable t) {
            Log.info("Can't fetch '" + key + "' system property.");
            return null;
        }
    }

    public static void main (String[] args)
    {
        MiCasaApp app = new MiCasaApp();
        try {
            // initialize the app
            app.init();
        } catch (IOException ioe) {
            Log.warning("Error initializing application.");
            Log.logStackTrace(ioe);
        }
        // and run it
        app.run();
    }

    protected MiCasaClient _client;
    protected MiCasaFrame _frame;
}

@@ -28,29 +28,29 @@
  1.1
-3
+4
  2004/0
-3/06 11:29:19
+8/27 01:41:21
  mdb
@@ -753,30 +753,174 @@
 s = 
-getProperty(%22client%22);
+null;%0A        try %7B%0A            cclass = System.getProperty(%22client%22);%0A        %7D catch (Throwable t) %7B%0A            // security manager in effect, no problem%0A        %7D
 %0A   
@@ -1389,16 +1389,73 @@
 id run (
+String server, int port, String username, String password
 )%0A    %7B%0A
@@ -1686,470 +1686,96 @@
  // 
-read our server and port settings%0A        String server = getProperty(%22server%22);%0A        if (server == null) %7B%0A            server = %22localhost%22;%0A        %7D%0A        int port = Client.DEFAULT_SERVER_PORT;%0A        String portstr = getProperty(%22port%22);%0A        if (portstr != null) %7B%0A            try %7B%0A                port = Integer.parseInt(portstr);%0A            %7D catch (NumberFormatException nfe) %7B%0A                Log.warning(%22Invalid port specification '
+pass them on to the client%0A        Log.info(%22Using %5Bserver=%22 + server + %22, port=
 %22 + port
 str 
@@ -1774,84 +1774,18 @@
 port
-str
  + %22
-'
+%5D
 .%22);%0A
-            %7D%0A        %7D%0A%0A        // pass them on to the client%0A
     
@@ -1889,110 +1889,8 @@
 gon%0A
-        String username = getProperty(%22username%22);%0A        String password = getProperty(%22password%22);%0A
     
@@ -2145,162 +2145,245 @@
     
-/**%0A     * Attempts to get a property but doesn't freak out if we fail%0A     * security check.%0A     */%0A    protected String getProperty (String key)
+public static void main (String%5B%5D args)%0A    %7B%0A        String server = %22localhost%22;%0A        if (args.length %3E 0) %7B%0A            server = args%5B0%5D;%0A        %7D%0A%0A        int port = Client.DEFAULT_SERVER_PORT;%0A        if (args.length %3E 1) %7B
 %0A    
-%7B%0A
     
@@ -2408,135 +2408,169 @@
     
+    
-return System.getProperty(key);%0A        %7D catch (Throwable t) %7B%0A            Log.info(%22Can't fetch '%22 + key + %22' system property
+port = Integer.parseInt(args%5B1%5D);%0A            %7D catch (NumberFormatException nfe) %7B%0A                Log.warning(%22Invalid port specification '%22 + args%5B1%5D + %22'
 .%22);
@@ -2586,20 +2586,9 @@
     
-return null;
+%7D
 %0A   
@@ -2598,64 +2598,133 @@
   %7D%0A
+%0A
     
-%7D%0A%0A
     
-public static void main (String%5B%5D args)%0A    %7B
+String username = (args.length %3E 2) ? args%5B2%5D : null;%0A        String password = (args.length %3E 3) ? args%5B3%5D : null;%0A
 %0A   
@@ -2967,32 +2967,33 @@
 ioe);%0A        %7D%0A
+%0A
         // and r
@@ -3014,16 +3014,48 @@
 app.run(
+server, port, username, password
 );%0A    %7D

Allow configuration to be passed on the command line.