1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.curre.prefcount.gui;
16
17 import java.awt.Font;
18 import java.util.HashMap;
19 import java.util.Map;
20 import javax.swing.JLabel;
21 import javax.swing.JTextField;
22 import javax.swing.SwingConstants;
23
24 import net.curre.prefcount.gui.aa.AAJLabel;
25 import net.curre.prefcount.gui.aa.AAJTextField;
26 import net.curre.prefcount.gui.type.Place;
27 import net.curre.prefcount.util.LocaleExt;
28
29 import info.clearthought.layout.TableLayout;
30 import info.clearthought.layout.TableLayoutConstraints;
31 import org.apache.commons.lang.StringUtils;
32
33
34
35
36
37
38
39
40
41 public class PlayersNamesPanel extends DialogInnerPanel {
42
43
44 protected Map<Place, JTextField> playersFields;
45
46
47 private PlayerDialogBaseFrame dialogWindow;
48
49
50
51
52
53 private JTextField currErrorField;
54
55
56
57
58
59
60
61
62 public PlayersNamesPanel(PlayerDialogBaseFrame dialogWindow, int numPlayers) {
63 super("pref.dialog.playerNames.message", PanelPosition.FIRST);
64
65 if (numPlayers != 3 && numPlayers != 4) {
66 throw new IllegalArgumentException("Player names panel can only handle 3 or 4 players right now!");
67 }
68
69 this.dialogWindow = dialogWindow;
70 this.playersFields = new HashMap<Place, JTextField>();
71
72 setLayout(new TableLayout(new double[][]{
73 {TableLayout.FILL, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, TableLayout.FILL},
74 {TableLayout.FILL, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, TableLayout.FILL}}));
75
76 createNameFieldsHelper(numPlayers);
77 }
78
79
80 @Override
81 public void focusFirstInputField() {
82 if (this.currErrorField == null) {
83 this.playersFields.get(Place.getPlaceForIndex(0)).requestFocus();
84 } else {
85 this.currErrorField.requestFocus();
86 }
87 }
88
89
90
91
92
93
94 @Override
95 public boolean validateFields() {
96 for (JTextField fieldName : this.playersFields.values()) {
97 if (StringUtils.isBlank(fieldName.getText())) {
98 this.dialogWindow.toggleErrorField("pref.dialog.errorLabel.playerNames");
99 this.currErrorField = fieldName;
100 fieldName.requestFocus();
101 return false;
102 }
103 }
104 this.dialogWindow.toggleErrorField(null);
105 return true;
106 }
107
108
109
110
111
112
113 @Override
114 public void doOnEntry() {
115 }
116
117
118 @Override
119 public void doOnLeave() {
120
121 Map<Place, String> playersNames = new HashMap<Place, String>();
122 for (Map.Entry<Place, JTextField> entry : this.playersFields.entrySet()) {
123 playersNames.put(entry.getKey(), entry.getValue().getText().trim());
124 }
125
126
127 for (int i = 1; i <= this.playersFields.size(); ++i) {
128 PlayerDataPanel panel = (PlayerDataPanel) this.dialogWindow.questionsPane.getComponent(i);
129 panel.adjustPlayersNames(playersNames);
130 }
131
132
133 this.dialogWindow.setPlayersNames(playersNames);
134 this.dialogWindow.refreshTable();
135 }
136
137
138
139
140
141
142
143 public boolean isSomeDataEntered() {
144 for (JTextField field : this.playersFields.values()) {
145 if (StringUtils.isNotBlank(field.getText())) {
146 return true;
147 }
148 }
149 return false;
150 }
151
152
153
154
155
156
157
158 private void createNameFieldsHelper(int numPlayers) {
159 Place[] sortedByIndex = new Place[numPlayers];
160 for (Place place : Place.getPlaces(numPlayers)) {
161 sortedByIndex[place.index] = place;
162 }
163 for (Place place : sortedByIndex) {
164 JLabel label = new AAJLabel(LocaleExt.getString(place.longKey, ":"));
165 LocaleExt.registerComponent(label, place.longKey, ":");
166 label.setHorizontalAlignment(SwingConstants.RIGHT);
167 final int row = 2 * place.index + 1;
168 add(label, new TableLayoutConstraints(1, row, 1, row, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
169 super.localeSensitiveComps.add(label);
170
171 JTextField field = new AAJTextField();
172 field.setColumns(7);
173 field.setFont(new Font("Arial Black", Font.PLAIN, 12));
174 field.setFocusable(true);
175 add(field, new TableLayoutConstraints(3, row, 3, row, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
176 this.playersFields.put(place, field);
177 }
178 }
179
180 }