//
//   QueryResultFrame.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 ParseException.java
//
// The Initial Developer of the Original Code is Frank Piorko.
//
// 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.
//
//   This code was initially contributed by SuSE GmbH Nuernberg, Germany.
//
//              (c) 2000 SuSE GmbH, Germany
//              (c) 2000 Frank Piorko <piorko@suse.de>
//
//   This file contains a class, which represents the frame, which the table with
//   the queryresults show.
//
//
//   $Id: QueryResultFrame.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//   $Log: QueryResultFrame.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:13  karl
//   import of SNIA 0_7
//
//   Revision 1.2  2001/03/20 13:22:05  davidsi
//   - Updated license.
//
//   Revision 1.1  2001/01/07 16:56:24  davidsi
//   - First checkin into main CIMOM tree.
//
//   Revision 1.7  2000/08/03 17:37:53  piorko
//   - added support for displaying array values
//
//   Revision 1.6  2000/07/17 10:13:47  piorko
//   - debug info removed
//
//   Revision 1.5  2000/07/14 17:25:41  piorko
//   - change key test for properties
//
//   Revision 1.4  2000/07/14 10:06:21  piorko
//   - some comments
//
//   Revision 1.3  2000/07/13 18:20:26  piorko
//   - key added
//   - lines added
//
//   Revision 1.2  2000/07/13 15:03:56  piorko
//   - output changed
//
//   Revision 1.1  2000/07/12 18:08:55  piorko
//   - initial version of integrated parser
//
//
//


package org.snia.wbemcmd.browser.query;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Hashtable;
import org.snia.wbem.cim.CIMInstance;
import org.snia.wbem.cim.CIMProperty;
import org.snia.wbem.cim.CIMQualifier;
import org.snia.wbem.cim.CIMDataType;

/**
 * This class represents the frame, which the results of a query in a table show.
 *
 * @author     Frank Piorko
 * @version    $Id: QueryResultFrame.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
 */
public class QueryResultFrame extends JFrame {

   /**
    * Constructor set the layout of the frame, in dependence, of the instance
    * in the vector.
    *
    * @param   strQuery	   The query string, which will show in the headline of the
    *			   frame.
    * @param   vResult	   The vector, with the instances, which shall show.
    * @param   windowPos   The Position of this window on the screen.
    */
   public QueryResultFrame(String strQuery, Vector vResult, Point windowPos) {
      //
      // set size, title and location
      //
      setSize(600, 500);
      setLocation(windowPos);
      setTitle(strQuery);
      addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
               dispose();
            }
      });
      //
      // load the line icon, which seperates the instances
      //
      ImageIcon icon=null;
      try {
        icon=new ImageIcon(ClassLoader.getSystemResource("org/snia/icons/line.gif"));
      } 
      catch (Exception e) {}
      if(icon==null) {
	 //
         // try the old location for the icons
         //
         icon=new ImageIcon("icons/line.gif");
      }
      JLabel iconLabel=null;
      if (icon!=null) {
	 iconLabel=new JLabel(icon);
      }
      //
      // get pane and set the layout
      //
      Container contentPane=getContentPane();
      JScrollPane pane=new JScrollPane();
      pane.setPreferredSize(new Dimension(600, 500));
      JPanel panel=new JPanel();
      GridBagLayout gridbag=new GridBagLayout();
      GridBagConstraints cons=new GridBagConstraints();
      panel.setLayout(gridbag);
      //
      // insert a invisible label for have more space between elements
      //
      cons.anchor=GridBagConstraints.CENTER;
      cons.gridx=0;
      int line=0;
      JLabel l=new JLabel(" ");
      cons.gridy=line;
      line++;
      gridbag.setConstraints(l, cons);
      panel.add(l);
      //
      // add a line
      //
      cons.gridy=line;
      line++;
      gridbag.setConstraints(iconLabel, cons);
      panel.add(iconLabel);
      //
      // insert a invisible label for have more space between elements
      //
      l=new JLabel(" ");
      cons.gridy=line;
      line++;
      gridbag.setConstraints(l, cons);
      panel.add(l);
      cons.anchor=GridBagConstraints.WEST;
      //
      // traverse all instances
      //
      for(int i=0; i<vResult.size(); i++) {
	 CIMInstance ci=(CIMInstance)vResult.elementAt(i);
	 //
	 // set the name of the instance, as labels
	 //
	 String strName=ci.getClassName()+".";
	 JLabel lName=new JLabel(strName);
	 lName.setForeground(Color.black);
	 cons.gridy=line;
	 line++;
	 gridbag.setConstraints(lName, cons);
	 panel.add(lName);
	 Vector keys=ci.getKeyValuePairs();
	 //
	 // set the keyvalues as labels
	 //
	 for(int j=0; j<keys.size(); j++) {
	    CIMProperty cp=(CIMProperty)keys.elementAt(j);
	    String strKey="     "+cp.getName()+"=\""+cp.getValue()+"\"";
	    if(j!=keys.size()-1) strKey=strKey+",";
	    JLabel lKey=new JLabel(strKey);
	    lKey.setForeground(Color.black);
	    cons.gridy=line;
	    line++;
	    gridbag.setConstraints(lKey, cons);
	    panel.add(lKey);
	 }
	 //
	 // insert a invisible label for have more space between elements
	 //
	 l=new JLabel(" ");
	 cons.gridy=line;
	 line++;
	 gridbag.setConstraints(l, cons);
	 panel.add(l);
	 //
	 // create a table from the instance properties and display them
	 //
	 Vector properties=ci.getProperties();
	 String[] columnNames={"Key", "Propertyname:", "Propertyvalue:"};
         Object aobjCells[][]=new Object[properties.size()][3];
	 for(int k=0; k<properties.size(); k++) {
	    CIMProperty cp=(CIMProperty)properties.elementAt(k);
	    Vector qualifiers=cp.getQualifiers();
	    for(int m=0; m<qualifiers.size(); m++) {
	       CIMQualifier qual=(CIMQualifier)qualifiers.elementAt(m);
	       if(qual.getName().equalsIgnoreCase(CIMQualifier.CIM_QUAL_KEY)) {
		  aobjCells[k][0]=qual.getName();
	       }
	    }
	    aobjCells[k][1]=cp.getName();
	    if(cp.getValue()!=null) {
	       //
	       // there is an array value
	       //
	       if((cp.getValue().isArrayValue())==true) {
		  Vector vCp=(Vector)cp.getValue().getValue();
		  String strTmpVal=new String("");
		  for(int o=0; o<vCp.size(); o++) {
		     strTmpVal+=(String)vCp.elementAt(o)+", ";
		  }
		  strTmpVal=strTmpVal.substring(0, strTmpVal.length()-2);
		  aobjCells[k][2]=strTmpVal;
	       }
	       //
	       // there is a non-array value
	       //
	       else {
		  aobjCells[k][2]=cp.getValue();
	       }
	    }
	 }
	 JTable tabInstance=new JTable(aobjCells, columnNames);
	 cons.gridy=line;
	 line++;
	 cons.fill=GridBagConstraints.HORIZONTAL;
	 cons.weightx=100;
	 gridbag.setConstraints(tabInstance, cons);
	 panel.add(tabInstance);
	 //
         // insert a invisible label for have more space between elements
         //
	 cons.fill=GridBagConstraints.NONE;
         cons.weightx=0;
	 l=new JLabel(" ");
	 cons.gridy=line;
	 line++;
	 gridbag.setConstraints(l, cons);
	 panel.add(l);
	 //
	 // add a line
	 //
	 cons.gridy=line;
	 line++;
	 cons.anchor=GridBagConstraints.CENTER;
	 iconLabel=new JLabel(icon);
	 gridbag.setConstraints(iconLabel, cons);
	 panel.add(iconLabel);
	 //
         // insert a invisible label for have more space between elements
         //
	 cons.anchor=GridBagConstraints.WEST;
	 l=new JLabel(" ");
	 cons.gridy=line;
	 line++;
	 gridbag.setConstraints(l, cons);
	 panel.add(l);
      }
      pane.getViewport().add(panel);
      contentPane.add(pane);
   }
}
