package parser.ServletParser.library;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.*;

public class ListDlg extends JDialog
{

    public Vector listData;
    private JList list ;
    private JButton chooseButton;
    private JButton cancelButton;
    private JPanel  listChooser;
	public String selectedItem;

	public ListDlg(String title,Vector data)
	{
		super();
		setModal(true);
		selectedItem = null;
		listData = data;
		setTitle(title);
		listChooser = makeForm();
		setContentPane(listChooser);
        pack();
		this.setSize(new Dimension(300,300));
        makeCenterPosition();
        this.addWindowListener( new dialogWindowClosing() );
        show();
	}
	
	public JPanel makeForm()
	{
  		JPanel panel = new JPanel();
  		panel.setLayout( new BorderLayout() );
        panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		list = new JList(listData);
		list.setSelectionForeground(Color.black);
		list.setSelectionBackground(Color.red);
		JScrollPane sp = new JScrollPane(list);
		JPanel buttonpanel = new JPanel();
        buttonpanel.setLayout(new BoxLayout(buttonpanel, BoxLayout.X_AXIS));
        buttonpanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
		JPanel sppanel = new JPanel();
 		sppanel.setLayout( new BorderLayout() );
        sppanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		
		chooseButton = new JButton("Choose");
		cancelButton= new JButton("Cancel");
		chooseButton.setMaximumSize(new Dimension(150, 20));
		cancelButton.setMaximumSize(new Dimension(150, 20));

		buttonpanel.add(chooseButton);
		buttonpanel.add(cancelButton);
		sppanel.add( sp, BorderLayout.CENTER);
		panel.add( sppanel, BorderLayout.CENTER);
		panel.add( buttonpanel, BorderLayout.SOUTH);

		chooseButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				chooseAction();}
		});
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				cancelAction();}
		});
        return panel;
	}

	public void chooseAction()
	{
		Object object = list.getSelectedValue();
		selectedItem = (String)object;
		closingWindow();
	}

	private void setSelectLine(int selectedIndex)
	{
		int lastIndex = listData.size() - 1;
		
		if ( selectedIndex > lastIndex )
		{
			list.setSelectedIndex(lastIndex);
		}
		else
		{
			list.setSelectedIndex(selectedIndex);
		}
	}	
	
	private void cancelAction()
	{
		closingWindow();
	}

	private void makeCenterPosition()
	{
		Dimension monitorSize = this.getToolkit().getScreenSize();
		Rectangle thisSize = this.getBounds();
		thisSize.x = (int)((monitorSize.width - thisSize.width)/2);
		thisSize.y = (int)((monitorSize.height - thisSize.height )/2);
		this.setBounds(thisSize);
	}

	private void closingWindow()
	{
		setVisible(false);
		dispose();
	}
	
    class dialogWindowClosing extends java.awt.event.WindowAdapter
    {
        public void windowClosing(java.awt.event.WindowEvent event)
        {
        	ListDlg.this.closingWindow();
        }
    }	
	
}

