1 | |
package com.liquidatom.derbyscore.ui; |
2 | |
|
3 | |
import com.liquidatom.derbyscore.domain.Team; |
4 | |
import java.awt.EventQueue; |
5 | |
import javax.annotation.concurrent.Immutable; |
6 | |
import javax.annotation.concurrent.ThreadSafe; |
7 | |
import javax.swing.JSpinner; |
8 | |
import javax.swing.event.ChangeEvent; |
9 | |
import javax.swing.event.ChangeListener; |
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
@Immutable |
16 | |
@ThreadSafe |
17 | |
public class TimeoutChangeListener implements ChangeListener { |
18 | |
|
19 | |
final private Team team; |
20 | |
|
21 | |
public TimeoutChangeListener(final Team team) { |
22 | 0 | super(); |
23 | 0 | if (team == null) { |
24 | 0 | throw new IllegalArgumentException("The parameter team must be non-null."); |
25 | |
} |
26 | 0 | this.team = team; |
27 | 0 | } |
28 | |
|
29 | |
public void stateChanged(final ChangeEvent e) { |
30 | 0 | assertDispatchThread(); |
31 | 0 | final Object eventSource = e.getSource(); |
32 | 0 | if (eventSource != null) { |
33 | 0 | if (eventSource instanceof JSpinner) { |
34 | 0 | final JSpinner spinner = (JSpinner)eventSource; |
35 | 0 | team.setTimeouts(((Number)spinner.getValue()).intValue()); |
36 | 0 | } |
37 | |
else { |
38 | 0 | throw new IllegalArgumentException("Unable to update the timeouts based on an event generated from an " + |
39 | |
"object of type '" + eventSource.getClass().getName() + "'."); |
40 | |
} |
41 | |
} |
42 | 0 | } |
43 | |
|
44 | |
protected void assertDispatchThread() { |
45 | 0 | if (!EventQueue.isDispatchThread()) { |
46 | 0 | throw new IllegalStateException("This method must be invoked from within the AWT Event Dispatch Thread!"); |
47 | |
} |
48 | 0 | } |
49 | |
} |