/*
 * 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.admin;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.logging.Level;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import edu.stanford.smi.protege.server.RemoteServer;
import edu.stanford.smi.protege.server.RemoteSession;
import edu.stanford.smi.protege.util.ComponentFactory;
import edu.stanford.smi.protege.util.Disposable;
import edu.stanford.smi.protege.util.Log;

public class ServerAdminPanel extends JPanel implements Disposable {

	private static final long serialVersionUID = -8915505640982212662L;

	private RemoteServer server;
	private RemoteSession session;
	private JTabbedPane tabbedPane;
	private Thread shutdownHook;

	public ServerAdminPanel(RemoteServer server, RemoteSession session) {
		this.server = server;
		this.session = session;
		initializeUI();
		installShutdownHook();
	}

	public void initializeUI() {
		setLayout(new BorderLayout());
		setPreferredSize(new Dimension(800, 600));
		tabbedPane = ComponentFactory.createTabbedPane(true);	
		if (server != null) {
			tabbedPane.addTab("Projects", createProjectsTab());
			tabbedPane.addTab("Sessions", createSessionsTab());
			tabbedPane.addTab("Server Control", createControlTab());
		}
		add(tabbedPane);
	}

	private JPanel createSessionsTab() {
		return new SessionServerPanel(server, session);
	}

	private JPanel createProjectsTab() {
		return new ProjectsServerPanel(server, session);
	}
	
	private JPanel createControlTab() {
		return new ControlServerPanel(server, session);
	}


	private void installShutdownHook() {
		shutdownHook = new Thread("Remote Project ShutdownHook") {
			@Override
			public void run() {
				try {
					server.closeSession(session);
				} catch (Throwable t) {
					Log.getLogger().log(Level.INFO, "Exception caught", t);
				}
			}
		};
		try {
			Runtime.getRuntime().addShutdownHook(shutdownHook);
		} catch (Exception e) { // happens in applets
			Log.getLogger().log(Level.WARNING, "Unable to install shutdown hook", e);			
		}        
	}

	private void uninstallShutdownHook() {
		try {
			Runtime.getRuntime().removeShutdownHook(shutdownHook);
		} catch (Exception e) { // happens in applets				
			Log.getLogger().log(Level.WARNING, "Unable to remove shutdown hook", e);			
		}
	}


	public void dispose() {
		try {
			server.closeSession(session);
		} catch (Throwable t) {
			Log.getLogger().log(Level.INFO, "Exception caught", t);
		}
		uninstallShutdownHook();
	}


}
