//
// CBROWNode.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 CBROWNode.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: CBROWNode.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
// $Log: CBROWNode.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.5  2000/08/16 05:59:55  shawn
// merge P_TOMCAT31 branch
//
// Revision 1.4.8.1  2000/08/10 00:03:31  shawn
// head merge
//
// Revision 1.4  2000/05/11 10:11:55  davidsi
// - Fixed tabs.
//
// Revision 1.3  2000/05/06 17:13:50  davidsi
// - Brought in line with SUN WBEM APIs 2.2
//
// 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.util.Vector;
import javax.swing.tree.DefaultMutableTreeNode;
import org.snia.wbem.cim.CIMClass;


//
//  Node for tree display of classes
//
class CBROWNode extends DefaultMutableTreeNode
{
    int index=0;

    public CBROWNode(String o) {
	super(o);
	index=0;
    }
    

    public boolean insertIntoSuper(String className,String parentClassName) {
	int childCount=getChildCount();
	for(int i=0;i<childCount;i++) {
	    CBROWNode child=(CBROWNode)getChildAt(i);
	    String data=(String)(child.getUserObject());

	    if (data.equalsIgnoreCase(parentClassName)) {
		 CBROWNode newNode = new CBROWNode(className);
		 child.insert(newNode,child.index++);
		 return(true);
	    }
	    if (child.insertIntoSuper(className,parentClassName))
		return(true);
	}
	return(false);
    }
 
    public void addCIMClass(CIMClass cimClass,Vector v) {
	String className=cimClass.getName();
    
	if (cimClass.getSuperClass()==null) {
	    CBROWNode newNode = new CBROWNode(className);
	    insert(newNode,index++);
	} else {
	    String parent=cimClass.getSuperClass();
	    if (insertIntoSuper(className,parent))
		return;
	    //
	    // Could not find parent, so search for it
	    //
	    for(int i=0;i<v.size();i++) {
		CIMClass cc=(CIMClass)(v.elementAt(i));
		if (cc.getName().equalsIgnoreCase(parent)) {
		    v.removeElementAt(i);
		    addCIMClass(cc,v);
		    insertIntoSuper(className,parent);
		    return;	// only have a single parent
		}
	    }
	    //
	    // Never found a class for the parent, simply ignore
	    //
	}
    }

    public int getChildCount() {
	return super.getChildCount();
    }
   
}
