import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
import javax.swing.border.*;
import java.sql.*;
import oracle.jdbc.*;
import oracle.sql.*;


public class Nevsor extends JDialog implements ActionListener, ItemListener, ListSelectionListener {


    private final String strOK = "OK";
    private final String strMegsem = "M\u00e9gsem";
    private JButton okGomb;
    private JButton megsemGomb;
	private JPanel adatPanel = null;
    private JComboBox cbox;
    private String m_loginName;
	private JTextField m_subNameBox;
	private JTextField m_db;
    private JPanel kontener;
    private Menu1 szuloPanel;
    private JList lista=null;
    private JScrollPane listagorgeto;
	private JPanel gombPanel= null;
	private JPanel listaPanel= null;
	private DefaultListModel listModel=null;
	int ndb=0;
	
	
public Nevsor(JFrame parent){
    	super(parent, "Hallgat\u00f3i lista", true);
    	szuloPanel = (Menu1)parent;
		setDefaultCloseOperation(HIDE_ON_CLOSE);
        this.setBounds(320,220,380,340);
        
        cbox = new JComboBox();
    	cbox.setPreferredSize(new Dimension(180,20));
    	cbox.addItem("<< v\u00e1lassz >>");
    	
       	try{
    	Statement statement = szuloPanel.con.createStatement();       
        ResultSet res = statement.executeQuery( "select Tant_kod from Tantargy" );
        String s;
            while ( res.next() )
            {
    			 s = res.getString(1);
                 cbox.addItem(s);
            }
	   	}
    	catch(SQLException ex){
    		System.err.println( ex.getMessage() );
    	}
    	
    	
    	
    	okGomb = new JButton(strOK);
	    okGomb.addActionListener(this);
	    megsemGomb = new JButton(strMegsem);
	    megsemGomb.addActionListener(this);
	    
	    gombPanel = new JPanel(new FlowLayout( FlowLayout.RIGHT));
	    gombPanel.add(new JLabel("db:"));
	    m_db = new JTextField("",3);
	    m_db.setBackground(this.getBackground());
		m_db.setEditable(false);
	    gombPanel.add(m_db);
	    gombPanel.add(new JLabel("                             "));
	    gombPanel.add(okGomb);
	    gombPanel.add(megsemGomb);
	    
	    adatPanel = new JPanel(new GridLayout(2,1,5,10));
	    adatPanel.setBorder(new EmptyBorder(10,0,0,10));
	    adatPanel.add(new JLabel("        Tant\u00e1rgy k\u00f3d:         "));
	    adatPanel.add(cbox);
	    
	    adatPanel.add(new JLabel("        Tant\u00e1rgy neve:     "));
        m_subNameBox = new JTextField("",16);
    
        m_subNameBox.setPreferredSize(new Dimension(180,20));
	    adatPanel.add( m_subNameBox );
	    cbox.addItemListener(this);
	    
	    listaPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
	    listModel = new DefaultListModel();
	    lista = new JList(listModel);	
	    lista.setSelectedIndex(0);			    
        lista.setFixedCellWidth(300);
        lista.setFixedCellHeight(20);
    
    
        lista.addListSelectionListener(this);
        listagorgeto = new JScrollPane(lista, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        listaPanel.add(new JLabel("Tant\u00e1rgyat felveheti: "));
        listaPanel.add(listagorgeto);

            
	    kontener = new JPanel(new BorderLayout());
	    kontener.add(adatPanel,BorderLayout.NORTH);
	    kontener.add(listaPanel,BorderLayout.CENTER);
	    kontener.add(gombPanel,BorderLayout.SOUTH);
	    getContentPane().add(kontener);
        setLocationRelativeTo(parent);
	   
	}
	
	 public void actionPerformed(ActionEvent ae){
 	   String str = ae.getActionCommand();
 	   if(str.equals(strOK)){
	
			szuloPanel.removePanel(this);
 	   }
 	   else if(str.equals(strMegsem)){
 		szuloPanel.removePanel(this);
 	   }
    }
    public void itemStateChanged(ItemEvent e){
    
    	String item = (String)e.getItem();
    	String[] elof = null;
    	String[] telj = null;
    	String nev,nkod;
    	ndb=0;
    	
    	listModel.removeAllElements();
    
    

		if(e.getStateChange()== ItemEvent.DESELECTED)
			return;
        if(e.getItem().equals("<< v\u00e1lassz >>"))
        {
        	m_subNameBox.setText("");
        	listModel.removeAllElements();
        	return;
        }
        else{
    	try{
    	PreparedStatement statement1 = szuloPanel.con.prepareStatement("SELECT T_nev FROM Tantargy WHERE Tant_kod = ? " );       
        statement1.setString(1, (String)e.getItem());
        ResultSet res1 = statement1.executeQuery();
        String s;
        while ( res1.next() )
        {
            s = res1.getString(1);
            // System.err.println(m_subNameBox);
            m_subNameBox.setText(s);
        	// System.err.println(m_subNameBox.getText() );
    	}
    	}
    	catch(SQLException ex){
 
        	JOptionPane.showMessageDialog(this,ex.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
    	}
    
    
    try{
    	PreparedStatement statement2 = szuloPanel.con.prepareStatement("SELECT elofeltetelek from Tantargy where tant_kod  = ? " );       
	   	statement2.setString(1, (String)e.getItem());
       
        ResultSet res2 = statement2.executeQuery();
          while ( res2.next() )
            {
            	ARRAY array = ((OracleResultSet)res2).getARRAY ("elofeltetelek");
             // System.out.println(array);

                elof = (String[])array.getArray ();
               
           /* for(int i= 0; i<elof.length; i++){
	               System.out.println(elof[i]);
	               
	               // listModel2.addElement(tr[i]);
	               
               }*/
            
            }
	   	}
    	catch(SQLException exx){
    	//	System.err.println( ex.getMessage() );
    	   JOptionPane.showMessageDialog(this,exx.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
    	}
    	

    	
    	try{
    	PreparedStatement statement2 = szuloPanel.con.prepareStatement("SELECT  h.nev, h.n_kod, l.teljesitesek from hallgato h, leckekonyv l where h.n_kod = l.n_kod" );       
	   	
        ResultSet res2 = statement2.executeQuery();
          while ( res2.next() )
            {
            	ARRAY array = ((OracleResultSet)res2).getARRAY ("teljesitesek");
             // System.out.println(array);

                telj = (String[])array.getArray ();
                nev = res2.getString("nev");
                nkod = res2.getString("n_kod");
                
             //   System.out.println("nev, nkod" + nev+nkod);
                if(!szerepelBenne1(item, telj)){
                 	
               	if(elof.length==0){
                	//	System.out.println("0"+" " + nev+" "+nkod);
                		listModel.addElement(nev+" - "+nkod);
               	        ndb++;	
                	}
                	else{
                		if(szerepelBenne(elof, telj)){
            
                			listModel.addElement(nev+" - "+nkod);
               	 			ndb++;
                		}
                	}
                }
            
            }
	   	}
    	catch(SQLException exx){
    
    	   JOptionPane.showMessageDialog(this,exx.getMessage(),"HIBA",JOptionPane.ERROR_MESSAGE);
    	}
    	m_db.setText(""+ndb);
        }
    }
    
    public void valueChanged(ListSelectionEvent event){
    	
    }
 
    public Object getComboBox(){
  	   Object o = cbox.getSelectedItem();
  	   return o;	
    }
    public boolean szerepelBenne1(String a, String[] t){
    	for(int i = 0 ; i <t.length ; i++ )
    	{
    		if(a.equals(t[i]))
    			return true;
    	}
    	return false;	
    }
    
    public boolean szerepelBenne(String[] t1, String[] t2){
    	for(int i = 0 ; i <t1.length ; i++ ){
    		if(!szerepelBenne1(t1[i],t2))
    			return false;
    	}
    	return true;
    }
	
   

}