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.ArrayList;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.ResourceBundle;
21 import javax.swing.*;
22
23 import net.curre.prefcount.bean.GameResultBean;
24 import net.curre.prefcount.bean.PlayerStatistics;
25 import net.curre.prefcount.util.Utilities;
26 import static net.curre.prefcount.util.Utilities.FieldType.INTEGER;
27 import net.curre.prefcount.gui.aa.AAJTextField;
28
29 import info.clearthought.layout.TableLayout;
30 import info.clearthought.layout.TableLayoutConstraints;
31
32
33
34
35
36
37
38
39
40 public class PlayerDataPanel extends DialogInnerPanel {
41
42
43 private String playerName = "";
44
45
46 protected JTextField mountField;
47
48
49 protected JTextField bulletField;
50
51
52 protected List<JTextField> fieldVists;
53
54
55 private int playerIndex;
56
57
58
59
60
61 private JTextField currErrorField;
62
63
64 private PlayerDialogBasePanel dialogWindow;
65
66
67
68
69
70
71
72
73 public PlayerDataPanel(PlayerDialogBasePanel dialogWindow,
74 int numPlayers, int playerIndex) {
75 super(null, PanelPosition.MIDDLE);
76 this.dialogWindow = dialogWindow;
77 this.playerIndex = playerIndex;
78 fieldVists = new ArrayList<JTextField>();
79 ResourceBundle bundle = ResourceBundle.getBundle("default");
80
81 TableLayout layout = new TableLayout(new double[][]{
82 {TableLayout.PREFERRED, 90, 4, 50, TableLayout.PREFERRED},
83 {11, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED,
84 TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}});
85 this.setLayout(layout);
86 layout.setHGap(5);
87 layout.setVGap(5);
88 this.add(new JPanel(null), new TableLayoutConstraints(1, 0, 1, 0,
89 TableLayoutConstraints.FULL,
90 TableLayoutConstraints.FULL));
91
92
93 mountField = createFieldsHelper(bundle.getString("pref.dialog.mount"), 1);
94
95
96 bulletField = createFieldsHelper(bundle.getString("pref.dialog.bullet"), 2);
97
98
99 JTextField vistField = createFieldsHelper(bundle.getString("pref.dialog.vistPrefix"), 3);
100 fieldVists.add(vistField);
101 vistField = createFieldsHelper(bundle.getString("pref.dialog.vistPrefix"), 4);
102 fieldVists.add(vistField);
103 if (numPlayers == 4) {
104 vistField = createFieldsHelper(bundle.getString("pref.dialog.vistPrefix"), 5);
105 fieldVists.add(vistField);
106 }
107 }
108
109
110 public void focusFirstInputField() {
111 if (currErrorField == null) {
112 mountField.requestFocus();
113 } else {
114 currErrorField.requestFocus();
115 }
116 }
117
118
119
120
121
122
123
124
125 public boolean validateFields() {
126 currErrorField = null;
127 if (!Utilities.validateTextField(mountField, INTEGER)) {
128 currErrorField = mountField;
129 } else if (!Utilities.validateTextField(bulletField, INTEGER)) {
130 currErrorField = bulletField;
131 } else {
132 for (JTextField fieldVist : fieldVists) {
133 if (!Utilities.validateTextField(fieldVist, INTEGER)) {
134 currErrorField = fieldVist;
135 }
136 }
137 }
138 if (currErrorField == null) {
139 dialogWindow.toggleErrorField(null);
140 return true;
141 } else {
142 currErrorField.requestFocus();
143 ResourceBundle bundle = ResourceBundle.getBundle("default");
144 dialogWindow.toggleErrorField(bundle.getString("pref.dialog.errorLabel.int"));
145 return false;
146 }
147 }
148
149
150
151
152
153
154 public void doOnEntry() {
155 }
156
157
158 public void doOnLeave() {
159 GameResultBean resultsBean = dialogWindow.mainWindow.playerResults;
160 List<PlayerStatistics> statsList = resultsBean.getPlayerStats();
161 PlayerStatistics stats = statsList.get(playerIndex);
162 stats.setMountainFromField(mountField);
163 stats.setBulletFromField(bulletField);
164 Iterator<JTextField> it = fieldVists.iterator();
165 for (int i = 0; i < statsList.size(); ++i) {
166 if (i != playerIndex) {
167 stats.setVistsForPlayerFromField(i, it.next());
168 }
169 }
170 }
171
172
173 public String getHeaderMessage() {
174 ResourceBundle bundle = ResourceBundle.getBundle("default");
175 return bundle.getString("pref.dialog.namePrefix") + " " + playerName;
176 }
177
178
179
180
181
182
183 public void adjustPlayersNames(List<String> playersNames) {
184 ResourceBundle bundle = ResourceBundle.getBundle("default");
185 playerName = playersNames.get(playerIndex);
186
187
188 String vistPrefix = bundle.getString("pref.dialog.vistPrefix");
189 for (int i = 0, t = 5; i < playersNames.size(); ++i) {
190 if (i != playerIndex) {
191 JLabel label = (JLabel) super.getComponents()[t];
192 label.setText(vistPrefix + " " + playersNames.get(i) + ":");
193 t += 2;
194 }
195 }
196 }
197
198
199
200
201
202
203
204
205
206 private JTextField createFieldsHelper(String labelMessage, int row) {
207 JLabel label = new JLabel();
208 final JTextField field = new AAJTextField();
209 field.setHorizontalAlignment(SwingConstants.RIGHT);
210 label.setText(labelMessage);
211 label.setHorizontalAlignment(SwingConstants.RIGHT);
212 this.add(label, new TableLayoutConstraints(1, row, 1, row,
213 TableLayoutConstraints.FULL,
214 TableLayoutConstraints.FULL));
215 this.add(field, new TableLayoutConstraints(3, row, 3, row,
216 TableLayoutConstraints.FULL,
217 TableLayoutConstraints.FULL));
218 label.setOpaque(true);
219 return field;
220 }
221
222 }