/*
 * Created on Jan 5, 2004
 *
 */
package LumiSoft.UI.Controls;

import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * 
 */
public class WComboPopUp extends JPopupMenu implements MouseListener,MouseMotionListener,KeyListener,PopupMenuListener
{
	private WComboBox        m_pWComboBox = null;
	private JList            m_pList      = null;
	private DefaultListModel m_pItems     = null; 
	
	public WComboPopUp(WComboBox combo)
	{
		m_pWComboBox = combo;
		combo.addKeyListener(this);
		
		m_pList  = new JList();
		
		m_pItems = new DefaultListModel();				
		for(WComboItem item : combo.getItems()){
			m_pItems.addElement(item);
		}
		
		m_pList.addMouseListener(this);
		m_pList.addMouseMotionListener(this);		
		m_pList.setModel(m_pItems);
		m_pList.setFont(combo.getFont());
		m_pList.setFocusable(false);
		if(m_pItems.size() > combo.getMaxVisibleItems()){
			m_pList.setVisibleRowCount(combo.getMaxVisibleItems());
		}
		else{
			m_pList.setVisibleRowCount(m_pItems.size());			
		}
		
		JScrollPane listScrollPane = new JScrollPane(m_pList);
		listScrollPane.setBorder(null);
	
		if(m_pList.getPreferredScrollableViewportSize().width + 20 < combo.getWidth()){
			this.setPopupSize(combo.getWidth(),m_pList.getPreferredScrollableViewportSize().height + 7);
		}
		else{
			this.setPopupSize(m_pList.getPreferredScrollableViewportSize().width + 20,m_pList.getPreferredScrollableViewportSize().height + 7);
		}
		
		this.add(listScrollPane);
	
		this.setBorder(BorderFactory.createEtchedBorder());
		this.setFocusable(false);
		this.addPopupMenuListener(this);
	}
	
	
	public void mouseClicked(MouseEvent e){}
	public void mouseEntered(MouseEvent e){}
	public void mouseExited(MouseEvent e){}
	public void mousePressed(MouseEvent e){}
	public void mouseDragged(MouseEvent e){}
	
	public void mouseMoved(MouseEvent e)
	{
		int index = m_pList.locationToIndex(e.getPoint());
		if(index > -1){
			m_pList.setSelectedIndex(index);
		}
	}
	
	public void mouseReleased(MouseEvent e)
	{
		if(e.getModifiers() == MouseEvent.BUTTON1_MASK){
			if(m_pList.getSelectedValue() != null){
				m_pWComboBox.OnPopupItemSelected((WComboItem)m_pList.getSelectedValue());
			}
			
			this.setVisible(false);
		}
	}
	
	public void keyTyped(KeyEvent e){}	
	public void keyPressed(KeyEvent e){}
	
	public void keyReleased(KeyEvent e)
	{
		if(e.getKeyCode() == KeyEvent.VK_UP){
			if(m_pList.getSelectedIndex() - 1 > -1){
				m_pList.setSelectedIndex(m_pList.getSelectedIndex() - 1);
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_DOWN){
			if(m_pList.getSelectedIndex() + 1 < m_pItems.size()){
				m_pList.setSelectedIndex(m_pList.getSelectedIndex() + 1);
			}
		}
		else if(e.getKeyCode() == KeyEvent.VK_ESCAPE){
			this.setVisible(false);
		}
		else if(e.getKeyCode() == KeyEvent.VK_ENTER){
			if(m_pList.getSelectedValue() != null){
				m_pWComboBox.OnPopupItemSelected((WComboItem)m_pList.getSelectedValue());
				
				this.setVisible(false);
			}
		}
	}
	
	public void popupMenuCanceled(PopupMenuEvent e){}	
	public void popupMenuWillBecomeVisible(PopupMenuEvent e){}
	
	public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
	{
		m_pWComboBox.OnPopupClosed();
	}

}
