forked from evantd/twidor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twidor.java
495 lines (443 loc) · 12.6 KB
/
Twidor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
Twidor: the twiddler typing tutor.
Copyright (C) 2005 James Fusia
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
USA.
*/
/**
* CCG: Twidor- The Twiddler Tutor!
* <pre>
* Twidor.java, the GUI of the program. Get Lots of glue, and a couple rolls
* of ducttape.
*
* Revisions:
* 0.5 17 July 2003
* Completed Tutor
* 0.1 22 May 2003
* Created class Twidor
* </pre>
* @author <a href="mailto:[email protected]">James Fusia</a>
* @version Version 0.5; 17 July 2003
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.URL;
public class Twidor extends JFrame implements TwidorConstants {
/**
* internal variables
*/
private EventHandler myEventHandler;
private TwidorMenu myMenuBar;
private TwiddlerPanel myTwiddler;
private TypingPanel myTypingPanel;
private StatsPanel myStatsPanel;
private InfoPanel myInfoPanel;
private KeyMap myKeyMap;
private boolean acceptInput;
private boolean showStats;
private LessonParser myLessonPlan;
private Lesson currentLesson;
private String currentSentence;
/**
* Default Constructor.
*/
public Twidor () {
ignoreInput(true);
showingStats(false);
/* JFrame Settings */
setSize(new Dimension(windowX, windowY));
setTitle(windowTitle);
setBackground(windowBackground);
setResizable(windowResizable);
/* Root Panel Settings */
Container tempRoot = getContentPane();
tempRoot.setLayout(new BorderLayout());
setEventHandler();
setKeyMap(DEFAULT_KEYMAP);
setLessonPlan(DEFAULT_LESSON);
setTwidorMenu();
setTwiddlerPanel(TWIDDLER_MIRROR, TWIDDLER_MIRROR);
setTypingPanel();
setStatsPanel();
setInfoPanel();
setTwidorIcon();
tempRoot.add(getTwiddlerPanel(), BorderLayout.WEST);
JPanel CenterPane = new JPanel();
CenterPane.setLayout(new GridLayout(3, 1));
CenterPane.add(getInfoPanel());
CenterPane.add(getTypingPanel());
CenterPane.add(getStatsPanel());
CenterPane.setBackground(TEXT_BACKGROUND);
CenterPane.setVisible(true);
tempRoot.add(CenterPane, BorderLayout.CENTER);
/* Show it all */
setVisible(true);
setLesson("Lesson 1");
ignoreInput(false);
}// end Twidor
// EventHandler stuff
/**
* Sets the EventHandler for the System
*/
private void setEventHandler() {
myEventHandler = new EventHandler(this);
addWindowListener(myEventHandler);
addKeyListener(myEventHandler);
}// end setEventHandler ()
/**
* Accessor for the EventHandler
* @return EventHandler for the system
*/
private EventHandler getEventHandler () {
return myEventHandler;
}// end getEventHandler ()
// KeyMap stuff
/**
* Sets the Keymap for the system
* @param String the source of the keymap
*/
private void setKeyMap (String source) {
myKeyMap = new KeyMap(source);
if (myKeyMap.appearsValid()) {
if (bDEBUG) System.out.println("Keymap Loaded");
}
else {
if (bDEBUG) System.out.println("Keymap Loading Failed");
}
}// end setKeymap (String)
/**
* Accessor for the KeyMap
* @return KeyMap the current keymap
*/
private KeyMap getKeyMap () {
return myKeyMap;
}// end getKeymap ()
// TwidorMenu stuff
/**
* Sets the JMenuBar for the options
*/
private void setTwidorMenu () {
myMenuBar = new TwidorMenu(this, getEventHandler(), getLessonPlan().getLessonCount());
setJMenuBar(myMenuBar);
}// end setTwidorMenu ()
/**
* Accessor for the TwidorMenu
* @return TwidorMenu the system's menubar
*/
private TwidorMenu getTwidorMenu () {
return myMenuBar;
}// end getTwidorMenu ()
// TwiddlerPanel stuff
/**
* Sets the TwiddlerPanel for the system
* @param boolean the Thumb Orientation
* @param boolean the Finger Orientation
*/
private void setTwiddlerPanel (boolean thumb, boolean finger) {
if (bDEBUG) System.out.println("Adding TwiddlerPanel");
myTwiddler = new TwiddlerPanel(getKeyMap(), thumb, finger);
}// end setTwiddlerPanel (boolean, boolean)
/**
* Accessor for the TwiddlerPanel
* @return TwiddlerPanel the TwiddlerPanel
*/
private TwiddlerPanel getTwiddlerPanel () {
return myTwiddler;
}// end void getTwiddlerPanel ()
// TypingPanel stuff
/**
* Sets the TypingPanel up
*/
private void setTypingPanel () {
if (bDEBUG) System.out.println("Adding TypingPanel");
myTypingPanel = new TypingPanel();
}// end setTypingPanel ()
/**
* Accessor for the TypingPanel
* @return TypingPanel the typing panel
*/
private TypingPanel getTypingPanel () {
return myTypingPanel;
}// end getTypingPanel ()
// StatsPanel stuff
/**
* Set up the StatsPanel
*/
private void setStatsPanel () {
if (bDEBUG) System.out.println("Adding StatsPanel");
myStatsPanel = new StatsPanel();
}// end setStatsPanel ()
/**
* Accessor for the StatsPanel
* @return StatsPanel the stats panel
*/
private StatsPanel getStatsPanel () {
return myStatsPanel;
}// end getStatsPanel ()
// InfoPanel stuff
/**
* Set up the InfoPanel
*/
private void setInfoPanel () {
if (bDEBUG) System.out.println("Adding InfoPanel");
myInfoPanel = new InfoPanel();
}// end setInfoPanel
/**
* Accessor for the InfoPanel
* @return InfoPanel the panel
*/
private InfoPanel getInfoPanel () {
return myInfoPanel;
}// end getInfoPanel ()
// Twidor Icon stuff
/**
* Load the ImageIcon from a file. Whee.
*/
private void setTwidorIcon () {
URL location = this.getClass().getResource("icon.gif");
ImageIcon temp = new ImageIcon(location);
setIconImage(temp.getImage());
}// end setTwidorIcon ()
// LessonParser stuff
/**
* Loads the Lesson Parser and parses the lesson file
* @param String the source file the lesson parser should use
*/
private void setLessonPlan (String source) {
myLessonPlan = new LessonParser(source);
}
/**
* Accessor for the Lessons system
* @return LessonParser the lessons
*/
private LessonParser getLessonPlan () {
return myLessonPlan;
}// end getLessons ()
// Lesson stuff
/**
* Sets the current lesson from the set of lessons
* @param String the name of the lesson to set
*/
private void setLesson (String lesson) {
LessonParser lpTemp = getLessonPlan();
Lesson lTemp;
for (int i = 0; i < lpTemp.getLessonCount(); i++) {
lTemp = lpTemp.getLesson(i);
if (lTemp.getLessonName().equals(lesson)) {
currentLesson = lTemp;
currentLesson.reloadSentences();
getStatsPanel().reset();
nextSentence();
return;
}
}
if (bDEBUG) System.out.println("Twidor: Unmatched lesson name: " + lesson);
}// end setLesson (String)
/**
* gets the current lesson
* @return Lesson the current lesson
*/
private Lesson getLesson () {
return currentLesson;
}// end getLesson ()
// Misc stuff
/**
* gets the current sentence
* @return String the current sentence
*/
public String getSentence () {
return currentSentence;
}// end getSentence ()
/**
* updates the current sentence, also checks if we need to
* switch Lessons.
*/
public void nextSentence () {
/* next sentence in this lesson */
currentSentence = getLesson().getSentence();
getTypingPanel().displaySentence(getSentence());
getStatsPanel().nextSentence(getSentence());
doHighlighting();
getInfoPanel().setTitle(getLesson().getLessonName() + ": Sentence " +
getLesson().getSentenceNumber());
}// end nextSentence ()
/**
* updates the current lesson
*/
public void nextLesson () {
int next = getLesson().getLessonNumber() + 1;
if (next > getLessonPlan().getLessonCount())
next--;
getTwidorMenu().makeSelectedLesson("Lesson " + next);
//setLesson("Lesson " + next);
}// nextLesson ()
/**
* show the statistics. woo!
*/
public void showStats () {
getTypingPanel().displayMessage("Press any key to continue");
getStatsPanel().showStats();
getInfoPanel().setTitle(getLesson().getLessonName() + ": Complete");
}// showStats ()
/**
* Does the highlighting.
*/
public void doHighlighting () {
getTwiddlerPanel().clear();
KeyElement match = getKeyMap().getKey(String.valueOf((char)KEY_ENTER));
int index = getTypingPanel().getCurrent();
if (index < getSentence().length()) {
String remainder = getSentence().substring(getTypingPanel().getCurrent());
match = getKeyMap().matchLargestChunk(remainder);
}
getTwiddlerPanel().highlight(match);
}// end doHighlighting ()
/**
* sets the Input flag (whether we are recording or not)
* @param boolean the new status
*/
private void ignoreInput (boolean input) {
acceptInput = input;
}// end ignoreInput (boolean)
/**
* gets the Input flag
* @return boolean whether or not we are recording
*/
private boolean ignoreInput () {
return acceptInput;
}// end ignoreInput ()
/**
* @param boolean the new status
*/
private void showingStats (boolean input) {
showStats = input;
}// end showingStats (boolean)
/**
* @return boolean whether or not we go to the next lesson
*/
private boolean showingStats () {
return showStats;
}// end showingStats ()
/**
* Accessor for the EventHandler to inform the Tutor a key has been
* pressed.
* @param char the key pressed
* @param long the system time when it was pressed
*/
public void charTyped (String key, long time) {
/* FIXME */
String sentence = getSentence();
KeyElement typed = getKeyMap().getKey(key);
//if (ignoreInput()) {
//if (bDEBUG) System.out.println("Ignoring input for now...");
//return;
//}
if (typed == null) {
if (bDEBUG) System.out.println("Really big problem with character typed.");
return;
}
if (bDEBUG) System.out.println("Accepting input");
/* Register the keypress */
getStatsPanel().charTyped(typed, time);
getTypingPanel().charTyped(typed);
/* Determine if we need to change sentences */
if (getTypingPanel().sentenceComplete()) {
if (getLesson().isComplete()) {
if (!showingStats()) {
showStats();
showingStats(true);
} else {
nextLesson();
showingStats(false);
}
} else {
nextSentence();
}
}
doHighlighting();
}// end charTyped (char, long)
/**
* function for effecting menubar based changes
* @param String the MenuItem that was selected
* @param boolean its new status
*/
public void booleanOption (String option, boolean status) {
/* FIXME */
if (bDEBUG) System.out.println("Changing " + option + ":" + status);
getTwidorMenu().itemSelected(option, status);
if (option.equals(TWIDDLER_SHOW_TEXT)) {
getTwiddlerPanel().setTwiddlerVisible(status);
getTwiddlerPanel().reOrient();
return;
}
if (option.equals(TWIDDLER_SHOW_LETTERS_TEXT)) {
getTwiddlerPanel().setThumbKeysVisible(status);
getTwiddlerPanel().setFingerKeysVisible(status);
getTwiddlerPanel().reOrient();
return;
}
if (option.equals(TWIDDLER_MIRROR_TEXT)) {
getTwiddlerPanel().setThumbOrientation(status);
getTwiddlerPanel().setFingerOrientation(status);
getTwiddlerPanel().reOrient();
return;
}
if (option.equals(HIGHLIGHT_ERRORS_TEXT)) {
getTypingPanel().setHighlightErrors(status);
return;
}
if (option.equals(HIGHLIGHT_HINT_TEXT)) {
return;
}
if (option.equals(HIGHLIGHT_KEYPRESS_TEXT)) {
return;
}
if (bDEBUG) System.out.println("Unhandled option");
}// end booleanOption (String, boolean)
/**
* function for effecting menubar based changes
* @param String the menu option that was selected
*/
public void menuOption (String option) {
if (bDEBUG) System.out.println("Option " + option);
getTwidorMenu().itemSelected(option, true);
if (option.equals(QUIT_TEXT)) {
twidorQuit();
}
else if (option.startsWith("Lesson")) {
setLesson(option);
/* FIXME */
//getInfoPanel().setTitle(getSentences().getSource() + ": Sentence " + getSentenceNumber());
//getTypingPanel().displaySentence(getSentence());
//getStatsPanel().nextSentence(getSentence());
}
}// end menuOption (String)
/**
* function for cleaning up and closing and stuff
*/
public void twidorQuit () {
if (bDEBUG) System.out.println("Exiting Twidor.");
setVisible(false);
// insert stats saving stuff here.
getStatsPanel().save();
dispose();
System.exit(0);
}// end twidorQuit ()
/**
* Good god. The Main.
*/
public static void main (String[] argv) {
Twidor tutor = new Twidor();
}// end main
}// end class Twidor