⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quizwizard1.java

📁 eclipse平台的CDT项目3.0版本的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * {@link cdt.gui.HTMLTextPane HTMLTextPanes}, and  places the
     * {@link cdt.projects.tree.nodes.Question Question} into the
     * {@link cdt.wizard.WizardData WizardData}.
     */
    private void saveQuestion() {
        this.question.setQuestion(this.questionText.getText());

        int selectedChoice = this.choicesList.getSelectedIndex();
        if(-1 != selectedChoice) {
            this.question.setChoice(selectedChoice, this.choiceText.getText());
            this.question.setFeedback(selectedChoice, this.feedbackText.getText());
        }

        // Places the question into the new Questions folder
        Hold qFolder = (Hold)data.get("newQuestionsFolder");
        if(null != qFolder) {
            question.setFile(Node.foldername(question.getName()) + ".html");
            qFolder.addNodeInto(question);
        }

        // Places the answers into the new Answers folder
        Node aFolder = (Node)data.get("newAnswersFolder");
        Hold answerFolder = (Hold)Node.findChildNode(aFolder, "Question #" +this.questionNumber);
        if(null == answerFolder) {
            answerFolder = new Hold("Question #" +this.questionNumber);
            answerFolder.setFile(Node.foldername(answerFolder.getName()));
            aFolder.addNodeInto(answerFolder);
        }

        ArrayList feedbacks = this.question.getFeedbacks();
        for(int i = 0; i < feedbacks.size(); ++i) {
            String feedback = (String)feedbacks.get(i);
            Item feedbackNode = new Item("Answer #" +(i+1));
            answerFolder.addNodeInto(feedbackNode);
            feedbackNode.setData(feedback);
            feedbackNode.setFile(Node.foldername(feedbackNode.getName()) + ".html");
        }
    }

    /**
     * Checks whether or not a correct answer is selected and pops a warning
     * up if there isn't.
     *
     * @return <code>true</code> if there is a correct choice selected, and
     *      <code>false</code> if there is no choice selected as correct.
     */
    private boolean checkForCorrectAnswer() {
        if(-1 == this.question.getCorrectChoice())  {
            cdt.ErrorD.ErrorDlg.ErrorMsg("You must choose a correct answer");
            return false;
        }
        return true;
    }

    /**
     * Moves to the next page in the wizard.
     */
    public void nextAction() {
        if(false == checkForCorrectAnswer()) {
            return;
        }
        pushFrame(this);
        setVisible(false);

        saveQuestion();

        new QuizWizard1(parent, (questionNumber+1));
    }

    /**
     * Goes back a page in the wizard.
     */
    public void previousAction() {
        if(this.questionNumber > 1) {
            final String questionString = "Question #" +(this.questionNumber-1);

            // Removes the previous question from the newly created
            // questions folder
            Node questionNode = Node.findChildNode((Node)data.get("newQuestionsFolder"), questionString);
            if(null != questionNode) {
                questionNode.removeNode();
            }

            // Removes the previous question's answers from the newly created
            // answers folder
            Node answersNode = Node.findChildNode((Node)data.get("newAnswersFolder"), questionString);
            if(null != answersNode) {
                answersNode.removeNode();
            }
        }

        super.previousAction();
    }

    /**
     * Creates the quiz and closes the wizard.
     */
    public void finishAction() {
        if(false == checkForCorrectAnswer()) {
            return;
        }
        saveQuestion();

        Node qParent = (Node) this.oldQuestionsFolder.getParent();
        Node aParent = (Node) this.oldAnswersFolder.getParent();

        // Closes the editor if the node displayed in the editor is from the quiz folder
        Node openNode = Project.currentProject.getOpenNode();
        if(null != openNode) {
            if(openNode.getParent() == this.oldQuestionsFolder || openNode.getParent() == this.oldAnswersFolder) {
                Frame1.frame.closeEditor();
            }
            if(null != openNode.getParent() && openNode.getParent().getParent() == this.oldAnswersFolder) {
                Frame1.frame.closeEditor();
            }
        }
        this.oldQuestionsFolder.removeNode();
        this.oldAnswersFolder.removeNode();
        Node newQuestionFolder = (Node)data.get("newQuestionsFolder");
        Node newAnswerFolder = (Node)data.get("newAnswersFolder");
        newQuestionFolder.setFile(Node.foldername(newQuestionFolder.getName()));
        newAnswerFolder.setFile(Node.foldername(newAnswerFolder.getName()));
        qParent.addNodeInto(newQuestionFolder);
        aParent.addNodeInto(newAnswerFolder);
        Project.currentProject.getTree().updateUI();

        // Cleans everything up
        super.finishAction();
    }

    /**
     * Changes the text in the JList when changes occur to the JCheckBox.
     */
    private class CheckBoxListener implements ActionListener {
        /** The list representing the different choices for the question. */
        private final JList choiceList;

        public CheckBoxListener(JList choiceList) {
            this.choiceList = choiceList;
        }

        public synchronized void actionPerformed(ActionEvent e) {
            ListSelectionListener listListeners[] = choiceList.getListSelectionListeners();
            for(int i = 0; i < listListeners.length; ++i) {
                if(listListeners[i] instanceof ChoiceSelectionListener) {
                    ((ChoiceSelectionListener)listListeners[i]).setActive(false);
                }
            }

            JCheckBox j = (JCheckBox)e.getSource();
            final int selectedChoice = this.choiceList.getSelectedIndex();
            if(selectedChoice == -1) { return; }
            question.setChoice(selectedChoice, choiceText.getText());
            if(j.isSelected()) {
                question.setCorrectChoice(selectedChoice);
            } else {
                question.setCorrectChoice(-1);
            }
            // Makes the correct choice show up in the JList using "**"
            ListModel oldListModel = choiceList.getModel();
            DefaultListModel newListModel = new DefaultListModel();
            for(int i = 0; i < oldListModel.getSize(); ++i) {
                String choiceName = (String)oldListModel.getElementAt(i);
                if(choiceName.endsWith(" **")) {
                    choiceName = choiceName.substring(0, (choiceName.length()-3));
                }
                if(question.getCorrectChoice() != i) {
                    newListModel.addElement(choiceName);
                } else {
                    newListModel.addElement(choiceName + " **");
                }
            }
            choiceList.setModel(newListModel);
            choiceList.setSelectedIndex(selectedChoice);

            for(int i = 0; i < listListeners.length; ++i) {
                if(listListeners[i] instanceof ChoiceSelectionListener) {
                    ((ChoiceSelectionListener)listListeners[i]).setActive(true);
                }
            }
        }
    }

    /**
     * Changes the text in the editor according to what choice is selected.
     */
    private class ChoiceSelectionListener implements ListSelectionListener {

        /** The currently 'selected' choice's index. */
        private int currentSelection;
        /** Whether this listener is currently listening. */
        private boolean isActive;

        /**
         * Creates a ChoiceSelectionListener that will affect a JTextComponent.
         */
        public ChoiceSelectionListener() {
            this.isActive = true;
            this.currentSelection = -1;
        }

        public boolean isActive() {
            return this.isActive;
        }

        public void setActive(boolean isActive) {
            this.isActive = isActive;
        }

        /**
         * Fired when an item in the registered list is selected.
         *
         * @param e The ListSelectionEvent that describes the selection.
         */
        public synchronized void valueChanged(ListSelectionEvent e) {
            if(false == isActive()) { return; }
            if(e.getValueIsAdjusting()) {
                // The selection is still changing, so don't do anything.
                return;
            }

            int newIndex = ((JList)e.getSource()).getSelectedIndex();
            if(newIndex == this.currentSelection) {
                // The selection hasn't changed, so don't do anything
                return;
            }

            // Saves the text from the previously selected answer
            if(this.currentSelection != -1) {
                question.setChoice(this.currentSelection, choiceText.getText());
                question.setFeedback(this.currentSelection, feedbackText.getText());
            }

            // Makes sure that the answerText and the feedbackText are
            // editable when an answer is selected from the list
            final Color backgroundColor = UIManager.getColor("controlLtHighlight");
            if(false == choiceText.isEditable()) {
                choiceText.setEditable(true);
                choiceText.setBackground(backgroundColor);
            }
            if(false == feedbackText.isEditable()) {
                feedbackText.setEditable(true);
                feedbackText.setBackground(backgroundColor);
            }
            if(false == isCorrectChoice.isEnabled()) {
                isCorrectChoice.setEnabled(true);
            }
            if(newIndex == question.getCorrectChoice()) {
                isCorrectChoice.setSelected(true);
            } else {
                isCorrectChoice.setSelected(false);
            }

            // Switches the text in the answerText and feedbackText to the
            // text corresponding to the answer that was selected
            choiceText.setText(question.getChoice(newIndex));
            feedbackText.setText(question.getFeedback(newIndex));
            setupBorder(answerPanel, "Answer " +(newIndex+1));
            this.currentSelection = newIndex;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -