1
2
3
4
5
6
7
8
9
10
11
12
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
46
47
48
49
50
51
52 public class ChoosePlayerDialog extends JDialog {
53 public ChoosePlayerDialog(Frame owner) {
54 super(owner);
55 initComponents();
56 }
57
58
59
60
61
62
63 public ChoosePlayerDialog(Dialog owner) {
64 super(owner);
65 initComponents();
66 }
67
68
69 private void initComponents() {
70
71
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
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
93 {
94 dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
95 dialogPane.setLayout(new BorderLayout());
96
97
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
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
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
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
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
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
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
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
167 ButtonGroup buttonGroup = new ButtonGroup();
168 buttonGroup.add(playerEastButton);
169 buttonGroup.add(playerSouthButton);
170 buttonGroup.add(playerWestButton);
171 buttonGroup.add(playerNorthButton);
172
173 }
174
175
176
177 private JPanel dialogPane;
178 private JRadioButton playerEastButton;
179 private JRadioButton playerSouthButton;
180 private JRadioButton playerWestButton;
181 private JRadioButton playerNorthButton;
182
183
184
185
186
187
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;
204 }
205 }
206
207
208
209
210
211
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 }