View Javadoc

1   /***
2    * Created on Jan 10, 2006, Copyright UC Regents
3    */
4   package org.telscenter.pas.steps.quickEditors.qti.assessment;
5   
6   import java.awt.Component;
7   import java.awt.Container;
8   import java.awt.event.ActionEvent;
9   import java.awt.event.ActionListener;
10  import java.io.Serializable;
11  import java.util.List;
12  
13  import javax.swing.BoxLayout;
14  import javax.swing.ButtonGroup;
15  import javax.swing.JButton;
16  import javax.swing.JFrame;
17  import javax.swing.JPanel;
18  import javax.swing.undo.UndoManager;
19  import javax.xml.bind.JAXBElement;
20  
21  import net.sf.sail.jaxb.extension.BlockInteractionType;
22  import net.sf.sail.jaxb.extension.JaxbQtiMarshallingUtils;
23  
24  import org.imsglobal.xsd.imsqti_v2p0.ChoiceInteractionType;
25  import org.imsglobal.xsd.imsqti_v2p0.FeedbackInlineType;
26  import org.imsglobal.xsd.imsqti_v2p0.ResponseDeclarationType;
27  import org.imsglobal.xsd.imsqti_v2p0.SimpleChoiceType;
28  import org.telscenter.pas.util.JaxbQtiCreationUtils;
29  
30  
31  /***
32   * @author aperritano
33   * 
34   */
35  public class ChoiceCardContainerUI extends JPanel {
36  
37  	private int choiceIndex = 0;
38  	
39  	private int numOfChoices = 0;
40  	
41  	private ChoiceHeaderUI choiceHeader;
42  
43  	private BlockInteractionType interaction;
44  
45  	private ResponseDeclarationType responseDeclarationType;
46  	
47  	private ButtonGroup correctAnswerButtonGroup;
48  
49  	private boolean hasCorrectAnswer;
50  
51  	private UndoManager undoManager;
52  
53  	public ChoiceCardContainerUI() {
54  		super();
55  		
56  			
57  	}
58  
59  	/***
60  	 * @param numberOfResponses 
61  	 * @param b
62  	 */
63  	public ChoiceCardContainerUI(boolean hasCorrectAnswer, boolean needsEmptyChoice, BlockInteractionType interaction, ResponseDeclarationType responseDeclarationType, UndoManager undoManager) {
64  		this.hasCorrectAnswer = hasCorrectAnswer;
65  		this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
66  		this.setOpaque(false);
67  		this.interaction = interaction;
68  		this.responseDeclarationType = responseDeclarationType;
69  		choiceHeader = new ChoiceHeaderUI();
70  		correctAnswerButtonGroup = new ButtonGroup(); 
71  		this.undoManager = undoManager;
72  		addHeaderPanel();
73  		
74  		if( needsEmptyChoice ) {
75  			this.addNewChoice(new ChoiceCardUI(hasCorrectAnswer, (ChoiceInteractionType) interaction,new SimpleChoiceType(),responseDeclarationType, undoManager));
76  		}// if
77  	}
78  
79  	protected void addHeaderPanel() {
80  		this.add(choiceHeader,0);
81  	}
82  
83  	public void addChoice(ChoiceCardUI choiceUI, boolean refresh) {
84  		addChoice(choiceUI);
85  		if( refresh )
86  			this.revalidate();
87  	}
88  
89  	public void addChoice(ChoiceCardUI choice) {
90  		
91  		choiceIndex++;
92  		
93  		//add to button group
94  		
95  		correctAnswerButtonGroup.add(choice.getCorrectAnswerRadio());
96  		choice.setChoiceContainer(this);
97  		choice.setChoiceNumber(choiceIndex);
98  		
99  		this.add(choice, choiceIndex);
100 		
101 		numOfChoices++;
102 		
103 		
104 		modButtons();
105 	}
106 	
107 	protected void modButtons() {
108 		ChoiceCardUI cc = (ChoiceCardUI) this.getComponent(1);
109 		if( numOfChoices == 1 ) {
110 			cc.enableDelete(false);
111 		} else {
112 			cc.enableDelete(true);
113 		}// if	
114 	}
115 
116 	public int getChoiceIndex() {
117 		return choiceIndex;
118 	}
119 	
120 	public void setChoiceIndex(int noteChoiceIndex) {
121 		this.choiceIndex = noteChoiceIndex;
122 	}
123 	
124 	public static void main(String[] args) {
125 //		JFrame frame = new JFrame("note choice test");
126 //
127 //		Container contentPane = frame.getContentPane();
128 //
129 //		JButton showButton = new JButton("show ui");
130 //
131 //		showButton.addActionListener(new ActionListener() {
132 //
133 //			public void actionPerformed(ActionEvent e) {
134 //
135 //				JFrame frame = new JFrame("note  choice test");
136 //
137 //				Container contentPane = frame.getContentPane();
138 //				ChoiceCardContainerUI noteUI = new ChoiceCardContainerUI();
139 //				noteUI.correctAnswerButtonGroup = new ButtonGroup();
140 //				
141 //				SimpleChoiceType sc = new SimpleChoiceType();
142 //				sc.getContent().add("bob");
143 //				
144 ////				ChoiceCardUI a = new ChoiceCardUI(sc);
145 ////				ChoiceCardUI b = new ChoiceCardUI(sc);
146 ////				ChoiceCardUI c = new ChoiceCardUI(sc);
147 //				
148 ////				noteUI.addChoice(a);
149 ////				noteUI.addChoice(b);
150 ////				noteUI.addChoice(c);
151 //				contentPane.add(noteUI);
152 //				frame.setSize(noteUI.getPreferredSize());
153 //				frame.setVisible(true);
154 //			}
155 //
156 //		});
157 //		contentPane.add(showButton);
158 //
159 //		frame.setSize(200, 200);
160 //		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
161 //		frame.setVisible(true);
162 	}
163 
164 	public int getNumOfChoices() {
165 		return numOfChoices;
166 	}
167 
168 	public void setNumOfChoices(int choicesLength) {
169 		this.numOfChoices = choicesLength;
170 	}
171 
172 	/***
173 	 * @param currentChoice 
174 	 * 
175 	 */
176 	public void addNewChoice(ChoiceCardUI currentCardUI) {
177 	
178 		int currentChoiceNumber = currentCardUI.getChoiceNumber();
179 		//int currentChoiceNumber 
180 		int newChoiceIndex = currentChoiceNumber + 1;
181 		
182 		// create new card add simple choice to that
183 		SimpleChoiceType newSimpleChoice = JaxbQtiMarshallingUtils.getJaxbObjectFactory().createSimpleChoiceType();
184 		
185 		if( interaction == null ) 
186 			return;
187 		
188 		JaxbQtiCreationUtils.addChoice(interaction, newSimpleChoice, newChoiceIndex, responseDeclarationType);
189 		
190 		// add new card
191 		ChoiceCardUI newChoice = new ChoiceCardUI(hasCorrectAnswer,(ChoiceInteractionType) interaction, newSimpleChoice,responseDeclarationType, undoManager);
192 		
193 		
194 		if( currentChoiceNumber == this.numOfChoices ) {
195 			this.addChoice(newChoice,true);
196 		} else {
197 		
198 			this.setChoiceIndex(0);
199 			this.setNumOfChoices(0);
200 
201 			Component[] components = this.getComponents();
202 
203 			this.removeAll();
204 			this.addHeaderPanel();
205 			
206 			for (int i = 1; i < components.length; i++) {
207 				Component component = components[i];
208 
209 				if (i == newChoiceIndex) {
210 
211 					this.addChoice(newChoice);
212 
213 					// add the next card that the new card replaced
214 					this.addChoice((ChoiceCardUI) component);
215 				} else {
216 					this.addChoice((ChoiceCardUI) component);
217 				}// if
218 
219 			}// for
220 			this.revalidate();
221 		}// if
222 	
223 	}
224 	
225 	protected void trashCard(ChoiceCardUI choiceUI) {
226 		
227 		JaxbQtiCreationUtils.removeChoice(interaction, choiceUI.getSimpleChoice());
228 		
229 		//remove from button group
230 		correctAnswerButtonGroup.remove(choiceUI.getCorrectAnswerRadio());
231 		//get the index and remove component
232 		this.remove(choiceUI.getChoiceNumber());
233 		this.numOfChoices--;
234 		this.choiceIndex--;
235 		//re index all of them
236 		Component[] components = this.getComponents();
237 		for (int i = 1; i < components.length; i++) {
238 			ChoiceCardUI cc = (ChoiceCardUI) components[i];
239 			System.out.println("c numbers: " + i);
240 			cc.setChoiceNumber(i);
241 			
242 		}// for
243 		this.modButtons();
244 		this.revalidate();
245 	}
246 
247 	/***
248 	 * @param interaction
249 	 */
250 	public void addInteraction(BlockInteractionType interaction) {
251 		this.interaction = interaction;
252 		
253 	}
254 	
255 	
256 	
257 	
258 }