001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/controllers/QuizStatus.java,v 1.1 2004/05/10 15:09:31 tj_willis Exp $
019     */
020    package net.sourceforge.pavlov.controllers;
021    import javax.swing.JLabel;
022    import net.sourceforge.pavlov.event.AnswerListener;
023    import net.sourceforge.pavlov.event.AnswerEvent;
024    import java.text.DecimalFormat;
025    import java.util.Date;
026    import java.util.HashMap;
027    import net.sourceforge.sillyview.*;
028    import org.apache.log4j.*;
029    
030    /**
031     * Feedback panel that displays information about the user's
032     * history with the specified question.
033     * @since 1.1
034     * @version $Revision: 1.1 $
035     */
036    public final class QuizStatus extends Widget implements AnswerListener {
037        //FIXED: rename to QuizStatus
038        //FIXED: move to pavlov.controllers
039        private DecimalFormat numberFormatter;
040        protected Date startDate;
041        public static final String ELAPSED_TIME = "QUIZ_ELAPSED_TIME";
042        public static final String NUM_ANSWERS = "QUIZ_NUMBER_ANSWERS";
043        public static final String NUM_CORRECT_ANSWERS = "QUIZ_NUMBER_CORRECT_ANSWERS";
044        public static final String NUM_INCORRECT_ANSWERS ="QUIZ_NUMBER_INCORRECT_ANSWERS";
045        public static final String PERCENT_SCORE = "QUIZ_PERCENT_SCORE";
046    
047        /**
048         * Create an empty status panel.
049         */
050        public QuizStatus (WidgetView myView) {
051            super (myView);
052            //myView.setToken(JPanelView.TITLE,"This Quiz");
053            myView.setToken (JPanelView.TEXT,
054                             "<HTML>No questions answered yet.</HTML>");
055            //     text = new JLabel();
056            //     add(text);
057            numberFormatter = new DecimalFormat ("##.##%");
058            startDate = new Date ();
059            answerEvent (null);
060        }
061    
062    
063    
064        /**
065         * Describe <code>answerEvent</code> method here.
066         *
067         * @param e an <code>AnswerEvent</code> value
068         */
069        public void answerEvent (AnswerEvent e) {
070            HashMap < Object, Object > h = new HashMap < Object, Object > ();
071    
072            if (e == null) {
073                h.put (NUM_ANSWERS, "" + 0);
074                h.put (NUM_CORRECT_ANSWERS, "" + 0);
075                h.put (NUM_INCORRECT_ANSWERS, "" + 0);
076                String scoreString = "unavailable";
077                h.put (PERCENT_SCORE, scoreString);
078                h.put (ELAPSED_TIME, "0 seconds");
079    
080            } else {
081                h.put (NUM_ANSWERS, "" + e.getNumberOfAnswers ());
082                h.put (NUM_CORRECT_ANSWERS, "" + e.getNumberOfCorrectAnswers ());
083                h.put (NUM_INCORRECT_ANSWERS,
084                       "" + e.getNumberOfIncorrectAnswers ());
085                String scoreString =
086                    numberFormatter.format (e.getPercentageOfCorrectAnswers () /
087                                            100.0);
088                h.put (PERCENT_SCORE, scoreString);
089                h.put (ELAPSED_TIME, getTimeString ());
090            }
091            //     //text.setText(foo);
092            //     h.put(JPanelView.TEXT,foo);
093            view.addTokens (h);
094        }
095    
096        protected String getTimeString () {
097            String foo = "";
098            Date nowDate = new Date ();
099            long a = startDate.getTime ();
100            long b = nowDate.getTime ();
101            long elmil = b - a;
102            long elsec = elmil / 1000;
103            long remaining = elsec;
104            //foo += "You've been doing this quiz for ";
105            if (elsec > 60 * 60) {
106                foo += "" + (elsec / (60 * 60)) + " hour";
107                if (elsec >= 60 * 60 * 2)
108                    foo += "s";
109                foo += " ";
110                remaining -= ((elsec / (60 * 60)) * (60 * 60));
111            }
112            if (remaining > 60) {
113                foo += "" + (elsec / (60)) + " minute";
114                if (elsec >= 60 * 2)
115                    foo += "s";
116                foo += " ";
117                remaining -= ((remaining / (60)) * (60));
118            }
119            foo += "" + remaining + " seconds";
120            return foo;
121        }
122    }