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.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   * Object of this class represents a player data
34   * panel, where all player scores are entered.
35   * <p/>
36   * Created date: Apr 3, 2007
37   *
38   * @author Yevgeny Nyden
39   */
40  public class PlayerDataPanel extends DialogInnerPanel {
41  
42    /** String for the player name. */
43    private String playerName = "";
44  
45    /** Reference to the mount text field. */
46    protected JTextField mountField;
47  
48    /** Reference to the bullet text field. */
49    protected JTextField bulletField;
50  
51    /** Reference to the vists text fields. */
52    protected List<JTextField> fieldVists;
53  
54    /** Player's index relative to other players (0 - 4). */
55    private int playerIndex;
56  
57    /**
58     * Current error field index - field that
59     * failed validation (null if none).
60     */
61    private JTextField currErrorField;
62  
63    /** Reference to the parent dialog frame. */
64    private PlayerDialogBasePanel dialogWindow;
65  
66    /**
67     * Constructor.
68     *
69     * @param dialogWindow Reference to the dialog (parent) window.
70     * @param numPlayers   Number of players in the game.
71     * @param playerIndex  Player's index relative to other players.
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      // ---- mount label ----
93      mountField = createFieldsHelper(bundle.getString("pref.dialog.mount"), 1);
94  
95      // ---- bullet label ----
96      bulletField = createFieldsHelper(bundle.getString("pref.dialog.bullet"), 2);
97  
98      // ---- vist labels ----
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   /** {@inheritDoc} */
110   public void focusFirstInputField() {
111     if (currErrorField == null) {
112       mountField.requestFocus();
113     } else {
114       currErrorField.requestFocus();
115     }
116   }
117 
118   /**
119    * Validates numeric fields (mount, bullet, vists);
120    * if any field is not valid, an error message will
121    * be displayed at the bottom of the panel.
122    *
123    * @return True if all numeric fields are valid; false otherwise.
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(); // need to try transfering focus
143       ResourceBundle bundle = ResourceBundle.getBundle("default");
144       dialogWindow.toggleErrorField(bundle.getString("pref.dialog.errorLabel.int"));
145       return false;
146     }
147   }
148 
149   /**
150    * Does nothing.
151    * <p/>
152    * {@inheritDoc}
153    */
154   public void doOnEntry() {
155   }
156 
157   /** {@inheritDoc} */
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   /** {@inheritDoc} */
173   public String getHeaderMessage() {
174     ResourceBundle bundle = ResourceBundle.getBundle("default");
175     return bundle.getString("pref.dialog.namePrefix") + " " + playerName;
176   }
177 
178   /**
179    * Adjusts players names on the panel.
180    *
181    * @param playersNames List with players names.
182    */
183   public void adjustPlayersNames(List<String> playersNames) {
184     ResourceBundle bundle = ResourceBundle.getBundle("default");
185     playerName = playersNames.get(playerIndex);
186 
187     // setting player name on the vists labels
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    * Creates a label and a text filed and
200    * adds it to the panel.
201    *
202    * @param labelMessage Label message.
203    * @param row          Table row.
204    * @return Created text field.
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 }