/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License");  you may not use this file except in 
 * compliance with the License.  You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 *
 * The Original Code is Protege-2000.
 *
 * The Initial Developer of the Original Code is Stanford University. Portions
 * created by Stanford University are Copyright (C) 2007.  All Rights Reserved.
 *
 * Protege was developed by Stanford Medical Informatics
 * (http://www.smi.stanford.edu) at the Stanford University School of Medicine
 * with support from the National Library of Medicine, the National Science
 * Foundation, and the Defense Advanced Research Projects Agency.  Current
 * information about Protege can be obtained at http://protege.stanford.edu.
 *
 */

package edu.stanford.smi.protege.server;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JFrame;

import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protege.ui.ProjectManager;
import edu.stanford.smi.protege.util.ApplicationProperties;
import edu.stanford.smi.protege.util.ComponentFactory;
import edu.stanford.smi.protege.util.Log;
import edu.stanford.smi.protege.util.SystemUtilities;

public class ClientConnectMain {
    private static Logger log = Log.getLogger(ClientConnectMain.class);
    
    private String host;
    private String user;
    private String password;
    private String projectName;
    
    private JFrame mainFrame;
   
    
    public ClientConnectMain(String[] args) {
        host = args[0];
        user = args[1];
        password = args[2];
        projectName = args[3];
    }
    
    public void connect() {
        RemoteProjectManager rpm = RemoteProjectManager.getInstance();
        Project p = rpm.getProject(host, user, password, projectName, true);
        initializeProtegeGui();
        ProjectManager.getProjectManager().setCurrentProject(p, true, false);
    }
    
    private void initializeProtegeGui() {
        ThreadGroup group = new ThreadGroup(Thread.currentThread().getThreadGroup(), "Safe Thread Group") {
            public void uncaughtException(Thread thread, Throwable throwable) {
                Log.getLogger().log(Level.SEVERE, "Uncaught Exception", throwable);
            }
        };
        SystemUtilities.initGraphics();
        mainFrame = ComponentFactory.createMainFrame();
        mainFrame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent event) {
                ProjectManager.getProjectManager().exitApplicationRequest();
            }
        });

        ProjectManager.getProjectManager().setRootPane(mainFrame.getRootPane());
        ApplicationProperties.restoreMainFrameProperties(mainFrame);
        mainFrame.setVisible(true);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        if (args.length < 4) {
            log.info("Usage: ClientConnect host/port user password project-name");
            System.exit(-1);
        }
        ClientConnectMain connectJob = new ClientConnectMain(args);
        connectJob.connect();
        // We have to give the thread time to get going or the entire application will exit!
        SystemUtilities.sleepMsec(5000);
    }

}
