package j3deditor.bin.editor.tools;

import j3deditor.bin.components.PopupDialog;

import j3deditor.bin.util.Utilities;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;


/**
 * A dialog that makes user to choose an index of something.
 * 
 * @author Risto Seene
 */
public class IndexDialog extends PopupDialog{
	
	private JComboBox index;

	/**
	 * Creates an instance of <code>IndexDialog</code>.
	 * 
	 * @param question
	 * 	question shown on this dialog
	 * @param indexCount
	 * 	number of indexes
	 */
	private IndexDialog(String question, int indexCount){
		this.initDialog(question, 300, 120);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		this.add(panel);

		this.index = new JComboBox();
		for(int i = 0; i < indexCount; i++){
			index.addItem(Utilities.translator.translate(this.getClass(), "atIndex") + " " + i);
		}
		index.setBounds(20, 20, 250, (int)(index.getPreferredSize().getHeight()));
		panel.add(index);

		JButton ok = new JButton(Utilities.translator.translate(this.getClass(), "ok"));
		ok.setBounds(170, 60, 100, (int)(ok.getPreferredSize().getHeight()));
		ok.addActionListener(new ActionListener(){

				public void actionPerformed(ActionEvent e){
					dispose();
				}
			});
		panel.add(ok);

		this.setVisible(true);
	}

	/**
	 * Returns the index user chose.
	 * 
	 * @return the index user chose
	 */
	private int getIndex(){
		return this.index.getSelectedIndex();
	}

	/**
	 * Creates a dialog for asking a index.
	 * 
	 * @param question
	 * 	question shown on this dialog
	 * @param indexCount
	 * 	number of available indexes
	 * @return index that user chose
	 */
	public static int askIndex(String question, int indexCount){
		return new IndexDialog(question, indexCount).getIndex();
	}

}
