/*
 *	Modulname:	Renderer
 *	Autor:		Eyer Leander
 *	Datum:		15.05.2006
 *
 *	(c) Copyright 2005
 */
package survey.gui;

import survey.model.Question;
import survey.model.DependencyListener;

import javax.swing.*;
import java.awt.*;

/**
 * Super class of all renderers
 */
public abstract class Renderer extends JPanel implements DependencyListener {

	/** Question rendererd by this object */
    private Question question;

	/** Constructor */
	public Renderer(Question question) {
		this.question = question;
		question.setRenderer(this);
		
		question.getDependencyHandler().addListener(this);
		if (question.getDependencyHandler().isFullfilled() == false) {
			setVisible(false);
		}

		//general layout stuff
		setAlignmentX(JComponent.LEFT_ALIGNMENT);

		//debug stuff, ignore
		//setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
		//setBackground(Color.yellow);
	}

	/** Calc the preferred and maximum size to restrain the horizontal space */
	public void restrainSize() {
		setPreferredSize(new Dimension(
				getPreferredSize().width,
				getPreferredSize().height));
		setMaximumSize(new Dimension(
				getPreferredSize().width,
				getPreferredSize().height));
	}

	/** Access the question */
	public Question getQuestion() {
		return question;
	}

	/** Update the question state (i.e. after a model element has been set programatically) */
	public abstract void updateRenderer();
	/** Change the Editable property */
	public abstract void setEditable(boolean editable);

	/** Get the preferred width of the question label */
	public abstract double getLabelWidth();
	/** Set the width of the question label */
	public abstract void setLabelWidth(double width);

	/** Get the size of the answer space */
	public abstract double getAnswerSpaceWidth();
	/** Set the answer space width */
	public abstract void setAnswerSpaceWdith(double width);

	/**
	 * The dependency status has changed
	 */
	public void dependencyChanged(boolean newStatus) {
        if (newStatus) {
			setVisible(true);
		} else {
			setVisible(false);
		}
	}

}

