View Javadoc

1   /**
2    * This program is free software: you can redistribute it and/or modify
3    * it under the terms of the GNU General Public License as published by
4    * the Free Software Foundation, version 3.
5    *
6    * This program is distributed in the hope that it will be useful,
7    * but WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9    * GNU General Public License for more details.
10   *
11   * You should have received a copy of the GNU General Public License
12   * along with this program. If not, see <http://www.gnu.org/licenses/>.
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   * Object of this class represents the last input panel,
36   * where target bullet is entered and final scores are
37   * computed (initiated).
38   * <p/>
39   * Created date: Apr 8, 2007
40   *
41   * @author Yevgeny Nyden
42   */
43  public class LastInputPanel extends DialogInnerPanel {
44  
45    /** List of references to the player names fields. */
46    private JTextField targetBullet;
47  
48    /** Reference to the parent dialog frame. */
49    private PlayerDialogBasePanel dialogWindow;
50  
51    /**
52     * Constructor.
53     *
54     * @param dialogWindow Reference to the dialog window/frame.
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      // ======== bulletPanel ========
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        //---- bulletLabel ----
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     //======== buttonPanel ========
102     JPanel buttonPanel = new JPanel();
103     buttonPanel.setOpaque(true);
104     {
105       buttonPanel.setLayout(new FlowLayout());
106 
107       //---- computeButton ----
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       // if there is no action menu bar,
116       // adding a shortcut to the compute button
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   /** {@inheritDoc} */
131   public void focusFirstInputField() {
132     targetBullet.requestFocus();
133   }
134 
135   /**
136    * Validates target bullet input field.
137    *
138    * @return True if target bullet input filed is valid; false otherwise.
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(); // trying to request focus
155     return false;
156   }
157 
158   /**
159    * {@inheritDoc}
160    * <p/>
161    *  
162    */
163   public void doOnEntry() {
164     // setting target bullet (and the test input field)
165     // only if the target bullet has not been set already (>0)
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    * Clearing players game data in the results bean.
185    * <p/>
186    * {@inheritDoc}
187    */
188   public void doOnLeave() {
189     ResultService.clearFinalResults(dialogWindow.mainWindow.playerResults);
190     // setting the target bullet
191     dialogWindow.mainWindow.playerResults.setTargetBullet(getTargetBulletValue());
192   }
193 
194   /**
195    * Sets target bullet text value.
196    *
197    * @param value String value to set.
198    */
199   public void setTargetBulletText(String value) {
200     targetBullet.setText(value);
201   }
202 
203   /**
204    * Parses and returns the target bullet text field value.
205    * This method assumes that the field has been validated.
206    *
207    * @return Target bullet text input field value. 
208    */
209   public int getTargetBulletValue() {
210     return Utilities.parseIntFromTextField(targetBullet);
211   }
212 
213   /** Private methods ***********************/
214 
215 }