//
// CBROWMethod.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 CBROWInstance.java
// 
// The Initial Developer of the Original Code is David Simons.
//
// Contributor(s): Scott Baker
//
//
// 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: CBROWMethod.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: CBROWMethod.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:14:59  karl
// import of SNIA 0_7
//
// Revision 1.5  2001/03/20 17:16:32  davidsi
// - Added support for parsing array elements.
//
// Revision 1.4  2000/08/16 05:59:55  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.3.8.1  2000/08/10 00:03:31  shawn
// head merge
//
// Revision 1.3  2000/05/11 10:11:55  davidsi
// - Fixed tabs.
//
// Revision 1.2  2000/05/06 17:13:50  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// Revision 1.1  2000/04/09 13:28:23  davidsi
// - Functionality moved from CBROWInstance so it can be used on
//   class definitions as well.
//
//
//
//
package org.snia.wbemcmd.browser;

import java.awt.event.*;
import java.util.Enumeration;
import java.util.Vector;
import java.util.StringTokenizer;
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.*;




    
class CBROWMethod {
    CIMClient cc;
    JFrame mainFrame;
    CIMObjectPath currentInstancePath;
    String methodName;
    JDialog argFrame;
    JTable methpropValueTable;
    TableModel methpropValues;
    Vector inParams = new Vector();
    CIMClass currentClass;
    Vector outParams = new Vector();
	
    int METHPROP_COL_TYPE = 0;
    int METHPROP_COL_NAME = 1;
    int METHPROP_COL_VALUE = 2;
    int MATHPROP_COL_WIDTH = 300;	// width
    int MATHPROP_COL_MARGIN = 20;	// margin for dialog
    int MATHPROP_COL_HEIGHT = 300;	// height	  
    int MATHPROP_COL_BHEIGHT = 100;	// button height
	
    CBROWMethod(JFrame mainFrame,
	    CIMClass currentClass,
	    CIMClient cc,
	    CIMObjectPath currentInstancePath,
	    String iMethodName) {
	this.cc=cc;
	this.mainFrame=mainFrame;
	this.currentClass=currentClass;
	this.currentInstancePath=currentInstancePath;
	methodName=new String(iMethodName);
	    
	argFrame = new JDialog(mainFrame,"Set Arguments",true);
	    
	//
	// Now position the window
	Point windowPos=mainFrame.getLocationOnScreen();
	argFrame.setLocation(windowPos.x+30,windowPos.y+30);
	
	    
	methpropValues=new TableModel(
		new String[] {"Type","Name","Value"});
	methpropValues.setColumnEditable(METHPROP_COL_VALUE,true);
	methpropValueTable=new JTable(methpropValues);
	JScrollPane methodScrollpane = new JScrollPane(
			    methpropValueTable);

	methodScrollpane.setPreferredSize(
		    new Dimension(MATHPROP_COL_WIDTH,
			     MATHPROP_COL_HEIGHT));
	    
	JPanel jp=new JPanel();
	    
	    
	JPanel jp2=new JPanel();
	    
	JButton jb=new JButton("Execute");
	jb.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		execute();
		argFrame.dispose();
	}});
	jp2.add(jb);
	    
	jb=new JButton("Cancel");
	jb.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent e) {
		argFrame.dispose();
	}});
	jp2.add(jb);
	    
	jp.add("Top",methodScrollpane);
	jp.add("Bottom",jp2);
	
	argFrame.getContentPane().add("Center", jp);
	argFrame.setBackground(Color.lightGray);
	argFrame.setSize(MATHPROP_COL_WIDTH+MATHPROP_COL_MARGIN,
		 MATHPROP_COL_HEIGHT+MATHPROP_COL_BHEIGHT);
	argFrame.addWindowListener( new WindowAdapter() {
	public void windowClosing(WindowEvent e) {
		argFrame.dispose();
	    }
	});
	updateTable();
	argFrame.show();
    }
	
	
    //
    // Updates property table
    //
    public void updateTable() {
	Vector params;
	CIMMethod method;
	    
	methpropValues.clearColumn(METHPROP_COL_TYPE);
	methpropValues.clearColumn(METHPROP_COL_NAME);
	methpropValues.clearColumn(METHPROP_COL_VALUE);
	    
	method=currentClass.getMethod(methodName);
	    
	params=method.getParameters();
	for(int i=0;i<params.size();i++) {
	    CIMParameter cp=(CIMParameter)(params.elementAt(i));  
	    if (cp.getQualifier("in")==null)
		continue;
	    methpropValues.addValue(METHPROP_COL_NAME,cp.getName());
	    methpropValues.addValue(METHPROP_COL_TYPE,cp.getType().toString());
	    methpropValues.addValue(METHPROP_COL_VALUE,"");
	}
	    
	methpropValueTable.revalidate();
	methpropValueTable.repaint();
    }
	
    void execute() {
	Vector params;
	CIMMethod method;
	    
	method=currentClass.getMethod(methodName);
	params=method.getParameters();
	for(int i=0;i<params.size();i++) {
	    CIMParameter cp=(CIMParameter)(params.elementAt(i)); 
	    if (cp.getQualifier("in")==null)
		continue;

	    //String text;
	    for(int j=0;j<methpropValues.getRowCount();j++) {
		if ( ((String)(methpropValues.getValueAt(j,METHPROP_COL_NAME))).
		      equalsIgnoreCase(cp.getName())) {
		    //
		    // SJB - 2/10/01
		    //
		    if ( cp.getType().isArrayType() ) {
		        StringTokenizer array = new StringTokenizer((String)(methpropValues.getValueAt(j,METHPROP_COL_VALUE)),",");
			Vector arrayParam = new Vector();
 		  	while ( array.hasMoreTokens() ) {
			    arrayParam.addElement( array.nextToken() );
			}
			  
	                inParams.addElement(new CIMProperty(cp.getName(),
			    new CIMValue((Vector) arrayParam,
			    cp.getType())));
		    } else
		    {
		    //
		    //
		        inParams.addElement(new CIMProperty(cp.getName(),
			    new CIMValue((String)(methpropValues.getValueAt(j,METHPROP_COL_VALUE)),
			    cp.getType())));
		    }
		}
	    }
	}
	    
	try {
	    String result;
	    CIMValue retValue;
		
	    retValue=cc.invokeMethod(currentInstancePath,
			methodName,inParams,outParams);
		
	    if (retValue==null)
		result="No Return Value";
	    else
		result=retValue.toString();
	    JOptionPane.showMessageDialog(mainFrame,
		"Finished executing "+methodName+"\nReturned:"+
		result, "Method Result",
		JOptionPane.INFORMATION_MESSAGE);
	} catch (CIMException ce) {
	    JOptionPane.showMessageDialog(mainFrame,
		    "Could not execute method :"+ce,
		    "Method Failed",
		    JOptionPane.ERROR_MESSAGE);
	}
    }
}

