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.Font;
18  import java.util.HashMap;
19  import java.util.Map;
20  import javax.swing.JLabel;
21  import javax.swing.JTextField;
22  import javax.swing.SwingConstants;
23  
24  import net.curre.prefcount.gui.aa.AAJLabel;
25  import net.curre.prefcount.gui.aa.AAJTextField;
26  import net.curre.prefcount.gui.type.Place;
27  import net.curre.prefcount.util.LocaleExt;
28  
29  import info.clearthought.layout.TableLayout;
30  import info.clearthought.layout.TableLayoutConstraints;
31  import org.apache.commons.lang.StringUtils;
32  
33  /**
34   * Object of this class represents a players names panel -
35   * the first panel, where player names/achronyms are entered.
36   * <p/>
37   * Created date: Apr 8, 2007
38   *
39   * @author Yevgeny Nyden
40   */
41  public class PlayersNamesPanel extends DialogInnerPanel {
42  
43    /** Map of references to the player names fields. */
44    protected Map<Place, JTextField> playersFields;
45  
46    /** Reference to the parent dialog frame. */
47    private PlayerDialogBaseFrame dialogWindow;
48  
49    /**
50     * Current error field index - field that
51     * failed validation (null if none).
52     */
53    private JTextField currErrorField;
54  
55    /**
56     * Constructor.
57     *
58     * @param dialogWindow Reference to the dialog window/frame.
59     * @param numPlayers   Number of players in the game.
60     * @throws IllegalArgumentException If number of players is not supported.
61     */
62    public PlayersNamesPanel(PlayerDialogBaseFrame dialogWindow, int numPlayers) {
63      super("pref.dialog.playerNames.message", PanelPosition.FIRST);
64  
65      if (numPlayers != 3 && numPlayers != 4) {
66        throw new IllegalArgumentException("Player names panel can only handle 3 or 4 players right now!");
67      }
68  
69      this.dialogWindow = dialogWindow;
70      this.playersFields = new HashMap<Place, JTextField>();
71  
72      setLayout(new TableLayout(new double[][]{
73          {TableLayout.FILL, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, TableLayout.FILL},
74          {TableLayout.FILL, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, 12, TableLayout.PREFERRED, TableLayout.FILL}}));
75  
76      createNameFieldsHelper(numPlayers);
77    }
78  
79    /** {@inheritDoc} */
80    @Override
81    public void focusFirstInputField() {
82      if (this.currErrorField == null) {
83        this.playersFields.get(Place.getPlaceForIndex(0)).requestFocus();
84      } else {
85        this.currErrorField.requestFocus();
86      }
87    }
88  
89    /**
90     * Validates players names fields.
91     *
92     * @return True if all players names are entered; false otherwise.
93     */
94    @Override
95    public boolean validateFields() {
96      for (JTextField fieldName : this.playersFields.values()) {
97        if (StringUtils.isBlank(fieldName.getText())) {
98          this.dialogWindow.toggleErrorField("pref.dialog.errorLabel.playerNames");
99          this.currErrorField = fieldName;
100         fieldName.requestFocus(); // need to try to transfer focus from the Next button
101         return false;
102       }
103     }
104     this.dialogWindow.toggleErrorField(null);
105     return true;
106   }
107 
108   /**
109    * Does nothing.
110    * <p/>
111    * {@inheritDoc}
112    */
113   @Override
114   public void doOnEntry() {
115   }
116 
117   /** {@inheritDoc} */
118   @Override
119   public void doOnLeave() {
120     // creating a list of players names/achronyms
121     Map<Place, String> playersNames = new HashMap<Place, String>();
122     for (Map.Entry<Place, JTextField> entry : this.playersFields.entrySet()) {
123       playersNames.put(entry.getKey(), entry.getValue().getText().trim());
124     }
125 
126     // setting/changing players names on the players input panels
127     for (int i = 1; i <= this.playersFields.size(); ++i) {
128       PlayerDataPanel panel = (PlayerDataPanel) this.dialogWindow.questionsPane.getComponent(i);
129       panel.adjustPlayersNames(playersNames);
130     }
131 
132     // setting players names in the game results bean
133     this.dialogWindow.setPlayersNames(playersNames);
134     this.dialogWindow.refreshTable();
135   }
136 
137   /**
138    * Checks if there is some data entered into the
139    * player names panel.
140    *
141    * @return true if at least one player name is not blank; false otherwise.
142    */
143   public boolean isSomeDataEntered() {
144     for (JTextField field : this.playersFields.values()) {
145       if (StringUtils.isNotBlank(field.getText())) {
146         return true;
147       }
148     }
149     return false;
150   }
151 
152   /**
153    * Helper method for creating and adding
154    * player name label and text field components.
155    *
156    * @param numPlayers number of players in the game.
157    */
158   private void createNameFieldsHelper(int numPlayers) {
159     Place[] sortedByIndex = new Place[numPlayers];
160     for (Place place : Place.getPlaces(numPlayers)) {
161       sortedByIndex[place.index] = place;
162     }
163     for (Place place : sortedByIndex) {
164       JLabel label = new AAJLabel(LocaleExt.getString(place.longKey, ":"));
165       LocaleExt.registerComponent(label, place.longKey, ":");
166       label.setHorizontalAlignment(SwingConstants.RIGHT);
167       final int row = 2 * place.index + 1;
168       add(label, new TableLayoutConstraints(1, row, 1, row, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
169       super.localeSensitiveComps.add(label);
170 
171       JTextField field = new AAJTextField();
172       field.setColumns(7);
173       field.setFont(new Font("Arial Black", Font.PLAIN, 12));
174       field.setFocusable(true);
175       add(field, new TableLayoutConstraints(3, row, 3, row, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
176       this.playersFields.put(place, field);
177     }
178   }
179 
180 }