//
//   QueryFrame.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 frame, where can
//   insert the query.
//
//
//   $Id: QueryFrame.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
//
//


package org.snia.wbemcmd.browser.query;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.util.Enumeration;

import org.snia.wbem.client.CIMClient;
import org.snia.wbem.cim.CIMClass;
import org.snia.wbem.cim.CIMNameSpace;
import org.snia.wbem.cim.CIMObjectPath;
import org.snia.wbem.cim.CIMException;
import org.snia.wbemcmd.browser.query.QueryResultFrame;

/**
 * This class represent the GUI for the frame, where the query can be insert.
 *
 * @author     Frank Piorko
 * @version    $Id: QueryFrame.java,v 1.1 2003/07/17 18:32:55 kumpf Exp $
 */
public class QueryFrame extends JFrame {

   /**
    * The textarea, where the query can be insert.
    */
   private JTextArea taQuery;
   /**
    * The button, which start the execution of the query.
    */
   private JButton btnParse;
   /**
    * The reference to the client.
    */
   private CIMClient ccl;
   /**
    * The vector, which holds the result of the query.
    */
   private Vector vQueryResult;
   /**
    * The window position of the mainframe.
    */
   private Point winPos;

   private CIMClass lcls;
   private CIMNameSpace lns;
   /**
    * The constructor setup the window, start the exexcution of a query and
    * start the display of the result.
    *
    * @param   cc          The reference to the client.
    * @param   windowPos   The position of the window.
    */
   public QueryFrame(CIMClient cc, CIMNameSpace ns, CIMClass cls, Point windowPos) {
      //
      // set the client and window position
      //
      ccl=cc;
      lcls=cls;
      lns=ns;
      winPos=windowPos;
      //
      // set title, size and position of the window
      //
      setTitle("Execute query");
      setSize(605, 300);
      winPos.x+=30;
      winPos.y+=180;
      setLocation(winPos);
      winPos.x-=30;
      winPos.y-=180;
      //
      // add the window listener
      //
      addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent e) {
            dispose();
         }
      });
      //
      // general layout instructions
      //
      Container contentPane=getContentPane();
      JPanel jp=new JPanel();
      GridBagLayout gridbag=new GridBagLayout();
      GridBagConstraints cons=new GridBagConstraints();
      jp.setLayout(gridbag);
      //
      // layout for the label
      //
      cons.fill=GridBagConstraints.NONE;
      cons.gridx=0;
      cons.gridy=0;
      cons.anchor=GridBagConstraints.WEST;
      JLabel label=new JLabel("Please insert a query:");
      label.setForeground(Color.black);
      gridbag.setConstraints(label, cons);
      jp.add(label);
      //
      // layout for the textarea
      //
      cons.fill=GridBagConstraints.HORIZONTAL;
      cons.gridx=0;
      cons.gridy=1;
      cons.weightx=100;
      cons.weighty=100;
      cons.fill=GridBagConstraints.BOTH;
      cons.anchor=GridBagConstraints.CENTER;
      taQuery=new JTextArea("SELECT Name FROM CIM_ManagedElement ", 5, 45);
      taQuery.setLineWrap(true);
      JScrollPane scrollPane=new JScrollPane(taQuery);
      gridbag.setConstraints(scrollPane, cons);
      jp.add(scrollPane);
      //
      // layout for the button
      //
      cons.fill=GridBagConstraints.NONE;
      cons.gridx=0;
      cons.gridy=2;
      cons.fill=GridBagConstraints.NONE;
      cons.weightx=0;
      cons.weighty=0;
      cons.anchor=GridBagConstraints.SOUTH;
      btnParse=new JButton("Execute query");
      gridbag.setConstraints(btnParse, cons);
      btnParse.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            //
            //
           try {
               Enumeration enum=ccl.execQuery(
                  new CIMObjectPath(lcls.getName(),lns.getNameSpace()),
                     taQuery.getText(),CIMClient.WQL1);
               vQueryResult=new Vector();
               while (enum.hasMoreElements())
                  vQueryResult.addElement(enum.nextElement());
               Point wPos=winPos;
               wPos.x+=30;
               wPos.y+=30;
               QueryResultFrame frameTbl=new QueryResultFrame(taQuery.getText()
                                             .trim(), vQueryResult, wPos);
               frameTbl.pack();
               frameTbl.show();
               wPos.x-=30;
               wPos.y-=30;
            }
            catch(CIMException cqe) {
               System.err.println("Exception ="+cqe);
               JOptionPane.showMessageDialog(getContentPane(), cqe.getMessage(),
                              "Query Error", JOptionPane.ERROR_MESSAGE);
            }
         }
      });
      jp.add(btnParse);
      contentPane.add(jp);
   }
}
