forked from evantd/twidor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FingerPanel.java
304 lines (278 loc) · 8.86 KB
/
FingerPanel.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
/*
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>
* FingerPanel.java, a class for making the management of the finger pictures
* easier. Close cousin to ThumbPanel.
*
* Revisions:
* 0.5 17 July 2003
* Completed Tutor
* 0.2 06 June 2003
* Lots of bugfixes. Almost Final.
* 0.1 29 May 2003
* Created class FingerPanel
* </pre>
* @author <a href="mailto:[email protected]">James Fusia</a>
* @version Version 0.5; 17 July 2003
*/
import java.awt.*;
import javax.swing.*;
import java.util.Vector;
import java.net.URL;
public class FingerPanel extends JPanel implements TwiddlerSubPanel, TwidorConstants {
/**
* The Array of JPanels that make up the buttons on our 'face'.
* For changing the background to highlighted color blah.
*/
private Vector myButtons;
/**
* default constructor
*/
private FingerPanel () {
}// end FingerPanel ()
/**
* default constructor
* @param int the which finger we are (0-3; index-pinky)
* @param boolean the orientation
* @param KeyMap the KeyMap to write on it
*/
public FingerPanel (int finger, boolean orient, KeyMap keys, boolean visibleKeys) {
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
JPanel panel;
Vector lookup;
if (bDEBUG) System.out.println("FingerPanel: creating panel");
setPreferredSize(new Dimension(twiddlerX, (int)(windowY / 5)));
initButtons();
/* How to arrange the keys) */
if (orient) {
setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
}
else {
setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
setLayout(gridbag); /* 6w x 4h grid */
setBackground(twiddlerBackground);
constraints.fill = GridBagConstraints.BOTH;
constraints.gridwidth = 1;
constraints.gridheight = GridBagConstraints.RELATIVE;
constraints.weightx = 1;
constraints.weighty = 1;
/* Row 1: Labels or Nothing */
for (int i = 0; i < 6; i++) {
panel = new JPanel();
panel.setBackground(twiddlerBackground);
if (finger == 0) {
if (i == 0) {
panel.add(new JLabel(loadIcon(ICON_RED)));
}
else if (i == 2) {
panel.add(new JLabel(loadIcon(ICON_BLUE)));
}
else if (i == 4) {
panel.add(new JLabel(loadIcon(ICON_GREEN)));
}
else {
JLabel label = new JLabel("n");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
label.setForeground(twiddlerBackground);
panel.add(label);
}
}
if (i == 5) {
constraints.gridwidth = GridBagConstraints.REMAINDER;
}
gridbag.setConstraints(panel, constraints);
add(panel);
}
constraints.gridwidth = 1;
constraints.gridheight = GridBagConstraints.REMAINDER;
constraints.weightx = 1;
constraints.weighty = 3;
for (int i = 0; i < 6; i++) {
Vector buttons = new Vector(16,0);
for (int x = 0; x < 16; x++) {
buttons.add(x, new KeyStatus());
}
((KeyStatus)buttons.elementAt(finger * FINGER_OFFSET +
(int)(i / 2))).setStatus(true);
panel = new JPanel();
if (i % 2 == 1) {
/* Labels */
panel.setBackground(twiddlerBackground);
panel.setLayout(new GridLayout(3, 1));
if (i == 5) {
constraints.gridwidth = GridBagConstraints.REMAINDER;
}
if (finger != 0 && visibleKeys) {
for (int j = 0; j < 3; j++) {
((KeyStatus)buttons.elementAt(INDEX_OFFSET +
j)).setStatus(true);
KeyElement myButton = keys.getKey(buttons);
if (myButton != null) {
JLabel label = new JLabel();
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
/* set the text, and the color */
if (myButton.displayLetter().length() > 1) {
label.setText("y");
label.setForeground(twiddlerBackground);
}
else {
label.setText(myButton.displayLetter().toUpperCase());
if (j == 0) {
label.setForeground(Color.RED);
}
else if (j == 1) {
label.setForeground(Color.BLUE);
}
else {
label.setForeground(Color.GREEN);
}
}
panel.add(label);
}
((KeyStatus)buttons.elementAt(INDEX_OFFSET +
j)).setStatus(false);
}
}
else {
JLabel label = new JLabel("z");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
label.setForeground(twiddlerBackground);
panel.add(label);
}
}
else {
/* Button */
//panel.setPreferredSize(new Dimension(buttonX, buttonY));
panel.setBackground(buttonBackground);
panel.setLayout(new GridLayout(3, 1));
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
KeyElement myButton = keys.getKey(buttons);
if (myButton != null && visibleKeys) {
JLabel label;
if (myButton.getNumber() == KEY_SPACE) {
// space
panel.add(new JLabel());
label = new JLabel("S");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
label = new JLabel("P");
}
else if (myButton.getNumber() == KEY_BACKSPACE) {
// backspace
panel.add(new JLabel());
label = new JLabel("B");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
label = new JLabel("S");
}
else if (myButton.getNumber() == KEY_DELETE) {
// delete
label = new JLabel("D");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
label = new JLabel("E");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
label = new JLabel("L");
}
else if (myButton.getNumber() == KEY_ENTER) {
// enter
label = new JLabel("E");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
label = new JLabel("N");
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
label = new JLabel("T");
}
else {
panel.add(new JLabel());
label = new JLabel(myButton.displayLetter().toUpperCase());
}
label.setFont(new Font("Monospaced", Font.BOLD, 12));
label.setHorizontalAlignment(JLabel.CENTER);
panel.add(label);
}
else {
panel.add(new JLabel());
}
}
gridbag.setConstraints(panel, constraints);
if (i % 2 == 0) {
/* We want to keep the "Buttons" around to 'highlight' */
getButtons().add(panel);
}
add(panel);
}
if (bDEBUG) System.out.println("FingerPanel: panel created");
}// end FingerPanel (boolean, KeyMap)
private ImageIcon loadIcon (String file) {
URL location;
location = this.getClass().getResource(file);
return new ImageIcon(location);
}// end loadIcon (String)
/**
* Quick internal accessor since I was confusing myself by accessing it directly
* @return Vector my button
*/
public Vector getButtons () {
return myButtons;
}// end getMyButton ()
/**
* Quick internal accessor to initialize my buttons
*/
private void initButtons () {
myButtons = new Vector();
}// end setButtons ()
/* --==<( TwiddlerSubPanel Requirement )>==-- */
/**
* method for highlighting a certain button to a certain color
* @param int which button (0-2)
* @param Color what color you want it to go to
*/
public void highlight (int which, Color hColor) {
if (bDEBUG) System.out.println("FingerPanel: highlight button " + which);
try {
((JPanel)getButtons().elementAt(which)).setBackground(hColor);
}
catch (ArrayIndexOutOfBoundsException e) {
if (bDEBUG) System.out.println("FingerPanel: array oob");
}
}// end highlight (Color)
/* --==<( TwiddlerSubPanel Requirement )>==-- */
/**
* method for clearing the background of the buttons
*/
public void clear () {
for (int i = 0; i < getButtons().size(); i++) {
((JPanel)getButtons().elementAt(i)).setBackground(buttonBackground);
}
}// end clear ()
}// end class FingerPanel