//
// CBROWNamespaces.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 CBROWNamespaces.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: CBROWNamespaces.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: CBROWNamespaces.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:02  karl
// import of SNIA 0_7
//
// Revision 1.7  2000/08/16 05:59:55  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.6.8.1  2000/08/10 00:03:31  shawn
// head merge
//
// Revision 1.6  2000/05/11 10:11:55  davidsi
// - Fixed tabs.
//
// Revision 1.5  2000/05/06 17:13:50  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.4  2000/04/09 17:54:57  davidsi
// - Restructured code so that dispose() only occurs over the graphical
//   element prior to extracting the selected namespace.
//
// Revision 1.3  2000/04/09 13:35:52  davidsi
// - No longer assumes there is a root namespace, instead the namespace
//   that is specified as the initial space will be assumed.
//
// 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.event.*;
import java.util.Enumeration;
import java.util.Vector;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.tree.*;
import org.snia.wbem.client.*;
import org.snia.wbem.cim.CIMNameSpace;
import org.snia.wbem.cim.CIMException;
import org.snia.wbem.cim.CIMValue;
import org.snia.wbem.cim.CIMDataType;
import org.snia.wbem.cim.CIMProperty;
import org.snia.wbem.cim.CIMObjectPath;
import org.snia.wbem.cim.CIMInstance;
import org.snia.wbem.cim.CIMClass;
import org.snia.wbem.cim.CIMMethod;

//
// Dialog displaying available namespaces and providing the ability
// to create new namespaces
//
class CBROWNamespaces	{
    String selectedNamespace="";
    private static final int NAMESPACE_NAME_COL = 0;
    //
    // Sizes
    //
    private static final int KEY_COL_SIZE   = 200;
    private static final int PROP_COL_SIZE = 400;
    private static final int XSPACING=40;
    private static final int YSPACING=20;

    
    Frame origParent;
    CIMClient cc;
    CIMNameSpace cn;
    NameSpaceDialog nameSpaceDialog;
    
    class NameSpaceDialog extends JDialog  {
    
    JTable propKeyTable;		// instances
    Vector nameSpaces=new Vector();	// a vector of strings containing name spaces
    TableModel propKeys;
    int currentIndex=-1;		// index into key list for current item
    int currentMethod=-1;		// index for current method
    boolean newInstance=false;
    JScrollPane scrollKeyPane;
    boolean realInstance=false; 	// TRUE when instance is a real
					// instance (e.g. not in process of
					// being created)
    JDialog anotherThis;

    
    
    
    public NameSpaceDialog(Frame parent,Point windowPos) {

	super(parent,"Select Namespace",true);
	anotherThis=this;
	selectedNamespace=cn.getNameSpace();
	
	setLocation(windowPos.x+30,windowPos.y+30);
	

		
	propKeys=new TableModel(new String[] {"Namespaces Available"});
	propKeyTable=new JTable(propKeys);

	
	final ListSelectionModel lsm=propKeyTable.getSelectionModel();
	lsm.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	//
	// Handles instance selections	
	//
	lsm.addListSelectionListener(new ListSelectionListener() {
	public void valueChanged(ListSelectionEvent e) {
		boolean forceinstanceupdate=false;
		int newindex=lsm.getMaxSelectionIndex();
		if (currentIndex==newindex || newindex==-1)
		    return;
    
		currentIndex=newindex;	    
		selectedNamespace=((String)(propKeys.getValueAt(currentIndex,NAMESPACE_NAME_COL)));
	    }
	});
	scrollKeyPane = new JScrollPane(propKeyTable);
	scrollKeyPane.setPreferredSize(new Dimension(KEY_COL_SIZE, 300));
	
	JPanel verticalPane=new JPanel();
	verticalPane.setLayout(new BoxLayout(verticalPane,BoxLayout.Y_AXIS));	    
	verticalPane.setAlignmentX(Component.LEFT_ALIGNMENT);
	
	verticalPane.add(scrollKeyPane);    
	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	
	//
	// add buttons
	// 
	JPanel horizPane=new JPanel();
	horizPane.setLayout(new BoxLayout(horizPane,BoxLayout.X_AXIS));     
	horizPane.setAlignmentX(Component.CENTER_ALIGNMENT);
	horizPane.add(Box.createRigidArea(new Dimension(XSPACING,0)));
	
	JButton createButton = new JButton("Create New");
	createButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		createNameSpace();
	    }
	});
	horizPane.add(createButton);
	horizPane.add(Box.createRigidArea(new Dimension(XSPACING,0)));

	JButton selectButton = new JButton("Select");
	selectButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		setVisible(false);
		dispose();	    // Needed on UnixWare to ensure dialog
				    // returns to caller (othewise it blocks
				    // indefinitely 
	    }
	});
	horizPane.add(selectButton);
	horizPane.add(Box.createRigidArea(new Dimension(XSPACING,0)));
	
	
	JButton deleteButton = new JButton("Delete");
	deleteButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		try {
		    cc.deleteNameSpace(new CIMNameSpace(null,selectedNamespace));
		    updateNameSpaceList();
		} catch (CIMException ce) {
		    CBROW.cimErrorMsg(anotherThis.getContentPane(),"Failed to delete namespace",ce); 
		}
	    }
	});
	horizPane.add(deleteButton);
	horizPane.add(Box.createRigidArea(new Dimension(XSPACING,0)));
	
	
	JButton OKButton = new JButton("Finish");
	OKButton.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		setVisible(false);
		selectedNamespace=null;
		dispose();	    // Needed on UnixWare to ensure dialog
				    // returns to caller (othewise it blocks
				    // indefinitely
		
	    }
	});
	horizPane.add(OKButton);
	horizPane.add(Box.createRigidArea(new Dimension(XSPACING,0)));

	verticalPane.add(horizPane);
	verticalPane.add(Box.createRigidArea(new Dimension(0,YSPACING)));
	updateNameSpaceList();
	
	Container cp=getContentPane();
	cp.add(verticalPane,BorderLayout.WEST);
	pack();
    }

    public void updateNameSpaceList() {
	Enumeration elist;
	int length=0;

	try {
	    elist=cc.enumNameSpace(new CIMObjectPath("",(Vector)null),
				   CIMClient.DEEP);
	    nameSpaces=new Vector();
	    propKeys.clearColumn(NAMESPACE_NAME_COL);
	    if (elist!=null) {
		while (elist.hasMoreElements()) {
		    String obj=(String)(elist.nextElement());
		    propKeys.addValue(0,obj);
		    nameSpaces.addElement(obj);
		    length++; 
		}
	    }
	} catch (CIMException ce) {
	    CBROW.cimErrorMsg(anotherThis,"Failed to fetch enumeration of instances",ce);
	}
	scrollKeyPane.setPreferredSize(new Dimension(KEY_COL_SIZE, 300));
		    
	propKeyTable.revalidate();
	propKeyTable.repaint();
    }
    }

    
   


    
    
    
    
    
    class CreateNamespaceDisplay  {

	JDialog argFrame;
	int PROPVAL_COL_WIDTH = 300;	    // width
	int PROPVAL_COL_MARGIN = 20;	    // margin for dialog
	int PROPVAL_COL_HEIGHT = 300;	    // height	      
	int PROPVAL_COL_BHEIGHT = 100;	    // button height
	

	final int VERT_MARGIN=10;	// marging around vertical
	final int HORIZ_MARGIN=10;	// marging around vertical
	final int VERT_SPACER=10;	// spacing vertically between items
	final int HORIZ_SPACER=40;	// spacing vertically between items
	
	CreateNamespaceDisplay(String inpNamespace)  {
	    final String pNamespace=inpNamespace;
	    
	    argFrame = new JDialog(origParent,
				   "Create Namespace in "+pNamespace,true);

	    //
	    // Position the window
	    //
	    Point windowPos=nameSpaceDialog.getContentPane().getLocationOnScreen();
	    argFrame.setLocation(windowPos.x+30,windowPos.y+30);
	    
	    JPanel jp=new JPanel();
	    jp.setLayout(new BoxLayout(jp,BoxLayout.X_AXIS));
	    jp.setAlignmentX(Component.LEFT_ALIGNMENT);
	    
	    JPanel jpText=new JPanel();
	    jpText.setLayout(new BoxLayout(jpText,BoxLayout.X_AXIS));
	    jpText.setAlignmentX(Component.LEFT_ALIGNMENT);
	    
	    JLabel nameLabel=new JLabel("Namespace:");
	
	    final JTextField propValue=new JTextField(15);
	    propValue.setEditable(true);
	    nameLabel.setMaximumSize(nameLabel.getPreferredSize());
	    jpText.add(nameLabel);
	    jpText.add(propValue);
	    
	    
	    JPanel jpButton=new JPanel();
	    jpButton.setLayout(new BoxLayout(jpButton,BoxLayout.X_AXIS));
	    jpButton.setAlignmentX(Component.LEFT_ALIGNMENT);
	    JButton jbOk=new JButton("OK");
	    jbOk.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    String text=propValue.getText();
		    try {
			cc.createNameSpace(new CIMNameSpace(cn.getHost(),
					    pNamespace+"/"+text));
			nameSpaceDialog.updateNameSpaceList();
			argFrame.dispose();
		    } catch ( CIMException ce) {
			CBROW.cimErrorMsg(nameSpaceDialog.getContentPane(),
					  "Failed to create namespace",ce); 
		    }
		    
	    }});
	    JButton jbCancel=new JButton("CANCEL");
	    jbCancel.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    argFrame.dispose();
	    }});
	    
	    jp.setBorder(BorderFactory.createEmptyBorder(
				HORIZ_MARGIN,VERT_MARGIN,VERT_SPACER,HORIZ_MARGIN));
			
	    jpButton.add(Box.createHorizontalGlue());
	    jpButton.add(jbOk);
	    jpButton.add(Box.createRigidArea(new Dimension(HORIZ_SPACER,0)));
	    jpButton.add(jbCancel);
	    jpButton.setBorder(BorderFactory.createEmptyBorder(
				    0,VERT_SPACER,HORIZ_MARGIN,VERT_MARGIN));
			
	    jp.add(jpText);
	    jp.setBorder(BorderFactory.createEmptyBorder(
				HORIZ_MARGIN,VERT_MARGIN,VERT_SPACER,HORIZ_MARGIN));
	    
	    Container jp2=argFrame.getContentPane();
	    jp2.add(jp,BorderLayout.NORTH);
	    jp2.add(jpButton,BorderLayout.SOUTH);
	    
	
	    argFrame.pack();
	    
	    argFrame.addWindowListener( new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
		    argFrame.dispose();
		}
	    });

	    argFrame.show();
	}
    }


    
    public CBROWNamespaces(Frame parent,CIMClient icc,CIMNameSpace icn,Point windowPos) {
	origParent=parent;
	cc=icc;
	cn=icn;
	nameSpaceDialog=new NameSpaceDialog(parent,windowPos);
	nameSpaceDialog.show();
    }
    
    
    private void createNameSpace() {
	CreateNamespaceDisplay pd=new CreateNamespaceDisplay(selectedNamespace);
    }


    public String getNamespace() {
	return(selectedNamespace);
    }
    

}
