View Javadoc

1   /***
2    * 
3    */
4   package org.telscenter.pas.steps;
5   
6   import java.awt.BorderLayout;
7   import java.awt.Color;
8   import java.awt.Cursor;
9   import java.awt.Dimension;
10  import java.awt.Font;
11  import java.awt.GridLayout;
12  import java.awt.event.ActionEvent;
13  import java.awt.event.ActionListener;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.logging.Logger;
17  
18  import javax.swing.AbstractAction;
19  import javax.swing.BorderFactory;
20  import javax.swing.BoxLayout;
21  import javax.swing.JButton;
22  import javax.swing.JComponent;
23  import javax.swing.JDialog;
24  import javax.swing.JFrame;
25  import javax.swing.JLabel;
26  import javax.swing.JPanel;
27  import javax.swing.JRadioButton;
28  import javax.swing.JScrollPane;
29  
30  import net.sf.sail.core.entity.ISock;
31  
32  import org.telscenter.pas.common.ui.CommonUI;
33  import org.telscenter.pas.common.ui.panel.SimpleGradientPanel;
34  import org.telscenter.pas.steps.actions.DialogSaveAction;
35  import org.telscenter.pas.steps.actions.LeaveStudentAssessmentAction;
36  import org.telscenter.pas.steps.domain.BlockInteraction;
37  import org.telscenter.pas.steps.domain.ChoiceInteraction;
38  import org.telscenter.pas.steps.domain.ResponseDeclaration;
39  import org.telscenter.pas.steps.domain.SimpleChoice;
40  import org.telscenter.pas.ui.dialog.PasDialogManager;
41  import org.telscenter.pas.ui.dialog.PasMessageDialogUI;
42  import org.telscenter.pas.ui.dialog.PasSaveStudentAssesmentDialogUI;
43  import org.telscenter.pas.ui.dialog.PasStudentAssessmentConfirmDialogUI;
44  import org.telscenter.pas.ui.util.PasColors;
45  import org.telscenter.pas.util.QTIUtils;
46  import org.xito.dialog.CustomDialog;
47  
48  /***
49   * @author aperritano
50   *
51   */
52  public class SelfAssessmentUI extends StudentAssessmentUI {
53  
54  	/***
55  	 * Logger for this class
56  	 */
57  	static final Logger logger = Logger
58  			.getLogger(SelfAssessmentUI.class.getName());
59  
60  	
61  	private static final long serialVersionUID = 1L;
62  
63  	protected JButton submitQuestionsButton;
64  
65  	protected String QUESTION_CARD = "QUESTION_CARD"; //$NON-NLS-1$
66  
67  	protected String RESULT_CARD = "RESULT_CARD"; //$NON-NLS-1$
68  
69  
70  	public boolean toLeaveStep = false;
71  
72  	protected JPanel submitButtonPanel;
73  
74  	protected LeaveStudentAssessmentAction leaveAction;
75  	
76  	public SelfAssessmentUI(Assessment assmt) {
77  		super(assmt);
78  	}
79  	
80  	
81  	/***
82  	 * Creates all the interactions, flag for long or tab layout.
83  	 * 
84  	 * @param interactions
85  	 * @return a panel with all the questions for an assessment
86  	 */
87  	protected JPanel questionFactory(List<BlockInteraction> interactions) {
88  		return super.questionFactory(interactions);
89  	}
90  	
91  
92  	protected JPanel createButtonPanel() {
93  		submitButtonPanel = new JPanel(new BorderLayout(0, 0));
94  		//submitButtonPanel.setOpaque(false);
95  		submitButtonPanel.setBackground(Color.white);
96  		submitQuestionsButton = new JButton("Submit Answers"); //$NON-NLS-1$
97  		submitQuestionsButton = CommonUI
98  				.makeEnhancedButton(submitQuestionsButton);
99  		submitQuestionsButton.setCursor(Cursor
100 				.getPredefinedCursor(Cursor.HAND_CURSOR));
101 		
102 		submitQuestionsButton.addActionListener(new ActionListener() {
103 
104 			public void actionPerformed(ActionEvent e) {
105 				
106 				cardPanel.showNextCard();
107 
108 				doSave();
109 				
110 			}
111 		});
112 
113 		submitButtonPanel.add(new JLabel(" "), BorderLayout.WEST);
114 		submitButtonPanel.add(new JLabel(" "), BorderLayout.EAST);
115 
116 		JPanel buttonFillerPanel = new JPanel();
117 		buttonFillerPanel.setOpaque(false);
118 		buttonFillerPanel.add(submitQuestionsButton);
119 		submitButtonPanel.add(buttonFillerPanel, BorderLayout.CENTER);
120 		// submitButtonPanel.setBorder(BorderFactory.createEtchedBorder());
121 		JPanel t = new JPanel(new GridLayout(1, 1));
122 		t.setOpaque(false);
123 		t.add(submitButtonPanel);
124 		return t;
125 	}
126 
127 	
128 	/***
129 	 * Switches to the results panel
130 	 */
131 	protected void showResultsCard() {
132 		submitButtonPanel.setVisible(false);
133 		if( cardPanel.getComponentCount() > 1 ) {
134 			cardPanel.remove(1);
135 		}
136 		JPanel resultsCard = createResultsCard();
137 		cardPanel.add(resultsCard, RESULT_CARD);
138 		
139 		logger.info("COUNT " + cardPanel.getComponentCount() );
140 		cardPanel.showNextCard();
141 	}
142 
143 	
144 	/***
145 	 * Creates the results of the assessment
146 	 * 
147 	 * @return results card
148 	 */
149 	protected JPanel createResultsCard() {
150 
151 		SimpleGradientPanel resultsCard = new SimpleGradientPanel(new BorderLayout(0, 0));
152 		resultsCard.setDirection(SimpleGradientPanel.VERTICAL);
153 		resultsCard.setStartColor(PasColors.compactNavBackgroundStartColor);
154 		resultsCard.setEndColor(PasColors.compactNavBackgroundEndColor);
155 		
156 		
157 		JLabel finishedLabel = new JLabel(Messages
158 				.getString("StudentAssessment.11")); //$NON-NLS-1$
159 		Font font = finishedLabel.getFont().deriveFont(Font.BOLD, 16f);
160 		finishedLabel.setFont(font);
161 		finishedLabel.setForeground(Color.white);
162 		finishedLabel.setBorder(BorderFactory.createEmptyBorder(15, 2, 15, 2));
163 
164 		JPanel itemsPanel = new JPanel();
165 		// answerScroller.setBorder(BorderFactory.createEmptyBorder());
166 		itemsPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
167 		itemsPanel.setLayout(new BoxLayout(itemsPanel, BoxLayout.PAGE_AXIS));
168 		itemsPanel.setOpaque(false);
169 		List<ResponseDeclaration> reponseDeclarations = assessment.getAssessmentItem().getResponseDeclarations();
170 
171 		for (ResponseDeclaration rd : reponseDeclarations) {
172 			String answerId = null;
173 			String humanReadableAnswer = null;
174 			String inlineFeedback = null;
175 			boolean isCorrect = false;
176 			ISock<String> sock = assessment.getResponseDeclarationToSocks().get(rd);
177 			if (sock != null && !sock.isEmpty()) {
178 				answerId = sock.peek();
179 
180 				humanReadableAnswer = Assessment.lastAnswer(assessment.getResponseDeclarationToSocks(), rd);
181 				
182 				BlockInteraction bi = Assessment.getInteractionByResponseDeclaration(assessment.getAssessmentItem().getItemBody().getInteractions(),rd);
183 				
184 				if (bi instanceof ChoiceInteraction) {
185 					ChoiceInteraction<String> ci = (ChoiceInteraction<String>) bi;
186 					
187 					List<SimpleChoice<String>> simpleChoices = ci.getSimpleChoices();
188 					for (SimpleChoice<String> simpleChoice : simpleChoices) {
189 						if( simpleChoice.getIdentifier().equals(answerId)) {
190 							humanReadableAnswer = (String) simpleChoice.getContent();
191 							inlineFeedback = (String) simpleChoice.getFeedbackInline().getContent();
192 							 isCorrect = QTIUtils.isCorrectAnswer(rd, simpleChoice.getIdentifier());
193 						}// if
194 					}// for
195 					
196 				} else {
197 					humanReadableAnswer = answerId;
198 				}
199 			} else {
200 				humanReadableAnswer = Messages
201 						.getString("StudentAssessment.12"); //$NON-NLS-1$
202 			}// if
203 
204 			JPanel aItemPanel = new JPanel(new BorderLayout(0, 0));
205 			aItemPanel.setBackground(Color.BLACK);
206 			aItemPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 1, 2));
207 
208 			JPanel promptPanel = new JPanel(new BorderLayout(0, 0));
209 
210 			JComponent editorPane = AssessmentUI.createPromptPanel( Assessment.getInteractionByResponseDeclaration(assessment.getAssessmentItem().getItemBody().getInteractions(), rd));
211 			editorPane.setBackground(PasColors.studentnAssessmentResultBackgroundColor);
212 			promptPanel.add(editorPane, BorderLayout.CENTER);
213 			promptPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
214 			promptPanel.setBackground(PasColors.studentnAssessmentResultBackgroundColor);
215 			aItemPanel.add(promptPanel, BorderLayout.NORTH);
216 
217 			JPanel answerPanel = new JPanel(new BorderLayout(0, 0));
218 
219 			humanReadableAnswer = wrapText(humanReadableAnswer,100.0);
220 			JLabel answerLabel = getFeedbackLbl(humanReadableAnswer,100.0);
221 			answerPanel.add(answerLabel,BorderLayout.CENTER);
222 			JLabel feedbackLabel = new JLabel(inlineFeedback);
223 			
224 			//check if correct add icon
225 			if( isCorrect ) {
226 				feedbackLabel.setIcon(greenCheck);
227 			} else {
228 				feedbackLabel.setIcon(redX);
229 			}// if
230 			
231 			answerPanel.add(feedbackLabel, BorderLayout.SOUTH);
232 			//answerPanel.add(new JLabel(humanReadableAnswer),
233 			//		BorderLayout.CENTER);
234 			System.out.println(humanReadableAnswer);
235 			answerPanel.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
236 			answerPanel.setBackground(Color.WHITE);
237 			aItemPanel.add(answerPanel, BorderLayout.CENTER);
238 
239 			// JPanel temp = new JPanel(new BorderLayout(0,0));
240 			// temp.setOpaque(false);
241 			// temp.add(aItemPanel,BorderLayout.NORTH);
242 
243 			itemsPanel.add(aItemPanel);
244 
245 		}// for
246 
247 		JPanel innerPanel = new JPanel(new BorderLayout(0, 0));
248 		innerPanel.setOpaque(false);
249 		innerPanel.add(finishedLabel, BorderLayout.NORTH);
250 		//innerPanel.setBackground(Color.red);
251 		JScrollPane answerScroller = new JScrollPane();
252 		answerScroller
253 				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
254 		answerScroller
255 				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
256 
257 		JPanel temp = new JPanel(new BorderLayout(0, 0));
258 		temp.add(itemsPanel, BorderLayout.NORTH);
259 		//temp.setBackground(Color.blue);
260 		temp.setOpaque(false);
261 		temp.setBorder(BorderFactory.createLineBorder(Color.black, 2));
262 		answerScroller.setViewportView(temp);
263 		answerScroller.setBorder(BorderFactory.createEmptyBorder());
264 		answerScroller.getViewport().setViewPosition(new java.awt.Point(0, 0));
265 		answerScroller.getVerticalScrollBar().setValue(
266 				answerScroller.getVerticalScrollBar().getMaximum());
267 		answerScroller.setBackground(Color.RED);
268 		//answerScroller.setOpaque(false);
269 		resultsCard.add(answerScroller, BorderLayout.CENTER);
270 
271 		resultsCard.add(innerPanel, BorderLayout.NORTH);
272 		// resultsCard.add(,BorderLayout.CENTER);
273 
274 		JButton backButton = new JButton(Messages
275 				.getString("StudentAssessment.13")); //$NON-NLS-1$
276 		CommonUI.makeEnhancedButton(backButton);
277 		backButton.addActionListener(new ActionListener() {
278 
279 			public void actionPerformed(ActionEvent e) {
280 				// responses = new HashMap();
281 				//hasSubmited = false;
282 				submitButtonPanel.setVisible(true);
283 				cardPanel.showCard(cardPanel.getComponent(0));
284 			}
285 		});
286 
287 		JPanel buttonFillerPanel = new JPanel();
288 		buttonFillerPanel.setOpaque(false);
289 		buttonFillerPanel.add(backButton);
290 
291 		resultsCard.add(buttonFillerPanel, BorderLayout.SOUTH);
292 		
293 		submitButtonPanel.setVisible(false);
294 		
295 		return resultsCard;
296 	}
297 	
298 	/***
299 	 * @param textLen
300 	 * @param adjustedWidth
301 	 * @param inlineFeedbackText
302 	 * @return
303 	 */
304 	private JLabel getFeedbackLbl(String inlineFeedbackText, double adjustedWidth) {
305 		double factor;
306 		int textlen = inlineFeedbackText.length();
307 		JLabel fiLabel = new JLabel();
308 		fiLabel.setLayout(new BoxLayout(fiLabel,BoxLayout.Y_AXIS));
309 		fiLabel.setBorder(BorderFactory.createLineBorder(PasColors.blackColor,1));
310 		Dimension thisDim = fiLabel.getPreferredSize();					
311 		factor = Math.ceil((double)textlen/adjustedWidth);
312 		thisDim.setSize(adjustedWidth,15*factor);
313 		fiLabel.setPreferredSize(thisDim);
314 		fiLabel.setMaximumSize(fiLabel.getPreferredSize());
315 		fiLabel.setMinimumSize(fiLabel.getPreferredSize());
316 		fiLabel.setText(inlineFeedbackText);
317 		fiLabel.setVisible(true);
318 //		fiLabel.setForeground(Color.RED);
319 		fiLabel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
320 		return fiLabel;
321 	}
322 
323 	private String wrapText(String comments, double textWidth){		
324 		String initHtml = "<html><body>";
325 		String finalHtml = "</body></html>";
326 		String finalStr = "";	
327 		int lastIndex = 0;
328 
329 		comments.trim();
330 
331 		while(comments.length() > textWidth){
332 			String substri = comments.substring(0,(int)textWidth);
333 			lastIndex = substri.lastIndexOf(" ");
334 			finalStr = finalStr + substri.substring(0,lastIndex) + "\n";
335 			comments = comments.substring((int)lastIndex, comments.length());
336 
337 		}
338 		
339 		return initHtml + finalStr + comments.substring(0,comments.length()) + finalHtml;		
340 	}
341 	
342 	/***
343 	 * Saves all the responses to rims
344 	 * 
345 	 * @param clientProperty
346 	 */
347 	protected void doSave() {
348 		Assessment.saveAssessmentItem(assessment.getAssessmentItem(), assessment.getResponses(),
349 				assessment.getResponseDeclarationToSocks());
350 		((SelfAssessment)assessment).setHasSubmitted(true);
351 		showResultsCard();
352 	}
353 	
354 	public JDialog showAssessmentPartsCheckDialog() {
355 		final PasMessageDialogUI ui = new PasMessageDialogUI(true);
356 		// TODO note save action NEEDs to ask the note service for
357 		// its info and have not passed in via constructor
358 		ui
359 				.setMessage("You need to complete all assessment questions before saving");
360 		return PasDialogManager.showPasMessageDialog("Warning", ui
361 				.createDialogPanel(), null, true);
362 	}
363 	
364 	public JDialog showAreYouSureCheckDialog(AbstractAction action) {
365 		final PasStudentAssessmentConfirmDialogUI ui = new PasStudentAssessmentConfirmDialogUI(action);
366 	
367 		ui
368 				.setMessage("Are all the questions answered? Once submitted all answers are final.");
369 		return PasDialogManager.showPasMessageDialog("Warning", ui
370 				.createDialogPanel(), null, true);
371 	}
372 	
373 	
374 	/***
375 	 * @return the leaveAction
376 	 */
377 	public LeaveStudentAssessmentAction getLeaveAction() {
378 		return leaveAction;
379 	}
380 	
381 	/***
382 	 * @param projectFrame
383 	 * @return 
384 	 */
385 	public JDialog  showSubmitDialog(JFrame projectFrame) {
386 		
387 		leaveAction = new LeaveStudentAssessmentAction(
388 				Messages.getString("StudentAssessment.51")); //$NON-NLS-1$
389 
390 		PasSaveStudentAssesmentDialogUI ui = new PasSaveStudentAssesmentDialogUI(
391 				leaveAction);
392 		ui.setMessage(Messages.getString("StudentAssessment.52")); //$NON-NLS-1$
393 		// TODO note save action NEEDs to ask the note service for its info and
394 		// have not passed in via constructor
395 
396 		// ui.setMessage("Your issue has not been sent to the TELS Team, would
397 		// you like to send it?");
398 		CustomDialog dialog = PasDialogManager
399 				.showPasMessageDialog(
400 						Messages.getString("StudentAssessment.53"), ui.createDialogPanel(), projectFrame, true); //$NON-NLS-1$
401 		
402 //		JDialog d = new JDialog(projectFrame, true);
403 //		d.getContentPane().add(ui.createDialogPanel());
404 //		d.pack();
405 		//d.setVisible(true);
406 		
407 //		if (leaveAction.hasLeft() == true) {
408 //			StudentAssessment.this.toLeaveStep = true;
409 //		} else {
410 //			StudentAssessment.this.toLeaveStep = false;
411 //		}
412 		return dialog;
413 //		     
414 //		});
415 		// System.out.println("StudentAssessment.showSubmitDialog()");
416 
417 	}
418 	
419 
420 
421 }