summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.ui.swing/src/main/java/net/wotonomy/ui/swing/components/InfoPanel.java
blob: 06938e9e2b7b0396e7200882b7ce624131c8921c (plain)
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Blacksmith, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.ui.swing.components;

import java.awt.AWTEventMulticaster;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.MethodDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.swing.Box;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * InfoPanel uses labels and textfields (or any other component - see below) to
 * display a list of keys and values in a well-aligned and consistent manner,
 * conforming to alignment and pixel spacing in the java look and feel
 * <a href="http://java.sun.com/products/jlf/dg/higg.htm#55417">design
 * guidelines</a>. <BR>
 * <BR>
 *
 * Each key is displayed in a label to the left of the component that contains
 * the corresponding value. Each row is displayed starting at the top of the
 * component's available area. Each row's height is the maximum preferred height
 * of its components and the field itself gets as much of the width as it can,
 * dependent on the length of the longest label. <BR>
 * <BR>
 *
 * The values in the fields can be editable, and the current value can be
 * retrieved using the key - for this reason, unique keys are recommended. <BR>
 * <BR>
 *
 * As a convenience, push buttons may be placed across the bottom of the panel
 * in a manner similar to ButtonPanel. <BR>
 * <BR>
 *
 * The panel forwards any ActionEvents generated by the components and buttons
 * on it to all registered listeners. <BR>
 * <BR>
 *
 * Optionally, any component can be used instead of a textfield. However,
 * <code>get/setValueForKey()</code> and <code>get/setEditable()</code> may not
 * work for those components. Use <code>getComponentForKey()</code> to access
 * them instead.
 *
 * @author michael@mpowers.net
 * @author $Author: cgruber $
 * @version $Revision: 904 $ $Date: 2006-02-18 18:19:05 -0500 (Sat, 18 Feb 2006)
 *          $
 */
public class InfoPanel extends JPanel implements ActionListener {
	/**
	 * Special label for an empty pair - a label and component that take up space
	 * but are hidden from view. This might be useful for achieving certain layouts.
	 */
	public static final String HIDDEN = "(hidden)";

	/** Cache for the introspectComponent method */
	private static Map _method_cache = Collections.synchronizedMap(new HashMap(30));

	protected Container listContainer = null;
	protected int hgap; // set in constructor
	protected int vgap; // set in constructor
	protected int margin; // set in constructor
	protected int columns; // set in constructor
	protected List<Component> fields = null;
	protected List<Component> labels = null;
	protected List<Component> fieldSpacers = null;
	protected ButtonPanel buttonPanel = null;
	protected boolean isEditable = true;
	protected String prefix;
	protected String postfix;
	protected int labelAnchor;
	protected int labelAlign;
//    protected Component marginStrut = null;

	// for action multicasting
	protected ActionListener actionListener = null;

	/**
	 * Constructs an empty InfoPanel.
	 */
	public InfoPanel() {
		hgap = 12; // per java l&f guidelines
		vgap = 6; // java l&f says 11
		columns = 1; // default columns
		margin = 0; // default margin: none
		prefix = ""; // default prefix: none
		postfix = ":"; // per java l&f guidelines
		fields = new ArrayList<>();
		labels = new ArrayList<>();
		labelAnchor = GridBagConstraints.NORTHWEST;
		// per java l&f guidelines (CENTER is nicer)
		labelAlign = SwingConstants.LEFT;
		// per java l&f guidelines

		doInitialLayout();
	}

	/**
	 * Constructs an InfoPanel with the specified labels each paired with a blank
	 * textfield.
	 * 
	 * @param labelArray An Array containing the labels in the order in which they
	 *                   should appear from top to bottom. A null value produces an
	 *                   empty panel.
	 */
	public InfoPanel(String[] labelArray) {
		this();
		setLabels(labelArray);
	}

	/**
	 * Creates a set of labels and empty textfields after first clearing all
	 * existing components on the panel.
	 * 
	 * @param labelArray An Array containing the labels in the order in which they
	 *                   should appear from top to bottom. A null value will clear
	 *                   the panel.
	 */
	public void setLabels(String[] labelArray) {
		removeAll();
		if (labelArray == null)
			return; // null clears panel
		for (int i = 0; i < labelArray.length; i++) {
			addPair(labelArray[i], new JTextField());
		}
	}

	/**
	 * Retrieves the labls for the components on the panel in the order in which
	 * they are displayed from top WIDTH bottom. These are the keys used to
	 * reference values or to reference the components directly.
	 * 
	 * @return An Array of Strings containing the labels.
	 */
	public String[] getLabels() {
		int length = fields.size();
		String[] labelArray = new String[length];
		for (int i = 0; i < length; i++) {
			labelArray[i] = ((Component) fields.get(i)).getName();
		}
		return labelArray;
	}

	/**
	 * Retrieves the constant used to anchor the labels in place. The default value
	 * is GridBagConstraints.NORTHWEST.
	 */
	public int getLabelAnchor() {
		return labelAnchor;
	}

	/**
	 * Sets the constant used to anchor the labels in place and reflows the layout.
	 * 
	 * @param anAnchorConstant An anchor constant from GridBagConstraints.
	 */
	public void setLabelAnchor(int anAnchorConstant) {
		labelAnchor = anAnchorConstant;
		updateLabels();
	}

	/**
	 * Retrieves the constant used to align the labels in place. The default value
	 * is GridBagConstraints.CENTER.
	 */
	public int getLabelAlignment() {
		return labelAlign;
	}

	/**
	 * Sets the constant used to align the labels in place and reflows the layout.
	 * 
	 * @param anAlignmentConstant LEFT, CENTER, or RIGHT constants from
	 *                            SwingUtilities.
	 */
	public void setLabelAlignment(int anAlignmentConstant) {
		labelAlign = anAlignmentConstant;
		updateLabels();
	}

	/**
	 * Factory method for creating panel spacers. This implementation returns a
	 * JPanel with opaque set to false. Override to customize.
	 */
	public JPanel createPanel() {
		JPanel result = new JPanel();
		result.setOpaque(false);
		return result;
	}

	/**
	 * This method is responsible for the initial layout of the panel. All labels
	 * and textfields will later added to listContainer. This method is responsible
	 * for initializing listContainer.
	 */
	protected void doInitialLayout() {
		listContainer = createPanel();
		listContainer.setLayout(new BetterGridBagLayout());
		this.setLayout(new BorderLayout());
		this.add(listContainer, BorderLayout.NORTH);

		// listContainer.setBackground( Color.blue ); // useful for testing
		// this.setBackground( Color.red );
	}

	/**
	 * Changes the horizontal spacing between the label and the components in the
	 * panel. Note: Assumes listContainer uses a GridBagLayout.
	 * 
	 * @param newHgap the new spacing, in pixels. May not be negative.
	 */
	public void setHgap(int newHgap) {
		if (newHgap < 0)
			return; // may not be negative
		this.hgap = newHgap;
		updateGaps();
		this.revalidate();
		this.repaint();

	}

	/**
	 * Gets the current horizontal spacing between components.
	 * 
	 * @return the current horizontal spacing, in pixels.
	 */
	public int getHgap() {
		return this.hgap;
	}

	/**
	 * Changes the vertical spacing between components in the panel. Note: Assumes
	 * listContainer uses a GridBagLayout.
	 * 
	 * @param newVgap the new spacing, in pixels. May not be negative.
	 */
	public void setVgap(int newVgap) {
		if (newVgap < 0)
			return; // may not be negative
		this.vgap = newVgap;
		updateGaps();
		this.revalidate();
		this.repaint();

	}

	/**
	 * Gets the current vertical spacing between components.
	 * 
	 * @return the current vertical spacing, in pixels.
	 */
	public int getVgap() {
		return this.vgap;
	}

	/**
	 * Sets the minimum width for the labels column. This left margin will grow if
	 * one of the labels is wider than this value. Note: assumes GridBagLayout.
	 * 
	 * @param newMargin the new minimum margin in pixels. May not be negative.
	 */
	public void setMargin(int newMargin) {
		if (newMargin < 0)
			return; // may not be negative
		this.margin = newMargin;

		if (listContainer.getLayout() instanceof GridBagLayout) {
			GridBagLayout gridBag = (GridBagLayout) listContainer.getLayout();
			GridBagConstraints constraints = null;
			Component c = null;
			int count = listContainer.getComponentCount();
			for (int i = 0; i < count; i++) {
				c = listContainer.getComponent(i);
				constraints = gridBag.getConstraints(c);
				if (constraints.gridy == 0 && constraints.gridx % 2 == 0) { // if this is a label spacer
																			// replace it with an appropriately sized
																			// box
					listContainer.remove(c);
					listContainer.add(Box.createHorizontalStrut(this.margin), constraints);
				}
			}
		}

		this.revalidate();
		this.repaint();

	}

	/**
	 * Gets the current minimum margin for the labels column.
	 * 
	 * @return the current minimum margin in pixels.
	 */
	public int getMargin() {
		return this.margin;
	}

	/**
	 * Sets the number of columns for the panel. Label/Component pairs will start
	 * from the top left and fill in to the right before wrapping to the next row.
	 * The default number of columns is one. Note: assumes GridBagLayout.
	 * 
	 * @param newColumns the new number of columns. May not be less than one.
	 */
	public void setColumns(int newColumns) {
		if (newColumns < 1)
			return; // may not be less than one.
		int oldColumns = this.columns;
		this.columns = newColumns;

		if (listContainer.getLayout() instanceof GridBagLayout) {
			GridBagLayout gridBag = (GridBagLayout) listContainer.getLayout();
			int count = listContainer.getComponentCount();
			Component[] components = listContainer.getComponents();
			GridBagConstraints[] constraints = new GridBagConstraints[components.length];
			for (int i = 0; i < components.length; i++) {
				constraints[i] = gridBag.getConstraints(components[i]);
			}
			listContainer.removeAll();
			for (int i = 0; i < components.length; i++) {
				if (constraints[i].gridy != 0) { // ignore first row which is reserved for spacers.

					// translate component to new position
					// (columns*2 accounts for two grid columns for one "actual" column)
					int index = (constraints[i].gridy - 1) * oldColumns * 2 + constraints[i].gridx;
					constraints[i].gridy = (index / (newColumns * 2)) + 1;
					constraints[i].gridx = index % (newColumns * 2);
					listContainer.add(components[i], constraints[i]);
				}
			}
			createSpacers(); // replace the spacers
			updateGaps();
		}

		this.revalidate();
		this.repaint();

	}

	/**
	 * Sets the vertical weight used for determining how to distribute additional
	 * vertical space in the component.
	 * 
	 * @param aComponent Key that exists in the layout.
	 * @return weighty The weight of the component, or -1.0 if not found.
	 */
	public double getVerticalWeightForKey(String key) {
		Container c = getCompositeComponentForKey(key);
		if (c == null)
			return -1.0;
		if (!(listContainer.getLayout() instanceof GridBagLayout))
			return -1.0;
		GridBagLayout layout = (GridBagLayout) listContainer.getLayout();
		GridBagConstraints gbc = layout.getConstraints(c);
		return gbc.weighty;
	}

	/**
	 * Sets the vertical weight used for determining how to distribute additional
	 * vertical space in the component. By default, all weights are zero, so each
	 * component gets its preferred height. If any weights are specified, then
	 * additional space is allocated to those components proportionately.
	 * 
	 * @param aComponent Key that exists in the layout.
	 * @param weighty    The new weight.
	 */
	public void setVerticalWeightForKey(String key, double weighty) {
		Container c = getCompositeComponentForKey(key);
		if (c == null)
			return;
		if (!(listContainer.getLayout() instanceof GridBagLayout))
			return;
		GridBagLayout layout = (GridBagLayout) listContainer.getLayout();
		GridBagConstraints gbc = layout.getConstraints(c);
		gbc.weighty = weighty;
		layout.setConstraints(c, gbc);
		// handle adding on-the-fly
		updateGaps();
		this.revalidate();
		this.repaint();
	}

	/**
	 * Gets the current number of columns.
	 * 
	 * @return the current number of columns.
	 */
	public int getColumns() {
		return this.columns;
	}

	/**
	 * Appends a label containing a key and the specified component to the bottom of
	 * the panel. Any registered action listeners will receive action events from
	 * the component - the key corresponding to the component will be used as the
	 * action command.
	 * 
	 * @param key       A string that will be displayed in a label, preferrably
	 *                  unique.
	 * @param component A component that will be placed next to the label. If null,
	 *                  a blank JPanel will be used.
	 */
	public void addPair(String key, Component component) {
		addRow(key, new Component[] { component });
	}

	/**
	 * Appends a label containing a key and the specified component to the bottom of
	 * the panel. Any registered action listeners will receive action events from
	 * the component - the key corresponding to the component will be used as the
	 * action command.
	 * 
	 * @param key       A string that will be displayed in a label, preferrably
	 *                  unique.
	 * @param component A component that will be placed next to the label. If null,
	 *                  a blank JPanel will appear.
	 */
	public void addRow(String key, Component component) {
		addRow(key, new Component[] { component });
	}

	/**
	 * Appends a label containing a key and the specified components to the bottom
	 * of the panel. Any registered action listeners will receive action events from
	 * the component - the key corresponding to the component will be used as the
	 * action command.
	 * 
	 * @param key        A string that will be displayed in a label, preferrably
	 *                   unique.
	 * @param components An array of components that will be placed next to the
	 *                   label. Any nulls in the list will be replaced with blank
	 *                   JPanels.
	 */
	public void addRow(String key, Component[] components) {
		addCompositeComponent(key, makeCompositeComponent(key, components));
	}

	/**
	 * Appends a label containing a key and the specified components to the bottom
	 * of the panel. Any registered action listeners will receive action events from
	 * the components - the key corresponding to the component will be used as the
	 * action command.
	 * 
	 * @param key    A string that will be displayed in a label, preferrably unique.
	 * @param west   A component that will appear to the left of the other
	 *               components, as wide as its preferred width and as tall as the
	 *               tallest of the other components. A null will be replaced with a
	 *               blank JPanel.
	 * @param center A component that will appear between the other components,
	 *               taking up available space. A null will be replaced with a blank
	 *               JPanel.
	 * @param east   A component that will appear to the right of the other
	 *               components, as wide as its preferred width and as tall as the
	 *               tallest of the other components. A null will be replaced with a
	 *               blank JPanel.
	 */
	public void addRow(String key, Component west, Component center, Component east) {
		addCompositeComponent(key, makeCompositeComponent(key, west, center, east));
	}

	/**
	 * Appends a label containing a key and the specified components to the bottom
	 * of the panel. Any registered action listeners will receive action events from
	 * the components - the key corresponding to the component will be used as the
	 * action command.
	 * 
	 * @param key    A string that will be displayed in a label, preferrably unique.
	 * @param west   A component that will appear to the left of the other
	 *               components, as wide as its preferred width and as tall as the
	 *               tallest of the other components. A null will be replaced with a
	 *               blank JPanel.
	 * @param north  A component that will appear above all the other components, as
	 *               tall as its preferred height and as wide as the info panel
	 *               itself.
	 * @param center A component that will appear between the other components,
	 *               taking up available space. A null will be replaced with a blank
	 *               JPanel.
	 * @param south  A component that will appear below all the other components, as
	 *               tall as its preferred height and as wide as the info panel
	 *               itself.
	 * @param east   A component that will appear to the right of the other
	 *               components, as wide as its preferred width and as tall as the
	 *               tallest of the other components. A null will be replaced with a
	 *               blank JPanel.
	 */
	public void addRow(String key, Component west, Component north, Component center, Component south, Component east) {
		addCompositeComponent(key, makeCompositeComponent(key, west, north, center, south, east));
	}

	/**
	 * Produces a container that contains the specified components, using
	 * GridLayout. Nulls are ignored. This implementation returns a JPanel.
	 */
	protected Container makeCompositeComponent(String key, Component[] components) {
		JPanel panel = createPanel();
		if (components.length != 0) {
			panel.setLayout(new GridLayout(1, components.length, hgap, vgap));

			Component c;
			for (int i = 0; i < components.length; i++) {
				c = components[i];
				if (c != null) {
					introspectComponent(c, key);
					panel.add(c);
				}
			}
		}
		return panel;
	}

	/**
	 * Produces a container that contains the specified components, using
	 * BorderLayout. Nulls are ignored. This implementation returns a JPanel.
	 */
	protected Container makeCompositeComponent(String key, Component west, Component center, Component east) {
		JPanel panel = createPanel();
		panel.setLayout(new BorderLayout(hgap, vgap));

		if (west != null) {
			introspectComponent(west, key);
			panel.add(west, BorderLayout.WEST);
		}

		if (center != null) {
			introspectComponent(center, key);
			panel.add(center, BorderLayout.CENTER);
		}

		if (east != null) {
			introspectComponent(east, key);
			panel.add(east, BorderLayout.EAST);
		}

		return panel;
	}

	/**
	 * Produces a container that contains the specified components, using
	 * BorderLayout. Nulls are ignored. This implementation returns a JPanel.
	 */
	protected Container makeCompositeComponent(String key, Component west, Component north, Component center,
			Component south, Component east) {
		JPanel panel = createPanel();
		panel.setLayout(new BorderLayout(hgap, vgap));

		if (west != null) {
			introspectComponent(west, key);
			panel.add(west, BorderLayout.WEST);
		}

		if (north != null) {
			introspectComponent(north, key);
			panel.add(north, BorderLayout.WEST);
		}

		if (center != null) {
			introspectComponent(center, key);
			panel.add(center, BorderLayout.CENTER);
		}

		if (south != null) {
			introspectComponent(south, key);
			panel.add(south, BorderLayout.CENTER);
		}

		if (east != null) {
			introspectComponent(east, key);
			panel.add(east, BorderLayout.EAST);
		}

		return panel;
	}

	/**
	 * Override to return a specific component to be used as a label. This
	 * implementation calls createLabel().
	 */
	protected Component createLabelForKey(String aKey) {
		return createLabel();
	}

	/**
	 * Provided for backwards compatibility, and called by the default
	 * implementation of createLabelForKey. This implementation returns a JLabel.
	 */
	protected JLabel createLabel() {
		return new JLabel();
	}

	/**
	 * Appends a label containing a key and the specified component to the bottom of
	 * the panel. Any registered action listeners will receive action events from
	 * the component - the key corresponding to the component will be used as the
	 * action command.
	 * 
	 * @param key       A string that will be displayed in a label, preferrably
	 *                  unique.
	 * @param component A component that will be placed next to the label. If null,
	 *                  a stock JTextField will be used.
	 */
	protected void addCompositeComponent(String key, Component component) {
		if (key == null) {
			key = "";
		}
		Component label = createLabelForKey(key);
		Component field = component;
		if (field == null) {
			field = new JTextField(15); // default to 15 columns
		}
		field.setName(key); // for association and reference
		label.setName(key); // ditto
		if (label instanceof JLabel) {
			((JLabel) label).setHorizontalAlignment(labelAlign);
			((JLabel) label).setLabelFor(field); // for accessibility
		}
		if ("".equals(key)) {
			setText(label, "");
		} else {
			setText(label, prefix + key + postfix);
		}
		field.setEnabled(this.isEditable); // was: setEditable

		GridBagConstraints gbc = new GridBagConstraints();

		if (listContainer.getComponentCount() == 0) { // we've just initialized or called removeAll
			createSpacers();
		}

		gbc.gridx = (fields.size() % this.columns) * 2;
		gbc.gridy = (fields.size() / this.columns) + 1; // spacer is at index zero
		gbc.weightx = 0.0;
		gbc.weighty = 0.0;
		gbc.anchor = this.labelAnchor;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		listContainer.add(label, gbc);

		gbc.fill = GridBagConstraints.BOTH;
		gbc.gridx = gbc.gridx + 1;
		// FIXME: components default to the labelAnchor - should be different?
		gbc.weightx = 1.0;
		gbc.weighty = 0.0;

		listContainer.add(field, gbc);

		if (key.equals(HIDDEN)) { // these components are not to be shown
			setText(label, "     ");
			field.setVisible(false);
		}

		fields.add(field); // using list not map to allow for duplicate keys
		labels.add(label); // ditto

		// handle adding on-the-fly
		updateGaps();
		this.revalidate();
		this.repaint();
	}

	/**
	 * Introspects a component to set the action command and to add the InfoPanel to
	 * its list of ActionListeners.
	 * 
	 * @param aComponent The Component to be introspected.
	 * @param aKey       The action command to be set.
	 */
	protected void introspectComponent(Component aComponent, String aKey) {
		// try to set properties of whatever component this might be
		try {
			Method[] methods = (Method[]) _method_cache.get(aComponent.getClass());
			if (methods == null) {
				Class componentClass = aComponent.getClass();
				BeanInfo info = Introspector.getBeanInfo(componentClass);

				MethodDescriptor[] descriptors = info.getMethodDescriptors();
				Method setMethod = null;
				Method addMethod = null;
				for (int i = 0; ((setMethod == null || addMethod == null) && i < descriptors.length); i++) {
					Method m = descriptors[i].getMethod();
					String name = m.getName();
					if (setMethod == null && name.equals("setActionCommand")) {
						setMethod = m;
					} else if (addMethod == null && name.equals("addActionListener")) {
						addMethod = m;
					}
				}

				methods = new Method[] { setMethod, addMethod };
				_method_cache.put(componentClass, methods);
			}
			if (methods[0] != null) {
				methods[0].invoke(aComponent, new Object[] { aKey });
			}
			if (methods[1] != null) {
				methods[1].invoke(aComponent, new Object[] { this });
				listenedToComponents.add(aComponent);
			}
		} catch (Exception exc) { // error occured while introspecting... move along.
			System.out.println("InfoPanel.introspectComponent: " + exc);
		}
	}

	/**
	 * Called to populate a label component with the specified text. This
	 * implementation attempts to call setText(String) on the component. Override to
	 * customize.
	 */
	protected void setText(Component c, String text) {
		try {
			Method m = c.getClass().getMethod("setText", new Class[] { String.class });
			if (m != null) {
				m.invoke(c, new Object[] { text });
			}
		} catch (Exception exc) {
			// no such method: ignore
		}
	}

	/**
	 * Creates spacer components on the reserved first grid row for each column of
	 * labels and fields. This allows us to set the margin for those label columns,
	 * and set the preferred width of the field columns. A list containing the field
	 * spacers should be assigned to the fieldSpacers instance variable.
	 */
	private void createSpacers() {
		if (listContainer.getLayout() instanceof GridBagLayout) {
			// insert spacers for labels column
			GridBagLayout gridBag = (GridBagLayout) listContainer.getLayout();
			GridBagConstraints constraints = new GridBagConstraints();
			constraints.gridy = 0;
			constraints.fill = GridBagConstraints.HORIZONTAL;

			fieldSpacers = new LinkedList();
			Component fieldSpacer;
			for (int i = 0; i < this.columns; i++) {
				constraints.gridx = i * 2;
				listContainer.add(Box.createHorizontalStrut(this.margin), constraints);

				constraints.gridx = i * 2 + 1;
				fieldSpacer = Box.createHorizontalStrut(0);
				fieldSpacers.add(fieldSpacer);
				listContainer.add(fieldSpacer, constraints);
			}
		}
	}

	/**
	 * Updates the insets for all components.
	 */
	protected void updateGaps() {
		if (listContainer.getLayout() instanceof GridBagLayout) {
			GridBagLayout layout = (GridBagLayout) listContainer.getLayout();
			Component c = null;
			GridBagConstraints gbc = null;
			double totalWeightY = 0.0;
			int count = listContainer.getComponentCount();
			int i;
			for (i = 0; i < count; i++) {
				c = listContainer.getComponent(i);
				gbc = layout.getConstraints(c);
				totalWeightY += gbc.weighty;
				if ((gbc.gridx + 1) % (this.columns * 2) == 0) { // if last component in row
					gbc.insets = new Insets(0, 0, this.vgap, 0);
				} else {
					if (gbc.gridx % 2 == 0) { // is a label column - NOTE: uses eleven pixels before component, per l&f
												// guide
						gbc.insets = new Insets(0, 0, this.vgap, 11);
					} else { // is a component column
						if (gbc.gridy != 0) {
							if (c instanceof JPanel)
								((JPanel) c).setPreferredSize(null);
							gbc.insets = new Insets(0, 0, this.vgap, this.hgap);
						}
					}
				}
				layout.setConstraints(c, gbc);
			}

			// hack: gridbag clumps components in center if weighty is zero
			// if sum of weighty is zero, top-justify the list container
			this.remove(listContainer);
			if (totalWeightY == 0.0) {
				this.add(listContainer, BorderLayout.NORTH);
			} else // put list container in center so it will grow
			{
				this.add(listContainer, BorderLayout.CENTER);
			}
		}
	}

	/**
	 * Updates the label alignment.
	 */
	protected void updateLabels() {
		if (listContainer.getLayout() instanceof GridBagLayout) {
			GridBagLayout layout = (GridBagLayout) listContainer.getLayout();
			Component c = null;
			GridBagConstraints gbc = null;
			Iterator it = labels.iterator();
			while (it.hasNext()) {
				c = (Component) it.next();
				if (c instanceof JLabel) {
					((JLabel) c).setHorizontalAlignment(labelAlign);
				}
				gbc = layout.getConstraints(c);
				gbc.anchor = this.labelAnchor;
				layout.setConstraints(c, gbc);
			}
		}
	}

	/**
	 * Convenience method that uses a stock JTextField.
	 * 
	 * @param key   A string that will be displayed in a label, preferrably unique.
	 * @param value A string that will be displayed in a textfield.
	 */
	public void addPair(String key, String value) {
		addPair(key, value, null);
	}

	/**
	 * Convenience method that uses the specified JTextField or subclass and sets it
	 * to the specified value.
	 * 
	 * @param key       A string that will be displayed in a label, preferrably
	 *                  unique.
	 * @param value     A string that will be displayed in a textfield.
	 * @param textField A JTextField or subclass that will be used to display the
	 *                  value. If null, a stock JTextField will be used.
	 */
	public void addPair(String key, String value, JTextField textField) {
		if (value == null) {
			value = "";
		}
		JTextField field = textField;
		if (field == null) {
			field = new JTextField(15); // default to 15 columns
		} else {
			field = textField;
		}
		field.setText(value);

		addPair(key, (Component) field);
	}

	/**
	 * Removes all components from the list. Buttons, if any, will remain unchanged
	 * - use setButtons( null ) to remove them. NOTE: does not call
	 * super.removeAll().
	 */
	public void removeAll() {
		Object component;
		Method method;
		Class[] paramClasses = new Class[] { ActionListener.class };
		Object[] paramObjects = new Object[] { this };

		Iterator iterator = listenedToComponents.iterator();
		while (iterator.hasNext()) {
			component = iterator.next();
			try {
				method = component.getClass().getMethod("removeActionListener", paramClasses);
				if (method != null) {
					method.invoke(component, paramObjects);
				}
			} catch (Exception exception) {
				// No removeActionListener() method, move along.
			}
		}

		listenedToComponents.clear();

		listContainer.removeAll();
		fields.clear();
		labels.clear();
		this.revalidate();
		this.repaint();

		// FIXME: It is very confusing that this
		// implementation does not call super.removeAll().
	}

	/**
	 * Adds one or buttons to the bottom of the panel with the specified labels from
	 * left to right. Any action listeners will receive action events from clicks on
	 * these buttons - the supplied label will be used as the action command.
	 * 
	 * @param buttons A string array containing the strings to be used for the
	 *                button labels and action commands. A null value will remove
	 *                the button panel.
	 * @see ButtonPanel
	 */
	public void setButtons(String[] buttons) {
		if (buttonPanel == null) {
			buttonPanel = new ButtonPanel();
			buttonPanel.setInsets(new Insets(6, 0, 0, 0));
			// button panel has a 11-pixel top inset
			// and java l&f guide says 17-pixels before command buttons
			buttonPanel.addActionListener(this);
			this.add(buttonPanel, BorderLayout.SOUTH);
		}
		if (buttons == null) {
			this.remove(buttonPanel);
			buttonPanel = null;
		} else {
			buttonPanel.setLabels(buttons);
		}

		this.revalidate();
		this.repaint();
	}

	protected Collection listenedToComponents = new LinkedList();

	/**
	 * Retrieves the names of the buttons that are displayed, if any.
	 * 
	 * @return A string array containing the strings used for the button labels and
	 *         action commands, or null if no buttons have been created.
	 * @see ButtonPanel
	 */
	public String[] getButtons() {
		if (buttonPanel == null) {
			return null; // none created
		}

		return buttonPanel.getLabels();
	}

	/**
	 * Retrieves the actual button panel, if any.
	 * 
	 * @return A button panel, or null if none has been created.
	 * @see ButtonPanel
	 */
	public ButtonPanel getButtonPanel() {
		return buttonPanel;
	}

	/**
	 * Sets whether the values displayed in the panel should be editable. Defaults
	 * to true.
	 * 
	 * @param isEditable Whether the values should be editable.
	 */
	public void setEditable(boolean isEditable) {
		this.isEditable = isEditable;
		Iterator enumeration = fields.iterator();
		while (enumeration.hasNext()) {
			((Component) enumeration.next()).setEnabled(isEditable);
		}
	}

	/**
	 * Gets whether the values displayed in the panel are editable.
	 * 
	 * @return Whether the values should be editable.
	 */
	public boolean isEditable() {
		return this.isEditable;
	}

	/**
	 * Sets the field associated with the key to the specified value. Note: If the
	 * component does not respond to setText() or setString() or setValue() the
	 * value will not be set. JTextFields and the like will work.
	 * 
	 * @param key   A string representing the key associated with the field. Nulls
	 *              are converted to an empty string.
	 * @param value A object to be displayed in the specified field. Nulls are
	 *              converted to an empty string.
	 */
	public void setValueForKey(String key, Object value) {
		setValueForKey(key, value, 0);
	}

	/**
	 * Sets the field associated with the key to the specified value. Note: If the
	 * component does not respond to setText() or setString() or setValue() the
	 * value will not be set. JTextFields and the like will work.
	 * 
	 * @param key   A string representing the key associated with the field. Nulls
	 *              are converted to an empty string.
	 * @param value A object to be displayed in the specified field. Nulls are
	 *              converted to an empty string.
	 */
	public void setValueForKey(String key, Object value, int index) {
		if (key == null) {
			key = "";
		}

		Container field = null;
		for (int i = 0; i < fields.size(); i++) {
			field = (Container) fields.get(i);
			if (key.equals(field.getName())) {
				setValueForIndex(index, i, value);
				return;
			}
		}
		// else not found - ignore
	}

	/**
	 * Sets the first field at the specified row index to the specified value. Note:
	 * If the component does not respond to setText() or setString() or setValue()
	 * the value will not be set. JTextFields and the like will work.
	 * 
	 * @param row   The row index of the component.
	 * @param value A object to be displayed in the specified field. Nulls are
	 *              converted to an empty string.
	 */
	public void setValueForIndex(int row, Object value) {
		setValueForIndex(row, 0, value);
	}

	/**
	 * Sets the field at the specified row index and column index to the specified
	 * value. Note: If the component does not respond to setText() or setString() or
	 * setValue() the value will not be set. JTextFields and the like will work.
	 * 
	 * @param row   The row index of the component.
	 * @param index The column index of the component.
	 * @param value A object to be displayed in the specified field. Nulls are
	 *              converted to an empty string.
	 */
	public void setValueForIndex(int row, int col, Object value) {
		Container field = (Container) fields.get(row);
		Component c = field.getComponent(col);
		setValueForComponent(c, value);
	}

	/**
	 * Sets the value in the field at the specified index. Note: If the component
	 * does not respond to setText() or setString() or setValue() this method will
	 * return null. JTextFields and the like will work.
	 * 
	 * @param A     valid index.
	 * @param value A object to be displayed in the specified field.
	 */
	protected void setValueForComponent(Component aComponent, Object value) {
		// try to set a text or string property
		try {
			BeanInfo info = Introspector.getBeanInfo(aComponent.getClass());
			MethodDescriptor[] methods = info.getMethodDescriptors();
			for (int i = 0; i < methods.length; i++) {
				Method m = methods[i].getMethod();
				Class[] paramTypes = m.getParameterTypes();
				if (paramTypes.length == 1) {
					if (m.getName().equals("setText")) {
						if (paramTypes[0].getName().equals(String.class.getName())) {
							m.invoke(aComponent, new Object[] { value });
						}
					}
					if (m.getName().equals("setString")) {
						if (paramTypes[0].getName().equals(String.class.getName())) {
							m.invoke(aComponent, new Object[] { value });
						}
					}
					if (m.getName().equals("setValue")) {
						if (paramTypes[0].getName().equals(Object.class.getName())) {
							m.invoke(aComponent, new Object[] { value });
						}
					}
				}
			}
		} catch (Exception exc) { // error occured while introspecting... move along.
									// FIXME: should log error in ErrorManager
			System.out.println("InfoPanel.setValueForComponent: " + exc);
		}
	}

	/**
	 * Gets the value in the field at the specified index. Note: If the component
	 * does not respond to getText() or getString() or getSelectedItem() this method
	 * will return null. JTextFields and the like will work.
	 * 
	 * @param A valid index.
	 * @return An object representing the value in the field at the specified index,
	 *         or null if the component does not have a text property or if the
	 *         index is out of bounds.
	 */
	public Object getValueForIndex(int anIndex) {
		return getValueForIndex(anIndex, 0);
	}

	/**
	 * Gets the value in the field at the specified row and column. Note: If the
	 * component does not respond to getText() or getString() or getSelectedItem()
	 * this method will return null. JTextFields and the like will work.
	 * 
	 * @param A valid index.
	 * @return An object representing the value in the field at the specified index,
	 *         or null if the component does not have a text property or if the
	 *         index is out of bounds.
	 */
	public Object getValueForIndex(int row, int col) {
		if ((row >= fields.size()) || (row < 0)) { // out of bounds
			return null;
		}

		Container field = (Container) fields.get(row);
		Component c = field.getComponent(col);
		return getValueForComponent(c);
	}

	/**
	 * Gets the value in the field associated with the key. Note: If the component
	 * does not respond to getText() or getString() or getSelectedItem() this method
	 * will return null. JTextFields and the like will work.
	 * 
	 * @param key An string representing the key associated with the field. Nulls
	 *            are converted to an empty string.
	 * @return An object representing the value in the field associated with the
	 *         key, or null if the key does not exist or if the component does not
	 *         have a text property.
	 */
	public Object getValueForKey(String key) {
		return getValueForKey(key, 0);
	}

	/**
	 * Gets the value in the field associated with the key. Note: If the component
	 * does not respond to getText() or getString() or getSelectedItem() this method
	 * will return null. JTextFields and the like will work.
	 * 
	 * @param key An string representing the key associated with the field. Nulls
	 *            are converted to an empty string.
	 * @return An object representing the value in the field associated with the
	 *         key, or null if the key does not exist or if the component does not
	 *         have a text property.
	 */
	public Object getValueForKey(String key, int index) {
		if (key == null) {
			key = "";
		}

		Container field = null;
		Iterator enumeration = fields.iterator();
		while (enumeration.hasNext()) { // finds first value in list with specified key
			field = (Container) enumeration.next();
			if (key.equals(field.getName())) {
				Component c = field.getComponent(index);
				if (c != null) {
					return getValueForComponent(c);
				}
			}
		}
		// else not found
		return null;
	}

	/**
	 * Gets the value in the specified component. Note: If the component does not
	 * respond to getText() or getString() or getSelectedItem() this method will
	 * return null. JTextFields and the like will work.
	 * 
	 * @param aComponent The specified component.
	 * @return An object representing the value in the component. or null if the
	 *         component does not have a text property.
	 */
	protected Object getValueForComponent(Component aComponent) {
		// try to get a text or string property
		try {
			BeanInfo info = Introspector.getBeanInfo(aComponent.getClass());
			MethodDescriptor[] methods = info.getMethodDescriptors();
			for (int i = 0; i < methods.length; i++) {
				Method m = methods[i].getMethod();
				Class[] paramTypes = m.getParameterTypes();
				if (m.getName().equals("getText")) {
					if (paramTypes.length == 0) {
						return m.invoke(aComponent, new Object[] {});
					}
				}
				if (m.getName().equals("getString")) {
					if (paramTypes.length == 0) {
						return m.invoke(aComponent, new Object[] {});
					}
				}
				if (m.getName().equals("getSelectedItem")) {
					if (paramTypes.length == 0) {
						return m.invoke(aComponent, new Object[] {});
					}
				}
				// TODO: should also handle variants of setValue()
			}
		} catch (Exception exc) { // error occured while introspecting... move along.
			System.out.println("InfoPanel.getValueFromComponent: " + exc);
		}

		// not found
		return null;
	}

	/**
	 * Gets the component associated with the key as a JTextField, for backwards
	 * compatibility.
	 * 
	 * @param key A string representing the key associated with the component. Nulls
	 *            are converted to an empty string.
	 * @return A JTextField that contains the value associated with the key, or null
	 *         if the key does not exist or if the component is not a JTextField.
	 */
	public JTextField getFieldForKey(String key) {
		Component c = getComponentForKey(key);
		if (c instanceof JTextField) {
			return (JTextField) c;
		}
		return null;
	}

	/**
	 * Gets the component associated with the key. If more than one component is
	 * associated with the key, returns the first such component.
	 * 
	 * @param key A string representing the key associated with the component. Nulls
	 *            are converted to an empty string.
	 * @return A component that contains the value associated with the key, or null
	 *         if the key does not exist.
	 */
	public Component getComponentForKey(String key) {
		return getComponentForKey(key, 0);
	}

	/**
	 * Gets the component associated with the key and index.
	 * 
	 * @param key A string representing the key associated with the component. Nulls
	 *            are converted to an empty string.
	 * @return A component that contains the value associated with the key, or null
	 *         if the key does not exist.
	 */
	public Component getComponentForKey(String key, int index) {
		Container c = getCompositeComponentForKey(key);
		if (c == null)
			return null;
		return c.getComponent(index);
	}

	/**
	 * Gets the component at the specified row. If more than one component exists on
	 * that row, returns the first such component.
	 * 
	 * @return A component or null if the row does not exist.
	 */
	public Object getComponentForIndex(int row) {
		return getComponentForIndex(row, 0);
	}

	/**
	 * Gets the component at the specified row and column.
	 * 
	 * @return A component or null if the index is out of bounds.
	 */
	public Object getComponentForIndex(int row, int col) {
		if ((row > fields.size()) || (row < 0)) { // out of bounds
			return null;
		}

		Container field = (Container) fields.get(row);
		return field.getComponent(col);
	}

	/**
	 * Gets the container associated with the key.
	 * 
	 * @param key A string representing the key associated with the component. Nulls
	 *            are converted to an empty string.
	 * @return A component that contains the value associated with the key, or null
	 *         if the key does not exist.
	 */
	protected Container getCompositeComponentForKey(String key) {
		if (key == null) {
			key = "";
		}

		JPanel field = null;
		Iterator enumeration = fields.iterator();
		while (enumeration.hasNext()) { // finds first value in list with specified key
			field = (JPanel) enumeration.next();
			if (key.equals(field.getName())) {
				return field;
			}
		}

		// else not found
		return null;
	}

	/**
	 * Provided for backwards compatibility: calls getLabelComponentForKey.
	 * 
	 * @param key A string representing the key associated with the compoent. Nulls
	 *            are converted to an empty string.
	 * @return Component label object associated with the key, or null if the key
	 *         does not exist or if the label component is not an instance of
	 *         JLabel.
	 */
	public JLabel getLabelForKey(String key) {
		Component result = getLabelComponentForKey(key);
		if (result instanceof JLabel)
			return (JLabel) result;
		return null;
	}

	/**
	 * Get the label component associated with the key.
	 * 
	 * @param key A string representing the key associated with the compoent. Nulls
	 *            are converted to an empty string.
	 * @return Component label object associated with the key, or null if the key
	 *         does not exist.
	 */
	public Component getLabelComponentForKey(String key) {
		if (key == null) {
			key = "";
		}

		Component label = null;
		Iterator enumeration = labels.iterator();
		while (enumeration.hasNext()) { // finds first value in list with specified key
			label = (Component) enumeration.next();
			if (key.equals(label.getName())) {
				return label;
			}
		}

		// else not found
		return null;
	}

	/**
	 * Replaces the first component associated with the key. Any value in the
	 * existing component will be copied to the new component.
	 * 
	 * @param key A string representing the key to be associated with the component.
	 *            Nulls are converted to an empty string.
	 * @param c   A component to be placed next to the label corresponding to the
	 *            key. Nulls are converted to a JTextField.
	 */
	public void setComponentForKey(String key, Component c) {
		setComponentForKey(key, c, 0);
	}

	/**
	 * Replaces the component associated with the key. Any value in the existing
	 * component will be copied to the new component.
	 * 
	 * @param key A string representing the key to be associated with the component.
	 *            Nulls are converted to an empty string.
	 * @param c   A component to be placed next to the label corresponding to the
	 *            key. Nulls are converted to a JTextField.
	 */
	public void setComponentForKey(String key, Component c, int index) {
		if (c == null) {
			c = new JTextField(15);
		}
		if (key == null) {
			key = "";
		}

		Container container = this.getCompositeComponentForKey(key);
		Component field = container.getComponent(index);
		Object value = this.getValueForKey(key, index);
		if (field != null) {
			container.remove(index);
			container.add(c, index);
			c.setEnabled(this.isEditable);
			introspectComponent(c, key);
			setValueForComponent(c, value);
		}
	}

	/**
	 * Replaces the first component in the specified row. Any value in the existing
	 * component will be copied to the new component.
	 * 
	 * @param row A valid index.
	 * @param c   A component to be placed next to the label corresponding to the
	 *            key.
	 */
	public void setComponentForIndex(int row, Component c) {
		setComponentForIndex(row, 0, c);
	}

	/**
	 * Replaces the component associated with the key. Any value in the existing
	 * component will be copied to the new component.
	 * 
	 * @param row A valid index.
	 * @param c   A component to be placed next to the label corresponding to the
	 *            key.
	 */
	public void setComponentForIndex(int row, int col, Component c) {
		setComponentForKey(getLabels()[row], c, col);
	}

	/**
	 * Sets the string that appears before each label's text on the panel.
	 * 
	 * @param aString A String to be used as the label prefix.
	 */
	public void setLabelPrefix(String aString) {
		prefix = aString;
		setLabels(getLabels()); // force refresh
	}

	/**
	 * Gets the string that appears before each label's text on the panel. Defaults
	 * to "", an empty string.
	 * 
	 * @return A String that is currently used as the label prefix.
	 */
	public String getLabelPrefix() {
		return prefix;
	}

	/**
	 * Sets the string that appears after each label's text on the panel. Defaults
	 * to ": ", a colon followed by a space.
	 * 
	 * @param aString A String to be used as the label postfix.
	 */
	public void setLabelPostfix(String aString) {
		postfix = aString;
		setLabels(getLabels()); // force refresh
	}

	/**
	 * Gets the string that appears after each label's text on the panel.
	 * 
	 * @return A String that is currently used as the label postfix.
	 */
	public String getLabelPostfix() {
		return postfix;
	}

	/**
	 * Adds an action listener to the list that will be notified by events occurring
	 * in the panel.
	 * 
	 * @param l An action listener to be notified.
	 */
	public void addActionListener(ActionListener l) {
		actionListener = AWTEventMulticaster.add(actionListener, l);
	}

	/**
	 * Removes an action listener from the list that will be notified by events
	 * occurring in the panel.
	 * 
	 * @param l An action listener to be removed.
	 */
	public void removeActionListener(ActionListener l) {
		actionListener = AWTEventMulticaster.remove(actionListener, l);
	}

	/**
	 * Notifies all registered action listeners of a pending Action Event.
	 * 
	 * @param e An action event to be broadcast.
	 */
	protected void broadcastEvent(ActionEvent e) {
		if (actionListener != null) {
			actionListener.actionPerformed(e);
		}
	}

	// interface ActionListener

	/**
	 * Called by buttons on panel and by other components that might be set to
	 * broadcast events to this listener. Simply forwards the action event
	 * unchanged.
	 * 
	 * @param e An action event to be received.
	 */
	public void actionPerformed(ActionEvent e) {
//        if ( e.getSource() instanceof AbstractButton )
//        {
		broadcastEvent(e);
//        }
	}

	/**
	 * GridBagLayout allocates weightx only after considering the preferred width of
	 * the components in a column. We'd prefer that preferred width wasn't
	 * considered, so that the layout worked more like a html-table. GridBagLayout
	 * is poorly factored for subclassing, so this code is going to get a little bit
	 * ugly. Really, what good is a protected method that returns a private class?
	 * Would have liked to just override getLayoutInfo and be done with it.
	 */
	private class BetterGridBagLayout extends GridBagLayout {
		public Dimension preferredLayoutSize(Container parent) {
			preprocess();
			return super.preferredLayoutSize(parent);
		}

		public Dimension minimumLayoutSize(Container parent) {
			preprocess();
			return super.minimumLayoutSize(parent);
		}

		public void layoutContainer(Container parent) {
			preprocess();
			super.layoutContainer(parent);
		}

		protected void preprocess() {
			if (fieldSpacers == null)
				return;
			Iterator i;

			// find the field with the widest preferred size
			Component c;
			int maxWidth = 0;
			i = fields.iterator();
			while (i.hasNext()) {
				c = (Component) i.next();
				maxWidth = Math.max(maxWidth, Math.max(c.getPreferredSize().width, c.getMinimumSize().width));
			}

			// set each column's spacers to that preferred size
			Dimension min = new Dimension(0, 0);
			Dimension pref = new Dimension(maxWidth, 0);
			i = fieldSpacers.iterator();
			while (i.hasNext()) {
				((Box.Filler) i.next()).changeShape(min, pref, pref);
			}
		}
	}
}