/*
 *	Modulname:	RendererNasa
 *	Autor:		Eyer Leander
 *	Datum:		06.06.2006
 *
 *	(c) Copyright 2005
 */
package survey.gui;

import survey.model.*;
import survey.Survey;

import javax.swing.*;
import java.awt.*;
import java.awt.Choice;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Iterator;

import info.clearthought.layout.TableLayout;

public class RendererNasa extends Renderer implements ActionListener{

	// connection to the model
	private TypeNasa type;

    private JLabel questionLabel;
//	private JTextPane questionLabel;
	private JPanel answerPane;

	private int selection = -1;
	private JButton [] buttons;

	/**
	 * Constructor
	 */
	public RendererNasa(Question question, TypeNasa type) {
		super(question);
		this.type = type;

		createUI();
		setEditable(getQuestion().isEditable());
	}

	/**
	 * Config the user interface
	 */
	private void createUI() {
		//setLayout(new BorderLayout(6, 6));
		String mandatoryFlag = getQuestion().isMandatory() ? "* " : "";

		String labelContent = "<html><body>" + mandatoryFlag + getQuestion().getLabel() + "</body></html>";
		questionLabel = new JLabel(labelContent);

		answerPane = new JPanel(new BorderLayout(12, 12));

		if (type.getLeftLabel().equals("") == false) {
			JLabel label = new JLabel(type.getLeftLabel());
			label.setHorizontalAlignment(JLabel.CENTER);
			answerPane.add(label, BorderLayout.WEST);
		}

		JPanel itemPane = new JPanel(new GridLayout(1, 0, 0, 0));
		answerPane.add(itemPane, BorderLayout.CENTER);
		int size = type.getMax() - type.getMin();
		buttons = new JButton [size];
		for (int i = 0; i < size; i++) {
			buttons [i] = new JButton("   ");
			buttons [i].setBackground(Color.lightGray);
			buttons [i].setFocusable(false);
			buttons [i].setBorder(BorderFactory.createCompoundBorder(
					BorderFactory.createLineBorder(Color.black, 1),
					BorderFactory.createEmptyBorder(3, 9, 3, 9)));
			buttons [i].setActionCommand(Integer.toString(i));
			buttons [i].addActionListener(this);


			itemPane.add(buttons [i]);
		}

		if (type.getRightLabel().equals("") == false) {
			JLabel label = new JLabel(type.getRightLabel());
			label.setHorizontalAlignment(JLabel.CENTER);
			answerPane.add(label, BorderLayout.EAST);
		}

		//setLayout(new BorderLayout(6, 6));
		//add(questionLabel, BorderLayout.CENTER);
		//add(answerPane, BorderLayout.SOUTH);

		setLayout(new TableLayout(new double[][]{{TableLayout.PREFERRED}, {TableLayout.PREFERRED, 6, TableLayout.PREFERRED}}));
		add(questionLabel, "0, 0");
		add(answerPane, "0, 2");
	}



	/**
	 * Update the question state (i.e. after a model element has been set programatically)
	 */
	public void updateRenderer() {
		if (selection >= 0)
			buttons [selection].setBackground(Color.lightGray);
		selection = type.getMin() + Integer.parseInt(type.getAnswer().toString());
		if (selection >= 0)
			buttons [selection].setBackground(Color.blue);
	}

	/**
	 * Change the Editable property
	 */
	public void setEditable(boolean editable) {
        for (JButton btn:buttons) {
			btn.setEnabled(editable);
		}
	}

	/**
	 * Get the preferred width of the question label
	 */
	public double getLabelWidth() {
		return questionLabel.getPreferredSize().width;
	}

	/**
	 * Set the width of the question label
	 */
	public void setLabelWidth(double width) {
		Dimension d = new Dimension((int) width, questionLabel.getPreferredSize().height);
		questionLabel.setMinimumSize(d);
		questionLabel.setPreferredSize(d);
		questionLabel.setMaximumSize(d);
	}

	/**
	 * Get the size of the answer space
	 */
	public double getAnswerSpaceWidth() {
		return answerPane.getPreferredSize().width;
	}

	/**
	 * Set the answer space width
	 */
	public void setAnswerSpaceWdith(double width) {
		Dimension d = new Dimension((int) width, answerPane.getPreferredSize().height);
		answerPane.setMinimumSize(d);
		answerPane.setPreferredSize(d);
		answerPane.setMaximumSize(d);
	}

	/**
	 * Invoked when an action occurs.
	 */
	public void actionPerformed(ActionEvent e) {
		if (selection != -1)
			buttons [selection].setBackground(Color.lightGray);

		selection = Integer.parseInt(e.getActionCommand());
		buttons [selection].setBackground(Color.blue);

		type.setAnswer(Integer.toString(selection + type.getMin()));
	}

}

