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 }
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
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 }
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
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
180 int newChoiceIndex = currentChoiceNumber + 1;
181
182
183 SimpleChoiceType newSimpleChoice = JaxbQtiMarshallingUtils.getJaxbObjectFactory().createSimpleChoiceType();
184
185 if( interaction == null )
186 return;
187
188 JaxbQtiCreationUtils.addChoice(interaction, newSimpleChoice, newChoiceIndex, responseDeclarationType);
189
190
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
214 this.addChoice((ChoiceCardUI) component);
215 } else {
216 this.addChoice((ChoiceCardUI) component);
217 }
218
219 }
220 this.revalidate();
221 }
222
223 }
224
225 protected void trashCard(ChoiceCardUI choiceUI) {
226
227 JaxbQtiCreationUtils.removeChoice(interaction, choiceUI.getSimpleChoice());
228
229
230 correctAnswerButtonGroup.remove(choiceUI.getCorrectAnswerRadio());
231
232 this.remove(choiceUI.getChoiceNumber());
233 this.numOfChoices--;
234 this.choiceIndex--;
235
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 }
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 }