public class QuestionDialog extends BaseDialog {
private Question[] questions;
private JLabel[] labels;
private JComponent[] compos;
public QuestionDialog(Frame owner, String whose,
Question[] questions) {
super(owner, whose + " Specific Conditions");
this.questions = questions;
createComponents();
layoutComponents();
setLocationRelativeTo(owner);
}
protected void setModelFromView() {
for (int i = 0; i < questions.length; i++) {
setAnswer(questions[i], compos[i]);
}
}
private void createComponents() {
compos = new JComponent[questions.length];
labels = new JLabel[questions.length];
for (int i = 0; i < questions.length; i++) {
compos[i] = createCompo(questions[i]);
labels[i] = new JLabel(questions[i].getQuestion());
}
}
private void layoutComponents() {
Container widgets = createMainBox();
JButton[] buttons = { stdOKButton(), stdCancelButton() };
stdLayout(widgets, buttons);
}
private Box createMainBox() {
Box box = Box.createVerticalBox();
matchCompoWidth(compos);
for (int i = 0; i < questions.length; i++) {
if (i > 0) { box.add(createVerticalStrut(5)); }
box.add(createQuestionBox(labels[i], compos[i]));
}
return box;
}
private JComponent createCompo(Question question) {
if (question.isMultiChoice()) {
return createMultiCombo(question);
} else if (question.isMultiAnswer()) {
return createMultiList(question);
} else if (question.isBoolean()) {
return createBooleanCombo(question);
} else {
return null;
}
}
|
|
private void setAnswer(Question question, JComponent compo) {
if (question.isMultiAnswer()) {
Container box = ((Container) compo);
Component[] comps = box.getComponents();
Answer[] answers = question.getAnswers();
for (int i = 0; i < answers.length; i++) {
answers[i].setYes(((JCheckBox) comps[i]).isSelected());
}
} else {
setAnswer(question, (JComboBox) compo);
}
}
private void setAnswer(Question question, JComboBox combo) {
int selection = combo.getSelectedIndex();
if (selection == 0) {
Answer[] answers = question.getAnswers();
for (int i = 0; i < answers.length; i++) {
answers[i].setDontKnow();
}
return;
}
if (question.isMultiChoice()) {
Answer[] answers = question.getAnswers();
for (int i = 0; i < answers.length; i++) {
answers[i].setYes(false);
}
boolean nota = selection == combo.getItemCount() - 1;
if (!nota) {
answers[selection - 1].setYes(true);
}
} else {
question.getAnswer().setYes(selection == 1);
}
}
|
|
private JComboBox createMultiCombo(Question question) {
JComboBox combo = new JComboBox();
combo.addItem("1. Don't Know");
Answer[] answers = question.getAnswers();
for (int i = 0; i < answers.length; i++) {
combo.addItem((i + 1) + ". " + answers[i].getName());
if (answers[i].isYes()) {
combo.setSelectedIndex(i + 1);
}
}
combo.addItem(combo.getItemCount() + ". None of the Above");
return combo;
}
private Box createMultiList(Question question) {
Answer[] answers = question.getAnswers();
Box box = Box.createVerticalBox();
Insets shrinkWrap = new Insets(0, 2, 0, 0);
for (int i = 0; i < answers.length; i++) {
String label = (i + 1) + ". " + answers[i].getName();
JCheckBox check = new JCheckBox(label, answers[i].isYes());
check.setMargin(shrinkWrap);
box.add(check);
}
box.setBorder(PartialBorder.COMPLETE);
return box;
}
private JComboBox createBooleanCombo(Question question) {
String[] choices = { "1. Don't Know", "2. Yes", "3. No" };
JComboBox combo = new JComboBox(choices);
Answer answer = question.getAnswer();
if (answer.isAnswered()) {
combo.setSelectedIndex(answer.isYes() ? 1 : 2);
}
return combo;
}
|
|