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 javax.swing.JRadioButton;
18  import javax.swing.JButton;
19  import javax.swing.JPanel;
20  import javax.swing.JDialog;
21  import javax.swing.JLabel;
22  import javax.swing.WindowConstants;
23  import javax.swing.ButtonGroup;
24  import javax.swing.border.EmptyBorder;
25  
26  import net.curre.prefcount.util.LocaleExt;
27  import net.curre.prefcount.util.Utilities;
28  import net.curre.prefcount.gui.type.Place;
29  import net.curre.prefcount.PrefCountRegistry;
30  import net.curre.prefcount.bean.GameResultBean;
31  import net.curre.prefcount.bean.PlayerStatistics;
32  import static net.curre.prefcount.gui.type.WindowComponent.DIALOG_FORWARD2;
33  
34  import java.util.ResourceBundle;
35  import java.util.Map;
36  import java.awt.Frame;
37  import java.awt.Dialog;
38  import java.awt.Container;
39  import java.awt.BorderLayout;
40  
41  import info.clearthought.layout.TableLayoutConstraints;
42  import info.clearthought.layout.TableLayout;
43  
44  /**
45   * Object of this class represents a modal dialog
46   * to request a player choice.
47   * <p/>
48   * Created date: Oct 14, 2008
49   *
50   * @author Yevgeny Nyden
51   */
52  public class ChoosePlayerDialog extends JDialog {
53    public ChoosePlayerDialog(Frame owner) {
54      super(owner);
55      initComponents();
56    }
57  
58    /**
59     * Creates a new <code>ChoosePlayerDialog</code> object.
60     *
61     * @param owner owner/parent dialog.
62     */
63    public ChoosePlayerDialog(Dialog owner) {
64  		super(owner);
65  		initComponents();
66  	}
67  
68    /** Initializes components. */
69    private void initComponents() {
70  		// JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
71  		// Generated using JFormDesigner non-commercial license
72  		ResourceBundle bundle = ResourceBundle.getBundle("default");
73  		dialogPane = new JPanel();
74  		JPanel contentPanel = new JPanel();
75  		playerEastButton = new JRadioButton();
76  		playerSouthButton = new JRadioButton();
77  		playerWestButton = new JRadioButton();
78  		playerNorthButton = new JRadioButton();
79  		JPanel buttonBar = new JPanel();
80  		JButton okButton = PrefCountRegistry.getInstance().getMenuItemsBean().createJButtonForChoosePlayerDialog(this.dialogPane);
81  		JPanel headerPanel = new JPanel();
82  		JLabel headerMsg = new JLabel();
83  
84  		//======== this ========
85  		setAlwaysOnTop(true);
86  		setModal(true);
87  		setTitle(bundle.getString("pref.dialog.choosePlayer.title"));
88  		setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
89  		Container contentPane = getContentPane();
90  		contentPane.setLayout(new BorderLayout());
91  
92  		//======== dialogPane ========
93  		{
94  			dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
95  			dialogPane.setLayout(new BorderLayout());
96  
97  			//======== contentPanel ========
98  			{
99  				contentPanel.setLayout(new TableLayout(new double[][] {
100 					{0.05, TableLayout.FILL, TableLayout.PREFERRED, TableLayout.FILL, 0.05},
101 					{TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED, TableLayout.PREFERRED}}));
102 				((TableLayout)contentPanel.getLayout()).setHGap(5);
103 				((TableLayout)contentPanel.getLayout()).setVGap(5);
104 
105 				//---- playerEastButton ----
106 				playerEastButton.setSelected(true);
107 				playerEastButton.setText(getPlayerString(Place.EAST));
108 				contentPanel.add(playerEastButton, new TableLayoutConstraints(2, 0, 2, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
109 
110 				//---- playerSouthButton ----
111 				playerSouthButton.setText(null);
112 				playerSouthButton.setText(getPlayerString(Place.SOUTH));
113 				contentPanel.add(playerSouthButton, new TableLayoutConstraints(2, 1, 2, 1, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
114 
115 				//---- playerWestButton ----
116 				playerWestButton.setText(null);
117 				playerWestButton.setText(getPlayerString(Place.WEST));
118 				contentPanel.add(playerWestButton, new TableLayoutConstraints(2, 2, 2, 2, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
119 
120 				//---- playerNorthButton ----
121 				playerNorthButton.setText(null);
122 				String northName = getPlayerString(Place.NORTH);
123 				if (northName == null) {
124 				  playerNorthButton.setVisible(false);
125 				} else {
126 				  playerNorthButton.setText(northName);
127 				}
128 				contentPanel.add(playerNorthButton, new TableLayoutConstraints(2, 3, 2, 3, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
129 			}
130 			dialogPane.add(contentPanel, BorderLayout.CENTER);
131 
132 			//======== buttonBar ========
133 			{
134 				buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
135 				buttonBar.setLayout(new TableLayout(new double[][] {
136 					{TableLayout.FILL, TableLayout.PREFERRED},
137 					{TableLayout.PREFERRED}}));
138 				((TableLayout)buttonBar.getLayout()).setHGap(5);
139 				((TableLayout)buttonBar.getLayout()).setVGap(5);
140 				buttonBar.add(okButton, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
141 			}
142 			dialogPane.add(buttonBar, BorderLayout.SOUTH);
143 
144 			//======== headerPanel ========
145 			{
146 				headerPanel.setLayout(new TableLayout(new double[][] {
147 					{20, TableLayout.PREFERRED, 20},
148 					{TableLayout.FILL, 10}}));
149 				((TableLayout)headerPanel.getLayout()).setHGap(5);
150 				((TableLayout)headerPanel.getLayout()).setVGap(5);
151 
152 				//---- headerMsg ----
153 				headerMsg.setText(bundle.getString("pref.dialog.choosePlayer.header"));
154 				headerMsg.setBackground(null);
155 				headerMsg.setFocusable(false);
156 				headerMsg.setRequestFocusEnabled(false);
157 				headerMsg.setBorder(null);
158 				headerPanel.add(headerMsg, new TableLayoutConstraints(1, 0, 1, 0, TableLayoutConstraints.FULL, TableLayoutConstraints.FULL));
159 			}
160 			dialogPane.add(headerPanel, BorderLayout.NORTH);
161 		}
162 		contentPane.add(dialogPane, BorderLayout.CENTER);
163 		pack();
164 		setLocationRelativeTo(getOwner());
165 
166 		//---- buttonGroup ----
167 		ButtonGroup buttonGroup = new ButtonGroup();
168 		buttonGroup.add(playerEastButton);
169 		buttonGroup.add(playerSouthButton);
170 		buttonGroup.add(playerWestButton);
171 		buttonGroup.add(playerNorthButton);
172 		// JFormDesigner - End of component initialization  //GEN-END:initComponents
173 	}
174 
175 	// JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
176 	// Generated using JFormDesigner non-commercial license
177 	private JPanel dialogPane;
178 	private JRadioButton playerEastButton;
179 	private JRadioButton playerSouthButton;
180 	private JRadioButton playerWestButton;
181 	private JRadioButton playerNorthButton;
182 	// JFormDesigner - End of variables declaration  //GEN-END:variables
183 
184   /**
185    * Gets selected player place.
186    *
187    * @return selected player place.
188    */
189   public Place getSelectedPlayer() {
190     if (this.playerEastButton.isSelected()) {
191       return Place.EAST;
192 
193     } else if (this.playerSouthButton.isSelected()) {
194       return Place.SOUTH;
195 
196     } else if (this.playerWestButton.isSelected()) {
197       return Place.WEST;
198 
199     } else if (this.playerNorthButton.isSelected()) {
200       return Place.NORTH;
201 
202     } else {
203       return Place.EAST; // setting default player anyway
204     }
205   }
206 
207   /**
208    * Method to create a radio button title.
209    *
210    * @param place player's place.
211    * @return player's radio button text or null if no such player exists in the game.
212    */
213   private String getPlayerString(Place place) {
214     GameResultBean rBean = PrefCountRegistry.getInstance().getGameResultBean();
215     Map<Place, PlayerStatistics> statsMap = rBean.getPlayerStats();
216     PlayerStatistics stats = statsMap.get(place);
217     if (stats == null) {
218       return null;
219     }
220     return LocaleExt.getString(place.longKey, ": " + stats.getPlayerName());
221   }
222 
223 }