FileDocCategorySizeDatePackage
QView.javaAPI DocExample5033Sun May 05 20:36:40 BST 2002None

QView

public class QView extends JPanel implements Observer
The View/Controller for ONE QUESTION in the TestEdit program. This is normally used in e.g., a TabPanel, to show one question. It would be lower overhead, but not true MVC, to have one of these and just load its contents when the question number changes.

Fields Summary
protected JTextArea
quesText
The question's text
protected Q
q
The question as a Q object
protected JCheckBox[]
cb
The choices - CheckBox
protected ButtonGroup
cbg
A ButtonGroup to make cb[] act as a radiobutton
protected JTextArea[]
tf
The choices - answer texts
protected JComponent
cnc
Chapter Number Chooser
Constructors Summary
QView(Q q)
Construct a QView, given the Q it is to maintain

		this.q = q;
		setupGUI();

		loadGUI();
	
Methods Summary
private voidloadGUI()

		quesText.setText(q.getQText());
		int theAnswer = q.getAns();
		if (theAnswer >= 0) {
			cb[theAnswer].setSelected(true);
		}
		// int nQs = q.getNum();
		// tf[q.getAns()].setText(ch.getText());
	
private voidsetupGUI()

		GridBagLayout gbl = new GridBagLayout();
		setLayout(gbl);
		
		// Top Left, and wide: the question text
		GridBagConstraints gbcQuesText = new GridBagConstraints();
		gbcQuesText.fill = GridBagConstraints.BOTH;
		gbcQuesText.gridx = 0;
		gbcQuesText.gridwidth = 5;
		gbcQuesText.weightx = 1.0;
		gbcQuesText.weighty = 1.0;

		// Top Right Left, and narrow: for the Chapter Number label
		GridBagConstraints gbcLabel = new GridBagConstraints();
		gbcLabel.fill = GridBagConstraints.BOTH;
		gbcLabel.gridwidth = GridBagConstraints.RELATIVE;
		gbcLabel.gridx = 5;
		gbcLabel.weighty = 1.0;

		// Top Right Right, and narrow: for the Chapter Number chooser
		GridBagConstraints gbcChoice = new GridBagConstraints();
		gbcChoice.fill = GridBagConstraints.BOTH;
		gbcChoice.gridwidth = GridBagConstraints.REMAINDER;
		gbcChoice.gridx = 7;
		gbcChoice.weighty = 1.0;

		// Left and narrow: for the CheckBoxes
		GridBagConstraints gbcCheckBox = new GridBagConstraints();
		gbcCheckBox.fill = GridBagConstraints.BOTH;
		gbcCheckBox.gridwidth = 1;
		gbcCheckBox.gridx = 0;
		gbcCheckBox.weighty = 1.0;
		gbcCheckBox.anchor = GridBagConstraints.EAST;

		// Right and wide: for the text question
		GridBagConstraints gbcAnsText = new GridBagConstraints();
		gbcAnsText.fill = GridBagConstraints.BOTH;
		gbcAnsText.gridwidth = GridBagConstraints.REMAINDER;
		gbcAnsText.gridx = GridBagConstraints.RELATIVE;
		gbcAnsText.weightx = 1.0;
		gbcAnsText.weighty = 1.0;

		// Now add stuff, starting with the question text
		quesText = new JTextArea(3,80);
		quesText.setLineWrap(true);
		gbl.setConstraints(quesText, gbcQuesText);
		add(quesText);

		JLabel cnl = new JLabel("Chapter", JLabel.RIGHT);
		gbl.setConstraints(cnl, gbcLabel);
		add(cnl);

		cnc = new JTextField("    0");
		gbl.setConstraints(cnc, gbcChoice);
		add(cnc);

		quesText.getDocument().addDocumentListener(new QListener(q, -1, quesText));
		quesText.setBackground(Color.pink);

		cbg = new ButtonGroup();
		cb = new JCheckBox[q.getCount()];
		tf = new JTextArea[q.getCount()];

		for (int i=0; i<q.getCount(); i++) {
			cb[i] = new JCheckBox(Q.labels[i], false);
			cb[i].setBackground(Color.red);
			gbl.setConstraints(cb[i], gbcCheckBox);
			add(cb[i]);
			cbg.add(cb[i]);
			tf[i] = new JTextArea(2,80);
			tf[i].setBackground(Color.green);
			add(tf[i]);
			gbl.setConstraints(tf[i], gbcAnsText);
			add(tf[i]);
			tf[i].setLineWrap(true);
			tf[i].getDocument().addDocumentListener(new QListener(q, i, tf[i]));
		}
		q.addObserver(this);
	
public voidupdate(java.util.Observable q, java.lang.Object change)
update is called from the model when its data changes.

		// System.out.println("UPDATE: " + q + "-->" + change);
		QChangeEvent ch = (QChangeEvent)change;
		switch(ch.getID()) {
		case QChangeEvent.CHANGE_QUESTION_TEXT:
			quesText.setText(ch.getText());
			break;
		case QChangeEvent.CHANGE_ANSWER_NUMBER:
			cb[ch.getAnsNumber()].setSelected(true);
			break;
		case QChangeEvent.CHANGE_ANSWER_TEXT  :
			tf[ch.getAnsNumber()].setText(ch.getText());
			break;
		default:
			throw new IllegalArgumentException("QVIEW.update: " + change);
		}