View Javadoc

1   package net.sourceforge.simplegamenet.dice;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import javax.swing.*;
6   import net.sourceforge.simplegamenet.specs.gui.GameSettingsPanel;
7   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
8   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
9   
10  public class DiceSettingsPanel extends GameSettingsPanel implements ActionListener,
11          DiceSettingOptions {
12  
13      private JLabel maximumParticipantsAmountLabel;
14      private JComboBox maximumParticipantsAmountComboBox;
15      private JLabel maximumConnectionsAmountLabel;
16      private JComboBox maximumConnectionsAmountComboBox;
17  
18      private int bonusDeval;
19      private int rightDeval;
20  
21      private static final String[][] valueList = {
22          {
23              "Dicorama", "First player", "-5",
24              "35", "25", "30", "40", "50"
25          },
26          {
27              "Left'r'us", "First player",
28              "-5", "100", "25", "30", "40", "50"
29          },
30          {
31              "5 of a frenzy", "First player",
32              "None", "35", "10", "15", "25", "100"
33          },
34          {
35              "Race", "First player",
36              "First player", "35", "25", "30", "40",
37              "50"
38          },
39          {
40              "Don't bug me", "None",
41              "None", "35", "25", "30", "40", "50"
42          },
43          {
44              "Right is wrong", "First player",
45              "-10", "35", "75", "80", "90", "100"
46          }
47      };
48  
49      private JLabel[] settingLabels = {
50          new JLabel("Predefined settings:"),
51          new JLabel("Bonus devaluation:"),
52          new JLabel("Right half devaluation:"),
53          new JLabel("Bonus:"),
54          new JLabel("Full house:"),
55          new JLabel("Small street:"),
56          new JLabel("Big street:"),
57          new JLabel("Five of a kind:")
58      };
59      // [0] predefinedSettingsLabel;
60      // [1] bonusDevaluationLabel;
61      // [2] rightHalfDevaluationabel;
62      // [3] bonusLabel;
63      // [4] fullHouseLabel;
64      // [5] smallStreetLabel;
65      // [6] bigStreetLabel;
66      // [7] fOAKLabel;
67  
68      private JComboBox[] settingComboBoxes = new JComboBox[valueList[0].length];
69      // [0] predefinedSettingsComboBox
70      // [1] bonusDevaluationComboBox
71      // [2] rightHalfDevaluationComboBox
72      // [3] bonusComboBox
73      // [4] fullHouseComboBox
74      // [5] smallStreetComboBox
75      // [6] bigStreetComboBox
76      // [7] fOAKComboBox
77  
78      private JPanel subPanel;
79      private JPanel startPointsPanel;
80  
81      public DiceSettingsPanel() throws IllegalArgumentException {
82          ProportionLayout layout = new ProportionLayout();
83          layout.appendColumn(0);                                 // 0 empty
84          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 1 labels
85          layout.appendColumn(10);                                // 2 empty
86          layout.appendColumn(0, 1.0);                            // 3 selections
87          layout.appendColumn(0);                                 // 4 empty
88          layout.appendRow(0, 1.0);                            // 0 empty
89          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 1 participants
90          layout.appendRow(10);                                // 2 empty
91          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 3 connections
92          layout.appendRow(10);                                // 4 empty
93          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 5 predefined settings
94          layout.appendRow(10);                                // 6 empty
95          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 7 subPanel
96          layout.appendRow(0, 1.0);                            // 8 empty
97          setLayout(layout);
98          ProportionLayout subLayout = new ProportionLayout();
99          subLayout.appendColumn(5);                                 // 0 empty
100         subLayout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 1 labels
101         subLayout.appendColumn(10);                                // 2 empty
102         subLayout.appendColumn(0, 1.0);                            // 3 selections
103         subLayout.appendColumn(5);                                 // 4 empty
104         subLayout.appendRow(5);                                 // 0 empty
105         subLayout.appendRow(0, ProportionLayout.NO_PROPORTION); // 1 bonus devaluation
106         subLayout.appendRow(10);                                // 2 empty
107         subLayout.appendRow(0, ProportionLayout.NO_PROPORTION); // 5 right devaluation
108         subLayout.appendRow(10);                                // 6 empty
109         subLayout.appendRow(0, ProportionLayout.NO_PROPORTION); // 7 start points panel
110         subLayout.appendRow(5);                                 // 8 empty
111         ProportionLayout startPointsPanelLayout = new ProportionLayout();
112         startPointsPanelLayout.appendColumn(5);
113         startPointsPanelLayout.appendColumn(0, ProportionLayout.NO_PROPORTION);
114         startPointsPanelLayout.appendColumn(10);
115         startPointsPanelLayout.appendColumn(0, 1.0);
116         startPointsPanelLayout.appendColumn(10);
117         startPointsPanelLayout.appendColumn(0, ProportionLayout.NO_PROPORTION);
118         startPointsPanelLayout.appendColumn(10);
119         startPointsPanelLayout.appendColumn(0, 1.0);
120         startPointsPanelLayout.appendColumn(5);
121         // 0 empty
122         // 1 first labels
123         // 2 empty
124         // 3 first points
125         // 4 empty
126         // 5 second labels
127         // 6 empty
128         // 7 second points
129         // 8 empty
130         startPointsPanelLayout.appendRow(5);
131         startPointsPanelLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
132         startPointsPanelLayout.appendRow(10);
133         startPointsPanelLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
134         startPointsPanelLayout.appendRow(10);
135         startPointsPanelLayout.appendRow(0, ProportionLayout.NO_PROPORTION);
136         startPointsPanelLayout.appendRow(5);
137         // 0 empty
138         // 1 bonus + big street
139         // 2 empty
140         // 3 full house + five of a kind
141         // 4 empty
142         // 5 small street
143         // 6 empty
144         startPointsPanel = new JPanel(startPointsPanelLayout);
145         startPointsPanel.setBorder(BorderFactory.createTitledBorder("Right half "
146                 + "starting points"));
147         subPanel = new JPanel(subLayout);
148         subPanel.setBorder(BorderFactory.createTitledBorder(""));
149         settingComboBoxes[0] = new JComboBox();
150         for (int i = 0; i < valueList.length; i++) {
151             settingComboBoxes[0].addItem(valueList[i][0]);
152         }
153         settingComboBoxes[0].addItem("Custom...");
154         for (int i = 1; i < settingComboBoxes.length; i++) {
155             settingComboBoxes[i] = new JComboBox();
156         }
157         for (int i = 1; i < settingComboBoxes.length; i++) {
158             if (i == 1 || i == 2) {
159                 settingComboBoxes[i].addItem("First player");
160                 settingComboBoxes[i].addItem("-5");
161                 settingComboBoxes[i].addItem("-10");
162                 settingComboBoxes[i].addItem("None");
163             } else {
164                 for (int j = 1; j <= 100; j++) {
165                     settingComboBoxes[i].addItem(Integer.toString(j));
166                 }
167             }
168         }
169         for (int i = 0; i < settingComboBoxes.length; i++) {
170             settingComboBoxes[i].setSelectedItem(valueList[0][i]);
171             settingComboBoxes[i].addActionListener(this);
172             if (i == 0) {
173                 add(settingLabels[i], new ProportionConstraints(1, 5 + (i * 2)));
174                 add(settingComboBoxes[i], new ProportionConstraints(3, 5 + (i * 2)));
175             } else if (i >= 1 && i <= 2) {
176                 subPanel.add(settingLabels[i],
177                         new ProportionConstraints(1, (i * 2) - 1));
178                 subPanel.add(settingComboBoxes[i],
179                         new ProportionConstraints(3, (i * 2) - 1));
180             } else if (i >= 3 && i <= 5) {
181                 startPointsPanel.add(settingLabels[i],
182                         new ProportionConstraints(1, (i * 2) - 5));
183                 startPointsPanel.add(settingComboBoxes[i],
184                         new ProportionConstraints(3, (i * 2) - 5));
185             } else if (i >= 6 && i <= 7) {
186                 startPointsPanel.add(settingLabels[i],
187                         new ProportionConstraints(5, (i * 2) - 11));
188                 startPointsPanel.add(settingComboBoxes[i],
189                         new ProportionConstraints(7, (i * 2) - 11));
190             }
191         }
192         settingComboBoxes[2].setSelectedIndex(1);
193         maximumParticipantsAmountLabel = new JLabel("Maximum amount of participants:");
194         maximumParticipantsAmountComboBox = new JComboBox();
195         maximumConnectionsAmountLabel = new JLabel("Maximum amount of connections:");
196         maximumConnectionsAmountComboBox = new JComboBox();
197         for (int i = 2; i <= 16; i++) {
198             Integer integer = new Integer(i);
199             if (i <= 8) {
200                 maximumParticipantsAmountComboBox.addItem(integer);
201                 if (i == 8) {
202                     maximumParticipantsAmountComboBox.setSelectedItem(integer);
203                 }
204             }
205             maximumConnectionsAmountComboBox.addItem(integer);
206             if (i == 8) {
207                 maximumConnectionsAmountComboBox.setSelectedItem(integer);
208             }
209         }
210         maximumParticipantsAmountComboBox.addActionListener(this);
211         maximumConnectionsAmountComboBox.addActionListener(this);
212         add(maximumParticipantsAmountLabel, new ProportionConstraints(1, 1));
213         add(maximumParticipantsAmountComboBox, new ProportionConstraints(3, 1));
214         add(maximumConnectionsAmountLabel, new ProportionConstraints(1, 3));
215         add(maximumConnectionsAmountComboBox, new ProportionConstraints(3, 3));
216         add(subPanel, new ProportionConstraints(1, 7, 3, 1));
217         subPanel.add(startPointsPanel, new ProportionConstraints(1, 5, 3, 1));
218     }
219 
220     public void actionPerformed(ActionEvent actionEvent) {
221         if (actionEvent.getSource() == maximumConnectionsAmountComboBox) {
222             if (((Integer) maximumParticipantsAmountComboBox.getSelectedItem()).intValue()
223                     > ((Integer) maximumConnectionsAmountComboBox.getSelectedItem())
224                     .intValue()) {
225                 maximumParticipantsAmountComboBox.setSelectedItem(
226                         maximumConnectionsAmountComboBox.getSelectedItem());
227             }
228         } else if (actionEvent.getSource() == maximumParticipantsAmountComboBox) {
229             if (((Integer) maximumParticipantsAmountComboBox.getSelectedItem()).intValue()
230                     > ((Integer) maximumConnectionsAmountComboBox.getSelectedItem())
231                     .intValue()) {
232                 maximumConnectionsAmountComboBox.setSelectedItem(
233                         maximumParticipantsAmountComboBox.getSelectedItem());
234             }
235         } else if (actionEvent.getSource() == settingComboBoxes[0]) {
236             for (int i = 0; i < valueList.length; i++) {
237                 if (settingComboBoxes[0].getSelectedIndex() == i) {
238                     for (int j = 1; j < valueList[0].length; j++) {
239                         settingComboBoxes[j].setSelectedItem(valueList[i][j]);
240                     }
241                     break;
242                 }
243             }
244         } else {
245             for (int k = 1; k < settingComboBoxes.length; k++) {
246                 if (actionEvent.getSource() == settingComboBoxes[k]) {
247                     for (int i = 0; i < valueList.length; i++) {
248                         boolean predefinedValueBoolean = true;
249                         for (int j = 1; j < valueList[0].length; j++) {
250                             if (!settingComboBoxes[j].getSelectedItem().toString()
251                                     .equals(valueList[i][j])) {
252                                 predefinedValueBoolean = false;
253                                 break;
254                             }
255                         }
256                         if (predefinedValueBoolean) {
257                             settingComboBoxes[0].setSelectedIndex(i);
258                             return;
259                         }
260                     }
261                     settingComboBoxes[0].setSelectedIndex(valueList.length);
262                     return;
263                 }
264             }
265         }
266     }
267 
268     public void setEnabled(boolean enabled) {
269         super.setEnabled(enabled);
270         for (int i = 0; i < settingComboBoxes.length; i++) {
271             settingComboBoxes[i].setEnabled(enabled);
272         }
273         maximumConnectionsAmountComboBox.setEnabled(enabled);
274         maximumParticipantsAmountComboBox.setEnabled(enabled);
275     }
276 
277     public int getSettingComboBoxesLength() {
278         return settingComboBoxes.length;
279     }
280 
281     public int getSettingComboBoxesSelectedIndex(int i) {
282         return settingComboBoxes[i].getSelectedIndex();
283     }
284 
285     public void setSettingComboBoxesSelectedIndex(int i, int selection) {
286         settingComboBoxes[i].setSelectedIndex(selection);
287     }
288 
289     public String getSettingComboBoxesScoresSelectedItem(int i) {
290         return (String) settingComboBoxes[i].getSelectedItem();
291     }
292 
293     public void setMaximumParticipantsComboBoxSelectedIndex(int i) {
294         maximumParticipantsAmountComboBox.setSelectedIndex(i);
295     }
296 
297     public Integer getMaximumParticipantsComboBoxSelectedItem() {
298         return (Integer) maximumParticipantsAmountComboBox.getSelectedItem();
299     }
300 
301     public void setMaximumConnectionsComboBoxSelectedIndex(int i) {
302         maximumConnectionsAmountComboBox.setSelectedIndex(i);
303     }
304 
305     public Integer getMaximumConnectionsComboBoxSelectedItem() {
306         return (Integer) maximumConnectionsAmountComboBox.getSelectedItem();
307     }
308 
309 }