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.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   * Object of this class represents a player data
39   * panel, where all player scores are entered.
40   * <p/>
41   * Created date: Apr 3, 2007
42   *
43   * @author Yevgeny Nyden
44   */
45  public class PlayerDataPanel extends DialogInnerPanel {
46  
47    /** String for the player name. */
48    private String playerName = "";
49  
50    /** Reference to the mount text field. */
51    protected JTextField mountField;
52  
53    /** Reference to the pool text field. */
54    protected JTextField poolField;
55  
56    /** Reference to the whists text fields. */
57    protected Map<Place, JTextField> whistFields;
58  
59    /** Reference to the whists label fields. */
60    private HashMap<Place, JLabel> whistLabels;
61  
62    /** Player's place in the game. */
63    private Place playerPlace;
64  
65    /**
66     * Current error field index - field that
67     * failed validation (null if none).
68     */
69    private JTextField currErrorField;
70  
71    /** Reference to the parent dialog frame. */
72    private PlayerDialogBaseFrame dialogWindow;
73  
74    /**
75     * Constructor.
76     *
77     * @param dialogWindow Reference to the dialog (parent) window.
78     * @param numPlayers   Number of players in the game.
79     * @param playerPlace  Player's place in the game.
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     // ---- mount label and text field ----
101     this.mountField = createFieldsHelper("pref.dialog.mount", 1, null);
102 
103     // ---- pool label and text field ----
104     this.poolField = createFieldsHelper("pref.dialog.pool", 2, null);
105 
106     // ---- whists header label ----
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     // ---- whists labels and text fields ----
116     int fieldRow = 4;
117     for (Place whistPlace : Place.getOtherPlayersWhistPlaces(this.playerPlace, numPlayers)) {
118       createFieldsHelper(null, fieldRow++, whistPlace);
119     }
120   }
121 
122   /** {@inheritDoc} */
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    * Validates numeric fields (mount, pool, whists);
134    * if any field is not valid, an error message will
135    * be displayed at the bottom of the panel.
136    *
137    * @return True if all numeric fields are valid; false otherwise.
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(); // need to try transfering focus
158       dialogWindow.toggleErrorField("pref.dialog.errorLabel.int");
159       return false;
160     }
161   }
162 
163   /**
164    * Does nothing.
165    * <p/>
166    * {@inheritDoc}
167    */
168   @Override
169   public void doOnEntry() {
170   }
171 
172   /** {@inheritDoc} */
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   /** {@inheritDoc} */
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    * Adjusts players names on the panel.
194    *
195    * @param playersNames map with players names.
196    */
197   public void adjustPlayersNames(Map<Place, String> playersNames) {
198     // setting the current player's name
199     this.playerName = playersNames.get(this.playerPlace);
200 
201     // setting players' names on the whists labels
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    * Creates a label and a text filed and adds it to the panel.
210    *
211    * @param textKey Label message key or null if this label doesn't have a key.
212    * @param row     Table row.
213    * @param place   whist place associate with created label and field,
214    *                or null when creating non-whist fields.
215    * @return Created text field.
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       // note, that the string we just constructed must be a unique key
252       // that's why we add a random number here, to insure that multiple whists
253       // will have different keys
254     }
255 
256     return field;
257   }
258 
259 }