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.awt.event.KeyAdapter;
19  import java.awt.event.KeyEvent;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.ResourceBundle;
24  import javax.swing.*;
25  
26  import net.curre.prefcount.gui.theme.skin.PrefSkin;
27  import net.curre.prefcount.gui.aa.AAJTextField;
28  import net.curre.prefcount.service.LafThemeService;
29  import net.curre.prefcount.util.Utilities;
30  
31  import org.apache.commons.lang.StringUtils;
32  
33  
34  /**
35   * Object of this class represents a players names panel -
36   * the first panel, where player names/achronyms are entered.
37   * <p/>
38   * Created date: Apr 8, 2007
39   *
40   * @author Yevgeny Nyden
41   */
42  public class PlayersNamesPanel extends DialogInnerPanel {
43  
44    /** List of references to the player names fields. */
45    protected List<JTextField> playersFields;
46  
47    /** Reference to the parent dialog frame. */
48    private PlayerDialogBasePanel dialogWindow;
49  
50    /**
51     * Current error field index - field that
52     * failed validation (null if none).
53     */
54    private JTextField currErrorField;
55  
56    /**
57     * Constructor.
58     *
59     * @param dialogWindow Reference to the dialog window/frame.
60     * @param numPlayers   Number of players in the game.
61     * @throws IllegalArgumentException If number of players is not supported.
62     */
63    public PlayersNamesPanel(PlayerDialogBasePanel dialogWindow,
64                             int numPlayers) {
65      super("pref.dialog.playerNames.message", PanelPosition.FIRST);
66      this.dialogWindow = dialogWindow;
67      playersFields = new ArrayList<JTextField>();
68  
69      GridBagLayout pLayout = new GridBagLayout();
70      super.setLayout(pLayout);
71      pLayout.columnWidths = new int[]{35, 40, 40, 40, 30, 0};
72      pLayout.rowHeights = new int[]{20, 40, 25, 40, 15, 0};
73      pLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
74      pLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0E-4};
75  
76      if (numPlayers == 3) {
77  
78        createNameFieldHelper(false, 3, 1, 1, 1, new Insets(0, 0, 5, 5));
79        createNameFieldHelper(false, 2, 3, 1, 1, new Insets(0, 0, 5, 5));
80        createNameFieldHelper(false, 1, 1, 1, 1, new Insets(0, 0, 5, 5));
81  
82        createNameFieldHelper(true, 0, 2, 1, 1, new Insets(0, 0, 5, 5));
83        createNameFieldHelper(true, 2, 2, 1, 1, new Insets(0, 0, 5, 5));
84        createNameFieldHelper(true, 4, 2, 1, 1, new Insets(0, 0, 5, 0));
85        createNameFieldHelper(true, 2, 4, 1, 1, new Insets(0, 0, 0, 5));
86        createNameFieldHelper(true, 2, 0, 1, 1, new Insets(0, 0, 5, 5));
87  
88      } else if (numPlayers == 4) {
89  
90        createNameFieldHelper(false, 2, 1, 1, 1, new Insets(0, 0, 5, 5));
91        createNameFieldHelper(false, 3, 2, 1, 1, new Insets(0, 0, 5, 5));
92        createNameFieldHelper(false, 2, 3, 1, 1, new Insets(0, 0, 5, 5));
93        createNameFieldHelper(false, 1, 2, 1, 1, new Insets(0, 0, 5, 5));
94  
95        createNameFieldHelper(true, 2, 0, 1, 1, new Insets(0, 0, 5, 5));
96        createNameFieldHelper(true, 4, 2, 1, 1, new Insets(0, 0, 5, 0));
97        createNameFieldHelper(true, 2, 4, 1, 1, new Insets(0, 0, 0, 5));
98        createNameFieldHelper(true, 0, 2, 1, 1, new Insets(0, 0, 5, 5));
99  
100     } else {
101       throw new IllegalArgumentException("Player names panel can only handle 3 or 4 players right now!");
102     }
103   }
104 
105   /** {@inheritDoc} */
106   public void focusFirstInputField() {
107     if (currErrorField == null) {
108       playersFields.get(0).requestFocus();
109     } else {
110       currErrorField.requestFocus();
111     }
112   }
113 
114   /**
115    * Validates players names fields.
116    *
117    * @return True if all players names are entered; false otherwise.
118    */
119   public boolean validateFields() {
120     for (JTextField fieldName : playersFields) {
121       if (StringUtils.isBlank(fieldName.getText())) {
122         ResourceBundle bundle = ResourceBundle.getBundle("default");
123         dialogWindow.toggleErrorField(bundle.getString("pref.dialog.errorLabel.playerNames"));
124         currErrorField = fieldName;
125         fieldName.requestFocus(); // need to try to transfer focus from the Next button
126         return false;
127       }
128     }
129     dialogWindow.toggleErrorField(null);
130     return true;
131   }
132 
133   /**
134    * Does nothing.
135    * <p/>
136    * {@inheritDoc}
137    */
138   public void doOnEntry() {
139   }
140 
141   /** {@inheritDoc} */
142   public void doOnLeave() {
143     // creating a list of players names/achronyms
144     List<String> playersNames = new ArrayList<String>();
145     for (JTextField field : playersFields) {
146       String playerLetter = Utilities.getFirstLetterFromField(field);
147       playersNames.add(playerLetter);
148     }
149     // setting/changing players names on the players input panels
150     for (int i = 1; i <= playersFields.size(); ++i) {
151       PlayerDataPanel panel = (PlayerDataPanel) dialogWindow.questionsPane.getComponent(i);
152       panel.adjustPlayersNames(playersNames);
153     }
154     // setting players names in the game results bean
155     dialogWindow.setPlayersNames(playersNames);
156   }
157 
158   /**
159    * Generates a text field for a player name input field
160    * and adds created field object to the panel and the
161    * playerFields list.
162    *
163    * @param isSpacer True if a spacer needs to be created; false - a text field.
164    * @param i        First argument for the <code>GridBagConstraints</code> ctor.
165    * @param i1       Second argument for the <code>GridBagConstraints</code> ctor.
166    * @param i2       Third argument for the <code>GridBagConstraints</code> ctor.
167    * @param i3       Fourth argument for the <code>GridBagConstraints</code> ctor.
168    * @param ins      Insert object for the <code>GridBagConstraints</code> ctor.
169    */
170   private void createNameFieldHelper(boolean isSpacer,
171                                      int i, int i1, int i2, int i3,
172                                      Insets ins) {
173     if (isSpacer) {
174       super.add(new JPanel(null),
175                 new GridBagConstraints(i, i1, i2, i3, 0d, 0d,
176                                        GridBagConstraints.CENTER,
177                                        GridBagConstraints.BOTH,
178                                        ins, 0, 0));
179     } else {
180       PrefSkin skin = LafThemeService.getInstance().getCurrentSkin();
181       JTextField textField = new AAJTextField(1);
182       textField.setHorizontalAlignment(JTextField.CENTER);
183       textField.setFont(skin.getPlayerNameFont());
184       textField.setFocusTraversalKeysEnabled(false);
185       textField.addKeyListener(new KeyAdapter() {
186         public void keyPressed(KeyEvent ke) {
187           if (ke.getKeyCode() == KeyEvent.VK_TAB) {
188             ke.consume();
189             focusNextNameField();
190           }
191         }
192       });
193       super.add(textField,
194                 new GridBagConstraints(i, i1, i2, i3, 0d, 0d,
195                                        GridBagConstraints.CENTER,
196                                        GridBagConstraints.BOTH,
197                                        ins, 0, 0));
198       textField.setOpaque(true);
199       playersFields.add(textField);
200     }
201   }
202 
203   /**
204    * Transfers focus to the next player name field
205    * according to the order of the fields as they are
206    * stored on the playersFields list.
207    */
208   private void focusNextNameField() {
209     // looking for the current focus among the player text fields
210     for (Iterator<JTextField> it = playersFields.iterator(); it.hasNext();) {
211       JTextField field = it.next();
212       if (field.isFocusOwner()) {
213         if (it.hasNext()) {
214           it.next().requestFocus();
215         } else {
216           dialogWindow.focusFirstNavigationalButton();
217         }
218         return;
219       }
220     }
221   }
222 
223 }