Mercurial > hg > mep
comparison ExperimentController.java @ 0:4031cbb02f08
Initial import.
Ignore-this: 87317e384f22bde48db996355191fa5f
author | Marcus Pearce <m.pearce@gold.ac.uk> |
---|---|
date | Tue, 18 May 2010 11:37:10 +0100 |
parents | |
children | 1fe7ac28a3ca |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4031cbb02f08 |
---|---|
1 /*============================================================================= | |
2 * File: ExperimentController.java | |
3 * Author: Marcus Pearce <m.pearce@gold.ac.uk> | |
4 * Created: <2007-12-14 12:06:10 marcusp> | |
5 * Time-stamp: <2010-05-18 10:51:31 marcusp> | |
6 *============================================================================= | |
7 */ | |
8 | |
9 import java.awt.*; | |
10 import java.awt.event.*; | |
11 import javax.swing.*; | |
12 | |
13 public class ExperimentController implements ActionListener, KeyListener { | |
14 | |
15 /* variables */ | |
16 ExperimentGui gui; | |
17 | |
18 Experiment exp; | |
19 SubjectResults results; | |
20 | |
21 InstructionsPanel ip; | |
22 StimulusPanel sp; | |
23 SubjectDataPanel sdp; | |
24 InterBlockPanel ibp; | |
25 | |
26 /* constructor */ | |
27 public ExperimentController(ExperimentGui eg) { | |
28 gui = eg; | |
29 exp = gui.getExperiment(); | |
30 results = exp.getSubjectResults(); | |
31 | |
32 ip = gui.getInstructionsPanel(); | |
33 ibp = gui.getInterBlockPanel(); | |
34 sp = gui.getStimulusPanel(); | |
35 sdp = gui.getSubjectDataPanel(); | |
36 | |
37 ip.addNextButtonListener(this); | |
38 sp.addAllListeners(this); | |
39 sdp.addFinishButtonListener(this); | |
40 ibp.addContinueButtonListener(this); | |
41 | |
42 sp.setFocusable(true); | |
43 sp.addKeyListener(this); | |
44 sp.addAllKeyListeners(this); | |
45 gui.setFocusable(true); | |
46 gui.addKeyListener(this); | |
47 } | |
48 | |
49 /* report erroneous input */ | |
50 protected void reportError (String msg) { | |
51 JOptionPane.showMessageDialog (gui, msg); | |
52 } | |
53 | |
54 /* actionPerformed */ | |
55 public void actionPerformed(ActionEvent e) { | |
56 long time = System.nanoTime(); | |
57 Object source = e.getSource(); | |
58 | |
59 ip = gui.getInstructionsPanel(); | |
60 ibp = gui.getInterBlockPanel(); | |
61 sp = gui.getStimulusPanel(); | |
62 sdp = gui.getSubjectDataPanel(); | |
63 | |
64 Block block = exp.getCurrentBlock(); | |
65 | |
66 // InstructionsPanel | |
67 if (source == ip.getNextButton()) { | |
68 int subjID = ip.getSubjectID(); | |
69 if (subjID == ip.INVALID_SUBJECT_ID || subjID < 1) | |
70 reportError("The Participant ID should be a positive integer."); | |
71 // TODO: check that subjID doesn't already exist | |
72 else { | |
73 exp.setSubjectID(subjID); | |
74 gui.showCard("stimulus"); | |
75 exp.runExperiment(); | |
76 } | |
77 } | |
78 | |
79 // InterBlockPanel | |
80 else if (source == ibp.getContinueButton()) { | |
81 block.initialiseBlock(); | |
82 sp.setSongNumberText(); | |
83 sp.defaultAnswers(); | |
84 gui.setAcceptingResponses(false); | |
85 gui.showCard("stimulus"); | |
86 exp.runExperiment(); | |
87 } | |
88 | |
89 // SubjectDataPanel | |
90 else if (e.getSource() == sdp.getFinishButton()) { | |
91 if (sdp.allDataEntered()) { | |
92 sdp.storeData(); | |
93 results.writeSubjectData(); | |
94 System.exit(0); | |
95 } else | |
96 reportError("You have not filled in all the information."); | |
97 } | |
98 | |
99 // StimulusPanel | |
100 else if (source == sp.getPlayButton()) { | |
101 // if (!exp.isRunning() && !exp.hasRun()) | |
102 // exp.runExperiment(); | |
103 // else | |
104 // reportError("You have already played the melody."); | |
105 } else if (source == sp.getNextButton()) { | |
106 if (!exp.hasRun() || exp.isRunning()) | |
107 reportError("You haven't finished playing the melody yet."); | |
108 else if (sp.unansweredQuestions()) | |
109 reportError("There are unanswered questions."); | |
110 else { | |
111 // store results | |
112 String answer1 = (String)(sp.getQ1Box().getSelectedItem()); | |
113 String answer2 = (String)(sp.getQ2Box().getSelectedItem()); | |
114 block.addMelodyQA("known", answer1); | |
115 block.addMelodyQA("liked", answer2); | |
116 block.storeMelodyResult(); | |
117 // proceed to ... | |
118 String nextFile = block.nextFile(); | |
119 if (nextFile == null) { | |
120 boolean nb = exp.nextBlock(); | |
121 if (nb) { | |
122 // ... next block of trials | |
123 gui.getInterBlockPanel().setText(); | |
124 gui.getInterBlockPanel().updateMessageDisplay(); | |
125 gui.showCard("interblock"); | |
126 } else { | |
127 // ... write results and subject questionnaire | |
128 results.writeResults(); | |
129 gui.showCard("subject"); | |
130 } | |
131 } else { | |
132 // ... next melody within block | |
133 sp.setSongNumberText(); | |
134 sp.defaultAnswers(); | |
135 gui.setAcceptingResponses(false); | |
136 block.initialiseBlock(); | |
137 exp.runExperiment(); | |
138 } | |
139 } | |
140 } else { | |
141 JButton[] rButtons = sp.getResponseButtons(); | |
142 for (int i = 0; i < rButtons.length; i++) { | |
143 if (source == rButtons[i] && (exp.isRunning() || exp.hasRun()) && gui.getAcceptingResponses()) { | |
144 block.addResponse(i+1, time); | |
145 gui.setAcceptingResponses(false); | |
146 } | |
147 } | |
148 } | |
149 } | |
150 | |
151 public void keyPressed(KeyEvent e) { | |
152 //System.out.println("Key pressed: " + e.getKeyChar()); | |
153 long time = System.nanoTime(); | |
154 | |
155 Block block = exp.getCurrentBlock(); | |
156 sp = gui.getStimulusPanel(); | |
157 JButton[] rButtons = sp.getResponseButtons(); | |
158 | |
159 for (int i = 0; i < rButtons.length; i++) { | |
160 if ((exp.isRunning() || exp.hasRun()) && gui.getAcceptingResponses()) { | |
161 //System.out.println("Char = " + Character.forDigit(i+1, 10)); | |
162 if (e.getKeyChar() == Character.forDigit(i+1, 10)) { | |
163 //System.out.println("Got rating: " + (i + 1)); | |
164 block.addResponse(i+1, time); | |
165 gui.setAcceptingResponses(false); | |
166 } | |
167 //else | |
168 // block.addResponse(0, time); | |
169 } | |
170 } | |
171 } | |
172 | |
173 public void keyReleased(KeyEvent e) { | |
174 // System.out.println("Key released: " + e.getKeyChar()); | |
175 } | |
176 public void keyTyped(KeyEvent e) { | |
177 //System.out.println("Key typed: " + e.getKeyChar()); | |
178 } | |
179 } |