View Javadoc

1   package net.sourceforge.simplegamenet.specs.tools;
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 MultiPlayerGameSettingsPanel extends GameSettingsPanel
11          implements ActionListener {
12  
13      private JLabel maximumParticipantsAmountLabel;
14      private JComboBox maximumParticipantsAmountComboBox;
15      private JLabel maximumConnectionsAmountLabel;
16      private JComboBox maximumConnectionsAmountComboBox;
17  
18      public MultiPlayerGameSettingsPanel() throws IllegalArgumentException {
19          this(2, 2, 8, 8, 2, 16);
20      }
21  
22      public MultiPlayerGameSettingsPanel(int defaultMaximumParticipantsAmount,
23                                          int defaultMaximumConnectionsAmount)
24              throws IllegalArgumentException {
25          this(defaultMaximumParticipantsAmount, 2, defaultMaximumParticipantsAmount,
26                  defaultMaximumParticipantsAmount, 2, defaultMaximumConnectionsAmount);
27      }
28  
29      public MultiPlayerGameSettingsPanel(int defaultMaximumParticipantsAmount,
30                                          int minimumMaximumParticipantsAmount,
31                                          int maximumMaximumParticipantsAmount,
32                                          int defaultMaximumConnectionsAmount,
33                                          int minimumMaximumConnectionsAmount,
34                                          int maximumMaximumConnectionsAmount)
35              throws IllegalArgumentException {
36          if (minimumMaximumParticipantsAmount <= 0
37                  || minimumMaximumConnectionsAmount <= 0) {
38              throw new IllegalArgumentException("All amounts must be larger then 0");
39          }
40          if (minimumMaximumParticipantsAmount > defaultMaximumParticipantsAmount
41                  || maximumMaximumParticipantsAmount < defaultMaximumParticipantsAmount) {
42              throw new IllegalArgumentException("Default participants must be between the "
43                      + "minimum and maximum amount");
44          }
45          if (minimumMaximumConnectionsAmount > defaultMaximumConnectionsAmount
46                  || maximumMaximumConnectionsAmount < defaultMaximumConnectionsAmount) {
47              throw new IllegalArgumentException("Default connections must be between the "
48                      + "minimum and maximum amount");
49          }
50          if (minimumMaximumConnectionsAmount < minimumMaximumParticipantsAmount
51                  || maximumMaximumConnectionsAmount < maximumMaximumParticipantsAmount
52                  || defaultMaximumConnectionsAmount < defaultMaximumParticipantsAmount) {
53              throw new IllegalArgumentException("Connection amounts must be equal or larger "
54                      + "than participant amounts");
55          }
56          ProportionLayout layout = new ProportionLayout();
57          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 0 labels
58          layout.appendColumn(10);                                // 1 empty
59          layout.appendColumn(0, 1.0);                            // 2 selections
60          layout.appendRow(0, 1.0);                            // 0 empty
61          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 1 participants
62          layout.appendRow(10);                                // 2 empty
63          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 3 connections
64          layout.appendRow(0, 1.0);                            // 4 empty
65          setLayout(layout);
66  
67          maximumParticipantsAmountLabel = new JLabel("Maximum amount of participants:");
68          maximumParticipantsAmountComboBox = new JComboBox();
69          maximumConnectionsAmountLabel = new JLabel("Maximum amount of connections:");
70          maximumConnectionsAmountComboBox = new JComboBox();
71          for (int i = minimumMaximumParticipantsAmount;
72                  i <= maximumMaximumConnectionsAmount; i++) {
73              Integer integer = new Integer(i);
74              if (i <= maximumMaximumParticipantsAmount) {
75                  maximumParticipantsAmountComboBox.addItem(integer);
76                  if (i == defaultMaximumParticipantsAmount) {
77                      maximumParticipantsAmountComboBox.setSelectedItem(integer);
78                  }
79              }
80              if (i >= minimumMaximumConnectionsAmount) {
81                  maximumConnectionsAmountComboBox.addItem(integer);
82                  if (i == defaultMaximumConnectionsAmount) {
83                      maximumConnectionsAmountComboBox.setSelectedItem(integer);
84                  }
85              }
86          }
87          maximumParticipantsAmountComboBox.addActionListener(this);
88          maximumConnectionsAmountComboBox.addActionListener(this);
89          add(maximumParticipantsAmountLabel, new ProportionConstraints(0, 1));
90          add(maximumParticipantsAmountComboBox, new ProportionConstraints(2, 1));
91          add(maximumConnectionsAmountLabel, new ProportionConstraints(0, 3));
92          add(maximumConnectionsAmountComboBox, new ProportionConstraints(2, 3));
93      }
94  
95      /***
96       * Invoked when an action occurs.
97       */
98      public void actionPerformed(ActionEvent actionEvent) {
99          if (actionEvent.getSource() == maximumConnectionsAmountComboBox) {
100             if (((Integer) maximumParticipantsAmountComboBox.getSelectedItem()).intValue()
101                     > ((Integer) maximumConnectionsAmountComboBox.getSelectedItem())
102                     .intValue()) {
103                 maximumParticipantsAmountComboBox.setSelectedItem(
104                         maximumConnectionsAmountComboBox.getSelectedItem());
105             }
106         } else if (actionEvent.getSource() == maximumParticipantsAmountComboBox) {
107             if (((Integer) maximumParticipantsAmountComboBox.getSelectedItem()).intValue()
108                     > ((Integer) maximumConnectionsAmountComboBox.getSelectedItem())
109                     .intValue()) {
110                 maximumConnectionsAmountComboBox.setSelectedItem(
111                         maximumParticipantsAmountComboBox.getSelectedItem());
112             }
113         }
114     }
115 
116     public int getMaximumParticipantsAmount() {
117         return ((Integer) maximumParticipantsAmountComboBox.getSelectedItem()).intValue();
118     }
119 
120     public int getMaximumConnectionsAmount() {
121         return ((Integer) maximumConnectionsAmountComboBox.getSelectedItem()).intValue();
122     }
123 
124     public void setEnabled(boolean enabled) {
125         super.setEnabled(enabled);
126         maximumParticipantsAmountComboBox.setEnabled(enabled);
127         maximumConnectionsAmountComboBox.setEnabled(enabled);
128     }
129 
130 }