//
// DialogConnect.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 DialogConnect.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s): Mahendra Ramachandran
//
//
// 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: DialogConnect.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: DialogConnect.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:10  karl
// import of SNIA 0_7
//
// Revision 1.13  2001/01/07 16:47:20  davidsi
// - Fixed extraction of password to use getPassword() - was using
//   getText() which was deprecated (missed from previous merge).
//
// Revision 1.12  2000/11/06 18:23:18  davidsi
// - Remove URL portion of dialog - no longer req'd as the url automatically
//   gets built.
//
// Revision 1.11  2000/08/16 05:59:55  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.10.2.1  2000/08/10 00:03:31  shawn
// head merge
//
// Revision 1.9  2000/08/02 16:35:23  davidsi
// - Added username and password fields, moved connection info to second
//   tabbed pane.
//
// Revision 1.8  2000/05/18 14:20:36  davidsi
// - Add support to look for icon in multiple locations.
//
// Revision 1.7  2000/05/12 13:54:09  davidsi
// - Added support for loading icons from org/snia/icons so that jar files
//   can be built easily (old icons/ is also still supported).
//
// Revision 1.6  2000/05/11 10:11:56  davidsi
// - Fixed tabs.
//
// Revision 1.5  2000/05/06 17:13:51  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.4  2000/04/09 18:14:08  davidsi
// - Fixed formatting problems.
//
// Revision 1.3  2000/04/09 13:33:40  davidsi
// - Added more configuration options to allow for communication with
//   other cimom implementations - now can specify url and initial class
//   to load.
//
// 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 javax.swing.JPanel;
import javax.swing.BorderFactory;
import javax.swing.JTabbedPane;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JDialog;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.ImageIcon;
import java.awt.*;
import java.awt.event.*;
import java.net.*;


//
// Provides the connection dialog when the browser first starts
//
class DialogConnect extends JDialog
{
    static int XML = 0;
    static private int YSPACING=14;		// distance between components
    static private String FONT_NAME="Dialog";
    static private int FONT_TYPE=Font.BOLD;
    static private String FIXED_FONT_NAME="Serif";
    static private int FIXED_FONT_TYPE=Font.BOLD;
    static private int FONT_SIZE=14;
    static private int XSPACING=20;
    JTextField userName;
    JPasswordField passWord;
    JTextField hostName;
    JTextField portNo;
    JTextField nameSpace;
    JTextField classText;
    JButton connectButton;
    JComboBox connectList;
    JComboBox httpList;
    
    boolean windowClosed=false;

    //
    // Attn: added by Bapu Patil (08/16/02)
    // for HTTP Type (https/http)
    //
    static int HTTP = 0;
    static int HTTPS = 1;
    
    public boolean forcedClose() {
	return(windowClosed);
    }
    
    public String getHostName() {
	 return(hostName.getText());
    }
    public int getPortNo() {
	Integer intv=new Integer(portNo.getText());
	return(intv.intValue());
    }
    
    public int getProtocol() {
	String connecttype=(String)connectList.getSelectedItem();
	return(XML);
    }

    //
    // Attn: added by Bapu Patil (08/16/02)
    // for HTTP Type (https/http)
    //
    public int getHTTPType() {
	String httptype=(String)httpList.getSelectedItem();
	if (httptype.equalsIgnoreCase("https"))
	    return(HTTPS);
	return(HTTP);
    }
    

	
    public String getNameSpace() {
	return(nameSpace.getText());
    }
    
    public String getClassText() {
	return(classText.getText());
    }
    


    public String getUserName() {
	return(userName.getText());
    }

    public String getPassWord() {
    	return(new String(passWord.getPassword()) );
    }
    
    public DialogConnect(Frame parent,String userNameText, String passWordText, String hostNameText, 
		String nameSpaceText,int inPortNo,int protocol,String urlText,String iclassText)
    {
	super(parent,"CIMOM Browser",true);
	String portNoText=new Integer(inPortNo).toString();
	int fontSize;
	Toolkit tk=Toolkit.getDefaultToolkit();
	Dimension screenDim=tk.getScreenSize();
	
	fontSize=FONT_SIZE;
	
	//
	// Reduce font size for smaller screens
	//
	if (screenDim.width<=800)
	    fontSize=10;
	else if (screenDim.width<=1024)
	    fontSize=12;
	
	if (hostNameText==null) {
	    try {
		hostNameText=InetAddress.getLocalHost().getHostName();
	    } catch (Exception e) {
		hostNameText="";
	    }
	}
	
	if (nameSpaceText==null)
	    nameSpaceText="";
	
	if (protocol != XML )
	    protocol=XML;
	
	JPanel horizontalPane=new JPanel();
	horizontalPane.setLayout(new BoxLayout(horizontalPane,BoxLayout.X_AXIS));
	
	ImageIcon icon=null;
	try {	
	    icon=new ImageIcon( ClassLoader.getSystemResource("org/snia/icons/wbemlogo.jpg"));
	} catch (Exception e) {}
	
	if (icon==null) {
	    //
	    // Try the original location for the icons
	    //
	    icon=new ImageIcon("icons/wbemlogo.jpg");
	}
	if (icon==null) {
		//
		// Try the new location for the icons
		//
		icon=new ImageIcon("org/snia/icons/wbemlogo.jpg");
	}
	if (icon!=null) {
	    JLabel iconLabel=new JLabel(icon );
	    horizontalPane.add(iconLabel);	
	    horizontalPane.add(Box.createRigidArea(new Dimension(XSPACING,0)));
	}
	JPanel mainVerticalPane=new JPanel();
	mainVerticalPane.setLayout(new BoxLayout(mainVerticalPane,BoxLayout.Y_AXIS));
	mainVerticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	mainVerticalPane.setAlignmentX(Component.LEFT_ALIGNMENT);
	
	//
	// Create first TAB on tabbed pannel
	//
	JTabbedPane tabbedPane = new JTabbedPane();
	
	
	JPanel verticalPane=new JPanel();
	verticalPane.setLayout(new BoxLayout(verticalPane,BoxLayout.Y_AXIS));	    
	verticalPane.setAlignmentX(Component.LEFT_ALIGNMENT);
	    

	//
	// This will be the longest label, so compute its size first
	//
	// Note that we use this size through out the dialog so that
	// flipping from tab to tab shows frames with the same kind
	// of alignment
	//
	JLabel connectLabel=new JLabel("Connection type:");
	connectLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	Dimension setSize=connectLabel.getPreferredSize();

	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));

	//
        // Attn: added by Bapu Patil (08/16/02)
	// for HTTP Type (https/http)
	//
	JLabel httpLabel=new JLabel("HTTP type:");
	httpLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	Dimension setHttpSize=httpLabel.getPreferredSize();

	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));


	//
	// User Name
	//
	JPanel userNamePanel=new JPanel();
	userNamePanel.setLayout(new BoxLayout(userNamePanel,BoxLayout.X_AXIS));
	userName=new JTextField(15);
	userName.setText(userNameText);
	Font f=userName.getFont();
	userName.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	userName.setEditable(true);
	JLabel userNameLabel=new JLabel("User Name:");
	userNameLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	userNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	userNamePanel.add(userNameLabel);
	userNamePanel.add(userName);

	userName.setMaximumSize(userName.getPreferredSize());
	userName.setMinimumSize(userName.getPreferredSize());
	userNameLabel.setMaximumSize(setSize);
	userNameLabel.setMinimumSize(setSize);
	userNamePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane.add(userNamePanel);
	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	//
	// Password
	//
	JPanel passWordPanel=new JPanel();
	passWordPanel.setLayout(new BoxLayout(passWordPanel,BoxLayout.X_AXIS));
	passWord=new JPasswordField(15);
	passWord.setText(passWordText);
	f=passWord.getFont();
	passWord.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	passWord.setEditable(true);
	passWord.setEchoChar('*');
	JLabel passWordLabel=new JLabel("Password:");
	passWordLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	passWordLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	passWordPanel.add(passWordLabel);
	passWordPanel.add(passWord);

	passWord.setMaximumSize(passWord.getPreferredSize());
	passWord.setMinimumSize(passWord.getPreferredSize());
	passWordLabel.setMaximumSize(setSize);
	passWordLabel.setMinimumSize(setSize);
	passWordPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane.add(passWordPanel);
	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	//
	// Host Name
	//
	JPanel hostNamePanel=new JPanel();
	hostNamePanel.setLayout(new BoxLayout(hostNamePanel,BoxLayout.X_AXIS));
	hostName=new JTextField(15);
	hostName.setText(hostNameText);
	f=hostName.getFont();
	hostName.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	hostName.setEditable(true);
	JLabel hostNameLabel=new JLabel("Host Name:");
	hostNameLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	hostNameLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	hostNamePanel.add(hostNameLabel);
	hostNamePanel.add(hostName);

	hostName.setMaximumSize(hostName.getPreferredSize());
	hostName.setMinimumSize(hostName.getPreferredSize());
	hostNameLabel.setMaximumSize(setSize);
	hostNameLabel.setMinimumSize(setSize);
	hostNamePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane.add(hostNamePanel);
	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));

	
	//
	// Name space
	//
	JPanel nameSpacePanel=new JPanel();
	nameSpacePanel.setLayout(new BoxLayout(nameSpacePanel,BoxLayout.X_AXIS));
	nameSpace=new JTextField(15);
	nameSpace.setText(nameSpaceText);
	nameSpace.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	hostName.setEditable(true);
	JLabel nameSpaceLabel=new JLabel("Name Space:");
	nameSpaceLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	nameSpacePanel.add(nameSpaceLabel);
	nameSpacePanel.add(nameSpace);

	nameSpace.setMaximumSize(hostName.getPreferredSize());
	nameSpace.setMinimumSize(hostName.getPreferredSize());
	nameSpaceLabel.setMaximumSize(setSize);
	nameSpaceLabel.setMinimumSize(setSize);
	nameSpacePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane.add(nameSpacePanel);
	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	


	//
	// Add second tab to tabbed panel
	//
	JPanel verticalPane2=new JPanel();
	verticalPane2.setLayout(new BoxLayout(verticalPane2,BoxLayout.Y_AXIS));     
	verticalPane2.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane2.add(Box.createRigidArea(new Dimension(0,YSPACING)));
    
	tabbedPane.addTab("Connection", null, verticalPane);
	mainVerticalPane.add(tabbedPane);
	mainVerticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	//
	// Port number
	//
	JPanel portNoPanel=new JPanel();
	portNoPanel.setLayout(new BoxLayout(portNoPanel,BoxLayout.X_AXIS));
	portNo=new JTextField(15);
	portNo.setText(portNoText);
	portNo.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	portNo.setEditable(true);
	JLabel portNoLabel=new JLabel("Port :");
	portNoLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	portNoLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	portNoPanel.add(portNoLabel);
	portNoPanel.add(portNo);

	portNo.setMaximumSize(portNo.getPreferredSize());
	portNo.setMinimumSize(portNo.getPreferredSize());
	portNoLabel.setMaximumSize(setSize);
	portNoLabel.setMinimumSize(setSize);
	verticalPane2.add(portNoPanel);
	
	verticalPane2.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	//
	// Connection Type
	//
	JPanel connectTypePane=new JPanel();
	connectTypePane.setLayout(new BoxLayout(connectTypePane,BoxLayout.X_AXIS));
		
	String[] connectTypes={"XML"};
	connectList=new JComboBox(connectTypes);
	    //
	    // Add listener to drop down list
	    //
	    connectList.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    JComboBox jc=(JComboBox)(e.getSource());
		    String connectName=(String)jc.getSelectedItem();
		}
	    });
	connectList.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	connectList.setSelectedItem(connectTypes[protocol]);
	connectList.setMaximumSize(connectList.getPreferredSize());
	connectList.setMinimumSize(connectList.getPreferredSize());
	connectLabel.setMaximumSize(setSize);
	connectLabel.setMinimumSize(setSize);
	
	connectTypePane.add(connectLabel);
	connectLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

	connectTypePane.add(connectList);
	connectTypePane.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane2.add(connectTypePane);
	verticalPane2.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	tabbedPane.addTab("Advanced", null, verticalPane2);
	
	//
        // Attn: added by Bapu Patil (08/16/02)
	// for HTTP Type (https/http)
	//
	JPanel httpTypePane=new JPanel();
	httpTypePane.setLayout(new BoxLayout(httpTypePane,BoxLayout.X_AXIS));
		
	String[] httpTypes={"HTTP","HTTPS"};
	httpList=new JComboBox(httpTypes);
	    //
	    // Add listener to drop down list
	    //
	    httpList.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    JComboBox jc=(JComboBox)(e.getSource());
		    String httpName=(String)jc.getSelectedItem();
		}
	    });
	httpList.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	httpList.setSelectedItem(httpTypes[protocol]);
	httpList.setMaximumSize(httpList.getPreferredSize());
	httpList.setMinimumSize(httpList.getPreferredSize());
	httpLabel.setMaximumSize(setHttpSize);
	httpLabel.setMinimumSize(setHttpSize);
	
	httpTypePane.add(httpLabel);
	httpLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

	httpTypePane.add(httpList);
	httpTypePane.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane2.add(httpTypePane);
	verticalPane2.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	tabbedPane.addTab("Advanced", null, verticalPane2);
	
	//
	// Add third tab to tabbed panel
	//
	JPanel verticalPane3=new JPanel();
	verticalPane3.setLayout(new BoxLayout(verticalPane3,BoxLayout.Y_AXIS));     
	verticalPane3.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane3.add(Box.createRigidArea(new Dimension(0,YSPACING)));

	JPanel classPanel=new JPanel();
	classPanel.setLayout(new BoxLayout(classPanel,BoxLayout.X_AXIS));
	
	classText=new JTextField(15);
	if (iclassText==null)
	    classText.setText("<none>");
	else
	    classText.setText(iclassText);
	classText.setFont(new Font(FIXED_FONT_NAME,FIXED_FONT_TYPE,fontSize));
	classText.setEditable(true);
	JLabel classLabel=new JLabel("Class :");
	classLabel.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	classLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	classPanel.add(classLabel);
	classPanel.add(classText);

	classText.setMaximumSize(portNo.getPreferredSize());
	classText.setMinimumSize(portNo.getPreferredSize());
	classLabel.setMaximumSize(setSize);
	classLabel.setMinimumSize(setSize);
	classLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	verticalPane3.add(classPanel);
	verticalPane3.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	tabbedPane.addTab("Classes", null, verticalPane3);
		
	//
	// Connect button
	//
	connectButton = new JButton("Connect");
	connectButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		setVisible(false);
		dispose();	    // Needed on UnixWare to ensure dialog
				    // returns to caller (othewise it blocks
				    // indefinitely
	    }
	});
	connectButton.setFont(new Font(FONT_NAME,FONT_TYPE,fontSize));
	//
	// Double space to the connect button...
	//
	mainVerticalPane.add(connectButton);
	mainVerticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	horizontalPane.add(mainVerticalPane);
	horizontalPane.add(Box.createRigidArea(new Dimension(XSPACING,0)));
	
	Container cp=getContentPane();

	cp.add(horizontalPane,BorderLayout.WEST);

	verticalPane.setMaximumSize(verticalPane.getPreferredSize());
	verticalPane.setMinimumSize(verticalPane.getPreferredSize());
    
	
	pack();
	setTitle("Connect to CIMOM");
	
	//
	// Now position the window in the centre of the screen
	//
	Dimension windowDim=getSize();
	setLocation((screenDim.width-windowDim.width)/2,
		    (screenDim.height-windowDim.height)/2);

	//
	// Add listener to see if window is closed 
	//
	addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent event) {
		windowClosed=true;
	    }
	});
	
	setResizable(false);
    }	
}
