public MultipleChoice(boolean singleSelection, String question, String[] options) { super(new BorderLayout(5, 5)); if (options == null) { options = new String[4]; options[0] = "A."; options[1] = "B."; options[2] = "C."; options[3] = "D."; } int n = options.length; scripts = new String[n]; Arrays.fill(scripts, ""); if (singleSelection) { invisibleButton = new JToggleButton(); buttonGroup = new ButtonGroup(); buttonGroup.add(invisibleButton); choices = new JRadioButton[n]; for (int i = 0; i < n; i++) { choices[i] = new JRadioButton(options[i]); choices[i].addItemListener(itemListener); buttonGroup.add(choices[i]); } } else { choices = new JCheckBox[n]; for (int i = 0; i < n; i++) { choices[i] = new JCheckBox(options[i]); choices[i].addItemListener(itemListener); } } questionBody = new BasicPageTextBox() { public void createPopupMenu() {} public JPopupMenu getPopupMenu() { return null; } }; questionBody.setText(question); questionBody.setEditable(false); questionBody.addMouseListener(popupListener); add(questionBody, BorderLayout.NORTH); choicePanel = new JPanel(); layChoices(); add(choicePanel, BorderLayout.CENTER); buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); questionBody.setBackground(buttonPanel.getBackground()); add(buttonPanel, BorderLayout.SOUTH); String s = Modeler.getInternationalText("CheckAnswer"); checkAnswerButton = new JButton(s != null ? s : "Check Answer"); checkAnswerButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { boolean selected = false; for (int i = 0; i < choices.length; i++) { if (choices[i].isSelected()) { selected = true; break; } } if (!selected) { String s = Modeler.getInternationalText("YouHaveNotChosenAnswer"); JOptionPane.showMessageDialog( MultipleChoice.this, s != null ? s : "You haven't chosen your answer.", "Error", JOptionPane.ERROR_MESSAGE); return; } if (answer == null) { String s = Modeler.getInternationalText("QuestionDesignerDidNotProvideAnswer"); JOptionPane.showMessageDialog( MultipleChoice.this, s != null ? s : "The question designer didn't provide an answer.", "Answer unknown", JOptionPane.ERROR_MESSAGE); } else { if (getSingleSelection()) { int[] selectedIndices = getUserSelectedIndices(); if (selectedIndices != null && selectedIndices.length == 1) { int si = selectedIndices[0]; notifyAnswerChecked(new MultipleChoiceEvent(MultipleChoice.this, si)); boolean b = si == answer[0]; if (b) { if (hasNoScripts()) { String s = Modeler.getInternationalText("Correct"); JOptionPane.showMessageDialog( MultipleChoice.this, s != null ? s : "Correct!", "Answer", JOptionPane.INFORMATION_MESSAGE); } } else { if (hasNoScripts()) { String s = Modeler.getInternationalText("TryAgain"); JOptionPane.showMessageDialog( MultipleChoice.this, s != null ? s : "Try again!", "Answer", JOptionPane.INFORMATION_MESSAGE); } for (int i = 0; i < choices.length; i++) { if (choices[i].isSelected()) { addCheckAnswerHistory((char) ('a' + i) + ""); break; } } } } } else { // multiple selection boolean b = true; boolean p; for (int i = 0; i < choices.length; i++) { p = false; for (int j = 0; j < answer.length; j++) { if (answer[j] == i) { p = true; break; } } if ((p && !choices[i].isSelected()) || (!p && choices[i].isSelected())) { b = false; break; } } notifyAnswerChecked(new MultipleChoiceEvent(MultipleChoice.this, b)); if (b) { if (hasNoScripts()) { String s = Modeler.getInternationalText("Correct"); JOptionPane.showMessageDialog( MultipleChoice.this, s != null ? s : "Correct!", "Answer", JOptionPane.INFORMATION_MESSAGE); } } else { if (hasNoScripts()) { String s = Modeler.getInternationalText("TryAgain"); JOptionPane.showMessageDialog( MultipleChoice.this, s != null ? s : "Try again!", "Answer", JOptionPane.INFORMATION_MESSAGE); } String s = ""; char c = 'a'; for (int i = 0; i < choices.length; i++) { if (choices[i].isSelected()) { s += (char) (c + i) + " "; } } if (!s.equals("")) addCheckAnswerHistory(s.trim()); } } } } }); buttonPanel.add(checkAnswerButton); s = Modeler.getInternationalText("ClearAnswer"); clearAnswerButton = new JButton(s != null ? s : "Clear Answer"); clearAnswerButton.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { clearAnswer(); } }); }