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/ChapterCoverage.java,v 1.2 2004/05/13 06:06:54 tj_willis Exp $ 019 */ 020 package net.sourceforge.pavlov.controllers; 021 022 import java.text.DecimalFormat; 023 import java.util.HashMap; 024 import javax.swing.*; 025 import net.sourceforge.pavlov.event.AnswerEvent; 026 import net.sourceforge.pavlov.event.AnswerListener; 027 import net.sourceforge.pavlov.user.ChapterData; 028 import net.sourceforge.pavlov.user.ChapterStatistics; 029 import net.sourceforge.sillyview.*; 030 import org.apache.log4j.*; 031 032 /** 033 * A GUI widget that shows the user information about his global 034 * history with this chapter. 035 * 036 * @author <a href="mailto:tjwillis@users.sourceforge.net">T.J. Willis</a> 037 * @version $Revision: 1.2 $ 038 * @since 1.1 039 */ 040 public final class ChapterCoverage extends Widget implements AnswerListener { 041 //FIXED: rename to ChapterCoverage 042 //FIXED: move to pavlov.controllers 043 private ChapterData chapterData; 044 private ChapterStatistics chapterStatistics; 045 private final DecimalFormat numberFormatter; 046 /** 047 * Describe constant <code>NUM_ANSWERED</code> here. 048 * 049 */ 050 public static final String NUM_ANSWERED = "NUM_ANSWERED"; 051 /** 052 * Describe constant <code>NUM_ANSWERED_ONLY_WRONG</code> here. 053 * 054 */ 055 public static final String NUM_ANSWERED_ONLY_WRONG = "NUM_ANSWERED_ONLY_WRONG"; 056 /** 057 * Describe constant <code>NUM_ANSWERED_RIGHT</code> here. 058 * 059 */ 060 public static final String NUM_ANSWERED_RIGHT = "NUM_ANSWERED_RIGHT"; 061 /** 062 * Describe constant <code>NUM_NOT_ANSWERED</code> here. 063 * 064 */ 065 public static final String NUM_NOT_ANSWERED = "NUM_NOT_ANSWERED"; 066 /** 067 * Describe constant <code>NUM_QUESTIONS</code> here. 068 * 069 */ 070 public static final String NUM_QUESTIONS = "NUM_QUESTIONS"; 071 /** 072 * Describe constant <code>NUM_RIGHT_ANSWERS</code> here. 073 * 074 */ 075 public static final String NUM_RIGHT_ANSWERS = "NUM_RIGHT_ANSWERS"; 076 /** 077 * Describe constant <code>NUM_WRONG_ANSWERS</code> here. 078 * 079 */ 080 public static final String NUM_WRONG_ANSWERS = "NUM_WRONG_ANSWERS"; 081 /** 082 * Describe constant <code>PCT_ANSWERED</code> here. 083 * 084 */ 085 public static final String PCT_ANSWERED = "PCT_ANSWERED"; 086 /** 087 * Describe constant <code>PCT_RIGHT_ANSWERS</code> here. 088 * 089 */ 090 public static final String PCT_RIGHT_ANSWERS = "PCT_RIGHT_ANSWERS"; 091 /** 092 * Describe constant <code>PCT_WRONG_ANSWERS</code> here. 093 * 094 */ 095 public static final String PCT_WRONG_ANSWERS = "PCT_WRONG_ANSWERS"; 096 097 /** 098 * Creates a new <code>TextCoverageWidget</code> instance. 099 * 100 * @param myView a <code>WidgetView</code> value 101 * @param _cd a <code>ChapterData</code> value 102 */ 103 public ChapterCoverage (WidgetView myView, ChapterData _cd) { 104 super (myView); 105 chapterData = _cd; 106 //myView.setToken(JPanelView.TITLE,"Chapter Coverage"); 107 //myView.setToken (JPanelView.TEXT, "<HTML></HTML>"); 108 109 // textLabel = new JLabel(); 110 // add(textLabel); 111 // String msg = new String(""); 112 113 numberFormatter = new DecimalFormat ("##.##%"); 114 refresh (); 115 } 116 117 118 119 /** 120 * Get a new ChapterStatistics and display it to the user. 121 * 122 * @param s a <code>ChapterStatistics</code> value 123 */ 124 private void setStatistics (ChapterStatistics s) { 125 HashMap < Object, Object > h = new HashMap < Object, Object > (); 126 127 h.put (NUM_QUESTIONS, "" + s.nQuestions); 128 h.put (NUM_ANSWERED, "" + s.nAnswered); 129 h.put (NUM_NOT_ANSWERED, "" + s.getNumberNeverAnswered ()); 130 h.put (PCT_ANSWERED, 131 numberFormatter.format (s. 132 getPercentOfQuestionsAnsweredAtLeastOnce 133 ())); 134 h.put (NUM_RIGHT_ANSWERS, "" + s.totRights); 135 h.put (NUM_WRONG_ANSWERS, "" + s.getTotalNumberOfWrongAnswers ()); 136 h.put (PCT_RIGHT_ANSWERS, 137 numberFormatter.format (s.getPercentageOfRightAnswers ())); 138 h.put (PCT_WRONG_ANSWERS, 139 numberFormatter.format (s.getPercentageOfWrongAnswers ())); 140 h.put (NUM_ANSWERED_RIGHT, "" + s.nOnceRights); 141 h.put (NUM_ANSWERED_ONLY_WRONG, "" + s.nOnlyWrongs); 142 view.addTokens (h); 143 } 144 145 146 147 /** 148 * Get a new copy of ChapterStatistics from chapterData. 149 * 150 */ 151 private void refresh () { 152 if (chapterStatistics == null) { 153 ChapterStatistics cs = chapterData.getChapterStatistics (); 154 155 chapterStatistics = cs.deepCopy (); 156 } 157 setStatistics (chapterStatistics); 158 159 } 160 161 /** 162 * User answered a question, refresh statistics and generate text. 163 * 164 * @param e a <code>AnswerEvent</code> value 165 */ 166 public void answerEvent (AnswerEvent e) { 167 if (chapterStatistics == null) { 168 ChapterStatistics cs = e.getChapterStatistics (); 169 170 chapterStatistics = cs.deepCopy (); 171 } 172 if (e.isCorrect ()) 173 chapterStatistics.addRight (e.getQuestionData ()); 174 else 175 chapterStatistics.addWrong (e.getQuestionData ()); 176 177 setStatistics (chapterStatistics); 178 } 179 180 }