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.*;
18 import java.util.ResourceBundle;
19 import javax.swing.*;
20
21 import info.clearthought.layout.TableLayout;
22 import info.clearthought.layout.TableLayoutConstraints;
23
24 import net.curre.prefcount.event.ComputeButtonActionListener;
25 import net.curre.prefcount.service.ResultService;
26 import net.curre.prefcount.util.Utilities;
27 import net.curre.prefcount.gui.aa.AAJButton;
28 import net.curre.prefcount.gui.aa.AAJPanel;
29 import net.curre.prefcount.gui.aa.AAJLabel;
30 import net.curre.prefcount.gui.aa.AAJTextField;
31
32 import static net.curre.prefcount.util.Utilities.FieldType.INTEGER;
33
34
35
36
37
38
39
40
41
42
43 public class LastInputPanel extends DialogInnerPanel {
44
45
46 private JTextField targetBullet;
47
48
49 private PlayerDialogBasePanel dialogWindow;
50
51
52
53
54
55
56 public LastInputPanel(PlayerDialogBasePanel dialogWindow) {
57 super("pref.dialog.computeMessage", PanelPosition.LAST);
58 this.dialogWindow = dialogWindow;
59 this.setLayout(new BorderLayout(10, 10));
60
61 ResourceBundle bundle = ResourceBundle.getBundle("default");
62
63
64 JPanel bulletPanel = new AAJPanel();
65 bulletPanel.setOpaque(true);
66 {
67 TableLayout layout = new TableLayout(new double[][]{
68 {5, TableLayout.PREFERRED, 4, 50, TableLayout.PREFERRED},
69 {19, TableLayout.PREFERRED, TableLayout.PREFERRED}});
70 bulletPanel.setLayout(layout);
71 layout.setHGap(5);
72 layout.setVGap(5);
73 bulletPanel.add(new JPanel(null),
74 new TableLayoutConstraints(2, 0, 2, 0,
75 TableLayoutConstraints.FULL,
76 TableLayoutConstraints.FULL));
77
78
79 JLabel bulletLabel = new AAJLabel();
80 bulletLabel.setOpaque(true);
81 bulletLabel.setText(bundle.getString("pref.dialog.maxBullet"));
82 bulletLabel.setHorizontalAlignment(SwingConstants.RIGHT);
83 bulletPanel.add(bulletLabel,
84 new TableLayoutConstraints(1, 1, 1, 1,
85 TableLayoutConstraints.FULL,
86 TableLayoutConstraints.FULL));
87 targetBullet = new AAJTextField();
88 targetBullet.setOpaque(true);
89 targetBullet.setHorizontalAlignment(SwingConstants.RIGHT);
90 bulletPanel.add(targetBullet,
91 new TableLayoutConstraints(3, 1, 3, 1,
92 TableLayoutConstraints.FULL,
93 TableLayoutConstraints.FULL));
94 bulletPanel.add(new JPanel(null),
95 new TableLayoutConstraints(2, 2, 2, 2,
96 TableLayoutConstraints.FULL,
97 TableLayoutConstraints.FULL));
98 }
99 this.add(bulletPanel, BorderLayout.CENTER);
100
101
102 JPanel buttonPanel = new JPanel();
103 buttonPanel.setOpaque(true);
104 {
105 buttonPanel.setLayout(new FlowLayout());
106
107
108 JButton computeButton = new AAJButton();
109 int index = Integer.parseInt(bundle.getString("pref.dialog.computeButton.shortcut.letterIndex"));
110 computeButton.setText(Utilities.underlineLetter(bundle.getString("pref.dialog.computeButton"), index));
111 ComputeButtonActionListener action = new ComputeButtonActionListener();
112 computeButton.addActionListener(action);
113 computeButton.addKeyListener(action);
114
115
116
117 if (dialogWindow.menuBar == null) {
118 InputMap map = dialogWindow.questionsPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
119 char mnemonicCode = bundle.getString("pref.dialog.computeButton.shortcut").charAt(0);
120 String actionName = "button" + mnemonicCode + "Action";
121 map.put(KeyStroke.getKeyStroke("control " + mnemonicCode), actionName);
122 dialogWindow.questionsPane.getActionMap().put(actionName, action);
123 }
124
125 buttonPanel.add(computeButton);
126 }
127 this.add(buttonPanel, BorderLayout.SOUTH);
128 }
129
130
131 public void focusFirstInputField() {
132 targetBullet.requestFocus();
133 }
134
135
136
137
138
139
140 public boolean validateFields() {
141 ResourceBundle bundle = ResourceBundle.getBundle("default");
142 String errorKey;
143 if (Utilities.validateTextField(targetBullet, INTEGER)) {
144 if (getTargetBulletValue() > 0) {
145 dialogWindow.toggleErrorField(null);
146 return true;
147 } else {
148 errorKey = "pref.dialog.errorLabel.negTarget";
149 }
150 } else {
151 errorKey = "pref.dialog.errorLabel.int";
152 }
153 dialogWindow.toggleErrorField(bundle.getString(errorKey));
154 targetBullet.requestFocus();
155 return false;
156 }
157
158
159
160
161
162
163 public void doOnEntry() {
164
165
166 if (getTargetBulletValue() < 1) {
167 int maxBullet = 0;
168 for (Component comp : dialogWindow.questionsPane.getComponents()) {
169 if (comp instanceof PlayerDataPanel) {
170 JTextField bulletField = ((PlayerDataPanel) comp).bulletField;
171 int bullet = Utilities.parseIntFromTextField(bulletField);
172 if (bullet > maxBullet) {
173 maxBullet = bullet;
174 }
175 }
176 }
177 if (maxBullet > 0) {
178 targetBullet.setText(String.valueOf(maxBullet));
179 }
180 }
181 }
182
183
184
185
186
187
188 public void doOnLeave() {
189 ResultService.clearFinalResults(dialogWindow.mainWindow.playerResults);
190
191 dialogWindow.mainWindow.playerResults.setTargetBullet(getTargetBulletValue());
192 }
193
194
195
196
197
198
199 public void setTargetBulletText(String value) {
200 targetBullet.setText(value);
201 }
202
203
204
205
206
207
208
209 public int getTargetBulletValue() {
210 return Utilities.parseIntFromTextField(targetBullet);
211 }
212
213
214
215 }