1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package net.curre.prefcount.gui;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import javax.swing.JLabel;
20 import javax.swing.JPanel;
21 import javax.swing.JTextField;
22 import javax.swing.SwingConstants;
23
24 import net.curre.prefcount.PrefCountRegistry;
25 import net.curre.prefcount.bean.GameResultBean;
26 import net.curre.prefcount.bean.PlayerStatistics;
27 import net.curre.prefcount.gui.aa.AAJLabel;
28 import net.curre.prefcount.gui.aa.AAJTextField;
29 import net.curre.prefcount.gui.type.Place;
30 import net.curre.prefcount.util.LocaleExt;
31 import net.curre.prefcount.util.Utilities;
32 import static net.curre.prefcount.util.Utilities.FieldType.INTEGER;
33
34 import info.clearthought.layout.TableLayout;
35 import info.clearthought.layout.TableLayoutConstraints;
36
37
38
39
40
41
42
43
44
45 public class PlayerDataPanel extends DialogInnerPanel {
46
47
48 private String playerName = "";
49
50
51 protected JTextField mountField;
52
53
54 protected JTextField poolField;
55
56
57 protected Map<Place, JTextField> whistFields;
58
59
60 private HashMap<Place, JLabel> whistLabels;
61
62
63 private Place playerPlace;
64
65
66
67
68
69 private JTextField currErrorField;
70
71
72 private PlayerDialogBaseFrame dialogWindow;
73
74
75
76
77
78
79
80
81 public PlayerDataPanel(PlayerDialogBaseFrame dialogWindow,
82 int numPlayers, Place playerPlace) {
83 super(null, PanelPosition.MIDDLE);
84 this.dialogWindow = dialogWindow;
85 this.playerPlace = playerPlace;
86 this.whistFields = new HashMap<Place, JTextField>();
87 this.whistLabels = new HashMap<Place, JLabel>();
88
89 TableLayout layout = new TableLayout(new double[][]{
90 {TableLayout.PREFERRED, 90d, 4d, 50d, 4d, 30d, TableLayout.PREFERRED},
91 {11d, TableLayout.PREFERRED, TableLayout.PREFERRED, 30d, TableLayout.PREFERRED,
92 TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}});
93 this.setLayout(layout);
94 layout.setHGap(5);
95 layout.setVGap(5);
96 this.add(new JPanel(null), new TableLayoutConstraints(2, 0, 2, 0,
97 TableLayoutConstraints.FULL,
98 TableLayoutConstraints.FULL));
99
100
101 this.mountField = createFieldsHelper("pref.dialog.mount", 1, null);
102
103
104 this.poolField = createFieldsHelper("pref.dialog.pool", 2, null);
105
106
107 JLabel whistsLabel = new JLabel(LocaleExt.getString("pref.dialog.whistsFor"));
108 whistsLabel.setHorizontalAlignment(SwingConstants.CENTER);
109 LocaleExt.registerComponent(whistsLabel, "pref.dialog.whistsFor");
110 this.localeSensitiveComps.add(whistsLabel);
111 this.add(whistsLabel, new TableLayoutConstraints(0, 3, 6, 3,
112 TableLayoutConstraints.CENTER,
113 TableLayoutConstraints.CENTER));
114
115
116 int fieldRow = 4;
117 for (Place whistPlace : Place.getOtherPlayersWhistPlaces(this.playerPlace, numPlayers)) {
118 createFieldsHelper(null, fieldRow++, whistPlace);
119 }
120 }
121
122
123 @Override
124 public void focusFirstInputField() {
125 if (this.currErrorField == null) {
126 this.mountField.requestFocus();
127 } else {
128 this.currErrorField.requestFocus();
129 }
130 }
131
132
133
134
135
136
137
138
139 @Override
140 public boolean validateFields() {
141 currErrorField = null;
142 if (!Utilities.validateTextField(mountField, INTEGER)) {
143 currErrorField = mountField;
144 } else if (!Utilities.validateTextField(poolField, INTEGER)) {
145 currErrorField = poolField;
146 } else {
147 for (JTextField fieldWhist : whistFields.values()) {
148 if (!Utilities.validateTextField(fieldWhist, INTEGER)) {
149 currErrorField = fieldWhist;
150 }
151 }
152 }
153 if (currErrorField == null) {
154 dialogWindow.toggleErrorField(null);
155 return true;
156 } else {
157 currErrorField.requestFocus();
158 dialogWindow.toggleErrorField("pref.dialog.errorLabel.int");
159 return false;
160 }
161 }
162
163
164
165
166
167
168 @Override
169 public void doOnEntry() {
170 }
171
172
173 @Override
174 public void doOnLeave() {
175 GameResultBean resultBean = PrefCountRegistry.getInstance().getGameResultBean();
176 PlayerStatistics stats = resultBean.getPlayerStats().get(this.playerPlace);
177 stats.setMountainFromField(this.mountField);
178 stats.setPoolFromField(this.poolField);
179 for (Map.Entry<Place, JTextField> entry : this.whistFields.entrySet()) {
180 stats.setWhistsForPlayerFromField(entry.getKey(), entry.getValue());
181 }
182 }
183
184
185 @Override
186 public void setHeaderMessage(JLabel messageLabel) {
187 String header = this.playerName + " (" + LocaleExt.getString(this.playerPlace.shortKey) + ")";
188 messageLabel.setText(LocaleExt.getString("pref.dialog.namePrefix", header));
189 LocaleExt.reregisterComponent(messageLabel, "pref.dialog.namePrefix", header);
190 }
191
192
193
194
195
196
197 public void adjustPlayersNames(Map<Place, String> playersNames) {
198
199 this.playerName = playersNames.get(this.playerPlace);
200
201
202 for (Map.Entry<Place, JLabel> entry : this.whistLabels.entrySet()) {
203 String nameStr = playersNames.get(entry.getKey()) + ":";
204 entry.getValue().setText(nameStr);
205 }
206 }
207
208
209
210
211
212
213
214
215
216
217 private JTextField createFieldsHelper(String textKey, int row, final Place place) {
218 JLabel label = new AAJLabel();
219 if (textKey != null) {
220 label.setText(LocaleExt.getString(textKey));
221 LocaleExt.registerComponent(label, textKey);
222 this.localeSensitiveComps.add(label);
223 } else {
224 label.setText("placeholder");
225 }
226 label.setHorizontalAlignment(SwingConstants.RIGHT);
227
228 this.add(label, new TableLayoutConstraints(1, row, 1, row,
229 TableLayoutConstraints.FULL,
230 TableLayoutConstraints.FULL));
231
232 final JTextField field = new AAJTextField();
233 field.setHorizontalAlignment(SwingConstants.RIGHT);
234 this.add(field, new TableLayoutConstraints(3, row, 3, row,
235 TableLayoutConstraints.FULL,
236 TableLayoutConstraints.FULL));
237 if (place != null) {
238 this.whistLabels.put(place, label);
239 this.whistFields.put(place, field);
240
241
242 final JLabel whistPlaceLabel = new AAJLabel("(" + LocaleExt.getString(place.shortKey) + ")");
243 this.add(whistPlaceLabel, new TableLayoutConstraints(5, row, 5, row,
244 TableLayoutConstraints.FULL,
245 TableLayoutConstraints.FULL));
246 LocaleExt.registerComponent(new LocaleExt.LocaleExec() {
247 public void doChange() {
248 whistPlaceLabel.setText("(" + LocaleExt.getString(place.shortKey) + ")");
249 }
250 }, "WHIST_LABEL_" + row + "_" + place.name() + "_" + (int) (Math.random() * 10));
251
252
253
254 }
255
256 return field;
257 }
258
259 }