/*
 *	Modulname:	MultipleChoiceQuestion
 *	Autor:		Eyer Leander
 *	Datum:		08.05.2006
 *
 *	(c) Copyright 2005
 */
package survey.model;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.Iterator;
import java.util.Vector;


/**
 * The Motherclass for Multiple Choice Selections
 */
public class TypeMultiChoice extends QuestionType {

    /** Alignment */
    private boolean isVertical = false;

    /** Start a new line (only for horizontal alignment  */
    private boolean newline = false;

    /** The left side Label */
    private String leftLabel = "";
    /** The right side Label */
    private String rightLabel = "";

    /** List of possible choices */
    private Vector<Choice> choices = new Vector<Choice>();

    /** Currently selected question */
    private Choice selection = null;

    /**
     * Create a multiple choice question
     * @param multRoot
     */
    public TypeMultiChoice(Question question, Element multRoot) {
	super(question);
	parseFromXML(multRoot);
    }

    /** Create the element from the information in the xml definition */
    private void parseFromXML(Element choiceRoot) {
	//read the alignment attribute (if set)
	if (choiceRoot.hasAttribute("alignment")) {
	    isVertical = choiceRoot.getAttribute("alignment").equals("vertical");
	}

	if (choiceRoot.hasAttribute("newline")) {
	    newline = choiceRoot.getAttribute("newline").equals("true");
	}

	if (choiceRoot.hasAttribute("leftlabel")) {
	    leftLabel = choiceRoot.getAttribute("leftlabel");
	}
	if (choiceRoot.hasAttribute("rightlabel")) {
	    rightLabel = choiceRoot.getAttribute("rightlabel");			
	}

	//parse the possible choices
	NodeList choiceNodes = choiceRoot.getElementsByTagName("choice");
	for (int i = 0; i < choiceNodes.getLength(); i++) {
	    Element choiceNode = (Element) choiceNodes.item(i);
	    Choice choice = new Choice(choiceNode);
	    choices.add(choice);
	}
    }

    /** Return an iterator over all possible choices */
    public Iterator<Choice> getChoices() {
	return choices.iterator();
    }

    public boolean isVertical() {
	return isVertical;
    }

    public void setVertical(boolean vertical) {
	isVertical = vertical;
    }

    /**
     * Return true if an answer has been set for this question
     */
    public boolean hasBeenAnswered() {
	return (selection != null);
    }

    public void setAnswer(Object value) {
	selection = (Choice) value;
	getQuestion().fireStateChagned();
    }

    public void setAnswerById(String answerId) throws Exception {
	for (Choice choice : choices) {
	    if (choice.getId().equals(answerId)) {
		setAnswer(choice);
		return;
	    }
	}
	throw new Exception("answerId not expected");
    }

	public Object getAnswer() {
	    if (selection == null)
		return "";
	    else
		return selection.getId();
	}

	/** Should a new line be started */
	public boolean startNewline() {
	    return newline;
	}

	/** Left Range Label */
	public String getLeftLabel() {
	    return leftLabel;
	}

	/** Right Range Label */
	public String getRightLabel() {
	    return rightLabel;
	}
    }

