//
// DialogBrowser.java
//
// The contents of this file are subject to the SNIA Public License Version 1.0
// (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.snia.org/resources/openSource.html
//
// 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 DialogBrowser.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s):
//
// This source uses the Solaris WBEM APIs version 2.2. 
// Synchronization will be maintained with the Solaris WBEM APIS,
// which are being standardized through the Java Community Process.
//
//
//
// $Id: DialogBrowser.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: DialogBrowser.java,v $
// Revision 1.1  2003/07/17 18:32:55  kumpf
// HP-BAPU: PEP-64 CIM Client and CIM Listener Java Interface
//
// Revision 1.1.1.1  2001/10/31 18:15:08  karl
// import of SNIA 0_7
//
// Revision 1.10  2001/07/08 16:09:03  davidsi
// - Get DEFAULT_PORT from CIMNameSpace.
//
// Revision 1.9  2000/11/12 14:32:58  davidsi
// - Fixed bug whereby the URL was being stored back as just the host
//   name causing corruption.
//
// Revision 1.8  2000/11/06 18:22:02  davidsi
// - Added support for using URLs to specify host and port number.
//
// Revision 1.7  2000/08/16 05:59:55  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.6.6.1  2000/08/10 00:03:31  shawn
// head merge
//
// Revision 1.6  2000/08/02 16:33:06  davidsi
// - Added support for obtaining user name and password (no longer rely
//   on HTTPClient to put up dialog box).
//
// Revision 1.5  2000/05/11 10:11:55  davidsi
// - Fixed tabs.
//
// Revision 1.4  2000/05/06 17:13:51  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.3  2000/04/09 13:37:02  davidsi
// - Support for new fields (url & initial class).
// - Improved reliablity when CIMOM cannot be reached.
// - Now ensures thread managing busy dialog is killed off when initial
//   load is complete.
//
// Revision 1.2  2000/02/13 15:09:40  davidsi
// - Added text to cover re-use of Solaris WBEM APIs.
//
// Revision 1.1.1.1  2000/02/06 16:41:03  davidsi
// Initial version.
//
//
//
package org.snia.wbemcmd.browser;

import java.awt.*;
import java.io.*;
import javax.swing.*;
import java.net.*;
import javax.swing.SwingUtilities;
import org.snia.wbem.client.CIMClient;
import org.snia.wbem.cim.CIMNameSpace;
import org.snia.wbem.cim.CIMException;
//import org.snia.wbemcmd.cimom.CIMServer;
import org.snia.wbemcmd.utils.Log;
import org.snia.wbemcfg.GlobalConfig;
//
// These are just to get hold of defaults...
//
import org.snia.wbemcmd.xml.XMLNameSpace;
import org.snia.wbemcmd.cimom.CIMOMHandleXML;

public class DialogBrowser extends Frame implements DialogStatus
{
    
    private JFrame connectFrame=null;
    JLabel lbl1;
    JLabel lbl2;
    JLabel lbl3;
    String hostName;
    JProgressBar pbar;
    boolean keepRunning=true;
    
    private void createConnectFrame(String ihostName,String nameSpace) {
	connectFrame=new JFrame("Connecting...");
	
	
	
	JPanel jp=new JPanel();
	
	try {
	    if (ihostName==null || ihostName.equals("")) {
		ihostName=InetAddress.getLocalHost().getHostName();
	    }
	} catch (Exception e) {
	}
	hostName=ihostName;
	
	BoxLayout bx=new BoxLayout(jp,BoxLayout.Y_AXIS);

	jp.setLayout(bx);
	
	jp.add( new JLabel(" "));
	
	lbl1=new JLabel("Connecting to host:"+hostName);
	lbl1.setFont(new Font("SansSerif", Font.BOLD, 14));
	lbl1.setForeground(Color.black);
	jp.add( lbl1);
	jp.add( new JLabel(" "));
	lbl2=new JLabel("Namespace :"+nameSpace);
	lbl2.setFont(new Font("SansSerif", Font.BOLD, 14));
	lbl2.setForeground(Color.black);
	jp.add( lbl2);
	
	jp.add( new JLabel(" "));
	lbl3=new JLabel("");
	lbl3.setFont(new Font("SansSerif", Font.BOLD, 14));
	lbl3.setForeground(Color.black);
	jp.add( lbl3);
	
	
	pbar=new JProgressBar(0,100);
	pbar.setValue(0);
    //	pbar.setMaximumSize(new Dimension(WIDTH-95,20));
    //	pbar.setMinimumSize(new Dimension(WIDTH-95,20));
    //	pbar.setSize(WIDTH/2,100);
	pbar.setAlignmentX(Component.LEFT_ALIGNMENT );
	jp.add(pbar);
	pbar.setVisible(false);
	connectFrame.getContentPane().add("Center", jp);
	connectFrame.setSize(400,150);
	
	
	//
	// Position in middle of screen
	//
	Toolkit tk=Toolkit.getDefaultToolkit();
	Dimension screenDim=tk.getScreenSize();
	Dimension windowDim=connectFrame.getSize();
	connectFrame.setLocation((screenDim.width-windowDim.width)/2,
		    (screenDim.height-windowDim.height)/2);
	
	
	connectFrame.setVisible(true);
    }
    
    private void setConnected() {
	lbl1.setText("Connected to host:"+hostName);
	connectFrame.setTitle("Connected - downloading");
	
	pbar.setVisible(true);
	
	Runnable doit=new Runnable() {
	    public void run() {
		int perc=0;
		
		while(keepRunning) {
		    try {
			Thread.sleep(300);
		    } catch (Exception e) {}
		    pbar.setValue(perc);
		    perc += 2;
		    if (perc>100)
			perc=0;
		}
		keepRunning=true;
	    }
	};
	Thread t=new Thread(doit);
	t.start();
	    
    }
    
    public void setStatus(int perc,String msg) {
	lbl3.setText(perc+"% complete : "+msg);
    }
    public void setStatus(String msg) {
	lbl3.setText(msg);
    }
    
    void start(String args[]) {

	CBROWTree tree;
	Frame fr;
	CIMClient cimClient;
	CIMNameSpace clientNameSpace;
	String hostName=null;
	String nameSpace=XMLNameSpace.DEFNAMESPACE;
	int portNo=CIMNameSpace.DEFAULT_PORT;
	int protocol=DialogConnect.XML;
	int httpType=DialogConnect.HTTP; // Added by Bapu Patil
	String urlText="/cimom";
	String classText=null;
	String username="";
	String password="";
	
	for(;;) {
	
	    fr=new Frame("CIM Browser");
	    fr.setSize(fr.getInsets().left + fr.getInsets().right + 405,
			fr.getInsets().top + fr.getInsets().bottom + 305);


	    for(;;) {
		DialogConnect connect=new DialogConnect(fr,username,password,hostName,nameSpace,portNo, protocol,urlText,classText);
		connect.show();

		 
		if (connect.forcedClose())
		    System.exit(0); // close the application

		try {
		    int proto=CIMClient.XML;
		    hostName=connect.getHostName();
		    nameSpace=connect.getNameSpace();
		    portNo=connect.getPortNo();
		    classText=connect.getClassText();
		    username=connect.getUserName();
		    password=connect.getPassWord();
		    String attachName=hostName;


		    if (protocol==connect.XML) {
			//
			// Added by Bapu ( 07/22/02)
			// For now I assume that if you specify
			// port 5989 I will always use SSL
			//
		        httpType =connect.getHTTPType(); 
		        if (httpType ==connect.HTTPS) 
			   attachName="https://"+hostName+":"+portNo;
			else 
			   attachName="http://"+hostName+":"+portNo;
		    }
		    clientNameSpace=new CIMNameSpace(attachName,nameSpace);
		    protocol=connect.getProtocol();

		    // Bapu
		    createConnectFrame(hostName,connect.getNameSpace());
		    cimClient=new CIMClient(clientNameSpace,
						username,password,
						proto);
			//
			// Go get the features of this server
			//
			if (protocol==DialogConnect.XML) {
			// TODO cimClient.getServerFeatures();
			}
		    break;
		} catch (Exception ce) {
		   // failed to connect - just loop around...
		}
		if (connectFrame!=null) {
		    connectFrame.dispose();
		    connectFrame=null;
		}
	    }
	
	
	
	    if (connectFrame!=null) {
		setConnected();
	    }
	    if (classText!=null && classText.equals("<none>"))
		classText=null;
	
	    try {
		tree=new CBROWTree(cimClient,clientNameSpace,this,classText);
		//
		// Connection made, break out of loop and start for real..
		//
		break;
	    } catch (CIMException ce) {
		keepRunning=false;
		if (connectFrame!=null) {
		    if (ce.toString().toLowerCase().indexOf("authorization")>0) {
			CBROW.cimErrorMsg(connectFrame,"Failed to gain authorization, please try again");
		    } else
			CBROW.cimErrorMsg(connectFrame,"Failed to complete connection",ce);
		    connectFrame.dispose();
		    connectFrame=null;
		}
	    }
	}
	keepRunning=false;
	
	if (connectFrame!=null) {
	    connectFrame.dispose();
	    connectFrame=null;
	}
	

    }

    
    static public void main(String args[])  {

	//Log.assignLogFileName(GlobalConfig.clientDebugFile);
	Log.assignLogFileName(GlobalConfig.clientLogFilePath);
	
	try {
	    UIManager.setLookAndFeel(
			"com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
		//"com.sun.java.swing.plaf.motif.MotifLookAndFeel");
	} catch (Exception e) {
	}
	new DialogBrowser().start(args);
    }
    
}

