1 | |
package com.liquidatom.derbyscore.ui; |
2 | |
|
3 | |
import com.liquidatom.derbyscore.domain.Bout; |
4 | |
import java.awt.EventQueue; |
5 | |
import java.awt.event.ActionEvent; |
6 | |
import java.awt.event.ActionListener; |
7 | |
import javax.annotation.concurrent.Immutable; |
8 | |
import javax.annotation.concurrent.ThreadSafe; |
9 | |
import javax.swing.ComboBoxModel; |
10 | |
import javax.swing.JComboBox; |
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
@Immutable |
19 | |
@ThreadSafe |
20 | |
public class PeriodActionListener implements ActionListener { |
21 | |
|
22 | |
final private Bout bout; |
23 | |
|
24 | |
public PeriodActionListener(final Bout bout) { |
25 | 0 | super(); |
26 | 0 | if (bout == null) { |
27 | 0 | throw new NullPointerException("The parameter bout must be non-null."); |
28 | |
} |
29 | 0 | this.bout = bout; |
30 | 0 | } |
31 | |
|
32 | |
public void actionPerformed(final ActionEvent e) { |
33 | 0 | assertDispatchThread(); |
34 | 0 | Object eventSource = e.getSource(); |
35 | 0 | if (eventSource != null) { |
36 | 0 | if (eventSource instanceof JComboBox) { |
37 | 0 | ComboBoxModel model = ((JComboBox)eventSource).getModel(); |
38 | 0 | Object selectedItem = model.getSelectedItem(); |
39 | 0 | int i = 0; |
40 | 0 | for (; i < model.getSize(); ++i) { |
41 | 0 | if (selectedItem.equals(model.getElementAt(i))) { |
42 | 0 | if (i == 0) { |
43 | 0 | bout.setOvertime(false); |
44 | 0 | bout.setPeriod(1); |
45 | |
} |
46 | 0 | else if (i == 1) { |
47 | 0 | bout.setOvertime(false); |
48 | 0 | bout.setPeriod(2); |
49 | |
} |
50 | 0 | else if (i == 2) { |
51 | 0 | bout.setOvertime(true); |
52 | 0 | bout.setPeriod(2); |
53 | |
} |
54 | |
break; |
55 | |
} |
56 | |
} |
57 | 0 | } |
58 | |
else { |
59 | 0 | throw new IllegalArgumentException("We are only capable of handling actions on JComboBox instances."); |
60 | |
} |
61 | |
} |
62 | 0 | } |
63 | |
|
64 | |
protected void assertDispatchThread() { |
65 | 0 | if (!EventQueue.isDispatchThread()) { |
66 | 0 | throw new IllegalStateException("This method must be invoked from within the AWT Event Dispatch Thread!"); |
67 | |
} |
68 | 0 | } |
69 | |
} |